-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazytree.cpp
135 lines (108 loc) · 2.78 KB
/
lazytree.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
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
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <deque>
#include <math.h>
#include <string>
#include <queue>
#include <string>
#include <limits.h>
#include <stack>
#include <list>
#include <set>
using namespace std;
vector<long> ans;
typedef pair<set<long> ::iterator, set<long> :: iterator> ps;
const long maxlong = 1000000000;
vector<long> arr;
vector<long> seg;
vector<long> lazy;
long build( long ss, long se, long si){
if(ss ==se){
seg[si]=arr[ss];
return seg[si];
}
long mid = (se+ss)/2;
seg[si]= build(ss,mid,2*si+1)+build(mid+1,se,2*si+2);
return seg[si];
}
void update(long ss, long se, long si, long i, long j, long val){
if(lazy[si]!=0){
seg[si]+=(lazy[si] * (se-ss+1));
if(ss!=se){//not leaf node
lazy[2*si+1]+=lazy[si];
lazy[2*si+2]+=lazy[si];
}
lazy[si]=0;
}
if(ss>se || ss>j || se<i)return; //out of range
if(i<=ss && j>= se){//segment is fully in range
seg[si] += (val * (se-ss+1));
if(se!=ss){
lazy[2*si+1]+=val;
lazy[2*si+2]+=val;
}
return;
}
if(ss!=se){
long mid = (se+ss)/2;
update(ss,mid,2*si+1,i,j,val);
update(mid+1,se,2*si+2,i,j,val);
seg[si]= seg[2*si+1]+seg[2*si+2];
}
else{
seg[si]+=val;
}
}
long getSum(long ss, long se, long qs, long qe,long si){
if( qs>se || qe<ss)return 0;
long ans=0;
if(lazy[si]!=0){
seg[si]+=lazy[si]*(se-ss+1);
if(ss!=se){
lazy[2*si+1]+=lazy[si];
lazy[2*si+2]+=lazy[si];
}
lazy[si]=0;
}
if(qs <= ss && qe >= se){
return seg[si];
}
long a=0,b=0;
long mid = (ss+se)/2;
a = getSum(ss,mid,qs,qe,2*si+1);
b = getSum(mid+1,se,qs,qe,2*si+2);
return a+b;
}
int main(){
long t;
cin>>t;
while(t--){
long N,C;
cin>>N>>C;
arr.resize(N);
long f = (long)(ceil(log2(N)));
long max_size = 2* pow(2,f) -1;
seg.resize(max_size,0);
lazy.resize(max_size,0);
for(long i=0; i<C; i++){
long a;
cin>>a;
if(a==0){//update tree
long rl,rr,val;
cin>>rl>>rr>>val;
update(0,N-1,0,rl-1,rr-1,val);
}
else{ //query tree
long rl,rr;
cin>>rl>>rr;
cout<<getSum(0, N-1, rl-1, rr-1, 0)<<endl;;
}
}
}
}