#include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; struct terytorium { int x1, x2, y1, y2; }; bool xComparator(const terytorium& lhs, const terytorium& rhs) { return lhs.x1 < rhs.x1; } bool yComparator(const terytorium& lhs, const terytorium& rhs) { return lhs.y1 < rhs.y1; } bool xxyyComparator(const terytorium& lhs, const terytorium& rhs) { if (lhs.x1 < rhs.x1) return true; else if (lhs.x1 > rhs.x1) return false; if (lhs.x2 < rhs.x2) return true; else if (lhs.x2 > rhs.x2) return false; if (lhs.y1 < rhs.y1) return true; else if (lhs.y1 > lhs.y1) return false; if (lhs.y2 < rhs.y2) return true; return false; } inline bool intercepts(const terytorium& a, const terytorium& b) { return a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1; } inline terytorium merge(const terytorium& a, const terytorium& b) { terytorium t = {min(a.x1, b.x1), max(a.x2, b.x2), min(a.y1, b.y1), max(a.y2, b.y2)}; return t; } int main() { int n; scanf("%d", &n); vector<terytorium> ter(n); for (int i = 0; i < n; ++i) { int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &x2, &y1, &y2); terytorium t = {x1, x2, y1, y2}; ter[i] = t; } sort(ter.begin(), ter.end(), xxyyComparator); int k = 0; while(true) { /* printf("tersize: %lld, k=%d\n", ter.size(),k); for (int i = 0; i < ter.size(); ++i) { printf("%d %d %d %d\n", ter[i].x1, ter[i].x2, ter[i].y1, ter[i].y2); } printf("\n"); */ if (k+1 >= ter.size()) break; bool clearRun = true; terytorium newTerytory = ter[k]; int eee = k+1; while (eee < ter.size() && newTerytory.x2 > ter[eee].x1) eee++; vector<int> tersToRem; tersToRem.push_back(k); for (int i = k+1; i < eee; ++i) { if (intercepts(newTerytory, ter[i])) { clearRun = false; newTerytory = merge(newTerytory, ter[i]); tersToRem.push_back(i); } } if (tersToRem.size() > 1) { /* printf("Ters to rem: "); for (int i = 0; i < tersToRem.size(); ++i) printf("%d ", tersToRem[i]); printf("\n"); */ for (int i = tersToRem.size()-1; i >= 0; --i) { ter.erase(ter.begin()+tersToRem[i]); } ter.insert(ter.begin()+k, newTerytory); //ter.push_back(newTerytory); //sort(ter.begin(), ter.end(), xComparator); } if (clearRun) { k++; } else { k = 0; } } sort(ter.begin(), ter.end(), xxyyComparator); printf("%lu\n", ter.size()); for (int i = 0; i < ter.size(); ++i) { printf("%d %d %d %d\n", ter[i].x1, ter[i].x2, ter[i].y1, ter[i].y2); } }
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 | #include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; struct terytorium { int x1, x2, y1, y2; }; bool xComparator(const terytorium& lhs, const terytorium& rhs) { return lhs.x1 < rhs.x1; } bool yComparator(const terytorium& lhs, const terytorium& rhs) { return lhs.y1 < rhs.y1; } bool xxyyComparator(const terytorium& lhs, const terytorium& rhs) { if (lhs.x1 < rhs.x1) return true; else if (lhs.x1 > rhs.x1) return false; if (lhs.x2 < rhs.x2) return true; else if (lhs.x2 > rhs.x2) return false; if (lhs.y1 < rhs.y1) return true; else if (lhs.y1 > lhs.y1) return false; if (lhs.y2 < rhs.y2) return true; return false; } inline bool intercepts(const terytorium& a, const terytorium& b) { return a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1; } inline terytorium merge(const terytorium& a, const terytorium& b) { terytorium t = {min(a.x1, b.x1), max(a.x2, b.x2), min(a.y1, b.y1), max(a.y2, b.y2)}; return t; } int main() { int n; scanf("%d", &n); vector<terytorium> ter(n); for (int i = 0; i < n; ++i) { int x1, x2, y1, y2; scanf("%d%d%d%d", &x1, &x2, &y1, &y2); terytorium t = {x1, x2, y1, y2}; ter[i] = t; } sort(ter.begin(), ter.end(), xxyyComparator); int k = 0; while(true) { /* printf("tersize: %lld, k=%d\n", ter.size(),k); for (int i = 0; i < ter.size(); ++i) { printf("%d %d %d %d\n", ter[i].x1, ter[i].x2, ter[i].y1, ter[i].y2); } printf("\n"); */ if (k+1 >= ter.size()) break; bool clearRun = true; terytorium newTerytory = ter[k]; int eee = k+1; while (eee < ter.size() && newTerytory.x2 > ter[eee].x1) eee++; vector<int> tersToRem; tersToRem.push_back(k); for (int i = k+1; i < eee; ++i) { if (intercepts(newTerytory, ter[i])) { clearRun = false; newTerytory = merge(newTerytory, ter[i]); tersToRem.push_back(i); } } if (tersToRem.size() > 1) { /* printf("Ters to rem: "); for (int i = 0; i < tersToRem.size(); ++i) printf("%d ", tersToRem[i]); printf("\n"); */ for (int i = tersToRem.size()-1; i >= 0; --i) { ter.erase(ter.begin()+tersToRem[i]); } ter.insert(ter.begin()+k, newTerytory); //ter.push_back(newTerytory); //sort(ter.begin(), ter.end(), xComparator); } if (clearRun) { k++; } else { k = 0; } } sort(ter.begin(), ter.end(), xxyyComparator); printf("%lu\n", ter.size()); for (int i = 0; i < ter.size(); ++i) { printf("%d %d %d %d\n", ter[i].x1, ter[i].x2, ter[i].y1, ter[i].y2); } } |