-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment Tree with Lazy Propagation - Range Sum
98 lines (82 loc) · 2.32 KB
/
Segment Tree with Lazy Propagation - Range Sum
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
long long a[100000+50];
long long seg[400000+50];
long long lazy[400000+50];
void build(int node,int l,int r){
if(l==r){
// cout<<node<<" : "<<a[l]<<"\n";
seg[node]=a[l];
return;
}
int mid=(l+r)/2;
build(2*node+1,l,mid);
build(2*node+2,mid+1,r);
// cout<<node<<" ::: "<<__gcd(seg[2*node+1],seg[2*node+2])<<"\n";
seg[node]=seg[2*node+1]+seg[2*node+2];
}
long long query(int node,int l,int r,int ql,int qr){
if(lazy[node]){
seg[node]+=lazy[node]*(r-l+1LL);
// cout<<node<<" : "<<lazy[node]<<" : ( "<<l<<" , "<<r<<" )\n";
if(l!=r){
lazy[2*node+1]+=lazy[node];
lazy[2*node+2]+=lazy[node];
}
lazy[node]=0;
}
// cout<<"l = "<<l<<" , r = "<<r<<" , ql= "<<ql<<" , qr = "<<qr<<"\n";
if(l==r){
if(ql<=l&&qr>=r){
// cout<<"1st return value = "<<seg[node]<<"\n";
// cout<<"When "<<"l = "<<l<<" , r = "<<r<<" , ql= "<<ql<<" , qr = "<<qr<<"\n";
return seg[node];
}
else{
return 0;
}
}
//full overlap
if(ql<=l&&qr>=r){
// cout<<"2nd return value = "<<seg[node]<<"\n";
// cout<<"When "<<"l = "<<l<<" , r = "<<r<<" , ql= "<<ql<<" , qr = "<<qr<<"\n";
return seg[node];
}
else if(qr<l||ql>r){
// cout<<"ni/gga\n";
return 0;
}
int mid=(l+r)/2;
int le=query(2*node+1,l,mid,ql,qr);
int ri=query(2*node+2,mid+1,r,ql,qr);
if(le==-1&&ri==-1){
return 0;
}
return le+ri;
}
void update(int node,int l,int r,int ql,int qr,long long val){
if(lazy[node]){
seg[node]+=lazy[node]*(r-l+1LL);
if(l!=r){
lazy[2*node+1]+=lazy[node];
lazy[2*node+2]+=lazy[node];
}
lazy[node]=0;
}
if(l>r||l>qr||r<ql){
return;//if no overlap
}
//full overlap
if(ql<=l&&qr>=r){
seg[node]+=val*(r-l+1LL);
if(l!=r){
lazy[2*node+1]+=val;
lazy[2*node+2]+=val;
}
}
else{
int mid=(l+r)/2;
update(2*node+1,l,mid,ql,qr,val);
update(2*node+2,mid+1,r,ql,qr,val);
seg[node]=seg[2*node+1]+seg[2*node+2];
}
// cout<<node<<" ,,,, "<<__gcd(seg[2*node+1],seg[2*node+2])<<"\n";
}