Tree-shaped array template problem, using CDQ to divide and conquer
CDQ Divide and Conquer:
Sub: Divide L to r into two interval operations
Rule: Operate on each of the separated intervals
However, CDQ and general division treatment is different, the general division of the left and right around two zones do not interfere with each other, but the CDQ division treatment is the effect of the interval between
Specific to the operation of the problem:
1, first the value of each one as modified
2, record each bit of operation, for the query operation L to R, divided into two segments, a section is L-1,val set to 1, a section is R,val set to 1, so when the calculation to r when you add the front 1 to l-1 weights, then 1 to l-1 plus -1* weight can eliminate the impact of
3, sort first keyword sorted by x-coordinate, second keyword sorted by operation Pos
4, start the recursive solution 1 to CNT (at this time the traversal is from the coordinates from left to right in turn traversal)
5, for the interval L to R, we first count is not the modification of the query, when the query operation is found, the results of the query plus the statistical value * Current Val (this step is equivalent to the statistics of all left intervals on the right side of the effect of the interval)
6, merging the two sides (similar to merge sort operations)
7, recursive to L,mid and mid+1,r intervals
#include <bits/stdc++.h>#defineFi first#defineSe Second#defineMP Make_pair#definePB Push_back#definePi ACOs (-1.0)#definell Long Long#defineMoD 1000000007#defineC 0.5772156649#defineLS l,m,rt<<1#defineRS M+1,r,rt<<1|1#definePiL pair<int,ll>#definePII pair<int,int>#defineull unsigned long Long#defineBase 1000000000000000000#defineFio Ios::sync_with_stdio (false); Cin.tie (0)using namespacestd;Const Doubleg=10.0, eps=1e- A;Const intn=500000+Ten, maxn=4000+Ten, inf=0x3f3f3f3f;structquery{intX,pos,val,id; Query () {} query (intXintPosintVAL,intID) {x=x,pos=pos,val=val,id=ID; } BOOL operator< (Constquery& RHS)Const{ returnx<rhs.x | | x==rhs.x&&pos<Rhs.pos; }}q[n*3],te[n*3];intans[n*3],cnt,totans;voidCdqintLintR) {//puts ("******************");//for (int i=1;i<=cnt;i++)//printf ("%d%d%d%d\n", q[i].x,q[i].pos,q[i].val,q[i].id); if(L==R)return ; intMid= (l+r) >>1, tot=0; for(inti=l;i<=r;i++) { if(!q[i].pos&&q[i].id<=mid) Tot+=q[i].val;//calculate the effect on the right side of the left Else if(Q[i].pos&&q[i].id>mid) ans[q[i].pos]+=q[i].val*tot; } intH1=l,h2=mid+1; for(inti=l;i<=r;i++)//Merge Sort { if(Q[i].id<=mid) te[h1++]=Q[i]; Elsete[h2++]=Q[i]; } for(inti=l;i<=r;i++) q[i]=Te[i]; CDQ (L,mid); CDQ (Mid+1, R);//To find the next depth}intMain () {intn,m; scanf ("%d%d",&n,&m); CNT=0, totans=0; for(intI=1; i<=n;i++) { intx; scanf ("%d",&x); ++CNT; Q[CNT]=query (I,0, x,cnt); } for(intI=1; i<=m;i++) { intOp,x,y; scanf ("%d%d%d",&op,&x,&y); if(op==1) { ++CNT; Q[CNT]=query (x,0, y,cnt); } Else { ++CNT; Q[CNT]=query (x1, ++totans,-1, CNT); ++CNT; Q[CNT]=query (Y,totans,1, CNT); } }//for (int i=1;i<=cnt;i++)//printf ("%d%d%d%d\n", q[i].x,q[i].pos,q[i].val,q[i].id);Sort (q+1, q+1+CNT);//puts ("******************");//for (int i=1;i<=cnt;i++)//printf ("%d%d%d%d\n", q[i].x,q[i].pos,q[i].val,q[i].id);Cdq1, CNT); for(intI=1; i<=totans;i++) printf ("%d\n", Ans[i]); return 0;}/********************5 2 3 4 (2 2) 5 3 5********************/View Code
P3374 "template" Tree array 1