cmqqxsb @ 2022-10-28 21:32:10
#include<bits/stdc++.h>
#define ll long long
#define lc k<<2
#define rc k<<2|1
const int maxn=10000010;
using namespace std;
int n,m,opt,x,y;
ll d;
struct node{
int l,r;
ll val,lazy;
}tree[4*maxn+10];
void pushup(int k){
tree[k].val=tree[lc].val+tree[rc].val;
}
void build(int k,int l,int r){
tree[k].l=l;
tree[k].r=r;
if(l==r){
scanf("%lld",&tree[k].val);
return;
}
int mid=(l+r)>>1;
build(lc,l,mid);
build(rc,mid+1,r);
pushup(k);
}
void pushdown(int k){
if(tree[k].lazy){
tree[lc].lazy+=tree[k].lazy;
tree[lc].val+=tree[k].lazy*(tree[lc].r-tree[lc].l+1);
tree[rc].lazy+=tree[k].lazy;
tree[rc].val+=tree[k].lazy*(tree[rc].r-tree[rc].l+1);
tree[k].lazy=0;
}
}
void update(int k,int l,int r,ll num){
if(tree[k].l==l&&tree[k].r==r){
tree[k].val+=num*(tree[k].r-tree[k].l+1);
tree[k].lazy+=num;
return;
}
pushdown(k);
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)
update(lc,l,r,num);
else if(l>mid)
update(rc,l,r,num);
else
update(lc,l,mid,num),update(rc,mid+1,r,num);
pushup(k);
}
ll ask(int k,int l,int r){
if(tree[k].l>=l&&tree[k].r<=r){
return tree[k].val;
}
pushdown(k);
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)
return ask(lc,l,r);
else if(l>mid)
return ask(rc,l,r);
else
return ask(lc,l,mid)+ask(rc,mid+1,r);
}
int main(){
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--){
scanf("%d%lld%lld",&opt,&x,&y);
if(opt==1){
scanf("%lld",&d);
update(1,x,y,d);
}
else{
printf("%lld\n",ask(1,x,y));
}
}
return 0;
}
by cmqqxsb @ 2022-10-28 21:37:56
(还有为什么maxn开到十的五次方会有七个点RE)
by Skies @ 2022-10-28 21:42:00
update和ask的第二个else删掉