-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Range Queries) Static Minimum Range Queries
66 lines (56 loc) · 1.31 KB
/
(Range Queries) Static Minimum Range Queries
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long mod =1e9+7;
vector<int> vec;
vector<int> segTree;
void buildTree(int pos, int l, int r){
if(l==r){
segTree[pos]=vec[l];
return;
}
int mid=(l+r)/2;
buildTree(2*pos, l, mid);
buildTree(2*pos+1, mid+1, r);
segTree[pos]=min(segTree[2*pos], segTree[2*pos+1]);
}
void updateTree(int k, int u, int l, int r, int pos){
if(l==r && k==l){
segTree[pos]=u;
return;
}
if(k <= r && k>= l){
int mid=(l+r)/2;
updateTree(k, u, l, mid, 2*pos);
updateTree(k, u, mid+1, r, 2*pos+1);
segTree[pos]=segTree[2*pos+1]+segTree[2*pos];
}
}
int minQuery(int a, int b, int l, int r, int pos){
if(a>r || b<l){
return INT32_MAX;
}
if(a<=l && b>=r){
return segTree[pos];
}
int mid=(l+r)/2;
return min(minQuery(a, b, l, mid, 2*pos), minQuery(a, b, mid+1, r, 2*pos+1));
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin>>n>>q;
vec.resize(n+1);
segTree.resize(4*n+1);
for(int i=1; i<=n; i++){
cin>>vec[i];
}
buildTree(1, 1, n);
for(int i=0; i<q; i++){
int a, b;
cin>>a>>b;
cout<<minQuery(a, b, 1, n, 1)<<endl;
}
return 0;
}