forked from kumaranshu72/LuciferDataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment Tree Lazy Propagation.cpp
67 lines (66 loc) · 1.08 KB
/
Segment Tree Lazy Propagation.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll tree[400001],tree1[400001];
void lazy(ll node,ll l,ll r)
{
tree[node]+=tree1[node]*(r-l+1);
if(l==r)
{
tree1[node]=0;
return;
}
tree1[2*node]+=tree1[node];
tree1[2*node+1]+=tree1[node];
tree1[node]=0;
}
ll q0(ll node,ll l,ll r,ll l1,ll r1,ll val=0){
//cout<<val<<endl;
lazy(node,l,r);
if(l1<=l&&r<=r1)
{
tree1[node]+=val;
lazy(node,l,r);
//cout<<tree[node]<<" ";
return tree[node];
}
else if(r<l1||r1<l)
{
return 0;
}
else{
ll mid=(l+r)/2;
ll x1=q0(2*node,l,mid,l1,r1,val);
ll x2=q0(2*node+1,mid+1,r,l1,r1,val);
tree[node]=tree[2*node]+tree[2*node+1];
return x1+x2;
}
}
int main() {
// your code goes here
ll t;
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
ll i,j,k;
memset(tree,0,sizeof(tree));
memset(tree1,0,sizeof(tree1));
while(m--)
{
ll x,p,q,v;
cin>>x;
if(x==0)
{
cin>>p>>q>>v;
q0(1,1,n,p,q,v);
}
else{
cin>>p>>q;
cout<<q0(1,1,n,p,q)<<endl;
}
}
}
return 0;
}