-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_tree.sublime-snippet
More file actions
62 lines (58 loc) · 1.59 KB
/
segment_tree.sublime-snippet
File metadata and controls
62 lines (58 loc) · 1.59 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
<snippet>
<content><![CDATA[
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
int a[N];
struct ST {
int t[4 * N];
static const int inf = 1e9;
ST() {
memset(t, 0, sizeof t);
}
void build(int n, int b, int e) {
if (b == e) {
t[n] = a[b];
return;
}
int mid = (b + e) >> 1, l = n << 1, r = l | 1;
build(l, b, mid);
build(r, mid + 1, e);
t[n] = max(t[l], t[r]); // change this
}
void upd(int n, int b, int e, int i, int x) {
if (b > i || e < i) return;
if (b == e && b == i) {
t[n] = x; // update
return;
}
int mid = (b + e) >> 1, l = n << 1, r = l | 1;
upd(l, b, mid, i, x);
upd(r, mid + 1, e, i, x);
t[n] = max(t[l], t[r]); // change this
}
int query(int n, int b, int e, int i, int j) {
if (b > j || e < i) return -inf; // return appropriate value
if (b >= i && e <= j) return t[n];
int mid = (b + e) >> 1, l = n << 1, r = l | 1;
return max(query(l, b, mid, i, j), query(r, mid + 1, e, i, j)); // change this
}
}t;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n = 5;
for (int i = 1; i <= n; i++) {
a[i] = i;
}
t.build(1, 1, n); // building the segment tree
t.upd(1, 1, n, 2, 10); // assiging 10 to the index 2 (a[2] := 10)
cout << t.query(1, 1, n, 1, 5) << '\n'; // range max query on the segment [1, 5]
return 0;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>segment_tree</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.c++</scope>
</snippet>