题目链接
树状数组区间修改区间查询。里面有很多细节地方和以前的不一样了,需要注意。关于树状数组的各种操作,大佬博客讲得非常详细,在这里分享一下。
#include<cstdio>
#define _rep(i,a,b) for(int i=(a);i<=(b);i++)
const int N=1e5+10;
typedef long long ll;
ll sum1[N],sum2[N],sum[N];
int num,n,q,a,b,c;
char com[2];
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int v){int i=x;while(i<=n)sum1[i]+=v,sum2[i]+=x*v,i+=lowbit(i);}
inline void range_add(int a,int b,int c){add(a,c);add(b+1,-c);}
inline ll ask(int x){ll ret=0;int i=x;while(i)ret+=(x+1)*sum1[i]-sum2[i],i-=lowbit(i);return ret;}
inline ll range_ask(int a,int b){return ask(b)-ask(a-1);}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&q);
_rep(i,1,n)scanf("%d",&num),sum[i]=sum[i-1]+num;
_rep(i,1,q)
{
scanf("%s",com);
if(com[0]=='C')scanf("%d%d%d",&a,&b,&c),range_add(a,b,c);
else scanf("%d%d",&a,&b),printf("%lld\n",sum[b]-sum[a-1]+range_ask(a,b));
}
return 0;
}