-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRecursive Lazy Segment Tree.CPP
144 lines (132 loc) · 3.44 KB
/
Recursive Lazy 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
class SegTree {
public:
vector<ll> st;
vector<ll> lazy;
vector<bool> isLazy;
ll ID = 0; // change this
int n = 0;
SegTree(int s) {
n = s;
st.assign(4*n+5, ID);
lazy.assign(4*n+5, ID);
isLazy.assign(4*n+5, false);
}
void merge(ll& curr, ll& a, ll& b) { // change this
curr = a + b;
}
void propogate(int node, int L, int R) { // change this
if(L!=R) {
isLazy[2*node] = 1;
isLazy[2*node + 1] = 1;
lazy[2*node] = lazy[node];
lazy[2*node + 1] = lazy[node];
}
st[node] = (R-L+1)*lazy[node];
lazy[node] = ID;
isLazy[node] = 0;
}
void Build(int node, int L, int R, vector<ll>& arr) {
if(L==R) {
st[node] = arr[L];
return;
}
int M = (L + R) / 2;
Build(2*node, L, M, arr);
Build(2*node+1, M+1, R, arr);
merge(st[node], st[2*node], st[2*node+1]);
}
void pUpdate(int node, int L, int R, int pos, ll val) {
if(L==R) {
st[node] = val;
return;
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
if(pos<=M) {
pUpdate(2*node, L, M, pos, val);
}
else {
pUpdate(2*node+1, M+1, R, pos, val);
}
merge(st[node], st[2*node], st[2*node+1]);
}
void Update(int node, int L, int R, int lo, int hi, ll val) {
if(R<lo || L>hi) {
return;
}
if(lo<=L && R<=hi) {
isLazy[node] = 1;
lazy[node] = val;
propogate(node, L, R);
return;
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
Update(2*node, L, M, lo, hi, val);
Update(2*node+1, M+1, R, lo, hi, val);
merge(st[node], st[2*node], st[2*node+1]);
}
ll Query(int node, int L, int R, int lo, int hi) {
if(R<lo || L>hi) {
return ID;
}
if(lo<=L && R<=hi) {
return st[node];
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
ll lft = Query(2*node, L, M, lo, hi);
ll rght = Query(2*node+1, M+1, R, lo, hi);
ll curr = ID;
merge(curr, lft, rght);
return curr;
}
ll pQuery(int node, int L, int R, int pos) {
if(L==R) {
return st[node];
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
if(pos<=M) {
return pQuery(2*node, L, M, pos);
}
else {
return pQuery(2*node+1, M+1, R, pos);
}
}
void build(vector<ll>& arr) {
Build(1, 1, n, arr);
}
void update(int pos, ll val) {
pUpdate(1, 1, n, pos, val);
}
void update(int l, int r, ll val) {
Update(1, 1, n, l, r, val);
}
ll query(int lo, int hi) {
return Query(1, 1, n, lo, hi);
}
ll query(int pos) {
return pQuery(1, 1, n, pos);
}
};