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
| #include<cstdio> #include<algorithm> using namespace std; const int MAXN=500005; int a[MAXN]; struct node{ int val; int lazy; }; node sgt[MAXN*4];
void build(int rt,int st,int ed){ sgt[rt].lazy=0; if(st==ed){ sgt[rt].val=a[st]; return; } int mid=(st+ed)>>1; build(rt<<1,st,mid); build(rt<<1|1,mid+1,ed); sgt[rt].val=sgt[rt<<1].val+sgt[rt<<1|1].val; }
inline void pd(int rt){ if(sgt[rt].lazy!=0){ sgt[rt<<1].lazy+=sgt[rt].lazy; sgt[rt<<1|1].lazy+=sgt[rt].lazy; sgt[rt<<1].val+=sgt[rt].lazy; sgt[rt<<1|1].val+=sgt[rt].lazy; sgt[rt].lazy=0; } }
int query(int rt,int nst,int ned,int qst,int qed){ if(qst>ned||qed<nst) return 0; if(qst<=nst&&qed>=ned) return sgt[rt].val; pd(rt); int mid=(nst+ned)>>1; return query(rt<<1,nst,mid,qst,qed)+query(rt<<1|1,mid+1,ned,qst,qed); }
void update(int rt,int nst,int ned,int ust,int ued,int val){ if(ust>ned||ued<nst) return; if(ust<=nst&&ued>=ned){ sgt[rt].lazy+=val; sgt[rt].val+=val; return; } pd(rt); int mid=(nst+ned)>>1; update(rt<<1,nst,mid,ust,ued,val); update(rt<<1|1,mid+1,ned,ust,ued,val); sgt[rt].val=sgt[rt<<1].val+sgt[rt<<1|1].val; }
int main(){ int n,m,x,y,t; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,1,n);
for(int i=1;i<=m;i++){ scanf("%d%d%d",&t,&x,&y); if(t==1){ update(1,1,n,x,x,y); } else{ printf("%d\n",query(1,1,n,x,y)); } } return 0; }
|