forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicRangeMinimumQueries.cpp
131 lines (115 loc) · 2.43 KB
/
dynamicRangeMinimumQueries.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Solution of dynamic range minimum query problem
//Using segment tree query & update methods
//There are two types of queries,
//Type 1: Find the minimum element in the given range
//Type 2: Given index of the array, update the element
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define F first
#define S second
typedef long long int ll;
const int mod = 1e9+7;
void FAST(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
}
void fileIO(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll binpow(ll x, ll y){
x%=mod;
ll res=1;
while(y>0){
if(y&1){
res = ((res%mod)*(x%mod))%mod;
}
x = ((x%mod)*(x%mod))%mod;
y >>=1;
}
return res;
}
vector<ll> prime;
void sieve(){
vector<bool> v(1e6+1, 1);
for(ll i=2 ; i<=1e6 ; i++){
if(v[i]){
for(ll j=i*i ; j<=1e6 ; j+=i){
v[j] = 0;
}
prime.push_back(i);
}
}
}
ll query(vector<ll>&st, ll tl, ll tr, ll tv, ll l, ll r){
// completely outside the given range
if(tl > r || tr < l){
return 1e9;
}
// completely inside the given range
if(tl >= l && tr <= r){
return st[tv];
}
// Partially inside and partially outside
ll tm = (tl+tr)/2;
ll ans1 = query(st, tl, tm, 2*tv, l, r);
ll ans2 = query(st, tm+1, tr, 2*tv+1, l, r);
return min(ans1,ans2);
}
void updateTree(vector<ll>&arr, vector<ll>&st, ll tl, ll tr, ll tv, ll idx, ll value){
if(tl==tr){
arr[idx] = value;
st[tv] = arr[idx];
return;
}
ll tm = (tl+tr)/2;
if(idx > tm){
updateTree(arr, st, tm+1, tr, 2*tv+1, idx, value);
} else {
updateTree(arr, st, tl, tm, 2*tv, idx, value);
}
st[tv] = min(st[2*tv],st[2*tv+1]);
}
void buildTree(vector<ll>&arr, vector<ll>&st, ll tl, ll tr, ll tv){
// BASE CASE
if(tl == tr){
st[tv] = arr[tl];
return;
}
ll tm =(tl+tr)/2;
// two recursive calls to build the left half and
// right half respectively
buildTree(arr, st, tl, tm, 2*tv);
buildTree(arr, st, tm+1, tr, 2*tv+1);
// the answer to the recursive calls gets stored in
// the tv
st[tv] = min(st[2*tv],st[2*tv + 1]);
}
int main(){
FAST();
fileIO();
ll n,q,x, a,b,k,u,qt;
cin>>n>>q;
vector<ll> arr(n);
vector<ll> st(4*n);
for(ll i=0 ; i<n ; i++){
cin>>arr[i];
}
buildTree(arr, st, 0,n-1,1);
for(ll i=0 ; i<q ; i++){
cin>>qt;
if(qt==1){
cin>>k>>u;
updateTree(arr, st, 0, n-1, 1, k-1, u);
}
if(qt==2){
cin>>a>>b;
cout<<query(st, 0, n-1, 1, a-1, b-1)<<"\n";
}
}
return 0;
}