#include <cstdio> #include <vector> #include <algorithm> // #define DEBUG 1 using namespace std; class M { public: int x0, y0, x1, y1; int u; M(){} M(int X0, int X1, int Y0, int Y1) : x0(X0), y0(Y0), x1(X1), y1(Y1), u(1) { #ifdef DEBUG puts("M::constructor"); dump(); #endif } void operator=(const M& b) { x0 = b.x0; y0 = b.y0; x1 = b.x1; y1 = b.y1; u = b.u; } bool operator==(const M& b) const { return x0 == b.x0 && y0 == b.y0 && x1 == b.x1 && y1 == b.y1; } bool operator!=(const M& b) const { return !(*this == b); } bool operator< (const M& b) const { #ifdef DEBUG2 printf("M: (%d %d) < (%d %d)\n", x0, y0, b.x0, b.y0); #endif if(x0 < b.x0) return true; if(x0 > b.x0) return false; if(y0 < b.y0) return true; if(y0 > b.y0) return false; if(x1 < b.x1) return false; if(x1 > b.x1) return true; if(y1 < b.y1) return false; if(y1 > b.y1) return true; } bool operator> (const M& b) const { return b < *this; } bool operator<=(const M& b) const { return !operator> (b); } bool operator>=(const M& b) const { return !operator< (b); } bool merge(M& b) { #ifdef DEBUG printf("Merge :\n"); dump(); b.dump(); printf("\tu=%d\n\tb.u=%d\n\t(x0 <= b.x0)=%d\n\t(x1 > b.x0)=%d\n\t(y0 <= b.y0)=%d\n\t(y1 > b.y0)=%d\n", u, b.u, x0 <= b.x0, x1 > b.x0, y0 <= b.y0, y1 > b.y0); #endif if(u && b.u && x0 <= b.x0 && x1 > b.x0 && y0 <= b.y0 && y1 > b.y0) { b.u = 0; if(x1 < b.x1) x1 = b.x1; if(y1 < b.y1) y1 = b.y1; #ifdef DEBUG printf("Merged\n"); dump(); #endif return true; } return false; } void dump() const { printf("%d %d %d %d\n", x0, x1, y0, y1); } }; typedef vector<M> TM; TM P = TM(); TM::iterator I, J, N; int n, a, b, c, d, r; int main() { scanf("%d", &n); P.reserve(n + 10); r = n; while(n--) { scanf("%d%d%d%d", &a, &b, &c, &d); P.push_back(M(a, b, c, d)); } sort(P.begin(), P.end()); for(I = P.begin(); I != P.end(); ++I) { if(I->u) { for(J = I + 1;J != P.end() && J->x0 < I->x1; ++J) { if(J->u && I->merge(*J)) { --r; J = I; } } } } printf("%d\n", r); for(I = P.begin(); I != P.end(); ++I) { if(I->u) I->dump(); } 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 | #include <cstdio> #include <vector> #include <algorithm> // #define DEBUG 1 using namespace std; class M { public: int x0, y0, x1, y1; int u; M(){} M(int X0, int X1, int Y0, int Y1) : x0(X0), y0(Y0), x1(X1), y1(Y1), u(1) { #ifdef DEBUG puts("M::constructor"); dump(); #endif } void operator=(const M& b) { x0 = b.x0; y0 = b.y0; x1 = b.x1; y1 = b.y1; u = b.u; } bool operator==(const M& b) const { return x0 == b.x0 && y0 == b.y0 && x1 == b.x1 && y1 == b.y1; } bool operator!=(const M& b) const { return !(*this == b); } bool operator< (const M& b) const { #ifdef DEBUG2 printf("M: (%d %d) < (%d %d)\n", x0, y0, b.x0, b.y0); #endif if(x0 < b.x0) return true; if(x0 > b.x0) return false; if(y0 < b.y0) return true; if(y0 > b.y0) return false; if(x1 < b.x1) return false; if(x1 > b.x1) return true; if(y1 < b.y1) return false; if(y1 > b.y1) return true; } bool operator> (const M& b) const { return b < *this; } bool operator<=(const M& b) const { return !operator> (b); } bool operator>=(const M& b) const { return !operator< (b); } bool merge(M& b) { #ifdef DEBUG printf("Merge :\n"); dump(); b.dump(); printf("\tu=%d\n\tb.u=%d\n\t(x0 <= b.x0)=%d\n\t(x1 > b.x0)=%d\n\t(y0 <= b.y0)=%d\n\t(y1 > b.y0)=%d\n", u, b.u, x0 <= b.x0, x1 > b.x0, y0 <= b.y0, y1 > b.y0); #endif if(u && b.u && x0 <= b.x0 && x1 > b.x0 && y0 <= b.y0 && y1 > b.y0) { b.u = 0; if(x1 < b.x1) x1 = b.x1; if(y1 < b.y1) y1 = b.y1; #ifdef DEBUG printf("Merged\n"); dump(); #endif return true; } return false; } void dump() const { printf("%d %d %d %d\n", x0, x1, y0, y1); } }; typedef vector<M> TM; TM P = TM(); TM::iterator I, J, N; int n, a, b, c, d, r; int main() { scanf("%d", &n); P.reserve(n + 10); r = n; while(n--) { scanf("%d%d%d%d", &a, &b, &c, &d); P.push_back(M(a, b, c, d)); } sort(P.begin(), P.end()); for(I = P.begin(); I != P.end(); ++I) { if(I->u) { for(J = I + 1;J != P.end() && J->x0 < I->x1; ++J) { if(J->u && I->merge(*J)) { --r; J = I; } } } } printf("%d\n", r); for(I = P.begin(); I != P.end(); ++I) { if(I->u) I->dump(); } return 0; } |