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
| typedef long long ll;
ll a[N],tr[4*N],t[4*N];
void pushup(int u)
{
tr[u]=tr[u<<1]+tr[u<<1|1];
}
void pushdown(int u,int l,int r)
{
if(t[u]!=0)
{
int mid=l+r>>1;
tr[u<<1]+=t[u]*(mid-l+1);
tr[u<<1|1]+=t[u]*(r-mid);
t[u<<1]+=t[u];
t[u<<1|1]+=t[u];
t[u]=0;
}
}
void build(int u,int l,int r)
{
if(l==r)
{
tr[u]=a[l];
return;
}
int mid=l+r>>1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
ll sum(int u,int l,int r,int s,int e)
{
if(s<=l&&e>=r)return tr[u];
int mid=l+r>>1;
pushdown(u,l,r);
ll res = 0;
if(s<=mid)res+=sum(u<<1,l,mid,s,e);
if(e>=mid+1)res+=sum(u<<1|1,mid+1,r,s,e);
return res;
}
void update(int u,int l,int r,int s,int e,ll c)
{
if(s<=l&&e>=r)
{
tr[u]+=c*(r-l+1);
t[u]+=c;
return ;
}
int mid = l+r>>1;
pushdown(u,l,r);
if(s<=mid)update(u<<1,l,mid,s,e,c);
if(e>=mid+1)update(u<<1|1,mid+1,r,s,e,c);
pushup(u);
}
|