-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsegtreelazy.sublime-snippet
executable file
·81 lines (75 loc) · 1.91 KB
/
segtreelazy.sublime-snippet
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
<snippet>
<content><![CDATA[
const int MAX = 1e5 + 5;
const int LIM = 3e5 + 5; //equals 2 * 2^ceil(log2(n))
int ar[MAX];
int tree[LIM];
int lazy[LIM];
int combine(int &a, int &b) {
return max(a, b);
}
void push(int l, int r, int node) {
if (lazy[node]) {
tree[node] += lazy[node];
if (l != r){
lazy[idx << 1] += lazy[index];
lazy[(idx << 1)|1] += lazy[index];
}
lazy[index] = 0;
}
}
void build(int l, int r, int idx) {
lazy[index] = 0;
if (l == r) {
tree[index] = a[l];
return ;
}
int mid = (l+r) >> 1;
build(l , mid , idx << 1);
build(mid + 1 , r , (idx << 1)|1);
tree[idx] = combine(tree[idx << 1], tree[(idx << 1)|1]);
}
void update(int l, int r, int ql, int qr, int val , int idx) {
push(idx, l, r);
if (l > qr || r < ql || r < l) {
return;
}
if (ql <= l && qr <= r) {
lazy[idx] += val;
push(idx, l, r);
return;
}
int mid = (l+r) >> 1;
update(l, mid, ql, qr, val , idx << 1);
update(mid + 1, r, ql, qr, val , (idx << 1)|1);
tree[idx] = combine(tree[idx << 1], tree[(idx << 1)|1]);
}
int query(int l, int r, int ql, int qr , int idx) {
push(idx, l, r);
if (l > qr || r < ql || r < l){
return 0;
}
if (ql <= l && r <= qr){
return tree[idx];
}
int mid = (l+r) >> 1;
if (ql <= mid) {
if (qr <= mid) {
return query(l, mid, ql, qr, idx << 1);
}else{
int a = query(l, mid, ql, qr, idx << 1);
int b = query(mid + 1,r ,rc ,ql , (idx << 1)|1);
return combine(a, b);
}
}else {
return query(mid + 1, r, ql, qr , (idx << 1)|1);
}
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>segtreelazy</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.cpp, source.c++, source.c</scope>
<!-- Optional: Description to show in the menu -->
<description>Lazy Segment Tree</description>
</snippet>