Http://wenku.baidu.com/link?url=Nq_9vn-F1a8xOarR8XVdGVzMISDDQETGRU2-5q9moFr8SVqLhsOcDgUDMfCR_ 994pnihtyfok0lte0bxnl4tnic-uwas7vnivvfe8hw8uwo
CDQ proposed a method of division and treatment.
Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88335#problem/B
Test instructions, + a B operation is to add a line segment "A",? C d is to ask how many segments have previously met A<=c<d<=b
Violent n^2
If all the queries are adding segments, we can sort the segments and queries according to Zoodang from small to large, and then monotone queue two pointers. When the left endpoint of the segment a<= query C, the segment pointer moves back, and the right end point of the segment is added to the tree array, and when the a>c is encountered, the subsequent segments are not satisfied, the answer to this query is the number of y in the tree array =d, You can use the sum (MAX)-sum (y-1) to get the complexity Logn, then the pointer of the query moves back one, so that each query and each segment will only walk once, the overall complexity nlogn. Similar to offline queries.
However, the insertion and query in this topic interspersed, is the online query, through the CDQ division can be converted to the above-mentioned offline query.
The idea of division and treatment is to solve the above problems defined as solve (l,r), the answer is solve (1,n)
Each interval is divided into L to Mid,mid+1 to R
The left interval of the + can be on the right interval? Make a contribution, and the time is earlier than the right query, so you can use the appeal offline algorithm in the Nlogn to complete,
It then solves the problem within the left and right intervals. Logn.
Overall complexity N*log (n) *log (n)
1 //#define DEBUG2 //#define Txtout3#include <cstdio>4#include <cstdlib>5#include <cstring>6#include <cmath>7#include <cctype>8#include <ctime>9#include <iostream>Ten#include <algorithm> One#include <vector> A#include <queue> -#include <stack> -#include <map> the#include <Set> - #defineMT (A, b) memset (A,b,sizeof (a)) - using namespacestd; -typedefLong LongLL; + Const Doubleeps=1e-8; - Const DoublePi=acos (-1.0); + Const intinf=0x3f3f3f3f; A Const intm=5e5+Ten; at structG { - Charop[4]; - intX,y,id; -FriendBOOL operator< (ConstG &a,ConstG &b) { - returna.x<b.x; - } in } G[m]; -vector<int>A; toVector<g>p,q; +map<int,int>MP; - intRes[m]; the classOne_tree_array {//One-dimensional tree-like array *typedefintTypev; $ Typev a[m];Panax Notoginseng Public: - voidinit () { theMT (A,0); + } A intLOWB (intt) { the returnt& (-t); + } - voidAddintI,typev v) { $ for(; i<m; a[i]+=v,i+=LOWB (i)); $ } -Typev sum (inti) { -Typev s=0; the for(; i>0; s+=a[i],i-=LOWB (i)); - returns;Wuyi } the }tree; - intBigy; Wu voidCdqintLintR) { - if(L==R)return ; About intMid= (l+r) >>1; $ p.clear (); - for(intI=l; i<=mid; i++) { - if(g[i].op[0]=='?')Continue; - P.push_back (G[i]); A } + q.clear (); the for(intI=mid+1; i<=r; i++) { - if(g[i].op[0]=='+')Continue; $ Q.push_back (G[i]); the } the sort (P.begin (), P.end ()); the sort (Q.begin (), Q.end ()); the intlp=p.size (); - intlq=q.size (); in intlen=0; the for(intI=0, j=0; i<lq;i++){ the while(j<lp&&p[j].x<=q[i].x) { AboutTree.add (P[J].Y,1); theJ + +; thelen=J; the } +Res[q[i].id]+=tree.sum (Bigy)-tree.sum (q[i].y-1); - } the for(intI=0; i<len;i++){BayiTree.add (p[i].y,-1); the } the CDQ (l,mid); -CDQ (mid+1, R); - } the intMain () { the #ifdef txtout theFreopen ("In.txt","R", stdin); theFreopen ("OUT.txt","W", stdout); - #endif the intN; the while(~SCANF ("%d",&N)) { the a.clear ();94 for(intI=1; i<=n; i++) { thescanf"%s%d%d",g[i].op,&g[i].x,&g[i].y); the //A.push_back (g[i].x); the A.push_back (G[I].Y);98 } About sort (A.begin (), A.end ()); - intLa=unique (A.begin (), A.end ())-A.begin ();101 mp.clear ();102 for(intI=0; i<la; i++) {103mp[a[i]]=i+1;104 } thebigy=0;106 for(intI=1; i<=n; i++) {107 //g[i].x=mp[g[i].x];108g[i].y=MP[G[I].Y];109bigy=Max (BIGY,G[I].Y); theG[i].id=i;111res[i]=0; the }113 tree.init (); theCdq1, n); the for(intI=1; i<=n; i++) { the if(g[i].op[0]=='+')Continue;117printf"%d\n", Res[i]);118 }119 } - return 0;121}View Code
End
Chen Danqi divided treatment