#include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <ctime> #include <map> #include <cstdio> #include <iomanip> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define REMIN(x,y) (x)=min((x),(y)) #define REMAX(x,y) (x)=max((x),(y)) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define un(v) v.erase(unique(ALL(v)),v.end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cerr<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)); #define ALL(x) (x).begin(),(x).end() #define tests int dsdsf;cin>>dsdsf;while(dsdsf--) #define testss int dsdsf;CZ(dsdsf);while(dsdsf--) using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } typedef pair<pii,pii> pr; ///////////////////// ////// INPUT //////// //////////////////// typedef unsigned long long ull; class INPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *bufend; void grabBuffer(); public: INPUT() { grabBuffer(); } bool eof() { return bufend==buffer; } char nextChar() { return *bufpos; } inline char readChar(); inline void skipWS(); unsigned readUnsigned(); int readInt(); ull readULL(); }; char INPUT::buffer[INPUT::BUFSIZE]; void INPUT::grabBuffer() { bufpos = buffer; bufend = buffer + fread( buffer, 1,BUFSIZE,stdin); } char INPUT::readChar() { char res = *bufpos++; if(bufpos==bufend) grabBuffer(); return res; } inline bool myisspace(char c) { return c<=' '; } void INPUT::skipWS() { while(!eof() && myisspace(nextChar())) readChar(); } unsigned INPUT::readUnsigned() { skipWS(); unsigned res = 0; while(!eof() && isdigit(nextChar())) { res = 10u * res + (readChar()-'0'); } return res; } ull INPUT::readULL() { skipWS(); ull res = 0; while(!eof() && isdigit(nextChar())) { res = 10ULL * res + (readChar()-'0'); } return res; } int INPUT::readInt() { skipWS(); bool neg = false; if(!eof() && nextChar()=='-') { neg=true; readChar(); } int res = static_cast<int>(readUnsigned()); if(neg) res = -res; return res; } ///////////////////// ////// OUTPUT ////// //////////////////// class OUTPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *BUFLIMIT; void flushBuffer(); public: OUTPUT():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~OUTPUT() { flushBuffer(); } inline void operator()(char c); inline void operator()(unsigned x); inline void operator()(int x); inline void operator()(unsigned long long int x); inline void operator()(long long int x); inline void operator()(const char*s); void operator()(const string&s) { operator()(s.c_str()); } template<class A,class B> void operator()(const A& a,const B& b) { operator()(a); operator()(b); } template<class A,class B,class C> void operator()(const A& a,const B& b,const C&c) { operator()(a); operator()(b); operator()(c); } template<class A,class B,class C,class D> void operator()(const A& a,const B& b,const C&c,const D&d) { operator()(a); operator()(b); operator()(c); operator()(d); } template<class A,class B,class C,class D,class E> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); } template<class A,class B,class C,class D,class E,class F> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e,const F&f) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); operator()(f); } }; char OUTPUT::buffer[OUTPUT::BUFSIZE]; void OUTPUT::flushBuffer() { char *p = buffer; while(p < bufpos) { p += fwrite( p,1, bufpos-p,stdout); } bufpos = buffer; } void OUTPUT::operator()(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned>(x)); } void OUTPUT::operator()(unsigned long long int x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(long long int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned long long int>(x)); } void OUTPUT::operator()(const char*s) { while(*s) operator()(*s++); } INPUT input; OUTPUT output; #define INF (1<<25) pr p[111111]; int n; pr b[111111]; pr przekroj(pr A,pr B) { pr W; W.fi.fi=max(A.fi.fi,B.fi.fi); W.fi.se=min(A.fi.se,B.fi.se); W.se.fi=max(A.se.fi,B.se.fi); W.se.se=min(A.se.se,B.se.se); return W; } pr suma(pr A,pr B) { pr W; W.fi.fi=min(A.fi.fi,B.fi.fi); W.fi.se=max(A.fi.se,B.fi.se); W.se.fi=min(A.se.fi,B.se.fi); W.se.se=max(A.se.se,B.se.se); return W; } bool nonempty(pr A) { return (A.fi.fi<A.fi.se && A.se.fi<A.se.se); } bool nalezy(pii X, pr A) { return (A.fi.fi<=X.fi && X.fi<=A.fi.se && A.se.fi<=X.se && X.se<=A.se.se); } bool rog_nalezy(pr A, pr B) { if (nalezy(mp(A.fi.fi,A.se.fi),B)) return 1; if (nalezy(mp(A.fi.se,A.se.fi),B)) return 1; if (nalezy(mp(A.fi.fi,A.se.se),B)) return 1; if (nalezy(mp(A.fi.se,A.se.se),B)) return 1; return 0; } pr losuj(int R=5) { int a=0,b=0; while(a>=b){a=rand()%R;b=rand()%R;} int c=0,d=0; while(c>=d){c=rand()%R;d=rand()%R;} return mp(mp(a,b),mp(c,d)); } void gen_test(int N,int R) { n=N; FOR(i,n) p[i+1]=losuj(R); } vector<pr> brut() { FOR(i,n) b[i]=p[i+1]; int N=n; while(1) { bool zmiana=0; FOR(i,N)FOR(j,i) { pr w=przekroj(b[i],b[j]); if (w.fi.fi<w.fi.se && w.se.fi<w.se.se) { zmiana=1; b[j]=suma(b[i],b[j]); swap(b[i],b[N-1]); N--; break; } } if (!zmiana) break; } sort(b,b+N); return vector<pr>(b,b+N); } pr K[111111]; vi X,Y; int lx,ly; set<pii> ev; set<pii> miotla; int fu[111111]; int szuk(int x) { if (fu[x]==x) return x; return fu[x]=szuk(fu[x]); } int unia(int x,int y) { x=szuk(x); y=szuk(y); if (x!=y) { fu[y]=x; K[x]=suma(K[x],K[y]); } return x; } int getX(int x){return lower_bound(ALL(X),x)-X.begin();} int getY(int x){return lower_bound(ALL(Y),x)-Y.begin();} void wywal_z_miotly(int nr) { miotla.erase(mp(K[nr].se.fi,-nr)); miotla.erase(mp(K[nr].se.se,nr)); } int polacz(int x,int y) { wywal_z_miotly(x); wywal_z_miotly(y); x=unia(x,y); return x; } vector<pr> sol2() { X.clear(); Y.clear(); REP(i,1,n) { X.pb(p[i].fi.fi);X.pb(p[i].fi.se); Y.pb(p[i].se.fi);Y.pb(p[i].se.se); } sort(ALL(X));un(X); sort(ALL(Y));un(Y); REP(i,1,n) { K[i].fi.fi=getX(p[i].fi.fi); K[i].fi.se=getX(p[i].fi.se)-1; K[i].se.fi=getY(p[i].se.fi); K[i].se.se=getY(p[i].se.se)-1; } //DBG(vector<pr>(K+1,K+n+1)); lx=X.sz; ly=Y.sz; REP(i,1,n) fu[i]=i; FOR(jj,n) { ev.clear(); miotla.clear(); REP(i,1,n) { if (szuk(i)==i) ev.insert(mp(K[i].fi.fi,-i)); } set<pii>::iterator it1,it2; //DBG(vector<pr>(K+1,K+n+1)); //DBG(vi(fu+1,fu+n+1)); while(ev.sz) { pii u=*(ev.begin());ev.erase(ev.begin()); //DBG(u); int nr=abs(u.se); if (u.se>0) { nr=szuk(nr); //DBG(nr); if (K[nr].fi.se==u.fi) { wywal_z_miotly(nr); } } else { while(1) { it1=miotla.insert(mp(K[nr].se.fi,-nr)).fi; it2=miotla.insert(mp(K[nr].se.se,nr)).fi; it1++; //DBG(*it1); if (*it1!=*it2) { int nr2=abs(it1->se); nr=polacz(nr,nr2); continue; } it2++; if (it2==miotla.end() || it2->se<0) break; int nr2=abs(it2->se); nr=polacz(nr,nr2); continue; } ev.insert(mp(K[nr].fi.se,nr)); } //DBG(u); } REP(i,1,n) fu[i]=szuk(i); } //DBG(vi(fu+1,fu+n+1)); vector<pr> wyn; REP(i,1,n) { if (szuk(i)==i) { pr f=K[i]; f.fi.fi=X[f.fi.fi]; f.fi.se=X[f.fi.se+1]; f.se.fi=Y[f.se.fi]; f.se.se=Y[f.se.se+1]; wyn.pb(f); } } sort(ALL(wyn)); return wyn; } int M; set<pii> d[1<<19]; void dod(int a,pii val) { a+=M; while(a) { d[a].insert(mp(-val.fi,val.se)); a/=2; } } void usu(int a,pii val) { a+=M; while(a) { d[a].erase(mp(-val.fi,val.se)); a/=2; } } int A,B,C; pii sum(int l,int r,int nr) { if (l>B || r<A) return mp(INF,0); if (A<=l && r<=B) { set<pii>::iterator it=d[nr].lower_bound(mp(-C,-INF)); if (it==d[nr].end()) return mp(INF,0); return *it; } pii p1=sum(l,(l+r)/2,nr*2); pii p2=sum((l+r)/2+1,r,nr*2+1); return (p1<p2?p1:p2); } pii query(int a,int b,int c)//[a,b]x[-inf,c] { int wyn=0; C=c; A=a; B=b; pii pp=sum(0,M-1,1); return mp(-pp.fi,pp.se); } set<pair<pii,int> > rogi; void wyczysc_rog(int nr) { //rogi.erase(mp(mp(K[nr].fi.fi,K[nr].se.fi),nr)); //rogi.erase(mp(mp(K[nr].fi.se,K[nr].se.fi),nr)); //rogi.erase(mp(mp(K[nr].fi.fi,K[nr].se.se),nr)); //rogi.erase(mp(mp(K[nr].fi.se,K[nr].se.se),nr)); usu(K[nr].fi.fi,mp(K[nr].se.fi,nr)); usu(K[nr].fi.se,mp(K[nr].se.fi,nr)); usu(K[nr].fi.fi,mp(K[nr].se.se,nr)); usu(K[nr].fi.se,mp(K[nr].se.se,nr)); } void dodaj_rog(int nr) { //rogi.insert(mp(mp(K[nr].fi.fi,K[nr].se.fi),nr)); //rogi.insert(mp(mp(K[nr].fi.se,K[nr].se.fi),nr)); //rogi.insert(mp(mp(K[nr].fi.fi,K[nr].se.se),nr)); //rogi.insert(mp(mp(K[nr].fi.se,K[nr].se.se),nr)); dod(K[nr].fi.fi,mp(K[nr].se.fi,nr)); dod(K[nr].fi.se,mp(K[nr].se.fi,nr)); dod(K[nr].fi.fi,mp(K[nr].se.se,nr)); dod(K[nr].fi.se,mp(K[nr].se.se,nr)); } vector<pr> sol() { X.clear(); Y.clear(); REP(i,1,n) { X.pb(p[i].fi.fi);X.pb(p[i].fi.se); Y.pb(p[i].se.fi);Y.pb(p[i].se.se); } sort(ALL(X));un(X); sort(ALL(Y));un(Y); M=1; while(M<=X.sz+2) M*=2; FOR(i,2*M)d[i].clear(); REP(i,1,n) { K[i].fi.fi=getX(p[i].fi.fi); K[i].fi.se=getX(p[i].fi.se)-1; K[i].se.fi=getY(p[i].se.fi); K[i].se.se=getY(p[i].se.se)-1; } //DBG(vector<pr>(K+1,K+n+1)); lx=X.sz; ly=Y.sz; REP(i,1,n) fu[i]=i; ev.clear(); miotla.clear(); REP(i,1,n) { if (szuk(i)==i) ev.insert(mp(K[i].fi.fi,-i)); } set<pii>::iterator it1,it2; //DBG(vector<pr>(K+1,K+n+1)); //DBG(vi(fu+1,fu+n+1)); while(ev.sz) { pii u=*(ev.begin());ev.erase(ev.begin()); //DBG(u); int nr=abs(u.se); if (u.se>0) { nr=szuk(nr); //DBG(nr); if (K[nr].fi.se==u.fi) { wywal_z_miotly(nr); } } else { while(1) { it1=miotla.insert(mp(K[nr].se.fi,-nr)).fi; it2=miotla.insert(mp(K[nr].se.se,nr)).fi; it1++; //DBG(*it1); if (*it1!=*it2) { int nr2=abs(it1->se); nr=polacz(nr,nr2); continue; } it2++; if (it2==miotla.end() || it2->se<0) break; int nr2=abs(it2->se); nr=polacz(nr,nr2); continue; } ev.insert(mp(K[nr].fi.se,nr)); } //DBG(u); } set<pair<pii,int > >::iterator it; rogi.clear(); REP(i,1,n) if (i==szuk(i)) dodaj_rog(i); REP(i,1,n) { { int nr=szuk(i); if (i>1 && nr==szuk(i-1)) continue; wyczysc_rog(nr); bool zmiana=1; while(zmiana) { zmiana=0; pii u=query(K[nr].fi.fi,K[nr].fi.se,K[nr].se.se); if (u.fi>=K[nr].se.fi) { zmiana=1; wyczysc_rog(u.se); nr=unia(nr,u.se); } /*for(it=rogi.begin();it!=rogi.end();it++) { pii u=it->fi; if (K[nr].fi.fi<=u.fi && u.fi<=K[nr].fi.se && K[nr].se.fi<=u.se && u.se<=K[nr].se.se) { zmiana=1; //DBG(*it); wyczysc_rog(it->se); nr=unia(nr,it->se); //DBG(nr); break; } }*/ //DBG(zmiana); } dodaj_rog(nr); } } /*REP(i,1,n)REP(j,1,i-1) { if (szuk(i)==i && szuk(j)==j) { pr prz=przekroj(K[i],K[j]); if (prz==K[i] || prz==K[j]) unia(i,j); } }*/ //DBG(vi(fu+1,fu+n+1)); vector<pr> wyn; REP(i,1,n) { if (szuk(i)==i) { pr f=K[i]; f.fi.fi=X[f.fi.fi]; f.fi.se=X[f.fi.se+1]; f.se.fi=Y[f.se.fi]; f.se.se=Y[f.se.se+1]; wyn.pb(f); } } sort(ALL(wyn)); return wyn; } int main2() { //srand(time(0)); int cnt=0; while(1) { cnt++; if (cnt%100==0) DBG(cnt); int N=1+rand()%20; int R=2+rand()%30; gen_test(N,R); if (brut()!=sol()) { DBG(vector<pr>(p+1,p+n+1)); DBG(brut()); DBG(sol()); return 0; } } return 0; } int main22() { //srand(time(0)); n=80000; REP(i,1,n) { p[i].fi.fi=rand(); p[i].fi.se=p[i].fi.fi+2; p[i].se.fi=rand(); p[i].se.se=p[i].se.fi+2; } //DBG(sol()); vector<pr> v=sol(); DBG(v.sz); //printf("%d\n",v.sz); //FOR(i,v.sz) printf("%d %d %d %d\n",v[i].fi.fi,v[i].fi.se,v[i].se.fi,v[i].se.se); return 0; } int main() { n=input.readInt(); REP(i,1,n) { p[i].fi.fi=input.readInt(); p[i].fi.se=input.readInt(); p[i].se.fi=input.readInt(); p[i].se.se=input.readInt(); } //DBG(sol()); vector<pr> v=sol(); //printf("%d\n",v.sz); output(v.sz,"\n"); FOR(i,v.sz) { output(v[i].fi.fi," ",v[i].fi.se); output(" ",v[i].se.fi," ",v[i].se.se,"\n"); //printf("%d %d %d %d\n",v[i].fi.fi,v[i].fi.se,v[i].se.fi,v[i].se.se); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | #include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <ctime> #include <map> #include <cstdio> #include <iomanip> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define REMIN(x,y) (x)=min((x),(y)) #define REMAX(x,y) (x)=max((x),(y)) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define un(v) v.erase(unique(ALL(v)),v.end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cerr<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)); #define ALL(x) (x).begin(),(x).end() #define tests int dsdsf;cin>>dsdsf;while(dsdsf--) #define testss int dsdsf;CZ(dsdsf);while(dsdsf--) using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } typedef pair<pii,pii> pr; ///////////////////// ////// INPUT //////// //////////////////// typedef unsigned long long ull; class INPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *bufend; void grabBuffer(); public: INPUT() { grabBuffer(); } bool eof() { return bufend==buffer; } char nextChar() { return *bufpos; } inline char readChar(); inline void skipWS(); unsigned readUnsigned(); int readInt(); ull readULL(); }; char INPUT::buffer[INPUT::BUFSIZE]; void INPUT::grabBuffer() { bufpos = buffer; bufend = buffer + fread( buffer, 1,BUFSIZE,stdin); } char INPUT::readChar() { char res = *bufpos++; if(bufpos==bufend) grabBuffer(); return res; } inline bool myisspace(char c) { return c<=' '; } void INPUT::skipWS() { while(!eof() && myisspace(nextChar())) readChar(); } unsigned INPUT::readUnsigned() { skipWS(); unsigned res = 0; while(!eof() && isdigit(nextChar())) { res = 10u * res + (readChar()-'0'); } return res; } ull INPUT::readULL() { skipWS(); ull res = 0; while(!eof() && isdigit(nextChar())) { res = 10ULL * res + (readChar()-'0'); } return res; } int INPUT::readInt() { skipWS(); bool neg = false; if(!eof() && nextChar()=='-') { neg=true; readChar(); } int res = static_cast<int>(readUnsigned()); if(neg) res = -res; return res; } ///////////////////// ////// OUTPUT ////// //////////////////// class OUTPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *BUFLIMIT; void flushBuffer(); public: OUTPUT():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~OUTPUT() { flushBuffer(); } inline void operator()(char c); inline void operator()(unsigned x); inline void operator()(int x); inline void operator()(unsigned long long int x); inline void operator()(long long int x); inline void operator()(const char*s); void operator()(const string&s) { operator()(s.c_str()); } template<class A,class B> void operator()(const A& a,const B& b) { operator()(a); operator()(b); } template<class A,class B,class C> void operator()(const A& a,const B& b,const C&c) { operator()(a); operator()(b); operator()(c); } template<class A,class B,class C,class D> void operator()(const A& a,const B& b,const C&c,const D&d) { operator()(a); operator()(b); operator()(c); operator()(d); } template<class A,class B,class C,class D,class E> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); } template<class A,class B,class C,class D,class E,class F> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e,const F&f) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); operator()(f); } }; char OUTPUT::buffer[OUTPUT::BUFSIZE]; void OUTPUT::flushBuffer() { char *p = buffer; while(p < bufpos) { p += fwrite( p,1, bufpos-p,stdout); } bufpos = buffer; } void OUTPUT::operator()(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned>(x)); } void OUTPUT::operator()(unsigned long long int x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(long long int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned long long int>(x)); } void OUTPUT::operator()(const char*s) { while(*s) operator()(*s++); } INPUT input; OUTPUT output; #define INF (1<<25) pr p[111111]; int n; pr b[111111]; pr przekroj(pr A,pr B) { pr W; W.fi.fi=max(A.fi.fi,B.fi.fi); W.fi.se=min(A.fi.se,B.fi.se); W.se.fi=max(A.se.fi,B.se.fi); W.se.se=min(A.se.se,B.se.se); return W; } pr suma(pr A,pr B) { pr W; W.fi.fi=min(A.fi.fi,B.fi.fi); W.fi.se=max(A.fi.se,B.fi.se); W.se.fi=min(A.se.fi,B.se.fi); W.se.se=max(A.se.se,B.se.se); return W; } bool nonempty(pr A) { return (A.fi.fi<A.fi.se && A.se.fi<A.se.se); } bool nalezy(pii X, pr A) { return (A.fi.fi<=X.fi && X.fi<=A.fi.se && A.se.fi<=X.se && X.se<=A.se.se); } bool rog_nalezy(pr A, pr B) { if (nalezy(mp(A.fi.fi,A.se.fi),B)) return 1; if (nalezy(mp(A.fi.se,A.se.fi),B)) return 1; if (nalezy(mp(A.fi.fi,A.se.se),B)) return 1; if (nalezy(mp(A.fi.se,A.se.se),B)) return 1; return 0; } pr losuj(int R=5) { int a=0,b=0; while(a>=b){a=rand()%R;b=rand()%R;} int c=0,d=0; while(c>=d){c=rand()%R;d=rand()%R;} return mp(mp(a,b),mp(c,d)); } void gen_test(int N,int R) { n=N; FOR(i,n) p[i+1]=losuj(R); } vector<pr> brut() { FOR(i,n) b[i]=p[i+1]; int N=n; while(1) { bool zmiana=0; FOR(i,N)FOR(j,i) { pr w=przekroj(b[i],b[j]); if (w.fi.fi<w.fi.se && w.se.fi<w.se.se) { zmiana=1; b[j]=suma(b[i],b[j]); swap(b[i],b[N-1]); N--; break; } } if (!zmiana) break; } sort(b,b+N); return vector<pr>(b,b+N); } pr K[111111]; vi X,Y; int lx,ly; set<pii> ev; set<pii> miotla; int fu[111111]; int szuk(int x) { if (fu[x]==x) return x; return fu[x]=szuk(fu[x]); } int unia(int x,int y) { x=szuk(x); y=szuk(y); if (x!=y) { fu[y]=x; K[x]=suma(K[x],K[y]); } return x; } int getX(int x){return lower_bound(ALL(X),x)-X.begin();} int getY(int x){return lower_bound(ALL(Y),x)-Y.begin();} void wywal_z_miotly(int nr) { miotla.erase(mp(K[nr].se.fi,-nr)); miotla.erase(mp(K[nr].se.se,nr)); } int polacz(int x,int y) { wywal_z_miotly(x); wywal_z_miotly(y); x=unia(x,y); return x; } vector<pr> sol2() { X.clear(); Y.clear(); REP(i,1,n) { X.pb(p[i].fi.fi);X.pb(p[i].fi.se); Y.pb(p[i].se.fi);Y.pb(p[i].se.se); } sort(ALL(X));un(X); sort(ALL(Y));un(Y); REP(i,1,n) { K[i].fi.fi=getX(p[i].fi.fi); K[i].fi.se=getX(p[i].fi.se)-1; K[i].se.fi=getY(p[i].se.fi); K[i].se.se=getY(p[i].se.se)-1; } //DBG(vector<pr>(K+1,K+n+1)); lx=X.sz; ly=Y.sz; REP(i,1,n) fu[i]=i; FOR(jj,n) { ev.clear(); miotla.clear(); REP(i,1,n) { if (szuk(i)==i) ev.insert(mp(K[i].fi.fi,-i)); } set<pii>::iterator it1,it2; //DBG(vector<pr>(K+1,K+n+1)); //DBG(vi(fu+1,fu+n+1)); while(ev.sz) { pii u=*(ev.begin());ev.erase(ev.begin()); //DBG(u); int nr=abs(u.se); if (u.se>0) { nr=szuk(nr); //DBG(nr); if (K[nr].fi.se==u.fi) { wywal_z_miotly(nr); } } else { while(1) { it1=miotla.insert(mp(K[nr].se.fi,-nr)).fi; it2=miotla.insert(mp(K[nr].se.se,nr)).fi; it1++; //DBG(*it1); if (*it1!=*it2) { int nr2=abs(it1->se); nr=polacz(nr,nr2); continue; } it2++; if (it2==miotla.end() || it2->se<0) break; int nr2=abs(it2->se); nr=polacz(nr,nr2); continue; } ev.insert(mp(K[nr].fi.se,nr)); } //DBG(u); } REP(i,1,n) fu[i]=szuk(i); } //DBG(vi(fu+1,fu+n+1)); vector<pr> wyn; REP(i,1,n) { if (szuk(i)==i) { pr f=K[i]; f.fi.fi=X[f.fi.fi]; f.fi.se=X[f.fi.se+1]; f.se.fi=Y[f.se.fi]; f.se.se=Y[f.se.se+1]; wyn.pb(f); } } sort(ALL(wyn)); return wyn; } int M; set<pii> d[1<<19]; void dod(int a,pii val) { a+=M; while(a) { d[a].insert(mp(-val.fi,val.se)); a/=2; } } void usu(int a,pii val) { a+=M; while(a) { d[a].erase(mp(-val.fi,val.se)); a/=2; } } int A,B,C; pii sum(int l,int r,int nr) { if (l>B || r<A) return mp(INF,0); if (A<=l && r<=B) { set<pii>::iterator it=d[nr].lower_bound(mp(-C,-INF)); if (it==d[nr].end()) return mp(INF,0); return *it; } pii p1=sum(l,(l+r)/2,nr*2); pii p2=sum((l+r)/2+1,r,nr*2+1); return (p1<p2?p1:p2); } pii query(int a,int b,int c)//[a,b]x[-inf,c] { int wyn=0; C=c; A=a; B=b; pii pp=sum(0,M-1,1); return mp(-pp.fi,pp.se); } set<pair<pii,int> > rogi; void wyczysc_rog(int nr) { //rogi.erase(mp(mp(K[nr].fi.fi,K[nr].se.fi),nr)); //rogi.erase(mp(mp(K[nr].fi.se,K[nr].se.fi),nr)); //rogi.erase(mp(mp(K[nr].fi.fi,K[nr].se.se),nr)); //rogi.erase(mp(mp(K[nr].fi.se,K[nr].se.se),nr)); usu(K[nr].fi.fi,mp(K[nr].se.fi,nr)); usu(K[nr].fi.se,mp(K[nr].se.fi,nr)); usu(K[nr].fi.fi,mp(K[nr].se.se,nr)); usu(K[nr].fi.se,mp(K[nr].se.se,nr)); } void dodaj_rog(int nr) { //rogi.insert(mp(mp(K[nr].fi.fi,K[nr].se.fi),nr)); //rogi.insert(mp(mp(K[nr].fi.se,K[nr].se.fi),nr)); //rogi.insert(mp(mp(K[nr].fi.fi,K[nr].se.se),nr)); //rogi.insert(mp(mp(K[nr].fi.se,K[nr].se.se),nr)); dod(K[nr].fi.fi,mp(K[nr].se.fi,nr)); dod(K[nr].fi.se,mp(K[nr].se.fi,nr)); dod(K[nr].fi.fi,mp(K[nr].se.se,nr)); dod(K[nr].fi.se,mp(K[nr].se.se,nr)); } vector<pr> sol() { X.clear(); Y.clear(); REP(i,1,n) { X.pb(p[i].fi.fi);X.pb(p[i].fi.se); Y.pb(p[i].se.fi);Y.pb(p[i].se.se); } sort(ALL(X));un(X); sort(ALL(Y));un(Y); M=1; while(M<=X.sz+2) M*=2; FOR(i,2*M)d[i].clear(); REP(i,1,n) { K[i].fi.fi=getX(p[i].fi.fi); K[i].fi.se=getX(p[i].fi.se)-1; K[i].se.fi=getY(p[i].se.fi); K[i].se.se=getY(p[i].se.se)-1; } //DBG(vector<pr>(K+1,K+n+1)); lx=X.sz; ly=Y.sz; REP(i,1,n) fu[i]=i; ev.clear(); miotla.clear(); REP(i,1,n) { if (szuk(i)==i) ev.insert(mp(K[i].fi.fi,-i)); } set<pii>::iterator it1,it2; //DBG(vector<pr>(K+1,K+n+1)); //DBG(vi(fu+1,fu+n+1)); while(ev.sz) { pii u=*(ev.begin());ev.erase(ev.begin()); //DBG(u); int nr=abs(u.se); if (u.se>0) { nr=szuk(nr); //DBG(nr); if (K[nr].fi.se==u.fi) { wywal_z_miotly(nr); } } else { while(1) { it1=miotla.insert(mp(K[nr].se.fi,-nr)).fi; it2=miotla.insert(mp(K[nr].se.se,nr)).fi; it1++; //DBG(*it1); if (*it1!=*it2) { int nr2=abs(it1->se); nr=polacz(nr,nr2); continue; } it2++; if (it2==miotla.end() || it2->se<0) break; int nr2=abs(it2->se); nr=polacz(nr,nr2); continue; } ev.insert(mp(K[nr].fi.se,nr)); } //DBG(u); } set<pair<pii,int > >::iterator it; rogi.clear(); REP(i,1,n) if (i==szuk(i)) dodaj_rog(i); REP(i,1,n) { { int nr=szuk(i); if (i>1 && nr==szuk(i-1)) continue; wyczysc_rog(nr); bool zmiana=1; while(zmiana) { zmiana=0; pii u=query(K[nr].fi.fi,K[nr].fi.se,K[nr].se.se); if (u.fi>=K[nr].se.fi) { zmiana=1; wyczysc_rog(u.se); nr=unia(nr,u.se); } /*for(it=rogi.begin();it!=rogi.end();it++) { pii u=it->fi; if (K[nr].fi.fi<=u.fi && u.fi<=K[nr].fi.se && K[nr].se.fi<=u.se && u.se<=K[nr].se.se) { zmiana=1; //DBG(*it); wyczysc_rog(it->se); nr=unia(nr,it->se); //DBG(nr); break; } }*/ //DBG(zmiana); } dodaj_rog(nr); } } /*REP(i,1,n)REP(j,1,i-1) { if (szuk(i)==i && szuk(j)==j) { pr prz=przekroj(K[i],K[j]); if (prz==K[i] || prz==K[j]) unia(i,j); } }*/ //DBG(vi(fu+1,fu+n+1)); vector<pr> wyn; REP(i,1,n) { if (szuk(i)==i) { pr f=K[i]; f.fi.fi=X[f.fi.fi]; f.fi.se=X[f.fi.se+1]; f.se.fi=Y[f.se.fi]; f.se.se=Y[f.se.se+1]; wyn.pb(f); } } sort(ALL(wyn)); return wyn; } int main2() { //srand(time(0)); int cnt=0; while(1) { cnt++; if (cnt%100==0) DBG(cnt); int N=1+rand()%20; int R=2+rand()%30; gen_test(N,R); if (brut()!=sol()) { DBG(vector<pr>(p+1,p+n+1)); DBG(brut()); DBG(sol()); return 0; } } return 0; } int main22() { //srand(time(0)); n=80000; REP(i,1,n) { p[i].fi.fi=rand(); p[i].fi.se=p[i].fi.fi+2; p[i].se.fi=rand(); p[i].se.se=p[i].se.fi+2; } //DBG(sol()); vector<pr> v=sol(); DBG(v.sz); //printf("%d\n",v.sz); //FOR(i,v.sz) printf("%d %d %d %d\n",v[i].fi.fi,v[i].fi.se,v[i].se.fi,v[i].se.se); return 0; } int main() { n=input.readInt(); REP(i,1,n) { p[i].fi.fi=input.readInt(); p[i].fi.se=input.readInt(); p[i].se.fi=input.readInt(); p[i].se.se=input.readInt(); } //DBG(sol()); vector<pr> v=sol(); //printf("%d\n",v.sz); output(v.sz,"\n"); FOR(i,v.sz) { output(v[i].fi.fi," ",v[i].fi.se); output(" ",v[i].se.fi," ",v[i].se.se,"\n"); //printf("%d %d %d %d\n",v[i].fi.fi,v[i].fi.se,v[i].se.fi,v[i].se.se); } return 0; } |