Sparse Table¶
Sparse Table is a data structure, that allows answering range queries.
It can answer most range queries in
The only drawback of this data structure is, that it can only be used on immutable arrays. This means, that the array cannot be changed between two queries. If any element in the array changes, the complete data structure has to be recomputed.
Intuition¶
Any non-negative number can be uniquely represented as a sum of decreasing powers of two.
This is just a variant of the binary representation of a number.
E.g.
By the same reasoning any interval can be uniquely represented as a union of intervals with lengths that are decreasing powers of two.
E.g.
The main idea behind Sparse Tables is to precompute all answers for range queries with power of two length. Afterwards a different range query can be answered by splitting the range into ranges with power of two lengths, looking up the precomputed answers, and combining them to receive a complete answer.
Precomputation¶
We will use a 2-dimensional array for storing the answers to the precomputed queries.
The
int st[K + 1][MAXN];
Because the range
std::copy(array.begin(), array.end(), st[0]);
for (int i = 1; i <= K; i++)
for (int j = 0; j + (1 << i) <= N; j++)
st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
The function
The time complexity of the precomputation is
Range Sum Queries¶
For this type of queries, we want to find the sum of all values in a range.
Therefore the natural definition of the function
long long st[K + 1][MAXN];
std::copy(array.begin(), array.end(), st[0]);
for (int i = 1; i <= K; i++)
for (int j = 0; j + (1 << i) <= N; j++)
st[i][j] = st[i - 1][j] + st[i - 1][j + (1 << (i - 1))];
To answer the sum query for the range
long long sum = 0;
for (int i = K; i >= 0; i--) {
if ((1 << i) <= R - L + 1) {
sum += st[i][L];
L += 1 << i;
}
}
Time complexity for a Range Sum Query is
Range Minimum Queries (RMQ)¶
These are the queries where the Sparse Table shines.
When computing the minimum of a range, it doesn't matter if we process a value in the range once or twice.
Therefore instead of splitting a range into multiple ranges, we can also split the range into only two overlapping ranges with power of two length.
E.g. we can split the range
This requires that we are able to compute
int lg[MAXN+1];
lg[1] = 0;
for (int i = 2; i <= MAXN; i++)
lg[i] = lg[i/2] + 1;
// C++20
#include <bit>
int log2_floor(unsigned long i) {
return std::bit_width(i) - 1;
}
// pre C++20
int log2_floor(unsigned long long i) {
return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
}
lg
array is slower because of cache misses.
Afterwards we need to precompute the Sparse Table structure. This time we define
int st[K + 1][MAXN];
std::copy(array.begin(), array.end(), st[0]);
for (int i = 1; i <= K; i++)
for (int j = 0; j + (1 << i) <= N; j++)
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
And the minimum of a range
int i = lg[R - L + 1];
int minimum = min(st[i][L], st[i][R - (1 << i) + 1]);
Time complexity for a Range Minimum Query is
Similar data structures supporting more types of queries¶
One of the main weakness of the
There are similar data structures that can handle any type of associative functions and answer range queries in
Practice Problems¶
- SPOJ - RMQSQ
- SPOJ - THRBL
- Codechef - MSTICK
- Codechef - SEAD
- Codeforces - CGCDSSQ
- Codeforces - R2D2 and Droid Army
- Codeforces - Maximum of Maximums of Minimums
- SPOJ - Miraculous
- DevSkill - Multiplication Interval (archived)
- Codeforces - Animals and Puzzles
- Codeforces - Trains and Statistics
- SPOJ - Postering
- SPOJ - Negative Score
- SPOJ - A Famous City
- SPOJ - Diferencija
- Codeforces - Turn off the TV
- Codeforces - Map
- Codeforces - Awards for Contestants
- Codeforces - Longest Regular Bracket Sequence
- Codeforces - Array Stabilization (GCD version)