-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1647 - Static Range Minimum Queries.cpp
More file actions
70 lines (59 loc) · 1.52 KB
/
1647 - Static Range Minimum Queries.cpp
File metadata and controls
70 lines (59 loc) · 1.52 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
69
70
#include <bits/stdc++.h>
using namespace std;
class Solution {
protected:
int n, q;
vector<int> a;
Solution() {
cin >> n >> q;
a.assign(n, 0);
for (auto &i : a) cin >> i;
}
public:
virtual void solve() = 0;
};
class SegmentTreeSolution : public Solution {
private:
vector<int> seg;
void build(int id, int l, int r) {
if (l == r)
seg[id] = a[l];
else {
int m = (l + r) / 2;
build(id * 2, l, m);
build(id * 2 + 1, m + 1, r);
seg[id] = min(seg[2 * id], seg[2 * id + 1]);
}
}
int query(pair<int, int> range, int id, int l, int r) {
if (range.first > range.second) return INT_MAX;
if (range.first > r || range.second < l) return INT_MAX;
if (range.first == l && range.second == r) return seg[id];
int m = (l + r) / 2;
int left =
query(make_pair(range.first, min(range.second, m)), 2 * id, l, m);
int right = query(make_pair(max(range.first, m + 1), range.second),
2 * id + 1, m + 1, r);
return min(left, right);
}
public:
SegmentTreeSolution() {
seg.assign(4 * n, 0);
build(1, 0, n - 1);
}
void solve() {
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
cout << query(make_pair(l - 1, r - 1), 1, 0, n - 1) << "\n";
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Solution *sol = new SegmentTreeSolution();
sol->solve();
return 0;
}