38std::vector<T> computeLogs(
const std::vector<T>& A) {
40 std::vector<T> logs(n);
42 for (
int i = 2; i < n; i++) {
43 logs[i] = logs[i / 2] + 1;
56std::vector<std::vector<T> > buildTable(
const std::vector<T>& A,
57 const std::vector<T>& logs) {
59 std::vector<std::vector<T> > table(20, std::vector<T>(n + 5, 0));
61 for (
int i = 0; i <= logs[n]; i++) {
63 for (
int j = 0; j + curLen < n; j++) {
68 std::min(table[i - 1][j], table[i - 1][j + curLen / 2]);
84int getMinimum(
int beg,
int end,
const std::vector<T>& logs,
85 const std::vector<std::vector<T> >& table) {
86 int p = logs[end - beg + 1];
88 return std::min(table[p][beg], table[p][end - pLen + 1]);
97 std::vector<int> A{1, 2, 0, 3, 9};
98 std::vector<int> logs = range_queries::sparse_table::computeLogs(A);
99 std::vector<std::vector<int> > table =
100 range_queries::sparse_table::buildTable(A, logs);
101 assert(range_queries::sparse_table::getMinimum(0, 0, logs, table) == 1);
102 assert(range_queries::sparse_table::getMinimum(0, 4, logs, table) == 0);
103 assert(range_queries::sparse_table::getMinimum(2, 4, logs, table) == 0);
Functions for Implementation of Sparse Table