-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCEPC11I.cpp
69 lines (68 loc) · 1.47 KB
/
DCEPC11I.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
#include<iostream>
#include<vector>
using namespace std;
#define ll long long
vector<ll> tr,upd,times;
//ll tr[100],upd[100],times[100];
void upd_node(ll node,ll b,ll e)
{
ll k=(e-b+1),p;
tr[node]+=(k*(2*upd[node]+(k-1)*times[node]))/2;
if(b!=e)
{
upd[node*2]+=upd[node];
times[node*2]+=times[node];
p=(b+e)/2+1-b+1;
upd[node*2+1]+=upd[node]+(p-1)*times[node];
times[node*2+1]+=times[node];
}
upd[node]=times[node]=0;
}
ll update(ll node,ll b,ll e,ll i,ll j)
{
ll k=e-b+1,p,v;
upd_node(node,b,e);
if(j<b || i>e) return tr[node];
if(b>=i && e<=j)
{
v=1+(b-i);
tr[node]+=(k*(2*v+(k-1)))/2;
if(b!=e)
{
upd[node*2]+=v;
times[node*2]++;
p=(b+e)/2+1-i+1;
upd[node*2+1]+=1+(p-1);
times[node*2+1]++;
}
return tr[node];
}
ll l,r;
l=update(node*2,b,(b+e)/2,i,j);
r=update(node*2+1,(b+e)/2+1,e,i,j);
return tr[node]=l+r;
}
ll read(ll node,ll b,ll e,ll i,ll j)
{
upd_node(node,b,e);
if(j<b || i>e) return 0;
if(b>=i && e<=j) return tr[node];
ll l,r;
l=read(node*2,b,(b+e)/2,i,j);
r=read(node*2+1,(b+e)/2+1,e,i,j);
return l+r;
}
int main()
{
ll n,i,j,k,q,t,a,b;
cin>>n>>q;
tr.resize(4*n,0);
upd.resize(4*n,0);
times.resize(4*n,0);
while(q--)
{
cin>>t>>a>>b;
if(t==0) update(1,1,n,a,b);
else cout<<read(1,1,n,a,b)<<"\n";
}
}