ybc2027_yangshoukuo @ 2024-03-27 22:48:46
全WA
#include <bits/stdc++.h>
using namespace std;
const int N=100005;//
long long n,m,tree[4*N],a,b,c,d,z[N],tag[4*N];
long long ls(long long k){
return k*2;
}
long long rs(long long k){
return k*2+1;
}
void up_update(long long rt){
tree[rt]=tree[ls(rt)]+tree[rs(rt)];
}
void down_update(long long rt,long long l,long long r){
if(tag[rt]!=0){
int mid=(l+r)/2;
tag[ls(rt)]+=tag[rt];
tree[ls(rt)]+=(mid-l+1)*tag[rt];
tag[rs(rt)]+=tag[rt];
tree[rs(rt)]+=(r-mid)*tag[rt];
tag[rt]=0;
}
return;
}
void begintree(long long l,long long r,long long rt){
tag[rt]=0;
if(l==r){
tree[rt]=z[l];
return;
}
long long mid=(l+r)/2;
begintree(l,mid,ls(rt));
begintree(mid+1,r,rs(rt));
tree[rt]=tree[ls(rt)]+tree[rs(rt)];
return;
}
void qj_num(long long dl,long long dr,long long l,long long r,long long k,long long rt){
if(l<=dl&&dr<=r){
tag[rt]+=k;
tree[rt]+=(dr-dl+1)*k;
return;
}
down_update(rt,dl,dr);
long long mid=(dl+dr)/2;
if(l<=mid) qj_num(dl,mid,l,r,k,rs(rt));
if(r>mid) qj_num(mid+1,dr,l,r,k,ls(rt));
up_update(rt);
return;
}
long long print_num(long long dl,long long dr,long long l,long long r,long long rt){
if(l<=dl&&dr<=r){
return tree[rt];
}
down_update(rt,dl,dr);
long long mid=(dl+dr)/2,res=0;
if(l<=mid) res+=print_num(dl,mid,l,r,rs(rt));
if(r>mid) res+=print_num(mid+1,dr,l,r,ls(rt));
return res;
}
int main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&z[i]);
begintree(1,n,1);
for(int i=1;i<=n;i++){
scanf("%lld%lld%lld",&a,&b,&c);
if(a==1){
scanf("%lld",&d);
qj_num(1,n,b,c,d,1);
}else{
printf("%lld\n",print_num(1,n,b,c,1));
}
}
return 0;
}