forked from Shubham28/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHORRIBLE.cpp
118 lines (96 loc) · 1.8 KB
/
HORRIBLE.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
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define FOR(A,B,C) for(int A=B;A<C;A++)
#define EFOR(A,B,C) for(int A=B;A<=C;A++)
#define RFOR(A,B,C) for(int A=B;A>=C;A--)
#define MEM(A,B) memset(A,B,sizeof(A))
#define LL long long
using namespace std;
inline void Input(int &N)
{
int ch,sign;
N=0;
while((ch<'0'||ch>'9') && ch!='-' && ch!=EOF)
ch=getchar();
if(ch=='-')
sign=-1,ch=getchar();
else
sign=1;
do
N=(N<<3)+(N<<1)+(ch-'0');
while((ch=getchar())>='0' && ch<='9');
N*=sign;
return;
}
int N,Q,fr,to,add;
LL P[(1<<18)+5],mrk[(1<<18)+5];
void update(int ind,int st,int end)
{
int nxt=ind<<1;
if(mrk[ind]>0){
P[ind]+=(LL)(end-st+1)*mrk[ind];
if(st!=end){
mrk[nxt]+=mrk[ind];
mrk[nxt+1]+=mrk[ind];
}
mrk[ind]=0;
}
if(fr>end || to<st) return;
if(fr<=st && end<=to){
P[ind]+=(LL)(end-st+1)*(LL)add;
if(st!=end){
mrk[nxt]+=(LL)add;
mrk[nxt+1]+=(LL)add;
}
return;
}
int mid=(st+end)>>1;
update(2*ind,st,mid);
update(2*ind+1,mid+1,end);
P[ind]=P[nxt]+P[nxt+1];
return;
}
LL query(int ind,int st,int end)
{
int nxt=ind<<1;
if(mrk[ind]>0){
P[ind]+=(LL)(end-st+1)*mrk[ind];
if(st!=end){
mrk[nxt]+=mrk[ind];
mrk[nxt+1]+=mrk[ind];
}
mrk[ind]=0;
}
if(fr>end || to<st) return -1;
if(st>=fr && end<=to) return P[ind];
int mid=(st+end)>>1;
LL p1=query(nxt,st,mid);
LL p2=query(nxt+1,mid+1,end);
if(p1==-1) return p2;
if(p2==-1) return p1;
return p1+p2;
}
int main()
{
int T,ch;
Input(T);
while(T--){
Input(N),Input(Q);
int lim=min((1<<18)+2,(N+1)<<2);
memset(P,0,sizeof(LL)*lim);
memset(mrk,0,sizeof(LL)*lim);
FOR(as,0,Q){
Input(ch),Input(fr),Input(to);
fr--,to--;
if(ch==0){
Input(add);
update(1,0,N-1);
} else
printf("%lld\n",query(1,0,N-1));
}
}
return 0;
}