您的位置:首页 > 其它

poj3468 A Simple Problem with Integers

2015-07-24 22:20 281 查看
        线段树模板题。

        要注意开long long。

        话不多说。

#include<stdio.h>
typedef long long ll;
ll n,m;
ll num[100000],tree[100005*4],sign[100005*4];
void pushup(ll node)
{
tree[node]=tree[node<<1]+tree[node<<1|1];
return ;
}
void pushdown(ll node,ll l,ll r)
{
if(sign[node])
{
sign[node<<1]+=sign[node];
sign[node<<1|1]+=sign[node];
ll mid=(l+r)>>1;
tree[node<<1]+=(mid-l+1)*sign[node];
tree[node<<1|1]+=(r-mid)*sign[node];
sign[node]=0;
}
}
void build(ll l,ll r,ll node)
{
sign[node]=0;
if(l==r)
{
scanf("%lld",&tree[node]);
return ;
}
ll mid=(l+r)>>1;
build(l,mid,node<<1);
build(mid+1,r,node<<1|1);
pushup(node);
}
void update(ll l,ll r,ll st,ll en,ll add,ll node)
{
if(st>=l&&en<=r)
{
tree[node]+=(en-st+1)*add;
sign[node]+=add;
return ;
}
pushdown(node,st,en);
ll mid=(st+en)>>1;
if(l<=mid)update(l,r,st,mid,add,node<<1);
if(r>mid)update(l,r,mid+1,en,add,node<<1|1);
pushup(node);
}
ll query(ll l,ll r,ll st,ll en,ll node)
{
if(st>=l&&en<=r)return tree[node];
pushdown(node,st,en);
ll mid=(st+en)>>1;
ll ret=0;
if(mid>=l)ret+=query(l,r,st,mid,node<<1);
if(mid<r)ret+=query(l,r,mid+1,en,node<<1|1);
return ret;
}
char s[3];
int main()
{
scanf("%lld %lld",&n,&m);
build(1,n,1);
for(ll i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
ll a,b;
scanf("%lld %lld",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}else
{
ll a,b,c;
scanf("%lld %lld %lld",&a,&b,&c);
update(a,b,1,n,c,1);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息