#include <cstdio> #include <algorithm> using namespace std; #define MAX_N 100000 struct Rect { int x1,x2,y1,y2; inline friend bool collision(Rect r1,Rect r2) { return r2.x2>r1.x1 && r1.x2>r2.x1 && r2.y2>r1.y1 && r1.y2>r2.y1; } inline friend Rect merge(Rect r1,Rect r2) { Rect ret; ret.x1=min(r1.x1,r2.x1); ret.x2=max(r1.x2,r2.x2); ret.y1=min(r1.y1,r2.y1); ret.y2=max(r1.y2,r2.y2); return ret; } inline bool operator < (const Rect r) const { return (x1==r.x1?(x2==r.x2?(y1==r.y1?y2<r.y2:y1<r.y1):x2<r.x2):x1<r.x1); } }; int n; Rect R[MAX_N]; int main() { scanf("%d",&n); for(int a=0;a<n;++a) { scanf("%d%d%d%d",&R[a].x1,&R[a].x2,&R[a].y1,&R[a].y2); } for(int p=0;p<n;++p) { START: if(R[p].x1!=-1) { for(int a=0;a<p;++a) { if(R[a].x1!=-1 && collision(R[p],R[a])) { R[p]=merge(R[p],R[a]); R[a].x1=-1; goto START; } } } } sort(R,R+n); int size=0; for(int p=0;p<n;++p) { if(R[p].x1==-1)size++; else break; } printf("%d\n",n-size); for(int p=size;p<n;++p) { printf("%d %d %d %d\n",R[p].x1,R[p].x2,R[p].y1,R[p].y2); } 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 | #include <cstdio> #include <algorithm> using namespace std; #define MAX_N 100000 struct Rect { int x1,x2,y1,y2; inline friend bool collision(Rect r1,Rect r2) { return r2.x2>r1.x1 && r1.x2>r2.x1 && r2.y2>r1.y1 && r1.y2>r2.y1; } inline friend Rect merge(Rect r1,Rect r2) { Rect ret; ret.x1=min(r1.x1,r2.x1); ret.x2=max(r1.x2,r2.x2); ret.y1=min(r1.y1,r2.y1); ret.y2=max(r1.y2,r2.y2); return ret; } inline bool operator < (const Rect r) const { return (x1==r.x1?(x2==r.x2?(y1==r.y1?y2<r.y2:y1<r.y1):x2<r.x2):x1<r.x1); } }; int n; Rect R[MAX_N]; int main() { scanf("%d",&n); for(int a=0;a<n;++a) { scanf("%d%d%d%d",&R[a].x1,&R[a].x2,&R[a].y1,&R[a].y2); } for(int p=0;p<n;++p) { START: if(R[p].x1!=-1) { for(int a=0;a<p;++a) { if(R[a].x1!=-1 && collision(R[p],R[a])) { R[p]=merge(R[p],R[a]); R[a].x1=-1; goto START; } } } } sort(R,R+n); int size=0; for(int p=0;p<n;++p) { if(R[p].x1==-1)size++; else break; } printf("%d\n",n-size); for(int p=size;p<n;++p) { printf("%d %d %d %d\n",R[p].x1,R[p].x2,R[p].y1,R[p].y2); } return 0; } |