Bzoj 2683 Simple topic CDQ divide + tree-like array

Source: Internet
Author: User

Test Instructions:Link * * Method: **CDQ + tree-like array parsing:First of all for this problem, after looking at the scope of the two-dimensional data structure is obviously not able to, so we may consider one-dimensional sorting after another on the data structure, but CDQ division can be very good to reflect its role. First of all, for each query sum, it is obvious that X is on its left and all the changes that appear in the time before it may have an effect on him. We follow the x first keyword, y second keyword, operation of the third keyword to sort all the queries, and then in the CDQ, each time the left half of the recursive processing, according to the x dynamic y this column value added to the tree array, to update the right half of all the query, notice here the tree array is needed to clear, That is, each CDQ is a different tree-like array. Another: This god pit, the array opening small is not re but WA array range is to open to 200000*4, because for each 1 operation, is an operation, and 2 operation according to the principle of repulsion, is 4 operations. So what's the complexity? O (nlog^2 (n)) cdq+ tree-like array. n<=800000 Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 800010#define Q Q[tot]Using namespace Std;struct node{intOptx,yANo, Belong;}Q[n]NQ[n];intNintJd,tot,t;intAns[n];intC[n];intLowbit (int x){return x& (-x);}intGetsum (int x){intret=0; while(x) {ret+=c[x],x-=lowbit (x);}returnRET;} void Update (int x,intV) { while(x<=n) {c[x]+=v;x+=lowbit (x);}}intCMP (node A,node b) {if(A.x==b.x&&a.y==b.y)returna.opt<b.opt;if(A.x==b.x)returnA.y<b.y;returnA.x<b.x;} void Sov (intLintR) {if(L==R)return;intMid= (l+r) >>1, l1=l,l2=mid+1; for(inti=l;i<=r;i++) {if(Q[i].No<=mid&&Q[i]. opt==1) Update (Q[i].y,Q[i]. A);if(Q[i].No>mid&&Q[i]. opt==2) ans[Q[i]. Belong]+=getsum (Q[i].y); } for(inti=l;i<=r;i++) {if(Q[i].No<=mid&&Q[i]. opt==1) Update (Q[i].y,-Q[i].    A); } l1=l,l2=mid+1; for(inti=l;i<=r;i++) {if(Q[i].No&LT;=MID) nq[l1++]=Q[i];ElseNq[l2++]=Q[i];} for(inti=l;i<=r;i++)Q[i]=nQ[i]; Sov (L,mid), Sov (mid+1, r);}intMain () {scanf ("%d", &n); while(SCANF ("%d", &AMP;JD) &&jd^3)    {if(jd==1){int x,y, A;SCANF (" %d%d%d",&x,&y, &a);Q[++tot]. opt=1Q.x=xQ.y=y, Q.a=a,q.No=tot;}Else{intX1,x2,y1,y2; scanf"%d %d%d%d", &x1,&y1,&x2,&y2);Q[++tot]. opt=2Q.x=x1-1Q.y=y1-1, q.a=1Q.No=tot,q.belong=++t;Q[++tot]. opt=2Q.x=x1-1Q.y=y2,q.a=-1Q.No=tot,q.belong=t;Q[++tot]. opt=2Q.x=x2,q.y=y1-1, q.a=-1Q.No=tot,q.belong=t;Q[++tot]. opt=2Q.x=x2,q.y=y2,q.a=1Q.No=tot,q.belong=t; }    }Sort(Q+1,Q+1+TOT,CMP); Sov1, tot); for(intI=1; i<=t;i++)printf("%d\ n", Ans[i]);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 2683 Simple topic CDQ divide + tree-like array

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.