-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic Range Minimum Queries.cpp
85 lines (68 loc) · 1.73 KB
/
Dynamic Range Minimum Queries.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forr(i, a, b) for (ll i = a; i < b; i++)
//------------------------------------------------------------------------
//------------------------------------------------------------------------
const int N = 2e5 + 5;
ll segmantTree[4 * N];
void build(ll arr[], ll rootPos, ll l, ll r) {
if (l == r) {
segmantTree[rootPos] = arr[l];
return;
}
ll m = (l + r) / 2;
build(arr, rootPos * 2, l, m);
build(arr, rootPos * 2 + 1, m + 1, r);
segmantTree[rootPos] =
min(segmantTree[rootPos * 2], segmantTree[rootPos * 2 + 1]);
return;
}
void update(ll rootPos, ll l, ll r, ll pos, ll newValue) {
if (pos < l || pos > r)
return;
if (l == r) {
segmantTree[rootPos] = newValue;
return;
}
ll m = (l + r) / 2;
if (l <= pos && pos <= m)
update(rootPos * 2, l, m, pos, newValue);
// if(l<=pos && pos<=m)
else
update(rootPos * 2 + 1, m + 1, r, pos, newValue);
segmantTree[rootPos] =
min(segmantTree[rootPos * 2], segmantTree[rootPos * 2 + 1]);
return;
}
ll query(ll rootPos, ll l, ll r, ll L, ll R) {
if (r < L || R < l)
if (L > R || l > r || l > R || r < L)
return LONG_LONG_MAX;
if (L <= l && r <= R)
return segmantTree[rootPos];
else {
ll m = (l + r) / 2;
ll ql = query(rootPos * 2, l, m, L, R);
ll qr = query(rootPos * 2 + 1, m + 1, r, L, R);
return min(ql, qr);
}
}
int main() {
ll q, n;
cin >> n >> q;
ll arr[n + 1];
forr(i, 1, n + 1) { cin >> arr[i]; }
build(arr, 1, 1, n);
forr(i, 0, q) {
ll k, a, b;
cin >> k >> a >> b;
if (k == 1) {
update(1, 1, n, a, b);
}
if (k == 2) {
cout << query(1, 1, n, a, b) << endl;
}
}
return 0;
}