1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include<iostream>
using namespace std;
struct segmentTree{
typedef long long ll;
const static ll MAX=100000;
ll v[4*MAX],m[4*MAX];
inline static ll lson(ll r){return r*2+1;}
inline static ll rson(ll r){return r*2+2;}
void build(ll r,ll * a,ll s,ll e){
m[r]=0;
if(s==e){v[r]=a[s];return;}
ll md=(s+e)>>1;
build(lson(r),a,s,md);
build(rson(r),a,md+1,e);
v[r]=v[lson(r)]+v[rson(r)];
}
void pushDown(ll r,ll s,ll e){
if(m[r]==0)return;
ll md=(s+e)>>1;
m[lson(r)]+=m[r];
m[rson(r)]+=m[r];
v[lson(r)]+=m[r]*(md-s+1);
v[rson(r)]+=m[r]*(e-md);
m[r]=0;
}
ll query(ll r,ll cs, ll ce,ll qs,ll qe){
if(qe<cs||ce<qs)return 0;
if(qs<=cs&&ce<=qe)return v[r];
pushDown(r,cs,ce);
ll cmd=(cs+ce)/2;
return query(lson(r),cs,cmd,qs,qe)+query(rson(r),cmd+1,ce,qs,qe);
}
void update(ll r,ll cs,ll ce,ll us,ll ue,ll addv){
if(ce<us||ue<cs)return;
if(us<=cs&&ce<=ue){v[r]+=addv*(ce-cs+1);m[r]+=addv;return;}
pushDown(r,cs,ce);
ll cmd=(cs+ce)>>1;
update(lson(r),cs,cmd,us,ue,addv);
update(rson(r),cmd+1,ce,us,ue,addv);
v[r]=v[lson(r)]+v[rson(r)];
}
}ST;
typedef long long ll;
ll n,m;
ll a[100005];
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
ST.build(1,a,1,n);
for(int i=1;i<=m;i++){
int op,x,y,k;
cin>>op;
if(op==1){
cin>>x>>y>>k;
ST.update(1,1,n,x,y,k);
}else{
cin>>x>>y;
cout<<ST.query(1,1,n,x,y)<<endl;
}
}
} |