-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment Tree.cpp
79 lines (67 loc) · 1.45 KB
/
Segment Tree.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
/**
* Made by: Diego Briaares
* At: 29.10.2021 15:20:06
**/
#include <bits/stdc++.h>
using namespace std;
void optimize() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int INF = (int) 1e9;
const int MAX = (int) 1e5;
int A[MAX];
int ST[4 * MAX];
void build(int l, int r, int node) {
if (l == r) {
ST[node] = A[l];
return;
}
if (l > r) return;
int mid = (l + r) / 2;
int left = node * 2 + 1;
int right = node * 2 + 2;
build(l, mid, left);
build(mid + 1, r, right);
ST[node] = min(ST[left], ST[right]);
}
void update(int l, int r, int node, int i, int v) {
if (l > r || l > i || r < i) return;
if (i <= l && r <= i) {
ST[node] = v;
return;
}
int mid = (l + r) / 2;
int left = node * 2 + 1;
int right = node * 2 + 2;
update(l, mid, left, i, v);
update(mid + 1, r, right, i, v);
ST[node] = min(ST[left], ST[right]);
}
int query(int l, int r, int node, int ql, int qr) {
if (l > r || l > qr || r < ql) return INF;
if (ql <= l && r <= qr) return ST[node];
int mid = (l + r) / 2;
int left = node * 2 + 1;
int right = node * 2 + 2;
return min(query(l, mid, left, ql, qr), query(mid + 1, r, right, ql, qr));
}
int main () {
optimize();
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) cin >> A[i];
build(0, n - 1, 0);
while (q--) {
int op, a, b;
cin >> op >> a >> b;
a--;
if (op == 1){
update(0, n - 1, 0, a, b);
}
else {
b--;
cout << query(0, n - 1, 0, a, b) << "\n";
}
}
}