#include <cstdio> #include <algorithm> #include <unordered_map> #include <vector> #include <array> using namespace std; int base = 1; class SegTree { unordered_map<int, pair<int, int>> tree; int pa, pb, pk; SegTree *pc1, *pc2; int _insert(int v, int a, int b) { if(b <= pa || a >= pb) return tree.count(v) ? tree[v].first : 0; auto &tr = tree[v]; tr.first = pk; if(a >= pa && b <= pb) tr.second = pk; else { tr.first = max(tr.first, tr.second); tr.first = max(tr.first, _insert(v * 2, a, (a + b) / 2)); tr.first = max(tr.first, _insert(v * 2 + 1, (a + b) / 2, b)); } return tr.first; } int _fix(int v, int a, int b) { if(b <= pa || a >= pb) return tree.count(v) ? tree[v].first : 0; auto &tr = tree[v], tr1 = pc1->tree.count(v) ? pc1->tree[v] : make_pair(0, 0), tr2 = pc2->tree.count(v) ? pc2->tree[v] : make_pair(0, 0); if(tr1.first == 0 && tr2.first == 0) return tr.first; tr.first = max(tr.first, max(tr1.first, tr2.first)); tr.second = max(tr.second, max(tr1.second, tr2.second)); if(a < pa || b > pb) { tr.first = max(tr.first, _fix(v * 2, a, (a + b) / 2)); tr.first = max(tr.first, _fix(v * 2 + 1, (a + b) / 2, b)); } return tr.first; } int _findMax(int v, int a, int b) { if(b <= pa || a >= pb) return 0; auto tr = tree[v]; if(a >= pa && b <= pb) return tr.first; return max(tr.second, max(_findMax(v * 2, a, (a + b) / 2), _findMax(v * 2 + 1, (a + b) / 2, b))); } public: void insert(int a, int b, int k) { pa = a; pb = b; pk = k; _insert(1, 0, base); } void fix(int a, int b, SegTree *child1, SegTree *child2) { pa = a; pb = b; pc1 = child1; pc2 = child2; _fix(1, 0, base); } int findMax(int a, int b) { pa = a; pb = b; return _findMax(1, 0, base); } }; class SegTree2D { vector<SegTree> tree1, tree2; int px1, px2, py1, py2, pk; void insert(int v, int a, int b) { if(b <= px1 || a >= px2) return; tree1[v].insert(py1, py2, pk); if(a >= px1 && b <= px2) tree2[v].insert(py1, py2, pk); else { insert(v * 2, a, (a + b) / 2); insert(v * 2 + 1, (a + b) / 2, b); tree1[v].fix(py1, py2, &tree1[v * 2], &tree1[v * 2 + 1]); } } int findMax(int v, int a, int b) { if(b <= px1 || a >= px2) return 0; if(a >= px1 && b <= px2) return tree1[v].findMax(py1, py2); int ans = tree2[v].findMax(py1, py2); return max(ans, max(findMax(v * 2, a, (a + b) / 2), findMax(v * 2 + 1, (a + b) / 2, b))); } public: SegTree2D() : tree1(base * 2), tree2(base * 2) { } void insert(int x1, int y1, int x2, int y2, int k) { px1 = x1; px2 = x2; py1 = y1; py2 = y2; pk = k; insert(1, 0, base); } int findMax(int x1, int y1, int x2, int y2) { px1 = x1; px2 = x2; py1 = y1; py2 = y2; return findMax(1, 0, base); } }; const int N = 100001; int X1[N], Y1[N], X2[N], Y2[N]; int main() { int n; scanf("%d", &n); vector<int> xp, yp; for(int i = 1; i <= n; i++) { scanf("%d %d %d %d", X1 + i, X2 + i, Y1 + i, Y2 + i); xp.push_back(X1[i]); xp.push_back(X2[i]); yp.push_back(Y1[i]); yp.push_back(Y2[i]); } sort(xp.begin(), xp.end()); sort(yp.begin(), yp.end()); for(int i = 1; i <= n; i++) { X1[i] = lower_bound(xp.begin(), xp.end(), X1[i]) - xp.begin(); X2[i] = lower_bound(xp.begin(), xp.end(), X2[i]) - xp.begin(); Y1[i] = lower_bound(yp.begin(), yp.end(), Y1[i]) - yp.begin(); Y2[i] = lower_bound(yp.begin(), yp.end(), Y2[i]) - yp.begin(); while(base < X2[i] || base < Y2[i]) base *= 2; } SegTree2D tree; for(int i = 1; i <= n; i++) { while(true) { int k = tree.findMax(X1[i], Y1[i], X2[i], Y2[i]); if(k == 0) break; X1[i] = min(X1[i], X1[k]); Y1[i] = min(Y1[i], Y1[k]); X2[i] = max(X2[i], X2[k]); Y2[i] = max(Y2[i], Y2[k]); tree.insert(X1[k], Y1[k], X2[k], Y2[k], 0); X1[k] = -1; } tree.insert(X1[i], Y1[i], X2[i], Y2[i], i); } vector<int> ans; for(int i = 1; i <= n; i++) if(X1[i] != -1) ans.push_back(i); sort(ans.begin(), ans.end(), [](int a, int b) { return make_tuple(X1[a], X2[a], Y1[a], Y2[a]) < make_tuple(X1[b], X2[b], Y1[b], Y2[b]); }); printf("%d\n", ans.size()); for(auto x: ans) printf("%d %d %d %d\n", xp[X1[x]], xp[X2[x]], yp[Y1[x]], yp[Y2[x]]); }
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 | #include <cstdio> #include <algorithm> #include <unordered_map> #include <vector> #include <array> using namespace std; int base = 1; class SegTree { unordered_map<int, pair<int, int>> tree; int pa, pb, pk; SegTree *pc1, *pc2; int _insert(int v, int a, int b) { if(b <= pa || a >= pb) return tree.count(v) ? tree[v].first : 0; auto &tr = tree[v]; tr.first = pk; if(a >= pa && b <= pb) tr.second = pk; else { tr.first = max(tr.first, tr.second); tr.first = max(tr.first, _insert(v * 2, a, (a + b) / 2)); tr.first = max(tr.first, _insert(v * 2 + 1, (a + b) / 2, b)); } return tr.first; } int _fix(int v, int a, int b) { if(b <= pa || a >= pb) return tree.count(v) ? tree[v].first : 0; auto &tr = tree[v], tr1 = pc1->tree.count(v) ? pc1->tree[v] : make_pair(0, 0), tr2 = pc2->tree.count(v) ? pc2->tree[v] : make_pair(0, 0); if(tr1.first == 0 && tr2.first == 0) return tr.first; tr.first = max(tr.first, max(tr1.first, tr2.first)); tr.second = max(tr.second, max(tr1.second, tr2.second)); if(a < pa || b > pb) { tr.first = max(tr.first, _fix(v * 2, a, (a + b) / 2)); tr.first = max(tr.first, _fix(v * 2 + 1, (a + b) / 2, b)); } return tr.first; } int _findMax(int v, int a, int b) { if(b <= pa || a >= pb) return 0; auto tr = tree[v]; if(a >= pa && b <= pb) return tr.first; return max(tr.second, max(_findMax(v * 2, a, (a + b) / 2), _findMax(v * 2 + 1, (a + b) / 2, b))); } public: void insert(int a, int b, int k) { pa = a; pb = b; pk = k; _insert(1, 0, base); } void fix(int a, int b, SegTree *child1, SegTree *child2) { pa = a; pb = b; pc1 = child1; pc2 = child2; _fix(1, 0, base); } int findMax(int a, int b) { pa = a; pb = b; return _findMax(1, 0, base); } }; class SegTree2D { vector<SegTree> tree1, tree2; int px1, px2, py1, py2, pk; void insert(int v, int a, int b) { if(b <= px1 || a >= px2) return; tree1[v].insert(py1, py2, pk); if(a >= px1 && b <= px2) tree2[v].insert(py1, py2, pk); else { insert(v * 2, a, (a + b) / 2); insert(v * 2 + 1, (a + b) / 2, b); tree1[v].fix(py1, py2, &tree1[v * 2], &tree1[v * 2 + 1]); } } int findMax(int v, int a, int b) { if(b <= px1 || a >= px2) return 0; if(a >= px1 && b <= px2) return tree1[v].findMax(py1, py2); int ans = tree2[v].findMax(py1, py2); return max(ans, max(findMax(v * 2, a, (a + b) / 2), findMax(v * 2 + 1, (a + b) / 2, b))); } public: SegTree2D() : tree1(base * 2), tree2(base * 2) { } void insert(int x1, int y1, int x2, int y2, int k) { px1 = x1; px2 = x2; py1 = y1; py2 = y2; pk = k; insert(1, 0, base); } int findMax(int x1, int y1, int x2, int y2) { px1 = x1; px2 = x2; py1 = y1; py2 = y2; return findMax(1, 0, base); } }; const int N = 100001; int X1[N], Y1[N], X2[N], Y2[N]; int main() { int n; scanf("%d", &n); vector<int> xp, yp; for(int i = 1; i <= n; i++) { scanf("%d %d %d %d", X1 + i, X2 + i, Y1 + i, Y2 + i); xp.push_back(X1[i]); xp.push_back(X2[i]); yp.push_back(Y1[i]); yp.push_back(Y2[i]); } sort(xp.begin(), xp.end()); sort(yp.begin(), yp.end()); for(int i = 1; i <= n; i++) { X1[i] = lower_bound(xp.begin(), xp.end(), X1[i]) - xp.begin(); X2[i] = lower_bound(xp.begin(), xp.end(), X2[i]) - xp.begin(); Y1[i] = lower_bound(yp.begin(), yp.end(), Y1[i]) - yp.begin(); Y2[i] = lower_bound(yp.begin(), yp.end(), Y2[i]) - yp.begin(); while(base < X2[i] || base < Y2[i]) base *= 2; } SegTree2D tree; for(int i = 1; i <= n; i++) { while(true) { int k = tree.findMax(X1[i], Y1[i], X2[i], Y2[i]); if(k == 0) break; X1[i] = min(X1[i], X1[k]); Y1[i] = min(Y1[i], Y1[k]); X2[i] = max(X2[i], X2[k]); Y2[i] = max(Y2[i], Y2[k]); tree.insert(X1[k], Y1[k], X2[k], Y2[k], 0); X1[k] = -1; } tree.insert(X1[i], Y1[i], X2[i], Y2[i], i); } vector<int> ans; for(int i = 1; i <= n; i++) if(X1[i] != -1) ans.push_back(i); sort(ans.begin(), ans.end(), [](int a, int b) { return make_tuple(X1[a], X2[a], Y1[a], Y2[a]) < make_tuple(X1[b], X2[b], Y1[b], Y2[b]); }); printf("%d\n", ans.size()); for(auto x: ans) printf("%d %d %d %d\n", xp[X1[x]], xp[X2[x]], yp[Y1[x]], yp[Y2[x]]); } |