Sparse Table
Answer range-minimum and range-maximum queries in a single step, after a one-time table-building pass, when the data never changes.
After this lesson you will be able to
- Explain why overlapping blocks are fine for min, max and gcd (idempotent operations) and not for sum.
- Build a sparse table in O(n log n) and answer a query in O(1) using two blocks of the largest power of two that fits.
- Compute that power of two with
bit_length, and index the table correctly. - Use two tables together (min and max) and combine a table with a two-pointer scan.
- Decide between a sparse table, a segment tree, a Fenwick tree and prefix sums.
Before you start. Prefix Sum (the idea of precomputing for fast queries) and Binary Search helps for the two-pointer example. Comfort with powers of two.