How a sparse table answers a static range minimum

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 length 2^k that cover it, where k = floor(log₂ L), and takes the better of the two. Overlap is legal only because taking the minimum twice does not change it.


What the problem actually asks

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₂ 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.

Sum is not idempotent. The overlap would add the middle twice. Minimum, maximum, and gcd are idempotent. The sparse table is for those operations.


What the algorithm guarantees

Preprocessing fills O(log n) rows. Row 0 is the array. Row k is the pairwise combine of two adjacent blocks from row k−1, 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.

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.


The working steps

  1. Set st[0][i] = a[i] for each i.
  2. For k from 1 to floor(log₂ n), set st[k][i] = min(st[k−1][i], st[k−1][i+2^{k−1}]) for every i where both blocks fit.
  3. For a query [l, r], let len = r−l+1 and k = floor(log₂ len).
  4. Return min(st[k][l], st[k][r−2^k+1]). The two blocks cover [l, r] and overlap.
  5. Do not use this query for sum. Use prefix[r] − prefix[l−1] instead.

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’s O(1) claim is false for sum if the two-block query is used unchanged, because the overlap is counted twice.


Approaches that look simpler and fail

Approach Why it fails when the operation counts an element twice
Add the two overlapping blocks and call it a range sum The overlap is included twice. Range sum wants a prefix array, or a disjoint cover such as a segment tree.
Update one array element and leave the table Every block length that covers the element is stale. The preprocessing assumed a static array.
Pick k = ceil(log₂ len) The block can stick out past the query. The exponent has to be the greatest power of two that does not exceed the length.
Query st[k][l] alone 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.

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’s range-minimum formulation of lowest common ancestors.


A working example

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 2.

import math
a = [5, 2, 4, 3, 2, 8]
n = len(a)
K = n.bit_length()
st = [[0] * n for _ in range(K)]
st[0] = a[:]
for k in range(1, K):
    span = 1 << (k - 1)
    for i in range(n - (1 << k) + 1):
        st[k][i] = min(st[k - 1][i], st[k - 1][i + span])

def rmq(L, R):
    k = (R - L + 1).bit_length() - 1
    return min(st[k][L], st[k][R - (1 << k) + 1])

assert rmq(2, 4) == min(a[2:5]) == 2
print(rmq(2, 4))

Where it is used

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.


When you can drop this algorithm

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.


Why this stays published

We publish this so a search for a sparse table returns the two overlapping blocks and the idempotent combine that makes the overlap legal. A sparse table answers a static idempotent range query in constant time by combining two overlapping dyadic blocks, after O(n log n) preprocessing.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *