-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
68 lines (59 loc) · 1.63 KB
/
Copy pathsparse_table.cpp
File metadata and controls
68 lines (59 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Copyright 2019. Author Paul Kovalov
* */
#include <cmath>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::vector;
template <class T>
class SparseTable {
// sparse table itself
vector<vector<T>> sparse_table;
void build_sparse_table(vector<T> arr) {
// get height of the table
int height = ceil(log(arr.size()) / log(2));
// sparse table is of size height * arr.size()
sparse_table.resize(arr.size());
for (size_t i = 0; i < sparse_table.size(); ++i) {
sparse_table[i].resize(height, 0);
sparse_table[i][0] = arr[i];
}
int arr_s = arr.size();
// building the table
for (int j = 1; (1 << j) <= arr.size(); ++j) {
for (int i = 0; (i + (1 << j) - 1) < arr.size(); ++i) {
// sparse_table[k][i] = min(sparse_table[k-1][i], sparse_table[k-1][i + pow(2, k-1)]);
sparse_table[i][j] = min(sparse_table[i][j - 1],
sparse_table[i + (1 << (j - 1))][j - 1]);
}
}
}
public:
// accepts an array of type T, for which sparse table is built
SparseTable(vector<T> arr) { build_sparse_table(arr); }
int query(int l, int r) {
int k = (int)log2(r - l + 1);
return min(sparse_table[l][k], sparse_table[r - (1 << k) + 1][k]);
}
};
int main() {
// size of the input array and ariable to read array's element in
int n, x;
cin >> n;
vector<int> v;
for (int i = 0; i < n; ++i) {
cin >> x;
v.push_back(x);
}
SparseTable<int> st(v);
cout << "Enter l, r" << endl;
int l, r;
cin >> l >> r;
cout << st.query(l, r) << endl;
return 0;
}