Sparse Table
Answer static range minima, maxima, or gcd queries in O(1) after O(n log n) preprocessing. Sparse tables are pure precomputation power.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Array
idx 0
7
idx 1
2
idx 2
3
idx 3
0
idx 4
5
idx 5
10
idx 6
3
idx 7
12
Sparse table layers
2^0 block length = 1
[0, 0]
7
[1, 1]
2
[2, 2]
3
[3, 3]
0
[4, 4]
5
[5, 5]
10
[6, 6]
3
[7, 7]
12
2^1 block length = 2
[0, 1]
2
[1, 2]
2
[2, 3]
0
[3, 4]
0
[4, 5]
5
[5, 6]
3
[6, 7]
3
2^2 block length = 4
[0, 3]
0
[1, 4]
0
[2, 5]
0
[3, 6]
0
[4, 7]
3
2^3 block length = 8
[0, 7]
0
Static queries, no updates
Use two overlapping blocks of length 4: [1, 4] and [3, 6]. This works because min is idempotent.
Answer for RMQ[1, 6]: 0
Family
Range queries
Static and dynamic range aggregates via indexed trees, segment trees, and precomputed tables.
Builds on
1 topic
Read these first if you want the surrounding context.
Unlocks
0 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
1
This topic appears in curated progressions so you can keep moving without guessing the next step.
Choose this over that
Static queries are where sparse tables shine
The price of `O(1)` query time is that the data cannot change afterward.
Prefix Sum
Choose this when: The problem is only static range sums and the simplest structure wins.
Choose something else when: You need minima, maxima, or other idempotent range queries beyond sums.
Sparse Table
Choose this when: The array is immutable and you want very fast static range queries.
Choose something else when: Any update must be supported.
Segment Tree
Choose this when: The data changes or the query pattern needs dynamic structure.
Choose something else when: The workload is entirely read-only.
Problem
You have a fixed array and need to answer lots of range queries like:
- minimum on
- maximum on
- gcd on
There are no updates. Since the array never changes, you should spend preprocessing time to make queries almost free.
Intuition
Precompute answers for every range whose length is a power of two:
- length 1
- length 2
- length 4
- length 8
- …
Let st[k][i] store the answer for the interval starting at i with length .
Then a large query can be answered with just two overlapping power-of-two blocks.
Build
st[0][i] <- arr[i]
for k in 1..LOG:
for i in 0..n - 2^k:
st[k][i] <- merge(st[k-1][i], st[k-1][i + 2^(k-1)])
For range minimum query, merge is just min.
Query for RMQ
For a query :
- let
len = R - L + 1 - let
k = floor(log2(len)) - answer with
Those two blocks cover the whole interval. They may overlap, but that is okay because min, max, and gcd are idempotent.
The idempotent restriction
Sparse tables are amazing only when overlap is harmless.
Good operations:
- min
- max
- gcd
- bitwise and/or
Bad operations:
- sum
- xor counts
- anything where double-counting changes the answer
If overlap breaks the merge, you need a segment tree, Fenwick tree, or another structure instead.
Why it feels like binary lifting
Sparse tables and binary lifting share the same core idea:
precompute answers for jumps of size
Sparse tables jump over interval lengths. Binary lifting jumps over ancestors. Same doubling pattern, different domain.
Complexity
| Operation | Time | Space |
|---|---|---|
| Build | ||
| Query |
Key takeaways
- Sparse tables are unbeatable for static idempotent range queries
- The structure is just doubling: every level merges two blocks from the previous level
- Two overlapping power-of-two blocks are enough for RMQ in
- Do not use a sparse table if the operation cannot tolerate overlap
- Binary lifting is the tree analogue of the same precompute-by-powers-of-two idea
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Minimum Absolute Difference Queries | 🔴 Hard | Static range information, often with heavy preprocessing |
| K-th Ancestor of a Tree Node | 🟡 Medium | Same doubling idea as sparse tables, but on ancestors |
| Static RMQ | 🟡 Medium | Range minimum query benchmark |
Relation to other topics
- Fenwick tree is dynamic but only naturally handles prefix-like merges
- Segment tree handles updates and general merges, but queries stay
- Binary lifting uses the same doubling table idea on rooted trees rather than arrays
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Lowest Common Ancestor
Find the deepest shared ancestor of two nodes in a rooted tree. LCA is a core tree-query problem that ties together DFS, binary lifting, and Euler tours.
Fenwick Tree (Binary Indexed Tree)
Maintain prefix sums with O(log n) updates and queries using one tiny bit trick. Fenwick trees are the lightest dynamic range-query structure worth knowing.
Segment Tree
Answer range queries and point updates in O(log n). Segment trees are the go-to structure for range sum, range min, and similar problems.
More from Range queries
Stay in the same family when you want parallel variations of the same mental model.
Prefix Sum
Precompute cumulative totals so any range sum becomes one subtraction. Prefix sums are the first serious range-query idea everyone should know.
Difference Array
Apply many range updates in O(1) each, then rebuild the final values with one prefix pass. Difference arrays are the update-side twin of prefix sums.
Fenwick Tree (Binary Indexed Tree)
Maintain prefix sums with O(log n) updates and queries using one tiny bit trick. Fenwick trees are the lightest dynamic range-query structure worth knowing.
Li Chao Tree
Maintain dynamic line insertion and min/max queries by storing line candidates in a segment tree over x-values.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Range-query toolkit
Start with static prefix tricks, then move into offline, persistent, dynamic, and precomputed range-query structures.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.