#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct tree{
ll data;
ll l;
ll r;
ll lazy=0;
};
tree a[400040];
ll b[100010];
ll wh[400040];
void pushdown(ll i){
if(a[i].lazy!=0){
if(a[i].l!=a[i].r){
ll ch=i<<1;
a[ch].lazy+=a[i].lazy;
a[ch+1].lazy+=a[i].lazy;
a[i].data+=(a[i].r-a[i].l+1)*a[i].lazy;
}
else a[i].data+=a[i].lazy;
a[i].lazy=0;
}
}
void update(ll i){
ll ch=i<<1;
pushdown(ch);
pushdown(ch+1);
a[i].data=a[ch].data+a[ch+1].data;
return ;
}
void build(ll l,ll r,ll i){
a[i].l=l;
a[i].r=r;
if(l==r){
a[i].data=b[l];
return ;
}
ll ch=i<<1;
ll mid=(l+r)>>1;
build(l,mid,ch);
build(mid+1,r,ch+1);
if(l!=r)update(i);
}
void change(ll l,ll r,ll i,ll add){
pushdown(i);
if(a[i].l>=l&&a[i].r<=r){
a[i].lazy+=add;
return ;
}
ll mid=(a[i].l+a[i].r)>>1;
ll ch=i<<1;
if(r<=mid){
change(l,r,ch,add);
}
else if(l>mid){
change(l,r,ch+1,add);
}
else{
change(l,mid,ch,add);
change(mid+1,r,ch+1,add);
}
update(i);
}
ll getsum(ll l,ll r,ll i){
pushdown(i);
if(a[i].l>=l&&a[i].r<=r){
return a[i].data;
}
ll mid=(a[i].l+a[i].r)>>1;
ll ch=i<<1;
if(r<=mid){
return getsum(l,r,ch);
}
else if(l>mid){
return getsum(l,r,ch+1);
}
else return getsum(l,mid,ch)+getsum(mid+1,r,ch+1);
}
int main(){
ll n,m;
scanf("%lld%lld",&n,&m);
ll i;
for(i=1;i<=n;i++){
scanf("%lld",&b[i]);
}
build(1,n,1);
while(m--){
ll way;
scanf("%d",&way);
switch(way){
case 1:{
ll l,r,k;
scanf("%lld%lld%lld",&l,&r,&k);
change(l,r,1,k);
break;
}
case 2:{
ll x,y;
scanf("%lld%lld",&x,&y);
cout<<getsum(x,y,1)<<endl;
break;
}
}
}
system("pause");
}
版权声明:本文为weixin_61168459原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。