Data Structures19 sections · 729 units
Open in Course

Disjoint Sparse Tables

For non-idempotent operations

Disjoint Sparse Tables achieve O(1)O(1) range queries for ANY associative operation, not idempotent ones.

The idea: instead of overlapping ranges, use non-overlapping decomposition. Precompute answers for ranges that share a midpoint.

For each level jj, divide the array into blocks of size 2j2^j. For each block, compute prefix from the middle to left and suffix from middle to right.

Query: find the level where ll and rr are in different halves of the same block. Combine the precomputed prefix/suffix. Construction: O(nlogn)O(n \log n). Query: O(1)O(1). This structure is more complex but handles sum, product, and other non-idempotent operations in constant time.