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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <bits/stdc++.h> using namespace std; const int maxn=1e6+5; typedef long long ll; ll a[maxn],w[maxn*4]; void pushup(const int u){ w[u]=w[u*2]+w[u*2+1]; }
void build(const int u,int L,int R){ if(L==R){ w[u]=a[L]; return; } int M=(L+R)/2; build((u*2),L,M); build((u*2)+1,M+1,R); pushup(u); }
ll lzy[maxn*4]; void maketag(int u,int len,ll x){ lzy[u]+=x; w[u]+=len*x; } void pushdown(int u,int L,int R){ int M=(L+R)/2; maketag(u*2,M-L+1,lzy[u]); maketag(u*2+1,R-M,lzy[u]); lzy[u]=0; }
ll query1(int u,int L,int R,int p){ pushdown(u,L,R); if(L==R){ return w[u]; } else { int M=(L+R)/2; if(M>=p) return query1((u*2),L,M,p); else return query1((u*2)+1,M+1,R,p); } return 0; } void update1(int u,int L,int R,int p,ll x){ if(L==R){ w[u]=x; } else{ int M=(L+R)/2; if(M>=p) update1((u*2),L,M,p,x); else update1((u*2)+1,M+1,R,p,x); pushup(u); } }
bool InRange(int L,int R,int l,int r){ return (l<=L)&&(R<=r); }
bool OutofRange(int L,int R,int l,int r){ return (L>r)||(R<l); } ll query(int u,int L,int R,int l,int r){ if(InRange(L,R,l,r)){ return w[u]; } if(OutofRange(L,R,l,r)) return 0; else{ int M=(L+R)/2; pushdown(u,L,R); return query((u*2),L,M,l,r)+query((u*2)+1,M+1,R,l,r); } return 0; }
void update(int u,int L,int R,int l,int r,ll x){ if(InRange(L,R,l,r)){ maketag(u,R-L+1,x); } else if(!OutofRange(L,R,l,r)){ int M=(L+R)/2; pushdown(u,L,R); update((u*2),L,M,l,r,x); update((u*2)+1,M+1,R,l,r,x); pushup(u); } } int main() { ios::sync_with_stdio(false); int n,m; cin >> n >> m; for(int i=1;i<=n;i++){ cin >> a[i]; } build(1,1,n); for(int t=1;t<=m;t++){ int op,x,y; ll k; cin >> op; if(op==1){ cin >> x >> y >> k; update(1,1,n,x,y,k); } else{ cin >> x >> y; cout << query(1,1,n,x,y) << endl; } } return 0; }
|