poj 3468 成段增减

时间:2023-03-09 06:54:54
poj 3468 成段增减

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
using namespace std;
const int maxn=;
ll sum[maxn<<];
ll cov[maxn<<];
int n,m,t;
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int m)
{
if(cov[rt])
{
cov[rt<<]+=cov[rt];
cov[rt<<|]+=cov[rt];
sum[rt<<]+=cov[rt]*(m-(m>>)); //注意括号
sum[rt<<|]+=cov[rt]*(m>>);
cov[rt]=;
}
}
void build(int l,int r,int rt)
{
cov[rt]=;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int add,int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
cov[rt]+=add;
sum[rt]+=(ll)add*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(add,L,R,lson);
if(m<R) update(add,L,R,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if (L<=l&&r<=R)
{
return sum[rt];
}
pushdown(rt,r-l+);
int m=(l+r)>>;
ll ret=;
if (L<=m) ret+=query(L,R,lson);
if (m<R) ret+=query(L,R,rson);
return ret;
}
int main()
{
int i,j,k,q;
//freopen("1.in","r",stdin);
scanf("%d%d",&n,&q);
build(,n,);
while(q--)
{
char s[];
scanf("%s",s);
if(s[]=='Q')
{
int L,R;
scanf("%d%d",&L,&R);
printf("%lld\n",query(L,R,,n,));
}
else
{
int L,R,c;
scanf("%d%d%d",&L,&R,&c);
update(c,L,R,,n,);
}
}
return ;
}