-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegtree(rmq).cpp
More file actions
69 lines (65 loc) · 1.49 KB
/
segtree(rmq).cpp
File metadata and controls
69 lines (65 loc) · 1.49 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define all(a) a.begin(),a.end()
#define dbg(n) cout<<#n<<' '<<n<<endl;
#define dbg_v(v) cout<<#v<<":";for(auto x:v) cout<<" "<<x; cout<<endl;
ll power(ll x,ll y){ll res = 1;while(y>0){if(y&1)res = (res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
const int MX=2e5+5,INF=1e9+5;
int n,segt[4*MX];
void build(int a[],int v,int tl,int tr){
if(tl==tr){
segt[v]=a[tl];
}
else{
int tm=(tl+tr)/2;
build(a,v*2,tl,tm);
build(a,v*2+1,tm+1,tr);
segt[v]=min(segt[v*2],segt[v*2+1]);//condition
}
}
int query(int v,int tl,int tr,int l,int r){
if (l>r)
return INF; //condition
if (l==tl&&r==tr){
return segt[v];
}
int tm=(tl+tr)/2;
return min(query(v*2,tl,tm,l,min(r, tm)),query(v*2+1,tm+1,tr,max(l,tm+1),r));//condition
}
void update(int v,int tl,int tr,int pos,int val){
if (tl==tr){
segt[v]=val;
}
else{
int tm=(tl+tr)/2;
if (pos<=tm)
update(v*2,tl,tm,pos,val);
else
update(v*2+1,tm+1,tr,pos,val);
segt[v]=min(segt[v*2],segt[v*2+1]);//condition
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>n>>q;
int a[n+1];
for(int i=1;i<=n;i++)
cin>>a[i];
build(a,1,1,n);
while(q--){
int t,l,r;
cin>>t>>l>>r;
if(t%2){
update(1,1,n,l,r);
}
else
cout<<query(1,1,n,l,r)<<endl;
}
}