BZOJ-3212: Pku3468 A Simple Problem with Integers

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3212

**思路:简单的树状数组维护求和就可以啦~ **

详细区间修改方法:https://www.jianshu.com/p/b93b3a55d21e

代码(很短~):

#include 

#include 

#include 

#define rep(a,b) for (int i=a;i<=b;i++)

#define lowbit(x)(((~x)+1)&x)

#define MAXN 100010

long long t0[MAXN],t1[MAXN],t[MAXN],n,m;

long long getsum(int l,int r){return t[r]-t[l-1];}

long long getsum0(int x){

    long long rec=0,i=x;

    while (i) rec+=t0[i],i-=lowbit(i);

    return rec;

}

long long getsum1(int x){

    long long rec=0,i=x;

    while (i) rec+=t1[i],i-=lowbit(i);

    return rec;

}

void Add0(int x,int y){while(x<=n)t0[x]+=y,x+=lowbit(x);}

void Add1(int x,int y){while(x<=n)t1[x]+=y,x+=lowbit(x);}

int main(){

    memset(t0,0,sizeof(t0));

    memset(t1,0,sizeof(t1));

    memset(t,0,sizeof(t));

    scanf("%d%d",&n,&m);

    int x;

    rep(1,n) scanf("%d",&x),t[i]=t[i-1]+x;

    while (m--){

        char z[1];

        int l,r,k;

        scanf("%s%d%d",&z,&l,&r);

        if (z[0]=='C'){

            scanf("%d",&k);

            Add0(l,k),Add0(r+1,-k);

            Add1(l,(l-1)*k),Add1(r+1,-r*k);

        } else printf("%lld\n",getsum(l,r)+(1-l)*getsum0(l)+r*getsum0(r)-(getsum1(r)-getsum1(l)));

    }

    return 0;

}


你可能感兴趣的:(BZOJ-3212: Pku3468 A Simple Problem with Integers)