#include <iostream> #include <cstdio> #include <algorithm> using namespace std; struct Rectangle{ int x1; int x2; int y1; int y2; }; struct Event{ int x; int y; int type; }; void doStuff_Y(int a,int b,int x); void mcount(int p,int q,char prio); int nbr; int nbr2; Event* E; Rectangle* R; bool oper1(Event e1, Event e2){ if(e1.y==e2.y)return(e1.type<e2.type); return (e1.y<e2.y); } bool oper2(Event e1, Event e2){ if(e1.x==e2.x)return(e1.type<e2.type); return (e1.x<e2.x); } void doStuff_X(int a,int b,int x){ // cout<<"XXXXXXXX "<<a<<" - "<<b<<endl; if(a==b)return; int sum=0; sort(E+a,E+b,oper2); for(int i=a;i<b-2;i++){ // cout<<" X "<<i<<" sum = "<<sum<<" E="<<E[i].x<<","<<E[i].y<<" t"<<E[i].type<<endl; sum=sum+E[i].type; if(sum==0){ nbr++; // doStuff_Y(a,i+1,0); // doStuff_Y(i+1,b,0); // cout<<"return to X "<<a<<"-"<<b<<endl; mcount(a,i+1,'y'); mcount(i+1,b,'y'); return; } } // if(x==0){ doStuff_Y(a,b,1);} // if(x==1)nbr++; } /* 3 1 2 1 2 3 4 4 5 5 6 1 2 */ void doStuff_Y(int a,int b,int x){ // cout<<"YYYYYYYY "<<a<<" - "<<b<<endl; if(a==b)return; int sum=0; sort(E+a,E+b,oper1); for(int i=a;i<b-2;i++){ // cout<<" Y "<<i<<" sum = "<<sum<<" E="<<E[i].x<<","<<E[i].y<<" t"<<E[i].type<<endl; sum=sum+E[i].type; if(sum==0){ nbr++; //doStuff_X(a,i+1,0); //doStuff_X(i+1,b,0); //cout<<"return to Y "<<a<<"-"<<b<<endl; mcount(a,i+1,'x'); mcount(i+1,b,'x'); return; } } // if(x==0){ doStuff_X(a,b,1);} // if(x==1)nbr++; } void mcount(int p,int q,char prio){ // cout<<" count : "<<p<<" * "<<q<<endl; int tmpnbr=nbr; if(prio='x'){ doStuff_X(p,q,0); if(nbr-tmpnbr==0)doStuff_Y(p,q,0); }else{ doStuff_Y(p,q,0); if(nbr-tmpnbr==0)doStuff_X(p,q,0); } if(nbr-tmpnbr==0){ //cout<<"####### : "<<p<<" * "<<q<<endl; int minx=E[p].x; int maxx=E[p].x; int miny=E[p].y; int maxy=E[p].y; for(int i=p;i<q;i++){ //cout<<"----------------> x="<<E[i].x<<" y="<<E[i].y<<" ;"<<minx<<" "<<maxx<<" "<<miny<<" "<<maxy<<endl; if(minx>E[i].x)minx=E[i].x; if(maxx<E[i].x)maxx=E[i].x; if(miny>E[i].y)miny=E[i].y; if(maxy<E[i].y)maxy=E[i].y; } R[nbr2].x1=minx; R[nbr2].x2=maxx; R[nbr2].y1=miny; R[nbr2].y2=maxy; nbr2++; } } int main(){ int n; scanf("%d",&n); /* int* X1=new int[n]; int* X2=new int[n]; int* Y1=new int[n]; int* Y2=new int[n];*/ int a,b,c,d; E=new Event[n]; R=new Rectangle[n]; for(int i=0;i<2*n;i=i+2){ // scanf("%d %d %d %d",&X1[i],&X2[i],&Y1[i],&Y2[i]); // scanf("%d %d %d %d",&R[i].x1,&R[i].x2,&R[i].y1,&R[i].y2); scanf("%d %d %d %d",&a,&b,&c,&d); E[i].x=a; E[i].y=c; E[i].type=1; E[i+1].x=b; E[i+1].y=d; E[i+1].type=-1; } //sort(E+0,E+2*n,oper1); //for(int i=0; i<2*n;i++)cout<<E[i].x<<" "<<E[i].y<<" "<<E[i].type<<endl; nbr=0; nbr2=0; //doStuff_Y(0,2*n,0); //doStuff_X(0,2*n,0); mcount(0,2*n,'x'); nbr++; // cout<<" RESULT= "<<nbr<<" 2->"<<nbr2<<endl; printf("%d\n",nbr); for(int i=0;i<nbr2;i++){ printf("%d %d %d %d\n",R[i].x1,R[i].x2,R[i].y1,R[i].y2); } //cin>>n; 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 | #include <iostream> #include <cstdio> #include <algorithm> using namespace std; struct Rectangle{ int x1; int x2; int y1; int y2; }; struct Event{ int x; int y; int type; }; void doStuff_Y(int a,int b,int x); void mcount(int p,int q,char prio); int nbr; int nbr2; Event* E; Rectangle* R; bool oper1(Event e1, Event e2){ if(e1.y==e2.y)return(e1.type<e2.type); return (e1.y<e2.y); } bool oper2(Event e1, Event e2){ if(e1.x==e2.x)return(e1.type<e2.type); return (e1.x<e2.x); } void doStuff_X(int a,int b,int x){ // cout<<"XXXXXXXX "<<a<<" - "<<b<<endl; if(a==b)return; int sum=0; sort(E+a,E+b,oper2); for(int i=a;i<b-2;i++){ // cout<<" X "<<i<<" sum = "<<sum<<" E="<<E[i].x<<","<<E[i].y<<" t"<<E[i].type<<endl; sum=sum+E[i].type; if(sum==0){ nbr++; // doStuff_Y(a,i+1,0); // doStuff_Y(i+1,b,0); // cout<<"return to X "<<a<<"-"<<b<<endl; mcount(a,i+1,'y'); mcount(i+1,b,'y'); return; } } // if(x==0){ doStuff_Y(a,b,1);} // if(x==1)nbr++; } /* 3 1 2 1 2 3 4 4 5 5 6 1 2 */ void doStuff_Y(int a,int b,int x){ // cout<<"YYYYYYYY "<<a<<" - "<<b<<endl; if(a==b)return; int sum=0; sort(E+a,E+b,oper1); for(int i=a;i<b-2;i++){ // cout<<" Y "<<i<<" sum = "<<sum<<" E="<<E[i].x<<","<<E[i].y<<" t"<<E[i].type<<endl; sum=sum+E[i].type; if(sum==0){ nbr++; //doStuff_X(a,i+1,0); //doStuff_X(i+1,b,0); //cout<<"return to Y "<<a<<"-"<<b<<endl; mcount(a,i+1,'x'); mcount(i+1,b,'x'); return; } } // if(x==0){ doStuff_X(a,b,1);} // if(x==1)nbr++; } void mcount(int p,int q,char prio){ // cout<<" count : "<<p<<" * "<<q<<endl; int tmpnbr=nbr; if(prio='x'){ doStuff_X(p,q,0); if(nbr-tmpnbr==0)doStuff_Y(p,q,0); }else{ doStuff_Y(p,q,0); if(nbr-tmpnbr==0)doStuff_X(p,q,0); } if(nbr-tmpnbr==0){ //cout<<"####### : "<<p<<" * "<<q<<endl; int minx=E[p].x; int maxx=E[p].x; int miny=E[p].y; int maxy=E[p].y; for(int i=p;i<q;i++){ //cout<<"----------------> x="<<E[i].x<<" y="<<E[i].y<<" ;"<<minx<<" "<<maxx<<" "<<miny<<" "<<maxy<<endl; if(minx>E[i].x)minx=E[i].x; if(maxx<E[i].x)maxx=E[i].x; if(miny>E[i].y)miny=E[i].y; if(maxy<E[i].y)maxy=E[i].y; } R[nbr2].x1=minx; R[nbr2].x2=maxx; R[nbr2].y1=miny; R[nbr2].y2=maxy; nbr2++; } } int main(){ int n; scanf("%d",&n); /* int* X1=new int[n]; int* X2=new int[n]; int* Y1=new int[n]; int* Y2=new int[n];*/ int a,b,c,d; E=new Event[n]; R=new Rectangle[n]; for(int i=0;i<2*n;i=i+2){ // scanf("%d %d %d %d",&X1[i],&X2[i],&Y1[i],&Y2[i]); // scanf("%d %d %d %d",&R[i].x1,&R[i].x2,&R[i].y1,&R[i].y2); scanf("%d %d %d %d",&a,&b,&c,&d); E[i].x=a; E[i].y=c; E[i].type=1; E[i+1].x=b; E[i+1].y=d; E[i+1].type=-1; } //sort(E+0,E+2*n,oper1); //for(int i=0; i<2*n;i++)cout<<E[i].x<<" "<<E[i].y<<" "<<E[i].type<<endl; nbr=0; nbr2=0; //doStuff_Y(0,2*n,0); //doStuff_X(0,2*n,0); mcount(0,2*n,'x'); nbr++; // cout<<" RESULT= "<<nbr<<" 2->"<<nbr2<<endl; printf("%d\n",nbr); for(int i=0;i<nbr2;i++){ printf("%d %d %d %d\n",R[i].x1,R[i].x2,R[i].y1,R[i].y2); } //cin>>n; return 0; } |