#include<iostream> #include<algorithm> #include<vector> #include<list> using namespace std; struct prostokat{ int x1, x2, y1, y2, n; }p; bool comp(prostokat a, prostokat b) { if(a.n == b.n) return true; return false; } bool operator<(prostokat a, prostokat b) { if(a.x1!=b.x1) return a.x1<b.x1; else { if(a.x2!=b.x2) return a.x2<b.x2; else { if(a.y1!=b.y1) return a.y1<b.y1; else { if(a.y2!=b.y2) return a.y2<b.y2; else return false; } } } } prostokat expo(prostokat a, prostokat b) { prostokat c; c.x1=min(a.x1, b.x1); c.x2=max(a.x2, b.x2); c.y1=min(a.y1, b.y1); c.y2=max(a.y2, b.y2); return c; } /*bool czwsp(prostokat a, prostokat b){ prostokat c; c.x1=max(a.x1, b.x1); c.x2=min(a.x2, b.x2); c.y1=max(a.y1, b.y1); c.y2=min(a.y2, b.y2); if( c.y1 > c.y2 || c.x1 > c.x2) return false; return true; }*/ bool czwsp(prostokat A, prostokat B) { if(B.x1 >= A.x2 || B.x2 <= A.x1 || B.y1 >= A.y2 || B.y2 <= A.y1) return false; else return true; } int main () { ios_base::sync_with_stdio(0); int n; vector<prostokat> L; bool B[100000+77]; cin>>n; for(int i=0; i<n; i++) { p.n=i; cin>>p.x1>>p.x2>>p.y1>>p.y2; L.push_back(p); B[i]=true; } bool c=true; while(c) { c=false; for(int i=0; i<n; i++) { if(B[L[i].n]) for(int j=0; j<n; j++) { if(B[L[j].n]) if(!comp(L[i],L[j])) if(czwsp(L[i], L[j])) { // cout<<i<<" "<<j<<endl; L[i]=expo(L[i], L[j]); B[L[j].n]=false; c=true; } } } } vector<prostokat> W; for(int i=0; i<n; i++) if(B[i]) { W.push_back(L[i]); } sort(W.begin(), W.end()); cout<<W.size()<<endl; for(int i=0; i<W.size(); i++) cout<<W[i].x1<<" "<<W[i].x2<<" "<<W[i].y1<<" "<<W[i].y2<<endl; 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 | #include<iostream> #include<algorithm> #include<vector> #include<list> using namespace std; struct prostokat{ int x1, x2, y1, y2, n; }p; bool comp(prostokat a, prostokat b) { if(a.n == b.n) return true; return false; } bool operator<(prostokat a, prostokat b) { if(a.x1!=b.x1) return a.x1<b.x1; else { if(a.x2!=b.x2) return a.x2<b.x2; else { if(a.y1!=b.y1) return a.y1<b.y1; else { if(a.y2!=b.y2) return a.y2<b.y2; else return false; } } } } prostokat expo(prostokat a, prostokat b) { prostokat c; c.x1=min(a.x1, b.x1); c.x2=max(a.x2, b.x2); c.y1=min(a.y1, b.y1); c.y2=max(a.y2, b.y2); return c; } /*bool czwsp(prostokat a, prostokat b){ prostokat c; c.x1=max(a.x1, b.x1); c.x2=min(a.x2, b.x2); c.y1=max(a.y1, b.y1); c.y2=min(a.y2, b.y2); if( c.y1 > c.y2 || c.x1 > c.x2) return false; return true; }*/ bool czwsp(prostokat A, prostokat B) { if(B.x1 >= A.x2 || B.x2 <= A.x1 || B.y1 >= A.y2 || B.y2 <= A.y1) return false; else return true; } int main () { ios_base::sync_with_stdio(0); int n; vector<prostokat> L; bool B[100000+77]; cin>>n; for(int i=0; i<n; i++) { p.n=i; cin>>p.x1>>p.x2>>p.y1>>p.y2; L.push_back(p); B[i]=true; } bool c=true; while(c) { c=false; for(int i=0; i<n; i++) { if(B[L[i].n]) for(int j=0; j<n; j++) { if(B[L[j].n]) if(!comp(L[i],L[j])) if(czwsp(L[i], L[j])) { // cout<<i<<" "<<j<<endl; L[i]=expo(L[i], L[j]); B[L[j].n]=false; c=true; } } } } vector<prostokat> W; for(int i=0; i<n; i++) if(B[i]) { W.push_back(L[i]); } sort(W.begin(), W.end()); cout<<W.size()<<endl; for(int i=0; i<W.size(); i++) cout<<W[i].x1<<" "<<W[i].x2<<" "<<W[i].y1<<" "<<W[i].y2<<endl; return 0; } |