您的位置:首页 > 其它

POJ 3468 A Simple Problem with Integers

2015-08-25 15:42 344 查看
//区间求和+区间更新//

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const LL MAX=100005;
LL sum[MAX<<2];
LL add[MAX<<2];
char s[8];
void pushup(LL rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(LL rt,LL m)
{
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*(m-(m>>1));
sum[rt<<1|1]+=add[rt]*(m>>1);
add[rt]=0;
}
}
void build(LL l,LL r,LL rt)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void updata(LL L,LL R,LL c,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
add[rt]+=c;
sum[rt]+=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
if(L<=m)
updata(L,R,c,lson);
if(R>m)
updata(L,R,c,rson);
pushup(rt);
}
LL querty(LL L,LL R,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
LL cnt=0;
if(L<=m)
cnt+=querty(L,R,lson);
if(R>m)
cnt+=querty(L,R,rson);
return cnt;
}
int main()
{
LL n,t;
scanf("%lld%lld",&n,&t);
build(1,n,1);
LL i;
LL x,y,z;
for(i=0;i<t;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%lld%lld",&x,&y);
printf("%lld\n",querty(x,y,1,n,1));
}
if(s[0]=='C')
{
scanf("%lld%lld%lld",&x,&y,&z);
updata(x,y,z,1,n,1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: