#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
struct node{
int l,r;
int sum;
int add;
}t[N*4];
int a[N];
int x,y,k,n,m;
void pushup(int p){
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void pushdown(int p){
if(t[p].add==0)
return;
t[p*2].add+=t[p].add;
t[p*2+1].add+=t[p].add;
t[p*2].sum+=(t[p*2].r-t[p*2].l+1)*t[p].add;
t[p*2+1].sum+=(t[p*2+1].r-t[p*2+1].l+1)*t[p].add;
t[p].add=0;
}
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
if(l==r){
t[p].sum=a[l];
return;
}
int mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
pushup(p);
}
void update(int p,int l,int r,int c){
if(t[p].l>=l&&t[p].r<=r){
t[p].add+=c;
t[p].sum+=(t[p].r-t[p].l+1)*c;
return;
}
int mid=(t[p].l+t[p].r)/2;
pushdown(p);
if(l<=mid) update(p*2,l,r,c);
if(r>mid) update(p*2+1,l,r,c);
pushup(p);
}
int query(int p,int l,int r){
if(t[p].l>=l&&t[p].r<=r)return t[p].sum;
int mid=(t[p].l+t[p].r)/2;
pushdown(p);
int ret=0;
if(l<=mid) ret+=query(p*2,l,r);
if(r>mid) ret+=query(p*2+1,l,r);
return ret;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(m--){
int op;
cin>>op;
if(op==1){
cin>>x>>y>>k;
update(1,x,y,k);
}
if(op==2){
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
}
return 0;
}撒旦
这是 2026年8月11日 存档的第 8 版,不是当前版本。查看当前版本