1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include<bits/stdc++.h> using namespace std; const int maxn=1e6+7; typedef long long ll; ll tree[maxn*4],lazy[maxn*4]; int n,q; void push_up(int rt){ tree[rt]=tree[rt*2]+tree[rt*2+1]; } void push_down(int l,int r,int rt){ int mid=(l+r)/2; if(lazy[rt]==0 || l==r) return; tree[rt*2]+=lazy[rt]*(mid-l+1); tree[rt*2+1]+=lazy[rt]*(r-mid); lazy[rt*2]+=lazy[rt]; lazy[rt*2+1]+=lazy[rt]; lazy[rt]=0; } void build(int l,int r,int rt){ if(l==r){ cin>>tree[rt]; return; } int mid=(l+r)/2; build(l,mid,rt*2); build(mid+1,r,rt*2+1); push_up(rt); }
void init(){ memset(tree,0,sizeof(tree)); memset(lazy,0,sizeof((lazy))); } void modify(int l,int r,int rt,int L,int R,ll add){ if(l>=L && r<=R){ tree[rt]+=(r-l+1)*add; lazy[rt]+=add; return; } push_down(l,r,rt); int mid=(l+r)/2; if(mid>=L) modify(l,mid,rt*2,L,R,add); if(mid+1<=R) modify(mid+1,r,rt*2+1,L,R,add); push_up(rt); }
ll query(int l,int r,int rt,int L,int R){ if(l>=L && r<=R){ return tree[rt]; } int mid=(l+r)/2; ll ret=0; push_down(l,r,rt); if(mid>=L) ret+=query(l,mid,rt*2,L,R); if(mid+1<=R) ret+=query(mid+1,r,rt*2+1,L,R); return ret; } int main(){ ios::sync_with_stdio(false); cin>>n>>q; init(); build(1,n,1); for(int i=1;i<=q;++i){ int x,y,z; cin>>x>>y>>z; if(x==1){ int temp; cin>>temp; modify(1,n,1,y,z,temp); }else{ cout<<query(1,n,1,y,z)<<endl; } } }
|