-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatic_Range_Minimum_Queries.cpp
More file actions
73 lines (69 loc) · 1.91 KB
/
Copy pathStatic_Range_Minimum_Queries.cpp
File metadata and controls
73 lines (69 loc) · 1.91 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
71
72
73
#include "bits/stdc++.h"
using namespace std;
class SegmentTree {
private:
vector<int> tree;
int n;
void build(vector<int>& arr, int node, int start, int end) {
if (start == end) {
tree[node] = arr[start];
}
else {
int mid = (start + end) / 2;
build(arr, 2 * node + 1, start, mid);
build(arr, 2 * node + 2, mid + 1, end);
tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
}
}
int query(int node, int start, int end, int l, int r) {
if (r < start || end < l) {
return INT_MAX;
}
if (l <= start && end <= r) {
return tree[node];
}
int mid = (start + end) / 2;
int leftMin = query(2 * node + 1, start, mid, l, r);
int rightMin = query(2 * node + 2, mid + 1, end, l, r);
return min(leftMin, rightMin);
}
void update(int node, int start, int end, int idx, int value) {
if (start == end) {
tree[node] = value;
} else {
int mid = (start + end) / 2;
if (start <= idx && idx <= mid) {
update(2 * node + 1, start, mid, idx, value);
} else {
update(2 * node + 2, mid + 1, end, idx, value);
}
tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
}
}
public:
SegmentTree(vector<int>& arr) {
n = arr.size();
tree.resize(4 * n);
build(arr, 0, 0, n - 1);
}
int rangeMinQuery(int l, int r) {
return query(0, 0, n - 1, l, r);
}
void updateValue(int idx, int value) {
update(0, 0, n - 1, idx, value);
}
};
int main(){
int n,q;
cin>>n>>q;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
SegmentTree st(a);
while(q--){
int l,r;
cin>>l>>r;
cout<<st.rangeMinQuery(l-1,r-1)<<endl;
}
}