【datastruct】codeforces R158 E. Dividing Kingdom

http://codeforces.com/contest/260/problem/E

      木有想到好办法,只会暴力处理=。=。。

      先将所有坐标离散化,之后将9个块全排列枚举,知道每个位置最终含有节点数有可以通过2分求出x坐标已经y坐标上各自的两条分隔线,但是这样分隔并不一定是一个答案,剩下的判断即转化成求某个[xi,xj]内[yi,yj]的节点数量,这个问题可以用树套树logn^2来解决,最后200行代码ac,还好不怎么debug就通过了,但看着tourist超短的代码就知道我又太暴力了。。

View Code
  1 //By Lin
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<map>
  5 #include<algorithm>
  6 #define maxn 100010
  7 #define X first
  8 #define Y second 
  9 using namespace std;
 10 
 11 struct    SplayNode{
 12     SplayNode *ch[2],*fa;
 13     int key,size;
 14     SplayNode(int _key){
 15         key = _key;
 16         size = 1;
 17         ch[0] = ch[1] = fa = NULL;
 18     }
 19     void updata(){
 20         size = 1;
 21         if ( ch[0] ) size += ch[0]->size;
 22         if ( ch[1] ) size += ch[1]->size;
 23     }
 24     void clear(){
 25         if ( ch[0] ) ch[0]->clear();
 26         if ( ch[1] ) ch[1]->clear();
 27         delete this;
 28     }
 29 };
 30 
 31 struct    Splaytree{
 32     SplayNode *root;
 33     void clear(){
 34         if ( root ) root->clear();
 35         root= NULL;
 36     }
 37     void Rotate(SplayNode *x ){
 38         SplayNode *y = x->fa;
 39         int d = y->ch[1] == x;
 40         if ( y->ch[d] = x->ch[d^1] ) y->ch[d]->fa = y;
 41         if ( x->fa = y->fa ) x->fa->ch[ x->fa->ch[1]==y ] = x;
 42         x->ch[d^1] = y;
 43         y->fa = x;
 44         y->updata();
 45         x->updata();
 46     }
 47     void Splay( SplayNode *x , SplayNode *y ){
 48         while ( x->fa != y ) 
 49             if ( x->fa->fa == y ) Rotate(x);
 50             else{
 51                 int d1 = x->fa->fa->ch[0] == x->fa , 
 52                     d2 = x->fa->ch[0] == x;
 53                 if ( d1 == d2 ) Rotate(x->fa),Rotate(x);
 54                 else Rotate(x),Rotate(x);
 55             }
 56     }
 57     void insert(SplayNode* &x , SplayNode *tmp){
 58         if ( !x ) x = tmp;
 59         else {
 60             if ( x->key<=tmp->key ) {
 61                 insert(x->ch[1],tmp);
 62                 x->ch[1]->fa = x; 
 63             }
 64             else {
 65                 insert(x->ch[0],tmp);
 66                 x->ch[0]->fa = x;
 67             }
 68             x->updata();
 69         }
 70     }
 71 
 72     void insert(int key){
 73         SplayNode *tmp = new SplayNode(key);
 74         insert( root , tmp );
 75         Splay( tmp , NULL );
 76         root = tmp;
 77     }
 78     SplayNode *Select( SplayNode *x ,int key ){
 79         if ( !x ) return NULL;
 80         if ( x->key > key ) return Select( x->ch[0] , key );
 81         SplayNode *ret = Select( x->ch[1] , key );
 82         return ret?ret:x;
 83     }
 84     int     ask(int key){
 85         SplayNode *tmp = Select(root,key);
 86         if ( !tmp ) return 0;
 87         Splay( root = tmp , NULL );
 88         return tmp->ch[0]?tmp->ch[0]->size+1:1;
 89     }
 90 }A;
 91 struct    Segtree{
 92     int left[maxn*4],right[maxn*4];
 93     Splaytree splaytree[maxn*4];
 94     void build(int l ,int r,int step ){
 95         left[step] = l , right[step] = r;
 96         splaytree[step].clear();
 97         if ( l == r ) return;
 98         int mid = ( l+r)/2;
 99         build( l , mid , step*2 );
100         build(mid+1, r , step*2+1 );
101     }
102     void insert(int x,int y,int step ){
103         splaytree[step].insert(y);
104         if ( left[step] == right[step] ) return;
105         int mid = ( left[step] + right[step] )/2;
106         if ( x <= mid ) insert( x , y , step*2 );
107         else insert( x , y , step*2+1 );
108     }
109     int     ask(int l,int r,int y,int step){
110         if ( l > r ) return 0;
111         if ( left[step] == l && right[step] == r ) 
112             return splaytree[step].ask(y);
113         int mid = ( left[step] + right[step] )/2;
114         if ( r <= mid ) return ask(l,r,y,step*2);
115         else if ( l > mid ) return ask(l,r,y,step*2+1);
116         else 
117             return ask(l,mid,y,step*2)+ask(mid+1,r,y,step*2+1);
118     }
119 }tree;
120 
121 int        n;
122 int        xx[maxn],yy[maxn],xcnt,ycnt,xsum[maxn],ysum[maxn];
123 int        num[9];
124 pair<int,int> data[maxn];
125 map<int,int>  mm;
126 
127 bool    xget(int key, int &ret ){
128     int g = 0 , h = xcnt; 
129     while ( g<=h ) {
130         int mid = (g+h)/2;
131         if ( xsum[mid] == key ) { ret = mid; return true; }
132         if ( xsum[mid] < key ) g = mid+1;
133         else h = mid-1;
134     }
135     return false;
136 }
137 bool    yget(int key, int &ret ){
138     int g = 0 , h = ycnt; 
139     while ( g<=h ) {
140         int mid = (g+h)/2;
141         if ( ysum[mid] == key ) { ret = mid; return true; }
142         if ( ysum[mid] < key ) g = mid+1;
143         else h = mid-1;
144     }
145     return false;
146 }
147 int        main(){
148 //    A.clear();
149 //    int x,y;
150 //    while ( ~scanf("%d%d", &x, &y ) ) {
151 //        if ( x == 1 ) A.insert(y);
152 //        else printf("%d\n" , A.ask(y) );
153 //    }
154 //    return 0;
155     scanf("%d", &n );
156     for (int i = 0; i<n; i++) {
157         scanf("%d%d", &data[i].X , &data[i].Y );
158         xx[i] = data[i].X;
159         yy[i] = data[i].Y;
160     }
161     sort( xx , xx+n );
162     xcnt = unique(xx,xx+n)-xx;
163     xx[xcnt] = xx[xcnt-1]+1;
164     for (int i = 0; i<xcnt; i++) mm[xx[i]] = i+1;
165     for (int i = 0; i<n; i++) xsum[data[i].X = mm[data[i].X]]++;
166     for (int i = 1; i<=xcnt; i++) xsum[i] += xsum[i-1];
167     mm.clear();
168     sort( yy , yy+n );
169     ycnt = unique(yy,yy+n)-yy;
170     yy[ycnt] = yy[ycnt-1]+1;
171     for (int i = 0; i<ycnt; i++) mm[yy[i]] = i+1;
172     for (int i = 0; i<n; i++) ysum[data[i].Y = mm[data[i].Y]]++;
173     for (int i = 1; i<=ycnt; i++) ysum[i] += ysum[i-1];
174 
175     tree.build(1,xcnt,1);
176     for (int i = 0; i<n; i++) tree.insert( data[i].X , data[i].Y , 1 );
177     for (int i = 0; i<9; i++) scanf("%d", &num[i] );
178     sort( num, num+9 );
179     bool flag = false;
180     do{
181         int x1 = num[0]+num[1]+num[2] , 
182             x2 = num[3]+num[4]+num[5] , 
183             x3 = num[6]+num[7]+num[8] , 
184             y1 = num[0]+num[3]+num[6] , 
185             y2 = num[1]+num[4]+num[7] , 
186             y3 = num[2]+num[5]+num[8] ;
187         int g , h , l , r;
188         if ( !xget( x1 , g ) )       continue;
189         if ( !xget( x1+x2 , h ) ) continue;
190         if ( !yget( y1 , l ) )    continue;
191         if ( !yget( y1+y2 , r ) ) continue;
192         if ( tree.ask(1,g,l,1) != num[0] ) continue;
193         if ( tree.ask(1,g,r,1) != num[0]+num[1] ) continue;
194         if ( tree.ask(g+1,h,l,1) != num[3] ) continue;
195         if ( tree.ask(g+1,h,r,1) != num[3]+num[4] ) continue;
196         if ( tree.ask(h+1,xcnt,l,1) != num[6] ) continue;
197         if ( tree.ask(h+1,xcnt,r,1) != num[6]+num[7] ) continue;
198         flag = true;
199         printf("%.6f %.6f\n" , xx[g]-0.6 , xx[h]-0.4 );
200         printf("%.6f %.6f\n" , yy[l]-0.6 , yy[r]-0.4 );
201         break;
202     } while ( next_permutation(num,num+9) );
203     if ( !flag ) printf("-1\n");
204 }

 

posted @ 2012-12-28 14:52  lzqxh  阅读(312)  评论(3编辑  收藏  举报