#include <cstdio> #include <algorithm> using namespace std; struct Rectangle { int x1, x2, y1, y2; void load() { scanf ("%d%d%d%d", &x1, &x2, &y1, &y2); } void print() { printf("%d %d %d %d\n", x1, x2, y1, y2); } bool operator< (const Rectangle &other) const { if (x1 == other.x1) { if (x2 == other.x2) { if (y1 == other.y1) return y2 < other.y2; return y1 < other.y1; } return x2 < other.x2; } return x1 < other.x1; } void absorb(const Rectangle &A) { if (x1 > A.x1) x1 = A.x1; if (y1 > A.y1) y1 = A.y1; if (x2 < A.x2) x2 = A.x2; if (y2 < A.y2) y2 = A.y2; } }; bool overlaps(const Rectangle &A, const Rectangle &B) { return ((B.x2 > A.x1) and (B.x1 < A.x2) and (B.y2 > A.y1) and (B.y1 < A.y2)); } Rectangle T[100000]; int main() { int n; scanf ("%d", &n); for (int i=0; i<n; i++) T[i].load(); int prevRes = -1; while (prevRes != n) { prevRes = n; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i == j) continue; if (overlaps(T[i], T[j])) { T[i].absorb(T[j]); swap(T[j], T[n-1]); n--, j--; } if (i >= n) i = j + 1; } } } sort(T, T + n); printf("%d\n", n); for (int i=0; i<n; i++) T[i].print(); 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 | #include <cstdio> #include <algorithm> using namespace std; struct Rectangle { int x1, x2, y1, y2; void load() { scanf ("%d%d%d%d", &x1, &x2, &y1, &y2); } void print() { printf("%d %d %d %d\n", x1, x2, y1, y2); } bool operator< (const Rectangle &other) const { if (x1 == other.x1) { if (x2 == other.x2) { if (y1 == other.y1) return y2 < other.y2; return y1 < other.y1; } return x2 < other.x2; } return x1 < other.x1; } void absorb(const Rectangle &A) { if (x1 > A.x1) x1 = A.x1; if (y1 > A.y1) y1 = A.y1; if (x2 < A.x2) x2 = A.x2; if (y2 < A.y2) y2 = A.y2; } }; bool overlaps(const Rectangle &A, const Rectangle &B) { return ((B.x2 > A.x1) and (B.x1 < A.x2) and (B.y2 > A.y1) and (B.y1 < A.y2)); } Rectangle T[100000]; int main() { int n; scanf ("%d", &n); for (int i=0; i<n; i++) T[i].load(); int prevRes = -1; while (prevRes != n) { prevRes = n; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if (i == j) continue; if (overlaps(T[i], T[j])) { T[i].absorb(T[j]); swap(T[j], T[n-1]); n--, j--; } if (i >= n) i = j + 1; } } } sort(T, T + n); printf("%d\n", n); for (int i=0; i<n; i++) T[i].print(); return 0; } |