-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLazy_Prop_Segment_Tree.cpp
More file actions
322 lines (288 loc) · 6.9 KB
/
Lazy_Prop_Segment_Tree.cpp
File metadata and controls
322 lines (288 loc) · 6.9 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//to inc. use +=, to set use = while updating in lazy prop.
// Lazy Propgation Segment Tree for and Range sum/max/min query and Range update (inc. all values in range by x)
// type = 1 for sum, type = 2 for max, type = 3 for min
struct SegTree {
public:
ll N;
vll seg, upd;
ll type;
SegTree(ll n, ll T)
{
N = n+1;
seg.resize(4*N,0);
upd.resize(4*N,0);
type = T;
}
void update(ll L,ll R,ll x,ll nd=1,ll l=1,ll r=-1)
{
if(r==-1) r=N;
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
if(type==1)
{
seg[nd]+=upd[nd]*(r-l+1);
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
}
else
{
seg[nd]+=upd[nd];
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
}
upd[nd]=0;
if(r<L || R<l) return;
if(r==l)
{
if(type==1) seg[nd]+=x*(r-l+1);
else seg[nd]+=x;
return;
}
if(L<=l && r<=R)
{
if(type==1)
{
seg[nd]+=x*(r-l+1);
upd[lnd]+=x;
upd[rnd]+=x;
}
else
{
seg[nd]+=x;
upd[lnd]+=x;
upd[rnd]+=x;
}
}
else
{
update(L,R,x,lnd,l,m);
update(L,R,x,rnd,m+1,r);
if(type == 1) seg[nd] = seg[lnd] + seg[rnd];
else if(type == 2) seg[nd] = max(seg[lnd],seg[rnd]);
else seg[nd] = min(seg[lnd],seg[rnd]);
}
}
ll query(ll L,ll R,ll nd=1,ll l=1,ll r=-1)
{
if(r==-1) r=N;
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
if(type==1)
{
seg[nd]+=upd[nd]*(r-l+1);
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
}
else
{
seg[nd]+=upd[nd];
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
}
upd[nd]=0;
if(r<L || R<l) return (type<=2)?0:1e18;
if(r==l) return seg[nd];
if(L<=l && r<=R) return seg[nd];
else
{
ll q1=query(L,R,lnd,l,m);
ll q2=query(L,R,rnd,m+1,r);
if(type == 1) seg[nd] = seg[lnd] + seg[rnd];
else if(type == 2) seg[nd] = max(seg[lnd],seg[rnd]);
else seg[nd] = min(seg[lnd],seg[rnd]);
if(type == 1) return q1+q2;
else if(type == 2) return max(q1,q2);
else return min(q1,q2);
}
}
};
//1-indexed
//Lazy Propgation Segment Tree for and Range min query and Range update (inc. all values in range by x)
ll n;
const ll N=2e5+1;
ll seg[4*N], upd[4*N];
void update(ll L,ll R,ll x,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
seg[nd]+=upd[nd];
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
upd[nd]=0;
if(r<L || R<l) return;
if(r==l)
{
seg[nd]+=x;
return;
}
if(L<=l && r<=R)
{
seg[nd]+=x;
upd[lnd]+=x;
upd[rnd]+=x;
}
else
{
update(L,R,x,lnd,l,m);
update(L,R,x,rnd,m+1,r);
seg[nd]=min(seg[lnd],seg[rnd]);
}
}
ll query(ll L,ll R,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
seg[nd]+=upd[nd];
if(l!=r)
{
upd[lnd]+=upd[nd];
upd[rnd]+=upd[nd];
}
upd[nd]=0;
if(r<L || R<l) return 1e18; //-1e18 for max
if(r==l) return seg[nd];
if(L<=l && r<=R) return seg[nd];
else
{
ll q1=query(L,R,lnd,l,m);
ll q2=query(L,R,rnd,m+1,r);
seg[nd]=min(seg[lnd],seg[rnd]);
return min(q1,q2);
}
}
//1-indexed
//Lazy Propgation Segment Tree for and Range sum query and Range update (set all values in range to x)
const ll N=2e5+1;
ll seg[4*N], upd[4*N];
void update(ll L,ll R,ll x,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
if(upd[nd]!=0)
{
seg[nd]=upd[nd]*(r-l+1);
if(l!=r)
{
upd[lnd]=upd[nd];
upd[rnd]=upd[nd];
}
upd[nd]=0;
}
if(r<L || R<l) return;
if(r==l)
{
seg[nd]=x*(r-l+1);
return;
}
if(L<=l && r<=R)
{
seg[nd]=x*(r-l+1);
upd[lnd]=x;
upd[rnd]=x;
}
else
{
update(L,R,x,lnd,l,m);
update(L,R,x,rnd,m+1,r);
seg[nd]=seg[lnd]+seg[rnd];
}
}
ll query(ll L,ll R,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
if(upd[nd]!=0)
{
seg[nd]=upd[nd]*(r-l+1);
if(l!=r)
{
upd[lnd]=upd[nd];
upd[rnd]=upd[nd];
}
upd[nd]=0;
}
if(r<L || R<l) return 0;
if(r==l) return seg[nd];
if(L<=l && r<=R) return seg[nd];
else
{
ll q1=query(L,R,lnd,l,m);
ll q2=query(L,R,rnd,m+1,r);
seg[nd]=seg[lnd]+seg[rnd];
return q1+q2;
}
}
//1-indexed
//Lazy Propgation Segment Tree for Range sum query and Range AP update (inc. all values in range by AP)
const ll N=2e5+1;
ll seg[4*N], ft[4*N], cd[4*N];
void update(ll L,ll R,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
seg[nd]+=sumAP(ft[nd],cd[nd],r-l+1);
if(l!=r)
{
ft[lnd]+=ft[nd];
cd[lnd]+=cd[nd];
ft[rnd]+=AP(ft[nd],cd[nd],m+1-l+1);
cd[rnd]+=cd[nd];
}
ft[nd]=0; cd[nd]=0;
if(r<L || R<l) return;
if(r==l)
{
seg[nd]+=l-L+1;
return;
}
if(L<=l && r<=R)
{
seg[nd]+=segAP(l-L+1,1,r-l+1);
ft[lnd]+=l-L+1;
cd[lnd]++;
ft[rnd]+=m+1-L+1;
cd[rnd]++;
}
else
{
update(L,R,lnd,l,m);
update(L,R,rnd,m+1,r);
seg[nd]=seg[lnd]+seg[rnd];
}
}
ll query(ll L,ll R,ll nd=1,ll l=1,ll r=n)
{
ll m=(l+r)/2, lnd=2*nd, rnd=2*nd+1;
seg[nd]+=segAP(ft[nd],cd[nd],r-l+1);
if(l!=r)
{
ft[lnd]+=ft[nd];
cd[lnd]+=cd[nd];
ft[rnd]+=AP(ft[nd],cd[nd],m+1-l+1);
cd[rnd]+=cd[nd];
}
ft[nd]=0; cd[nd]=0;
if(r<L || R<l) return 0;
if(r==l) return seg[nd];
if(L<=l && r<=R) return seg[nd];
else
{
ll q1=query(L,R,lnd,l,m);
ll q2=query(L,R,rnd,m+1,r);
seg[nd]=seg[lnd]+seg[rnd];
return q1+q2;
}
}
// Problem on Lazy Propagation Segment Tree (including mx,mn,sum range queries and update) and its beautiful implementation
// https://codeforces.com/contest/1439/problem/C
// https://codeforces.com/contest/1439/submission/120268200
// https://codedrills.io/contests/amrita-icpc-practice-session-7/problems/subarray-mex-sum
// https://codedrills.io/submissions/81644