#include <cstdio> #include <algorithm> #include <list> using namespace std; #define MIN(a, b) a < b ? a : b #define MAX(a, b) a > b ? a : b #define MAXN 100000 typedef class _Punkt { public: int x; int y; _Punkt() : x(0), y(0) { } _Punkt(int x, int y) : x(x), y(y) { } } Punkt; typedef class _Prostokat { public: Punkt leftDown; Punkt rightUp; _Prostokat() : leftDown(), rightUp() { } _Prostokat(Punkt a, Punkt b) : leftDown(a), rightUp(b) { } void print() { // fprintf(stderr, "(%d %d) x (%d %d)\n", leftDown.x, leftDown.y, // rightUp.x, rightUp.y); printf("%d %d %d %d\n", leftDown.x, rightUp.x, leftDown.y, rightUp.y); } } Prostokat; bool przecina(Prostokat a, Prostokat b) { if ((a.rightUp.x <= b.leftDown.x) || (b.rightUp.x <= a.leftDown.x) || (a.rightUp.y <= b.leftDown.y) || (b.rightUp.y <= a.leftDown.y)) { return false; } return true; } Prostokat przeciecie(Prostokat a, Prostokat b) { return Prostokat( Punkt(MIN(a.leftDown.x, b.leftDown.x), MIN(a.leftDown.y, b.leftDown.y)), Punkt(MAX(a.rightUp.x, b.rightUp.x), MAX(a.rightUp.y, b.rightUp.y))); } bool compare (const Prostokat& first, const Prostokat& second) { if (first.leftDown.x == second.leftDown.x) { if (first.rightUp.x == second.rightUp.x) { if (first.leftDown.y == second.leftDown.y) { return first.rightUp.y < second.rightUp.y; } else { return first.leftDown.y < second.leftDown.y; } } else { return first.rightUp.x < second.rightUp.x; } } else { return first.leftDown.x < second.leftDown.x; } } list<Prostokat> prostokaty; void dodaj(Prostokat p) { for (list<Prostokat>::iterator it = prostokaty.begin(); it != prostokaty.end(); ++it) { if (przecina(*it, p)) { Prostokat r = przeciecie(*it, p); prostokaty.erase(it); dodaj(r); return; } } prostokaty.push_back(p); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); dodaj(Prostokat(Punkt(x1, y1), Punkt(x2, y2))); } prostokaty.sort(compare); printf("%lu\n", prostokaty.size()); for (list<Prostokat>::iterator it = prostokaty.begin(); it != prostokaty.end(); ++it) { it->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 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <cstdio> #include <algorithm> #include <list> using namespace std; #define MIN(a, b) a < b ? a : b #define MAX(a, b) a > b ? a : b #define MAXN 100000 typedef class _Punkt { public: int x; int y; _Punkt() : x(0), y(0) { } _Punkt(int x, int y) : x(x), y(y) { } } Punkt; typedef class _Prostokat { public: Punkt leftDown; Punkt rightUp; _Prostokat() : leftDown(), rightUp() { } _Prostokat(Punkt a, Punkt b) : leftDown(a), rightUp(b) { } void print() { // fprintf(stderr, "(%d %d) x (%d %d)\n", leftDown.x, leftDown.y, // rightUp.x, rightUp.y); printf("%d %d %d %d\n", leftDown.x, rightUp.x, leftDown.y, rightUp.y); } } Prostokat; bool przecina(Prostokat a, Prostokat b) { if ((a.rightUp.x <= b.leftDown.x) || (b.rightUp.x <= a.leftDown.x) || (a.rightUp.y <= b.leftDown.y) || (b.rightUp.y <= a.leftDown.y)) { return false; } return true; } Prostokat przeciecie(Prostokat a, Prostokat b) { return Prostokat( Punkt(MIN(a.leftDown.x, b.leftDown.x), MIN(a.leftDown.y, b.leftDown.y)), Punkt(MAX(a.rightUp.x, b.rightUp.x), MAX(a.rightUp.y, b.rightUp.y))); } bool compare (const Prostokat& first, const Prostokat& second) { if (first.leftDown.x == second.leftDown.x) { if (first.rightUp.x == second.rightUp.x) { if (first.leftDown.y == second.leftDown.y) { return first.rightUp.y < second.rightUp.y; } else { return first.leftDown.y < second.leftDown.y; } } else { return first.rightUp.x < second.rightUp.x; } } else { return first.leftDown.x < second.leftDown.x; } } list<Prostokat> prostokaty; void dodaj(Prostokat p) { for (list<Prostokat>::iterator it = prostokaty.begin(); it != prostokaty.end(); ++it) { if (przecina(*it, p)) { Prostokat r = przeciecie(*it, p); prostokaty.erase(it); dodaj(r); return; } } prostokaty.push_back(p); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &x2, &y1, &y2); dodaj(Prostokat(Punkt(x1, y1), Punkt(x2, y2))); } prostokaty.sort(compare); printf("%lu\n", prostokaty.size()); for (list<Prostokat>::iterator it = prostokaty.begin(); it != prostokaty.end(); ++it) { it->print(); } return 0; } |