#include <cstdio> #include <vector> #include <set> #include <map> #include <unordered_map> #include <cmath> using namespace std; //int CALLS_GGETMAX=0, CALLS_SSETMAX=0; struct Rect { int left, right, top, bottom; bool operator == (const Rect& that) const { return left==that.left && right==that.right && top==that.top && bottom==that.bottom; } bool operator < (const Rect& that) const { if(left!=that.left) return left<that.left; if(right!=that.right) return right<that.right; if(bottom!=that.bottom) return bottom<that.bottom; return top<that.top; } Rect() { left=-1; } Rect(int l, int r, int b, int t) : left(l), right(r), top(t), bottom(b) {} }; Rect maxRect(const Rect& r1, const Rect& r2) { if(r1.left==-1) return r2; if(r2.left==-1) return r1; Rect sum; sum.left = min(r1.left, r2.left); sum.right = max(r1.right, r2.right); sum.bottom = min(r1.bottom, r2.bottom); sum.top = max(r1.top, r2.top); return sum; } struct HashRect { size_t operator()(const Rect& r) const { return r.left * 4430159 ^ r.right*6271747 ^r.top*7187459 ^ r.bottom*1406707; } }; struct QTree { int size; unordered_map<Rect, Rect, HashRect> values; unordered_map<Rect, Rect, HashRect> maximums; QTree(int s) : size(s) {} void setMax(Rect r, Rect v) { ssetMax(Rect(0, size, 0, size), r, v); } void ssetMax(Rect at, Rect request, Rect value) { //CALLS_SSETMAX++; if(request.left<at.left) request.left = at.left; if(request.right>at.right) request.right = at.right; if(request.bottom<at.bottom) request.bottom = at.bottom; if(request.top>at.top) request.top = at.top; if(request.left<request.right && request.bottom<request.top){}else return; maximums[at] = maxRect(maximums[at], value); if(at==request) { values[at] = maxRect(values[at], value); } else { int midx = at.left + (at.right-at.left)/2; int midy = at.bottom + (at.top-at.bottom)/2; ssetMax(Rect(at.left, midx, at.bottom, midy), request, value); ssetMax(Rect(midx, at.right, at.bottom, midy), request, value); ssetMax(Rect(at.left, midx, midy, at.top), request, value); ssetMax(Rect(midx, at.right, midy, at.top), request, value); } } Rect getMax(Rect r, Rect biggerThan) { return ggetMax(Rect(0, size, 0, size), r, biggerThan); } Rect ggetMax(Rect at, Rect request, Rect biggerThan) { //CALLS_GGETMAX++; if(request.left<at.left) request.left = at.left; if(request.right>at.right) request.right = at.right; if(request.bottom<at.bottom) request.bottom = at.bottom; if(request.top>at.top) request.top = at.top; if(request.left<request.right && request.bottom<request.top){}else return Rect(); if(!maximums.count(at)) return Rect(); if(maxRect(maximums[at], biggerThan)==biggerThan) return Rect(); if(at==request) return maximums[at]; else if(values[at]==maximums[at]) return maximums[at]; else { int midx = at.left + (at.right-at.left)/2; int midy = at.bottom + (at.top-at.bottom)/2; Rect result = values[at]; result = maxRect(result, ggetMax(Rect(at.left, midx, at.bottom, midy), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(midx, at.right, at.bottom, midy), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(at.left, midx, midy, at.top), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(midx, at.right, midy, at.top), request, maxRect(result, biggerThan) ) ); return result; } } }; int main() { int n; scanf("%d", &n); vector<Rect> tribes; set<int> x, y; for(int i=0;i<n;i++) { Rect r; //printf("i=%d\n", i); scanf("%d%d%d%d", &r.left, &r.right, &r.bottom, &r.top); tribes.push_back(r); x.insert(r.left); x.insert(r.right); y.insert(r.bottom); y.insert(r.top); } map<int, int> map_x, map_y; vector<int> reverse_x{0}, reverse_y{0}; for(int xv : x) { map_x[xv] = reverse_x.size(); reverse_x.push_back(xv); } for(int yv : y) { map_y[yv] = reverse_y.size(); reverse_y.push_back(yv); } int TREE_SIZE=8; while(TREE_SIZE<map_x.size() || TREE_SIZE<map_y.size()) TREE_SIZE*=2; QTree t(TREE_SIZE); for(Rect& r : tribes) { Rect base = r; base.left = map_x[base.left]; base.right = map_x[base.right]; base.top = map_y[base.top]; base.bottom = map_y[base.bottom]; while(true) { Rect maks = maxRect(t.getMax(base, base), base); if(maks==base) break; base = maks; } t.setMax(base, base); } set<Rect> after; for(Rect& r : tribes) { Rect base = r; base.left = map_x[base.left]; base.right = map_x[base.right]; base.top = map_y[base.top]; base.bottom = map_y[base.bottom]; after.insert( t.getMax(base, Rect()) ); } printf("%ld\n", after.size()); for(const Rect& r : after) { printf("%d %d %d %d\n",reverse_x[r.left], reverse_x[r.right], reverse_y[r.bottom], reverse_y[r.top]); } //fprintf(stderr, "n=%d: ss=%d, gg=%d | ss/(n*log^2(n))=%f, gg/n=%f\n", n, CALLS_SSETMAX, CALLS_GGETMAX, // (double)CALLS_SSETMAX/n/(log(n)/log(2))/(log(n)/log(2)), (double)CALLS_GGETMAX/n); }
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 | #include <cstdio> #include <vector> #include <set> #include <map> #include <unordered_map> #include <cmath> using namespace std; //int CALLS_GGETMAX=0, CALLS_SSETMAX=0; struct Rect { int left, right, top, bottom; bool operator == (const Rect& that) const { return left==that.left && right==that.right && top==that.top && bottom==that.bottom; } bool operator < (const Rect& that) const { if(left!=that.left) return left<that.left; if(right!=that.right) return right<that.right; if(bottom!=that.bottom) return bottom<that.bottom; return top<that.top; } Rect() { left=-1; } Rect(int l, int r, int b, int t) : left(l), right(r), top(t), bottom(b) {} }; Rect maxRect(const Rect& r1, const Rect& r2) { if(r1.left==-1) return r2; if(r2.left==-1) return r1; Rect sum; sum.left = min(r1.left, r2.left); sum.right = max(r1.right, r2.right); sum.bottom = min(r1.bottom, r2.bottom); sum.top = max(r1.top, r2.top); return sum; } struct HashRect { size_t operator()(const Rect& r) const { return r.left * 4430159 ^ r.right*6271747 ^r.top*7187459 ^ r.bottom*1406707; } }; struct QTree { int size; unordered_map<Rect, Rect, HashRect> values; unordered_map<Rect, Rect, HashRect> maximums; QTree(int s) : size(s) {} void setMax(Rect r, Rect v) { ssetMax(Rect(0, size, 0, size), r, v); } void ssetMax(Rect at, Rect request, Rect value) { //CALLS_SSETMAX++; if(request.left<at.left) request.left = at.left; if(request.right>at.right) request.right = at.right; if(request.bottom<at.bottom) request.bottom = at.bottom; if(request.top>at.top) request.top = at.top; if(request.left<request.right && request.bottom<request.top){}else return; maximums[at] = maxRect(maximums[at], value); if(at==request) { values[at] = maxRect(values[at], value); } else { int midx = at.left + (at.right-at.left)/2; int midy = at.bottom + (at.top-at.bottom)/2; ssetMax(Rect(at.left, midx, at.bottom, midy), request, value); ssetMax(Rect(midx, at.right, at.bottom, midy), request, value); ssetMax(Rect(at.left, midx, midy, at.top), request, value); ssetMax(Rect(midx, at.right, midy, at.top), request, value); } } Rect getMax(Rect r, Rect biggerThan) { return ggetMax(Rect(0, size, 0, size), r, biggerThan); } Rect ggetMax(Rect at, Rect request, Rect biggerThan) { //CALLS_GGETMAX++; if(request.left<at.left) request.left = at.left; if(request.right>at.right) request.right = at.right; if(request.bottom<at.bottom) request.bottom = at.bottom; if(request.top>at.top) request.top = at.top; if(request.left<request.right && request.bottom<request.top){}else return Rect(); if(!maximums.count(at)) return Rect(); if(maxRect(maximums[at], biggerThan)==biggerThan) return Rect(); if(at==request) return maximums[at]; else if(values[at]==maximums[at]) return maximums[at]; else { int midx = at.left + (at.right-at.left)/2; int midy = at.bottom + (at.top-at.bottom)/2; Rect result = values[at]; result = maxRect(result, ggetMax(Rect(at.left, midx, at.bottom, midy), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(midx, at.right, at.bottom, midy), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(at.left, midx, midy, at.top), request, maxRect(result, biggerThan) ) ); result = maxRect(result, ggetMax(Rect(midx, at.right, midy, at.top), request, maxRect(result, biggerThan) ) ); return result; } } }; int main() { int n; scanf("%d", &n); vector<Rect> tribes; set<int> x, y; for(int i=0;i<n;i++) { Rect r; //printf("i=%d\n", i); scanf("%d%d%d%d", &r.left, &r.right, &r.bottom, &r.top); tribes.push_back(r); x.insert(r.left); x.insert(r.right); y.insert(r.bottom); y.insert(r.top); } map<int, int> map_x, map_y; vector<int> reverse_x{0}, reverse_y{0}; for(int xv : x) { map_x[xv] = reverse_x.size(); reverse_x.push_back(xv); } for(int yv : y) { map_y[yv] = reverse_y.size(); reverse_y.push_back(yv); } int TREE_SIZE=8; while(TREE_SIZE<map_x.size() || TREE_SIZE<map_y.size()) TREE_SIZE*=2; QTree t(TREE_SIZE); for(Rect& r : tribes) { Rect base = r; base.left = map_x[base.left]; base.right = map_x[base.right]; base.top = map_y[base.top]; base.bottom = map_y[base.bottom]; while(true) { Rect maks = maxRect(t.getMax(base, base), base); if(maks==base) break; base = maks; } t.setMax(base, base); } set<Rect> after; for(Rect& r : tribes) { Rect base = r; base.left = map_x[base.left]; base.right = map_x[base.right]; base.top = map_y[base.top]; base.bottom = map_y[base.bottom]; after.insert( t.getMax(base, Rect()) ); } printf("%ld\n", after.size()); for(const Rect& r : after) { printf("%d %d %d %d\n",reverse_x[r.left], reverse_x[r.right], reverse_y[r.bottom], reverse_y[r.top]); } //fprintf(stderr, "n=%d: ss=%d, gg=%d | ss/(n*log^2(n))=%f, gg/n=%f\n", n, CALLS_SSETMAX, CALLS_GGETMAX, // (double)CALLS_SSETMAX/n/(log(n)/log(2))/(log(n)/log(2)), (double)CALLS_GGETMAX/n); } |