Rokua. 3374. [Template] Tree-like array 1 (CDQ)

Source: Internet
Author: User

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&LT;=M&AMP;&AMP;P2&LT;=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]&LT;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&LT;=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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.