#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include <tuple> using namespace std; const int N = 100002; namespace pie { const int TR = 1; int x1[N], x2[N], y1[N], y2[N]; int x[2*N], y[2*N]; bool removed[N]; bool changed[N]; const int INF = 0x7fffffff; typedef tuple<int,int,int,int> t4; typedef tuple<bool,int,int,int,int> t5; bool overlaps(int a, int b) { if (x2[b] <= x1[a]) return false; if (x1[b] >= x2[a]) return false; if (y2[b] <= y1[a]) return false; if (y1[b] >= y2[a]) return false; return true; } t5 join(const t5 &a, const t5 &b) { if (!get<0>(a)) return b; if (!get<0>(b)) return a; return make_tuple( true, min(get<1>(a), get<1>(b)), max(get<2>(a), get<2>(b)), min(get<3>(a), get<3>(b)), max(get<4>(a), get<4>(b)) ); } t5 join(int a, int b) { x1[a] = min(x1[a], x1[b]); x2[a] = max(x2[a], x2[b]); y1[a] = min(y1[a], y1[b]); y2[a] = max(y2[a], y2[b]); // printf("join(%d, %d)[%d %d][%d %d]\n", a, b, x1[a]/2, y1[a]/2, x2[a]/2, y2[a]/2); removed[b] = true; changed[a] = true; return make_tuple(true, x1[a], x2[a], y1[a], y2[a]); } bool checkChanged(const vector<int> &in) { for (int i : in) { if (changed[i]) return true; } return false; } void clearChanged(const vector<int> &in) { for (int i : in) { changed[i] = false; } } t5 split(const vector<int> &in, vector<int> &r1, vector<int> &r2, vector<int> &r3, vector<int> &r4, t5 tx) { t5 ret (false, 0, 0, 0, 0); int n = in.size(); for (int i = 0; i < n; ++i) { const int j = in[i]; x[2 * i] = x1[j]; x[2 * i + 1] = x2[j]; y[2 * i] = y1[j]; y[2 * i + 1] = y2[j]; } nth_element(x, x + n + n, x + n); int xm = x[n] - 1; nth_element(y, y + n + n, y + n); int ym = y[n] - 1; int mid = -1; for (int i = 0; i < n; ++i) { const int j = in[i]; if (removed[j]) continue; if (x1[j] < xm && x2[j] > xm && y1[j] < ym && y2[j] > ym) { if (mid >= 0) { auto rt5 = join(mid, j); ret = join(ret, rt5); tx = join(tx, rt5); continue; } else { mid = j; } } if (x1[j] < xm && y1[j] < ym) { r1.push_back(j); } if (x1[j] < xm && y2[j] > ym) { r2.push_back(j); } if (x2[j] > xm && y1[j] < ym) { r3.push_back(j); } if (x2[j] > xm && y2[j] > ym) { r4.push_back(j); } } if (get<0>(tx)) { if (xm <= get<1>(tx)) { r1.clear(); r2.clear(); } if (xm >= get<2>(tx)) { r3.clear(); r4.clear(); } if (ym <= get<3>(tx)) { r1.clear(); r3.clear(); } if (ym >= get<4>(tx)) { r2.clear(); r3.clear(); } } /* if (!checkChanged(r1)) r1.clear(); if (!checkChanged(r2)) r2.clear(); if (!checkChanged(r3)) r3.clear(); if (!checkChanged(r4)) r4.clear(); */ return ret; } t5 checkAll(const vector<int> &in) { t5 ret (false, 0, 0, 0, 0); const int n = in.size(); for (int i = 0; i < n; ++i) { if (removed[in[i]]) continue; for (int j = i + 1; j < n; ++j) { if (removed[in[j]]) continue; if (overlaps(in[i], in[j])) { auto rtx = join(in[i], in[j]); ret = join(ret, rtx); i = -1; break; } } } return ret; } t5 go(const vector<int> &in) { // printf("go(%d)\n", (int)in.size()); const int n = in.size(); if (n < TR) { return checkAll(in); } t5 ret (false, 0, 0, 0, 0); t5 r(true, -INF, INF, -INF, INF); do { vector<int> v1, v2, v3, v4; r = split(in, v1, v2, v3, v4, r); size_t vs = max(max(v1.size(), v2.size()), max(v3.size(), v4.size())); if (vs == in.size()) { return join(join(ret, r), checkAll(in)); } r = join(r, go(v1)); r = join(r, go(v2)); r = join(r, go(v3)); r = join(r, go(v4)); ret = join(r, ret); } while (get<0>(r)); return ret; } int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; ++i) { scanf("%d%d%d%d", x1 + i, x2 + i, y1 + i, y2 + i); x1[i] *= 2; x2[i] *= 2; y1[i] *= 2; y2[i] *= 2; removed[i] = false; changed[i] = true; v[i] = i; } go(v); vector<t4> ret; for (int i = 0; i < n; ++i) { if (removed[i]) continue; ret.push_back(make_tuple(x1[i]/2, x2[i]/2, y1[i]/2, y2[i]/2)); } sort(ret.begin(), ret.end()); printf("%d\n", (int)ret.size()); for (size_t i = 0; i < ret.size(); ++i) { printf("%d %d %d %d\n", get<0>(ret[i]), get<1>(ret[i]), get<2>(ret[i]), get<3>(ret[i])); } return 0; } } // namespace pie int main() { return pie::main(); }
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include <tuple> using namespace std; const int N = 100002; namespace pie { const int TR = 1; int x1[N], x2[N], y1[N], y2[N]; int x[2*N], y[2*N]; bool removed[N]; bool changed[N]; const int INF = 0x7fffffff; typedef tuple<int,int,int,int> t4; typedef tuple<bool,int,int,int,int> t5; bool overlaps(int a, int b) { if (x2[b] <= x1[a]) return false; if (x1[b] >= x2[a]) return false; if (y2[b] <= y1[a]) return false; if (y1[b] >= y2[a]) return false; return true; } t5 join(const t5 &a, const t5 &b) { if (!get<0>(a)) return b; if (!get<0>(b)) return a; return make_tuple( true, min(get<1>(a), get<1>(b)), max(get<2>(a), get<2>(b)), min(get<3>(a), get<3>(b)), max(get<4>(a), get<4>(b)) ); } t5 join(int a, int b) { x1[a] = min(x1[a], x1[b]); x2[a] = max(x2[a], x2[b]); y1[a] = min(y1[a], y1[b]); y2[a] = max(y2[a], y2[b]); // printf("join(%d, %d)[%d %d][%d %d]\n", a, b, x1[a]/2, y1[a]/2, x2[a]/2, y2[a]/2); removed[b] = true; changed[a] = true; return make_tuple(true, x1[a], x2[a], y1[a], y2[a]); } bool checkChanged(const vector<int> &in) { for (int i : in) { if (changed[i]) return true; } return false; } void clearChanged(const vector<int> &in) { for (int i : in) { changed[i] = false; } } t5 split(const vector<int> &in, vector<int> &r1, vector<int> &r2, vector<int> &r3, vector<int> &r4, t5 tx) { t5 ret (false, 0, 0, 0, 0); int n = in.size(); for (int i = 0; i < n; ++i) { const int j = in[i]; x[2 * i] = x1[j]; x[2 * i + 1] = x2[j]; y[2 * i] = y1[j]; y[2 * i + 1] = y2[j]; } nth_element(x, x + n + n, x + n); int xm = x[n] - 1; nth_element(y, y + n + n, y + n); int ym = y[n] - 1; int mid = -1; for (int i = 0; i < n; ++i) { const int j = in[i]; if (removed[j]) continue; if (x1[j] < xm && x2[j] > xm && y1[j] < ym && y2[j] > ym) { if (mid >= 0) { auto rt5 = join(mid, j); ret = join(ret, rt5); tx = join(tx, rt5); continue; } else { mid = j; } } if (x1[j] < xm && y1[j] < ym) { r1.push_back(j); } if (x1[j] < xm && y2[j] > ym) { r2.push_back(j); } if (x2[j] > xm && y1[j] < ym) { r3.push_back(j); } if (x2[j] > xm && y2[j] > ym) { r4.push_back(j); } } if (get<0>(tx)) { if (xm <= get<1>(tx)) { r1.clear(); r2.clear(); } if (xm >= get<2>(tx)) { r3.clear(); r4.clear(); } if (ym <= get<3>(tx)) { r1.clear(); r3.clear(); } if (ym >= get<4>(tx)) { r2.clear(); r3.clear(); } } /* if (!checkChanged(r1)) r1.clear(); if (!checkChanged(r2)) r2.clear(); if (!checkChanged(r3)) r3.clear(); if (!checkChanged(r4)) r4.clear(); */ return ret; } t5 checkAll(const vector<int> &in) { t5 ret (false, 0, 0, 0, 0); const int n = in.size(); for (int i = 0; i < n; ++i) { if (removed[in[i]]) continue; for (int j = i + 1; j < n; ++j) { if (removed[in[j]]) continue; if (overlaps(in[i], in[j])) { auto rtx = join(in[i], in[j]); ret = join(ret, rtx); i = -1; break; } } } return ret; } t5 go(const vector<int> &in) { // printf("go(%d)\n", (int)in.size()); const int n = in.size(); if (n < TR) { return checkAll(in); } t5 ret (false, 0, 0, 0, 0); t5 r(true, -INF, INF, -INF, INF); do { vector<int> v1, v2, v3, v4; r = split(in, v1, v2, v3, v4, r); size_t vs = max(max(v1.size(), v2.size()), max(v3.size(), v4.size())); if (vs == in.size()) { return join(join(ret, r), checkAll(in)); } r = join(r, go(v1)); r = join(r, go(v2)); r = join(r, go(v3)); r = join(r, go(v4)); ret = join(r, ret); } while (get<0>(r)); return ret; } int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; ++i) { scanf("%d%d%d%d", x1 + i, x2 + i, y1 + i, y2 + i); x1[i] *= 2; x2[i] *= 2; y1[i] *= 2; y2[i] *= 2; removed[i] = false; changed[i] = true; v[i] = i; } go(v); vector<t4> ret; for (int i = 0; i < n; ++i) { if (removed[i]) continue; ret.push_back(make_tuple(x1[i]/2, x2[i]/2, y1[i]/2, y2[i]/2)); } sort(ret.begin(), ret.end()); printf("%d\n", (int)ret.size()); for (size_t i = 0; i < ret.size(); ++i) { printf("%d %d %d %d\n", get<0>(ret[i]), get<1>(ret[i]), get<2>(ret[i]), get<3>(ret[i])); } return 0; } } // namespace pie int main() { return pie::main(); } |