-
Notifications
You must be signed in to change notification settings - Fork 381
/
Lazy Propagation
104 lines (94 loc) · 2.01 KB
/
Lazy Propagation
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
#include<bits/stdc++.h>
using namespace std;
#define intmax 1000000
#define endl "\n"
#define add(a,b) a+b
typedef long long ll ;
ll tree[intmax];
ll lazy[intmax];
void update(ll node , ll start , ll end, ll l ,ll r, ll value)
{
if(lazy[node]!= 0)
{
tree[node]+= (end-start +1)*lazy[node];
if(start != end)
{
lazy[node*2]+= lazy[node];
lazy[node*2+1]+= lazy[node];
}
lazy[node]=0;
}
if(start>end || r <start || l>end)
return;
if(l<= start && end<= r)
{
tree[node]+=(end-start +1) *value;
if(start != end)
{
lazy[node*2]+=value;
lazy[node*2+1]+= value;
}
return;
}
ll mid = (start+end)/2;
update(node*2,start,mid,l,r,value);
update(node*2+1,mid+1,end,l, r,value);
tree[node]=tree[node*2]+tree[node*2+1];
}
ll query (ll node , ll start, ll end , ll l, ll r)
{
if(r<start || l> end ||start >end)
{
return 0;
}
if(lazy[node]!= 0)
{
tree[node]+=(end-start+1)*lazy[node];
if(start != end)
{
lazy[node*2]+= lazy[node];
lazy[node*2+1]+=lazy[node];
}
lazy[node]= 0;
}
if(l<= start && end <= r)
{
return tree[node];
}
ll mid = (start+end)/2;
ll p1 = query(2*node, start ,mid , l, r);
ll p2 = query(2*node +1, mid +1 , end, l , r);
return (add(p1,p2));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin >> t;
while(t--){
memset(tree, 0, sizeof(tree));
memset(lazy, 0, sizeof(lazy));
ll n , q;
cin >> n>> q;
while(q--)
{
int ch ;
cin >> ch;
if(ch == 1)
{
ll l ,r;
cin >> l >>r;
cout<<query(1,0,n-1,l-1,r-1)<<endl;
}
else
{
ll l , r, value;
cin >> l>>r>>value;
update(1,0,n-1,l-1, r-1,value);
}
}
}
return 0;
}