#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Rct { int x1, x2, y1, y2; Rct(int a, int b, int c, int d) : x1(a), x2(b), y1(c), y2(d) { } }; bool col(Rct a, Rct b) { // Prostokąt if( b.x1 <= a.x2 && b.x1 >= a.x1 && b.y2 >= a.y1 && b.y2 <= a.y2) return true; if( b.x1 <= a.x2 && b.x1 >= a.x1 && b.y1 >= a.y1 && b.y1 <= a.y2) return true; if( b.x2 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y1 <= a.y2) return true; if( b.x2 >= a.x1 && b.x2 <= a.x2 && b.y2 >= a.y1 && b.y2 <= a.y2) return true; if( b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 <= a.y1 && b.y2 >= a.y2) return true; if( b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y2 <= a.y2) return true; return false; } bool fsort(Rct a, Rct b) { if(a.x1<b.x1) return true; else return false; if(a.x2<b.x2) return true; else return false; if(a.y1<b.y1) return true; else return false; if(a.y2<b.y2) return true; else return false; } bool dodpol(Rct a, Rct b) { if(a.x2 - b.x1 == 0 || a.x1 - b.x2 == 0 || a.y1 - b.y2 == 0 || b.y1 - a.y2 == 0) return false; return true; } int main() { ios_base::sync_with_stdio(0); int n; // 1 <= n <= 100 000 cin >> n; vector<Rct> rcts; int a, b, c, d; for(int i=0; i<n; ++i) { cin >> a >> b >> c >> d; rcts.push_back(Rct(a, b, c, d)); } for(int i=0; i<rcts.size(); ++i) { for(int j=0; j<rcts.size(); ++j) { if(i==j) continue; if(col(rcts[i], rcts[j])) { if(!dodpol(rcts[i], rcts[j])) continue; rcts.push_back( Rct(min(rcts[i].x1, rcts[j].x1), max(rcts[i].x2, rcts[j].x2), min(rcts[i].y1, rcts[j].y1), max(rcts[i].y2, rcts[j].y2) ) ); rcts.erase(rcts.begin()+j); if(i>j) rcts.erase(rcts.begin()+i-1); else rcts.erase(rcts.begin()+i); i=0; j=-1; } } } sort(rcts.begin(), rcts.end(), fsort); cout << rcts.size() << endl; for(int i=0; i<rcts.size(); ++i) { cout << rcts[i].x1 << " " << rcts[i].x2 << " " << rcts[i].y1 << " " << rcts[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 <vector> #include <algorithm> using namespace std; struct Rct { int x1, x2, y1, y2; Rct(int a, int b, int c, int d) : x1(a), x2(b), y1(c), y2(d) { } }; bool col(Rct a, Rct b) { // Prostokąt if( b.x1 <= a.x2 && b.x1 >= a.x1 && b.y2 >= a.y1 && b.y2 <= a.y2) return true; if( b.x1 <= a.x2 && b.x1 >= a.x1 && b.y1 >= a.y1 && b.y1 <= a.y2) return true; if( b.x2 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y1 <= a.y2) return true; if( b.x2 >= a.x1 && b.x2 <= a.x2 && b.y2 >= a.y1 && b.y2 <= a.y2) return true; if( b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 <= a.y1 && b.y2 >= a.y2) return true; if( b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y2 <= a.y2) return true; return false; } bool fsort(Rct a, Rct b) { if(a.x1<b.x1) return true; else return false; if(a.x2<b.x2) return true; else return false; if(a.y1<b.y1) return true; else return false; if(a.y2<b.y2) return true; else return false; } bool dodpol(Rct a, Rct b) { if(a.x2 - b.x1 == 0 || a.x1 - b.x2 == 0 || a.y1 - b.y2 == 0 || b.y1 - a.y2 == 0) return false; return true; } int main() { ios_base::sync_with_stdio(0); int n; // 1 <= n <= 100 000 cin >> n; vector<Rct> rcts; int a, b, c, d; for(int i=0; i<n; ++i) { cin >> a >> b >> c >> d; rcts.push_back(Rct(a, b, c, d)); } for(int i=0; i<rcts.size(); ++i) { for(int j=0; j<rcts.size(); ++j) { if(i==j) continue; if(col(rcts[i], rcts[j])) { if(!dodpol(rcts[i], rcts[j])) continue; rcts.push_back( Rct(min(rcts[i].x1, rcts[j].x1), max(rcts[i].x2, rcts[j].x2), min(rcts[i].y1, rcts[j].y1), max(rcts[i].y2, rcts[j].y2) ) ); rcts.erase(rcts.begin()+j); if(i>j) rcts.erase(rcts.begin()+i-1); else rcts.erase(rcts.begin()+i); i=0; j=-1; } } } sort(rcts.begin(), rcts.end(), fsort); cout << rcts.size() << endl; for(int i=0; i<rcts.size(); ++i) { cout << rcts[i].x1 << " " << rcts[i].x2 << " " << rcts[i].y1 << " " << rcts[i].y2 << endl; } return 0; } |