#include <cstdio> #include <algorithm> #include <set> #include <vector> #define PB push_back using namespace std; struct str { int x1, y, x2, y2; }; const int MX_N = 100010; int n, _x1, _y1, _x2, _y2, x1[MX_N], y[MX_N], x2[MX_N], y2[MX_N]; bool del[MX_N]; set<int> s; vector<str> v; bool czyWspolne(int i, int j) { if (i == j) return false; return (max(x1[i], x1[j]) < min(x2[i], x2[j])) && (max(y[i], y[j]) < min(y2[i], y2[j])); } bool cmp(str i, str j) { if (i.x1 != j.x1) return i.x1 < j.x1; if (i.x2 != j.x2) return i.x2 < j.x2; if (i.y != j.y) return i.y < j.y; return i.y2 < j.y2; } int main () { scanf ("%d", &n); for (int i = 0; i < n; ++i) { scanf ("%d%d%d%d", &_x1, &_x2, &_y1, &_y2); x1[i] = min(_x1, _x2); x2[i] = max(_x1, _x2); y[i] = min(_y1, _y2); y2[i] = max(_y1, _y2); s.insert(i); } for (int i = 0; i < n; ++i) { if (del[i]) continue; set<int>::iterator j; j = s.begin(); while(j != s.end()) { while ((j != s.end()) && !czyWspolne(i, *j)) ++j; if (j != s.end()) // czyWspolne(i, j) == true { del[*j] = true; x1[i] = min(x1[i], x1[*j]); x2[i] = max(x2[i], x2[*j]); y[i] = min(y[i], y[*j]); y2[i] = max(y2[i], y2[*j]); s.erase(j); j = s.begin(); } else break; } } for (int i = 0; i < n; ++i) { if (!del[i]) { str pom; pom.x1 = x1[i]; pom.x2 = x2[i]; pom.y = y[i]; pom.y2 = y2[i]; v.PB(pom); } } sort(v.begin(), v.end(), cmp); int siz = v.size(); printf ("%d\n", siz); for (int i = 0; i < siz; ++i) printf ("%d %d %d %d\n", v[i].x1, v[i].x2, v[i].y, v[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 | #include <cstdio> #include <algorithm> #include <set> #include <vector> #define PB push_back using namespace std; struct str { int x1, y, x2, y2; }; const int MX_N = 100010; int n, _x1, _y1, _x2, _y2, x1[MX_N], y[MX_N], x2[MX_N], y2[MX_N]; bool del[MX_N]; set<int> s; vector<str> v; bool czyWspolne(int i, int j) { if (i == j) return false; return (max(x1[i], x1[j]) < min(x2[i], x2[j])) && (max(y[i], y[j]) < min(y2[i], y2[j])); } bool cmp(str i, str j) { if (i.x1 != j.x1) return i.x1 < j.x1; if (i.x2 != j.x2) return i.x2 < j.x2; if (i.y != j.y) return i.y < j.y; return i.y2 < j.y2; } int main () { scanf ("%d", &n); for (int i = 0; i < n; ++i) { scanf ("%d%d%d%d", &_x1, &_x2, &_y1, &_y2); x1[i] = min(_x1, _x2); x2[i] = max(_x1, _x2); y[i] = min(_y1, _y2); y2[i] = max(_y1, _y2); s.insert(i); } for (int i = 0; i < n; ++i) { if (del[i]) continue; set<int>::iterator j; j = s.begin(); while(j != s.end()) { while ((j != s.end()) && !czyWspolne(i, *j)) ++j; if (j != s.end()) // czyWspolne(i, j) == true { del[*j] = true; x1[i] = min(x1[i], x1[*j]); x2[i] = max(x2[i], x2[*j]); y[i] = min(y[i], y[*j]); y2[i] = max(y2[i], y2[*j]); s.erase(j); j = s.begin(); } else break; } } for (int i = 0; i < n; ++i) { if (!del[i]) { str pom; pom.x1 = x1[i]; pom.x2 = x2[i]; pom.y = y[i]; pom.y2 = y2[i]; v.PB(pom); } } sort(v.begin(), v.end(), cmp); int siz = v.size(); printf ("%d\n", siz); for (int i = 0; i < siz; ++i) printf ("%d %d %d %d\n", v[i].x1, v[i].x2, v[i].y, v[i].y2); } |