Topic links
A simple CDQ and divided treatment course
//Each operation is decomposed into an ordered number pair (t,p), i.e. (time, position of operation), time is ordered by default, and the position is sorted by CDQ division (may be wrong not to care too much about the welcome point)#include <cstdio>#include <cctype>#define GC () GetChar ()//typedef Long long LL;Const intn=5e5+5;intN,m,ans[n];structques{intId,pos,val;//id:0:modify 1:left Point 2:right PointQues () {;} Ques (intIintPintV): ID (i), POS (P), Val (v) {};//ques (int i=0,int p=0,int v=0) {id=i,pos=p,val=v;} BOOL operator< (ConstQues &a)Const{returnpos==a.pos?id<a.id:pos<a.pos; }}q[n*3],tmp[n*3];inline intRead () {intnow=0, f=1;Register CharC=GC (); for(;! IsDigit (c); C=GC ())if(c=='-') f=-1; for(; IsDigit (c); now=now*Ten+c-' 0 ', C=GC ());returnNow*f;}voidCDQ (intLintR) {if(L<r) {intM=l+r>>1; CDQ (L,m), CDQ (M+1, R);intP1=l,p2=m+1, t=0, sum=0; while(P1<=M&&P2<=R) {//Pay attention to understanding the connotation of merging: For the same is divided to the left/right, the influence between them has been in the consolidation of the large interval before the calculation, so currently do not need to tube if(Q[P1]<Q[P2]) {//For the left side of the interval statistic modified value (left means that the operation is left so that the query on the right will have an impact) if(!q[p1].id) Sum+=q[p1].val; Tmp[t++]=q[p1++]; }Else{//For the right interval update query if(q[p2].id==1) Ans[q[p2].val]-=sum;Else if(q[p2].id==2) Ans[q[p2].val]+=sum; Tmp[t++]=q[p2++]; } } while(p1<=m) tmp[t++]=q[p1++];//No query anymore while(P2<=R) {if(q[p2].id==1) Ans[q[p2].val]-=sum;Else if(q[p2].id==2) Ans[q[p2].val]+=sum; Tmp[t++]=q[p2++]; } for(intI=0; i<t; ++i) Q[l+i]=tmp[i]; }}intMain () {N=read (), M=read ();intQcnt=0, acnt=0; for(intI=1; i<=n; ++i) Q[++qcnt]=ques (0, I,read ()); for(intId,l,r,i=1; i<=m; ++i) {id=read (), L=read (), R=read ();if(id==1) Q[++qcnt]=ques (0, l,r);Else++ACNT, Q[++qcnt]=ques (1, l-1, acnt), Q[++qcnt]=ques (2, r,acnt); } CDQ (1, qcnt); for(intI=1; i<=acnt; ++i) printf ("%d\n", Ans[i]);return 0;}
Rokua. 3374. [Template] Tree-like array 1 (CDQ)