-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorrible.cpp
More file actions
80 lines (78 loc) · 2.03 KB
/
horrible.cpp
File metadata and controls
80 lines (78 loc) · 2.03 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
#include<bits/stdc++.h>
using namespace std;
void updateTreeLazy(long* tree,long* lazy,int curPos,int low,int high,int startR,int endR,long val) {
if(low>high)
return;
if(lazy[curPos]!=0) {
tree[curPos]+=(high-low+1)*lazy[curPos];
if(low!=high) {
lazy[2*curPos]+=lazy[curPos];
lazy[2*curPos+1]+=lazy[curPos];
}
lazy[curPos]=0;
}
if(low>endR||high<startR)
return;
if(low>=startR&&high<=endR) {
tree[curPos]+=(high-low+1)*val;
if(low!=high) {
lazy[2*curPos]+=val;
lazy[2*curPos+1]+=val;
}
return;
}
int mid=(low+high)/2;
updateTreeLazy(tree,lazy,2*curPos,low,mid,startR,endR,val);
updateTreeLazy(tree,lazy,2*curPos+1,mid+1,high,startR,endR,val);
tree[curPos]=tree[2*curPos]+tree[2*curPos+1];
}
long queryTree(long* tree,long* lazy,int curPos,int low,int high,int startR,int endR) {
if(low>high)
return 0;
if(lazy[curPos]!=0) {
// Make pending updates to this node. Note that this
// node represents sum of elements in arr[low..high] and
// all these elements must be increased by lazy[curPos]
tree[curPos]+=(high-low+1)*lazy[curPos];
if(low!=high) {
lazy[2*curPos]+=lazy[curPos];
lazy[2*curPos+1]+=lazy[curPos];
}
lazy[curPos]=0;
}
if(low>endR||high<startR)
return 0;
if(low>=startR&&high<=endR) {
return tree[curPos];
}
int mid=(low+high)/2;
long ans1=queryTree(tree,lazy,2*curPos,low,mid,startR,endR);
long ans2=queryTree(tree,lazy,2*curPos+1,mid+1,high,startR,endR);
return ans1+ans2;
}
int main() {
// Write your code here
int t;
cin>>t;
while(t--) {
int n,c;
cin>>n>>c;
long* tree=new long[4*n+1]();
long* lazy=new long[4*n+1]();
while(c--) {
int x;
cin>>x;
if(x==0) {
long p,q,v;
cin>>p>>q>>v;
updateTreeLazy(tree,lazy,1,0,n-1,p-1,q-1,v);
} else {
int p,q;
cin>>p>>q;
long ans=queryTree(tree,lazy,1,0,n-1,p-1,q-1);
cout<<ans<<"\n";
}
}
}
return 0;
}