-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticRangeMinimumQueries.cpp
More file actions
57 lines (46 loc) · 1.23 KB
/
StaticRangeMinimumQueries.cpp
File metadata and controls
57 lines (46 loc) · 1.23 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
//Static Range Minimum Queries - https://cses.fi/problemset/task/1647
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int inf = 1e9 + 17;
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto compute_log2 = [&](ll x) -> int {
return x > 0 ? __builtin_clzll(1) - __builtin_clzll(x) : -1;
};
int k = compute_log2(n);
vector<vector<int>> sparse_table(k + 1, vector<int>(n + 1, inf));
for (int i = 0; i < n; i++) {
sparse_table[0][i] = a[i];
}
//cp-algorithms code
for (int i = 1; i <= k; i++) {
for (int j = 0; j + (1 << i) <= n; j++) {
sparse_table[i][j] = min(sparse_table[i - 1][j], sparse_table[i - 1][j + (1 << (i - 1))]);
}
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--, r--;
int lg = compute_log2(r - l + 1);
cout << min(sparse_table[lg][l], sparse_table[lg][r - (1 << lg) + 1]) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}