题目链接

http://poj.org/problem?id=3468

解题思路

区间修改,区间查询

AC代码

//我服了,不都开ll居然wa???要数据规模啥用???(或许wtcl,debug4小时!!!)
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;

const int N=1e6+10;
struct TREE
{
    ll l,r,lazy;
    ll sum;    
}tree[N];
ll n,m,x,y;
ll cc;
ll a[N];
char op;
void Build(int i,ll l,ll r)
{
    tree[i].l=l;
    tree[i].r=r;
    tree[i].sum=0;
    tree[i].lazy=0;
    if(l==r)
    {
        tree[i].sum=a[l];
        return ;
    }
    ll mid=(l+r)>>1;
    Build(i<<1,l,mid);//left
    Build(i<<1|1,mid+1,r);//right
    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
    return ;
}
void PushDown(int i)
{
    if(tree[i].lazy==0) return ;
    tree[i<<1].lazy+=tree[i].lazy;//left//注意是+=
    tree[i<<1|1].lazy+=tree[i].lazy;//right//注意是+=
    tree[i<<1].sum+=tree[i].lazy*(tree[i<<1].r-tree[i<<1].l+1);//left//注意是+=
    tree[i<<1|1].sum+=tree[i].lazy*(tree[i<<1|1].r-tree[i<<1|1].l+1);//right//注意是+=
    tree[i].lazy=0;
    return ;
}
void Update(int i,ll l,ll r,ll c)
{
    if(tree[i].l>r||tree[i].r<l) return;
    if(l<=tree[i].l && tree[i].r<=r)
    {
        tree[i].lazy+=c;//注意是+=
        tree[i].sum+=c*(tree[i].r-tree[i].l+1);//注意是+=
        return ;
    }
    PushDown(i);
    if(tree[i<<1].r>=l) Update(i<<1,l,r,c);
    if(tree[i<<1|1].l<=r) Update(i<<1|1,l,r,c);
    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
    return ;
}
ll Query(int i,ll l,ll r)
{

    if(l<=tree[i].l && tree[i].r<=r)
    return tree[i].sum;
    if(tree[i].l>r || tree[i].r<l) return 0;
    PushDown(i);
    ll res=0;
    if(tree[i<<1].r>=l) res+=Query(i<<1,l,r);
    if(tree[i<<1|1].l<=r) res+=Query(i<<1|1,l,r);
    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
    return res;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    Build(1,1,n);
    while(m--)
    {
        getchar();//注意
        scanf("%c",&op);
        if(op=='C')
        {
            scanf("%lld%lld%lld",&x,&y,&cc);
            Update(1,x,y,cc);
        }
        else 
        {
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",Query(1,x,y));
        }
    } 

    return 0;
}