#include <cstdio> #include <list> #include <algorithm> using namespace std; int n; struct Rect { int x1,x2,y1,y2; }; list<Rect> s; inline bool operator< (const Rect& lhs, const Rect& rhs) { if (lhs.x1 != rhs.x1) return lhs.x1 < rhs.x1; if (lhs.x2 != rhs.x2) return lhs.x2 < rhs.x2; if (lhs.y1 != rhs.y1) return lhs.y1 < rhs.y1; return lhs.y2 < rhs.y2; } bool intersect(const Rect& r1, const Rect& r2) { return r1.x2 > r2.x1 && r1.x1 < r2.x2 && r1.y2 > r2.y1 && r1.y1 < r2.y2; } void insert(Rect& r) { bool changed = true; while (changed) { changed = false; for (auto it = s.begin(); it != s.end(); it++) { if (intersect(r, *it)) { r.x1 = min(r.x1, it->x1); r.x2 = max(r.x2, it->x2); r.y1 = min(r.y1, it->y1); r.y2 = max(r.y2, it->y2); changed = true; s.erase(it); break; } } if (!changed) s.push_front(r); } } int main() { Rect r; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2); insert(r); } s.sort(); printf("%d\n", s.size()); for (auto it = s.begin(); it != s.end(); it++) printf("%d %d %d %d\n", it->x1, it->x2, it->y1, it->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 | #include <cstdio> #include <list> #include <algorithm> using namespace std; int n; struct Rect { int x1,x2,y1,y2; }; list<Rect> s; inline bool operator< (const Rect& lhs, const Rect& rhs) { if (lhs.x1 != rhs.x1) return lhs.x1 < rhs.x1; if (lhs.x2 != rhs.x2) return lhs.x2 < rhs.x2; if (lhs.y1 != rhs.y1) return lhs.y1 < rhs.y1; return lhs.y2 < rhs.y2; } bool intersect(const Rect& r1, const Rect& r2) { return r1.x2 > r2.x1 && r1.x1 < r2.x2 && r1.y2 > r2.y1 && r1.y1 < r2.y2; } void insert(Rect& r) { bool changed = true; while (changed) { changed = false; for (auto it = s.begin(); it != s.end(); it++) { if (intersect(r, *it)) { r.x1 = min(r.x1, it->x1); r.x2 = max(r.x2, it->x2); r.y1 = min(r.y1, it->y1); r.y2 = max(r.y2, it->y2); changed = true; s.erase(it); break; } } if (!changed) s.push_front(r); } } int main() { Rect r; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2); insert(r); } s.sort(); printf("%d\n", s.size()); for (auto it = s.begin(); it != s.end(); it++) printf("%d %d %d %d\n", it->x1, it->x2, it->y1, it->y2); return 0; } |