https://www.hackerearth.com/practice/notes/sparse-table/
https://cp-algorithms.com/data_structures/sparse-table.html
https://cp-algorithms.com/data_structures/sparse-table.html
Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in , but its true power is answering range minimum queries (or equivalent range maximum queries). For those queries it can compute the answer in time.
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. . For a number there can be at most summands.
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. , where the complete interval has length 13, and the individual intervals have the lengths 8, 4 and 1 respectably. And also here the union consists of at most many intervals.
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. will store the answer for the range of length . The size of the 2-dimensional array will be , where is the biggest possible array length. has to satisfy , because is the biggest power of two range, that we have to support. For arrays with reasonable length ( elements), is a good value.
int st[MAXN][K + 1];
Because the range of length splits nicely into the ranges and , both of length , we can generate the table efficiently using dynamic programming:
for (int i = 0; i < N; i++)
st[i][0] = f(array[i]);
for (int j = 1; j <= K; j++)
for (int i = 0; i + (1 << j) <= N; i++)
st[i][j] = f(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
The function will depend on the type of query. For range sum queries it will compute the sum, for range minimum queries it will compute the minimum.
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 is . We can construct the data structure with:
long long st[MAXN][K];
for (int i = 0; i < N; i++)
st[i][0] = array[i];
for (int j = 1; j <= K; j++)
for (int i = 0; i + (1 << j) <= N; i++)
st[i][j] = st[i][j-1] + st[i + (1 << (j - 1))][j - 1];
To answer the sum query for the range , we iterate over all powers of two, starting from the biggest one. As soon as a power of two is smaller or equal to the length of the range (), we process the first the first part of range , and continue with the remaining range .
long long sum = 0;
for (int j = K; j >= 0; j--) {
if ((1 << j) <= R - L + 1) {
sum += st[L][j];
L += 1 << j;
}
}
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 into the ranges and . The range minimum of is clearly the same as the minimum of the range minimum of and the range minimum of . So we can compute the minimum of the range with:
This requires that we are able to compute fast. You can accomplish that by precomputing all logarithms:
int log[MAXN+1];
log[1] = 0;
for (int i = 2; i <= MAXN; i++)
log[i] = log[i/2] + 1;
Afterwards we need to precompute the Sparse Table structure. This time we define with .
int st[MAXN][K];
for (int i = 0; i < N; i++)
st[i][0] = array[i];
for (int j = 1; j <= K; j++)
for (int i = 0; i + (1 << j) <= N; i++)
st[i][j] = min(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
And the minimum of a range can be computed with:
int j = log[R - L + 1];
int minimum = min(st[L][j], st[R - (1 << j) + 1][j]);
We have an array arr[0 . . . n-1]. We need to efficiently find the minimum value from index L (query start) to R (query end) where 0 <= L <= R <= n-1. Consider a situation when there are many range queries.
Example:
Input: arr[] = {7, 2, 3, 0, 5, 10, 3, 12, 18}; query[] = [0, 4], [4, 7], [7, 8] Output: Minimum of [0, 4] is 0 Minimum of [4, 7] is 3 Minimum of [7, 8] is 12
The idea is to precompute minimum of all subarrays of size 2j where j varies from 0 to Log n. We make a table lookup[i][j] such that lookup[i][j] contains minimum of range starting from i and of size 2j. For example lookup[0][3] contains minimum of range [0, 7] (starting with 0 and of size 23)
How to fill this lookup or sparse table?
The idea is simple, fill in bottom up manner using previously computed values. We compute ranges with current power of 2 using values of lower power of two. For example, to find minimum of range [0, 7] (Range size is power of 3), we can use minimum of following two.
a) Minimum of range [0, 3] (Range size is power of 2)
b) Minimum of range [4, 7] (Range size is power of 2)
The idea is simple, fill in bottom up manner using previously computed values. We compute ranges with current power of 2 using values of lower power of two. For example, to find minimum of range [0, 7] (Range size is power of 3), we can use minimum of following two.
a) Minimum of range [0, 3] (Range size is power of 2)
b) Minimum of range [4, 7] (Range size is power of 2)