A Sparse Table maintains an array of numbers by maintaining
which costs a space of .
Initialize
This is an algorithm that initializes the Sparse Table with in time and space.
Lemma
Applying the lemma yields an algorithm that solves the problem in time and space.
f[0] = a;
for (int i = 1; i <= std::__lg(n); i++) {
for (int j = 0; j + (1 << i) <= n; j++) {
f[i][j] = std::max(f[i - 1][j], f[i - 1][j + (1 << (i - 1))]);
}
}Range Maximum Query
This is an algorithm that computes in time and space.
Lemma
Applying the lemma yields an algorithm that solves the problem in time and space.
int i = std::__lg(r - l);
return std::max(f[i][l], f[i][r - (1 << i)]);