#include <algorithm> #include <cstdio> #include <map> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,b) for (int i = (b) - 1; i >= (a); --i) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) struct L { int y, i; }; struct T { int x1, x2, why1, y2, i; L bot() const { L l; l.y = why1; l.i = i; return l; } }; bool bottomSort(const T& t1, const T& t2) { return t1.why1 > t2.why1; } bool lexSort(const T& t1, const T& t2) { if (t1.x1 != t2.x1) return t1.x1 < t2.x1; if (t1.x2 != t2.x2) return t1.x2 < t2.x2; if (t1.why1 != t2.why1) return t1.why1 < t2.why1; return t1.y2 < t2.y2; } bool operator<(const L& l1, const L& l2) { if (l1.y != l2.y) return l1.y < l2.y; return l1.i > l2.i; } struct Node { set<L> full, partial; }; int n, x1[100000], x2[100000], why1[100000], y2[100000], bottom; map<int, int> backX, backY; vector<T> t, t2; set<int> existing; Node nodes[1 << 19]; void reshape(int* a, int* b, map<int, int>& back) { map<int, int> m; REP(i,n) m[a[i]] = m[b[i]] = -1; int ii = 0; FORE(it,m) { it->second = ii; back[ii] = it->first; ++ii; } REP(i,n) { a[i] = m[a[i]]; b[i] = m[b[i]]; } } template <class V> void visit(V& visitor, int x, int y, int i, int level) { int xx = (i - (1 << level)) << (bottom - level); int yy = (i - (1 << level) + 1) << (bottom - level); if (x == xx && y == yy) { visitor.final(nodes[i]); return; } int mid = (xx + yy) >> 1; if (x < mid) visit(visitor, x, min(y, mid), i << 1, level + 1); if (mid < y) visit(visitor, max(x, mid), y, (i << 1) | 1, level + 1); visitor.internal(nodes[i]); } template <class V> void visit(V& visitor, int x, int y) { visit(visitor, x, y, 1, 0); } void join(const T& t1, const T& t2, T& t3) { t3.x1 = min(t1.x1, t2.x1); t3.x2 = max(t1.x2, t2.x2); t3.why1 = min(t1.why1, t2.why1); t3.y2 = max(t1.y2, t2.y2); } int findIn(const T& tt, const set<L> s) { set<L>::iterator it = s.begin(); if (it == s.end()) return -1; if (it->y < tt.y2) return it->i; return -1; } struct AddVisitor { const T& tt; AddVisitor(const T& tt) : tt(tt) {} void internal(Node& node) { node.partial.insert(tt.bot()); } void final(Node& node) { node.full.insert(tt.bot()); } }; struct RemoveVisitor { const T& tt; RemoveVisitor(const T& tt) : tt(tt) {} void internal(Node& node) { node.partial.erase(tt.bot()); } void final(Node& node) { node.full.erase(tt.bot()); } }; struct FindVisitor { const T& tt; int found; FindVisitor(const T& tt) : tt(tt), found(-1) {} void internal(Node& node) { if (found >= 0) return; found = findIn(tt, node.full); } void final(Node& node) { if (found >= 0) return; found = findIn(tt, node.full); if (found < 0) found = findIn(tt, node.partial); } }; void add(int i) { for (;;) { FindVisitor fv(t[i]); visit(fv, t[i].x1, t[i].x2); if (fv.found == -1) { AddVisitor av(t[i]); visit(av, t[i].x1, t[i].x2); existing.insert(i); break; } RemoveVisitor rv(t[fv.found]); visit(rv, t[fv.found].x1, t[fv.found].x2); existing.erase(fv.found); T tt; join(t[i], t[fv.found], tt); i = tt.i = t.size(); t.push_back(tt); } } int main() { scanf("%d", &n); REP(i,n) scanf("%d%d%d%d", &x1[i], &x2[i], &why1[i], &y2[i]); reshape(x1, x2, backX); reshape(why1, y2, backY); int xx = 0; t.reserve(n); REP(i,n) { T tt; tt.x1 = x1[i]; tt.x2 = x2[i]; tt.why1 = why1[i]; tt.y2 = y2[i]; t.push_back(tt); xx = max(xx, x2[i]); } sort(t.begin(), t.end(), bottomSort); REP(i,n) t[i].i = i; while ((1 << bottom) < xx) ++bottom; REP(i,n) add(i); int m = existing.size(); t2.reserve(m); FORE(it,existing) t2.push_back(t[*it]); sort(t2.begin(), t2.end(), lexSort); printf("%d\n", m); REP(i,m) printf("%d %d %d %d\n", backX[t2[i].x1], backX[t2[i].x2], backY[t2[i].why1], backY[t2[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 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 | #include <algorithm> #include <cstdio> #include <map> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) #define FORD(i,a,b) for (int i = (b) - 1; i >= (a); --i) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) struct L { int y, i; }; struct T { int x1, x2, why1, y2, i; L bot() const { L l; l.y = why1; l.i = i; return l; } }; bool bottomSort(const T& t1, const T& t2) { return t1.why1 > t2.why1; } bool lexSort(const T& t1, const T& t2) { if (t1.x1 != t2.x1) return t1.x1 < t2.x1; if (t1.x2 != t2.x2) return t1.x2 < t2.x2; if (t1.why1 != t2.why1) return t1.why1 < t2.why1; return t1.y2 < t2.y2; } bool operator<(const L& l1, const L& l2) { if (l1.y != l2.y) return l1.y < l2.y; return l1.i > l2.i; } struct Node { set<L> full, partial; }; int n, x1[100000], x2[100000], why1[100000], y2[100000], bottom; map<int, int> backX, backY; vector<T> t, t2; set<int> existing; Node nodes[1 << 19]; void reshape(int* a, int* b, map<int, int>& back) { map<int, int> m; REP(i,n) m[a[i]] = m[b[i]] = -1; int ii = 0; FORE(it,m) { it->second = ii; back[ii] = it->first; ++ii; } REP(i,n) { a[i] = m[a[i]]; b[i] = m[b[i]]; } } template <class V> void visit(V& visitor, int x, int y, int i, int level) { int xx = (i - (1 << level)) << (bottom - level); int yy = (i - (1 << level) + 1) << (bottom - level); if (x == xx && y == yy) { visitor.final(nodes[i]); return; } int mid = (xx + yy) >> 1; if (x < mid) visit(visitor, x, min(y, mid), i << 1, level + 1); if (mid < y) visit(visitor, max(x, mid), y, (i << 1) | 1, level + 1); visitor.internal(nodes[i]); } template <class V> void visit(V& visitor, int x, int y) { visit(visitor, x, y, 1, 0); } void join(const T& t1, const T& t2, T& t3) { t3.x1 = min(t1.x1, t2.x1); t3.x2 = max(t1.x2, t2.x2); t3.why1 = min(t1.why1, t2.why1); t3.y2 = max(t1.y2, t2.y2); } int findIn(const T& tt, const set<L> s) { set<L>::iterator it = s.begin(); if (it == s.end()) return -1; if (it->y < tt.y2) return it->i; return -1; } struct AddVisitor { const T& tt; AddVisitor(const T& tt) : tt(tt) {} void internal(Node& node) { node.partial.insert(tt.bot()); } void final(Node& node) { node.full.insert(tt.bot()); } }; struct RemoveVisitor { const T& tt; RemoveVisitor(const T& tt) : tt(tt) {} void internal(Node& node) { node.partial.erase(tt.bot()); } void final(Node& node) { node.full.erase(tt.bot()); } }; struct FindVisitor { const T& tt; int found; FindVisitor(const T& tt) : tt(tt), found(-1) {} void internal(Node& node) { if (found >= 0) return; found = findIn(tt, node.full); } void final(Node& node) { if (found >= 0) return; found = findIn(tt, node.full); if (found < 0) found = findIn(tt, node.partial); } }; void add(int i) { for (;;) { FindVisitor fv(t[i]); visit(fv, t[i].x1, t[i].x2); if (fv.found == -1) { AddVisitor av(t[i]); visit(av, t[i].x1, t[i].x2); existing.insert(i); break; } RemoveVisitor rv(t[fv.found]); visit(rv, t[fv.found].x1, t[fv.found].x2); existing.erase(fv.found); T tt; join(t[i], t[fv.found], tt); i = tt.i = t.size(); t.push_back(tt); } } int main() { scanf("%d", &n); REP(i,n) scanf("%d%d%d%d", &x1[i], &x2[i], &why1[i], &y2[i]); reshape(x1, x2, backX); reshape(why1, y2, backY); int xx = 0; t.reserve(n); REP(i,n) { T tt; tt.x1 = x1[i]; tt.x2 = x2[i]; tt.why1 = why1[i]; tt.y2 = y2[i]; t.push_back(tt); xx = max(xx, x2[i]); } sort(t.begin(), t.end(), bottomSort); REP(i,n) t[i].i = i; while ((1 << bottom) < xx) ++bottom; REP(i,n) add(i); int m = existing.size(); t2.reserve(m); FORE(it,existing) t2.push_back(t[*it]); sort(t2.begin(), t2.end(), lexSort); printf("%d\n", m); REP(i,m) printf("%d %d %d %d\n", backX[t2[i].x1], backX[t2[i].x2], backY[t2[i].why1], backY[t2[i].y2]); } |