可以用树状数组,线段树,分块做。代码是分块
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
int n, m, uu, vv, ww, blc, bel[100005], l[100005], r[100005];
ll sum[100005], tag[100005], a[100005];
char ss[15];
ll query(int uu, int vv){
int p=bel[uu], q=bel[vv];
ll re=0;
if(p==q){
for(int i=uu; i<=vv; i++) re += a[i];
re += tag[p] * (vv - uu + 1);
}
else{
for(int i=p+1; i<=q-1; i++) re += sum[i];
for(int i=uu; i<=r[p]; i++) re += a[i] + tag[p];
for(int i=l[q]; i<=vv; i++) re += a[i] + tag[q];
}
return re;
}
void update(int uu, int vv, int ww){
int p=bel[uu], q=bel[vv];
if(p==q){
for(int i=uu; i<=vv; i++) a[i] += ww;
sum[p] += ww * (vv - uu + 1);
}
else{
for(int i=p+1; i<=q-1; i++){
sum[i] += ww * (r[i] - l[i] + 1);
tag[i] += ww;
}
for(int i=uu; i<=r[p]; i++) a[i] += ww;
sum[p] += ww * (r[p] - uu + 1);
for(int i=l[q]; i<=vv; i++) a[i] += ww;
sum[q] += ww * (vv - l[q] + 1);
}
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
blc = sqrt(n);
for(int i=1; i<=n; i++){
bel[i] = (i - 1) / blc + 1;
sum[bel[i]] += a[i];
}
for(int i=1; i<=blc; i++){
l[i] = (i - 1) * blc + 1;
r[i] = i * blc;
}
if(blc*blc<n){
blc++;
l[blc] = r[blc-1] + 1;
r[blc] = n;
}
while(m--){
scanf("%s", ss);
if(ss[0]==‘Q‘){
scanf("%d %d", &uu, &vv);
printf("%lld\n", query(uu, vv));
}
else{
scanf("%d %d %d", &uu, &vv, &ww);
update(uu, vv, ww);
}
}
return 0;
}