#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;structnode{intL,r;Long LongSum,c;} t[400000+5];voidBuildintRootintLintR) {t[root].l=l; T[root].r=r; t[root].sum=0; T[root].c=0;if(L==R)return; Build2*root+1, L, (l+r)/2); Build2*root+2, (L+R)/2+1, r);}voidUpdateintRootintIintV) {if(t[root].l==i&&t[root].r==i) {t[root].sum=v;return; } t[root].sum+=v;intMid= (T[ROOT].L+T[ROOT].R)/2;if(i<=mid) Update (2*root+1, i,v);ElseUpdate2*root+2, i,v);}voidAddintRootintLintRLong Longc) {if(T[ROOT].L==L&&T[ROOT].R==R) {t[root].c+=c;return; } t[root].sum+=c* (r-l+1);intMid= (T[ROOT].L+T[ROOT].R)/2;if(R<=mid) Add (2*root+1, l,r,c);Else if(L>mid) Add (2*root+2, l,r,c);Else{Add (2*root+1, l,mid,c); Add2*root+2, mid+1, r,c); }}Long LongQueryintRootintLintR) {if(T[ROOT].L==L&&T[ROOT].R==R) {returnt[root].sum+ (r-l+1) *t[root].c; } t[root].sum+= (t[root].r-t[root].l+1) *t[root].c;intMid= (T[ROOT].R+T[ROOT].L)/2; Add2*root+1, T[ROOT].L,MID,T[ROOT].C); Add2*root+2, mid+1, T[ROOT].R,T[ROOT].C); T[root].c=0;if(R<=mid)returnQuery2*root+1, l,r);Else if(l>=mid+1)returnQuery2*root+2, l,r);Else{return(Query (2*root+1, L,mid) +query (2*root+2, mid+1, R)); }}intMain () {intI,n,q,v,a,b;Long LongCChars[Ten]; while(~scanf("%d%d", &n,&q)) {Build (0,1, n); for(i=1; i<=n;i++) {scanf("%d", &v); Update0, i,v); } for(i=0; i<q;i++) {scanf('%s ', s);if(s[0]==' C ') {scanf("%d%d%lld", &a,&b,&c); Add0, a,b,c); }Else if(s[0]==' Q ') {scanf("%d%d", &a,&b);Long LongAns=query (0, A, b);printf("%lld\n", ans); } } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3468 A Simple problem with integers segment tree