#include<bits/stdc++.h>
#define int long long
#define love return
#define tyw 0
using namespace std;
int a[100100],c[400100];
int lazy[400400];
void push_up(int now){
c[now]=c[now<<1]+c[(now<<1)+1];
}void add(int l,int r,int x,int now){
c[now]+=(r-l+1)*x;
lazy[now]+=x;
}void push_down(int now,int l,int r){
if(!lazy[now]) return;
add(l,((l+r)>>1),lazy[now],(now<<1));
add(((l+r)>>1)+1,r,lazy[now],(now<<1)+1);
lazy[now]=0;
}
void build(int l,int r,int now){
lazy[now]=0;
if(l==r){
c[now]=a[l];
return;
}build(l,((l+r)>>1),(now<<1));
build(((l+r)>>1)+1,r,(now<<1)+1);
push_up(now);
}int ask(int l,int r,int L,int R,int now){
if(l>R||r<L) return 0;
if(l>=L&&r<=R) return c[now];
push_down(now,l,r);
return ask(l,((l+r)>>1),L,R,(now<<1))+ask(((l+r)>>1)+1,r,L,R,(now<<1)+1);
}void add(int l,int r,int L,int R,int now,int x){
if(l>R||r<L) return ;
if(l>=L&&r<=R){
add(l,r,x,now);
return;
}
push_down(now,l,r);
add(l,(l+r)>>1,L,R,now<<1,x);
add(((l+r)>>1)+1,r,L,R,(now<<1)+1,x);
push_up(now);
}
signed main(){
int n,q;
scanf("%lld%lld",&n,&q);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}build(1,n,1);
while(q--){
int f,x,y,k;
scanf("%lld",&f);
if(f==1){
scanf("%lld%lld%lld",&x,&y,&k);
add(1,n,x,y,1,k);
}else{
scanf("%lld%lld",&x,&y);
printf("%lld\n",ask(1,n,x,y,1));
}
}
love tyw;
}
剪贴板
剪贴板 j2sc1rh9
剪贴板信息
剪贴板信息
- 收录
- 更新