{"id":126,"date":"2026-11-13T12:32:05","date_gmt":"2026-11-13T04:32:05","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-sparse-table-answers-a-static-range-minimum\/"},"modified":"2026-11-13T21:50:53","modified_gmt":"2026-11-13T13:50:53","slug":"how-a-sparse-table-answers-a-static-range-minimum","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-sparse-table-answers-a-static-range-minimum\/","title":{"rendered":"How a sparse table answers a static range minimum"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>sparse table<\/strong> answers a static range minimum, or any other <strong>idempotent<\/strong> range query, in <strong>O(1)<\/strong> time after <strong>O(n log n)<\/strong> preprocessing. Cell (k, i) stores the minimum of the 2^k elements that start at i. A query of length L uses the two overlapping blocks of length 2^k that cover it, where k = floor(log\u2082 L), and takes the better of the two. Overlap is legal only because taking the minimum twice does not change it.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A static array will be queried many times and never updated. A segment tree answers each query in O(log n). The idempotent case can do better. Any interval of length L contains two aligned blocks of length 2^k, with k = floor(log\u2082 L), that together cover it and that overlap in the middle. If the operation returns the same answer when an element is included twice, the two block answers combine into the interval answer.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Sum is not idempotent. The overlap would add the middle twice. Minimum, maximum, and gcd are idempotent. The sparse table is for those operations.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Preprocessing fills O(log n) rows. Row 0 is the array. Row k is the pairwise combine of two adjacent blocks from row k\u22121, which is O(n) work per row and O(n log n) overall. A query reads two cells and combines them, which is O(1) for a constant-time combine. Extra memory is n log n. There is no update path. A changed element invalidates every block that contains it.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Range sum on a static array does not use this overlap trick. It uses a prefix array and one subtraction. The sparse-table query is the wrong shape for sum, and the prefix array is the wrong shape for minimum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set st[0][i] = a[i] for each i.<\/li>\n<li>For k from 1 to floor(log\u2082 n), set st[k][i] = min(st[k\u22121][i], st[k\u22121][i+2^{k\u22121}]) for every i where both blocks fit.<\/li>\n<li>For a query [l, r], let len = r\u2212l+1 and k = floor(log\u2082 len).<\/li>\n<li>Return min(st[k][l], st[k][r\u22122^k+1]). The two blocks cover [l, r] and overlap.<\/li>\n<li>Do not use this query for sum. Use prefix[r] \u2212 prefix[l\u22121] instead.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Constant-time queries require idempotence and a static array. A segment tree is the structure that survives updates, at O(log n) per query. A Fenwick tree handles prefix sums with updates. The sparse table&#8217;s O(1) claim is false for sum if the two-block query is used unchanged, because the overlap is counted twice.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the operation counts an element twice<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Add the two overlapping blocks and call it a range sum<\/td>\n<td>The overlap is included twice. Range sum wants a prefix array, or a disjoint cover such as a segment tree.<\/td>\n<\/tr>\n<tr>\n<td>Update one array element and leave the table<\/td>\n<td>Every block length that covers the element is stale. The preprocessing assumed a static array.<\/td>\n<\/tr>\n<tr>\n<td>Pick k = ceil(log\u2082 len)<\/td>\n<td>The block can stick out past the query. The exponent has to be the greatest power of two that does not exceed the length.<\/td>\n<\/tr>\n<tr>\n<td>Query st[k][l] alone<\/td>\n<td>One block of length 2^k covers a prefix of the query, not the whole interval, whenever the length is not itself a power of two.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Updates belong to a segment tree. Static sums belong to prefix arrays. The sparse table is the constant-time structure for static idempotent ranges, and it is the usual preprocessing behind binary lifting&#8217;s range-minimum formulation of lowest common ancestors.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment builds a sparse table for a static range minimum. A query covers the range with two overlapping blocks of length 2^k. It prints <code>2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\na = [5, 2, 4, 3, 2, 8]\nn = len(a)\nK = n.bit_length()\nst = [[0] * n for _ in range(K)]\nst[0] = a[:]\nfor k in range(1, K):\n    span = 1 &lt;&lt; (k - 1)\n    for i in range(n - (1 &lt;&lt; k) + 1):\n        st[k][i] = min(st[k - 1][i], st[k - 1][i + span])\n\ndef rmq(L, R):\n    k = (R - L + 1).bit_length() - 1\n    return min(st[k][L], st[k][R - (1 &lt;&lt; k) + 1])\n\nassert rmq(2, 4) == min(a[2:5]) == 2\nprint(rmq(2, 4))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A sparse table answers a static range minimum after an O(n log n) build, which is the last step of the usual reduction from a lowest common ancestor to a range query. The array does not change. A segment tree is the structure once updates arrive.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a sparse table when the array will be updated. Build a segment tree. Drop it for range sums even when the array is static. Keep it for static range minimum, maximum, or gcd, when many queries should each be two table reads.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for a sparse table returns the two overlapping blocks and the idempotent combine that makes the overlap legal. <strong>A sparse table answers a static idempotent range query in constant time by combining two overlapping dyadic blocks, after O(n log n) preprocessing.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: A sparse table answers a static range minimum, or any other idempotent range query, in O(1) time after O(n log n) preprocessing. Cell (k, i) stores the minimum of the 2^k elements that start at i. A query of length L uses the two overlapping blocks of&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34],"tags":[],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-trees"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/126\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}