题解:一种特殊的树状数组写法
http://codevs.cn/problem/1082/
#include <bits/stdc++.h>
#define ll long long
const int MAXN=2e5+10;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
ll a[MAXN],b[MAXN];
int get_id(int x){return x&(-x);}
int n;
void add1(int x,ll vul){
if(x<=0)return ;
for(int i=x;i<=n;i+=get_id(i))a[i]+=vul;
}
void add2(int x,ll vul){
if(x<=0)return ;
for(int i=x;i>0;i-=get_id(i))b[i]+=vul;
}
//ll ans;
ll Sum1(int x){
if(x<=0)return 0;
ll ans=0;
for(int i=x;i>0;i-=get_id(i))ans+=a[i];
return ans;
}
ll Sum2(int x){
if(x<=0)return 0;
ll ans=0;
for(int i=x;i<=n;i+=get_id(i))ans+=b[i];
return ans;
}
int main(){
n=read();ll t;int p=n;n++;
for(int i=1;i<=p;i++)t=read(),add1(i,1ll*i*t),add2(i,t),add1(i-1,1ll*(i-1)*-t),add2(i-1,t*-1);
// cout<<Sum1(1)<<" "<<Sum2(2)<<endl;
// cout<<Sum1(3)+1ll*3*Sum2(3+1)-Sum1(2-1)-1ll*(2-1)*Sum2(2)<<endl;
int q;q=read();
int op,l,r;ll vul;
for(int i=1;i<=q;i++){
op=read();l=read();r=read();
if(op==1){
vul=read();add1(r,1ll*r*vul);add2(r,vul);
add1(l-1,1ll*(l-1)*-vul);add2(l-1,vul*-1);
}
else{
printf("%lld\n",Sum1(r)+1ll*r*Sum2(r+1)-Sum1(l-1)-1ll*(l-1)*Sum2(l));
}
}
return 0;
}