#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #ifdef LOCAL #define debug(x) { cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; void mini(int& a4, int b4){a4=min(a4, b4); } void maxi(int& a4, int b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} const int N = 1e6 + 5; struct Rect { int coor[2][2]; }; ostream& operator<<(ostream& out, Rect& r) { REP (i, 2) { REP (j, 2) { out << r.coor[i][j] + j << " "; } } return out; } bool cmp_rects(Rect a, Rect b) { REP (i, 2) { REP (j, 2) { if (a.coor[i][j] != b.coor[i][j]) { return a.coor[i][j] < b.coor[i][j]; } } } return 0; } struct Comp { Rect bb; set<PII> nearest[2][2]; }; Comp comp[N]; bool if_intersect_on_axis(int a, int b, int axis) { return !(comp[b].bb.coor[axis][1] < comp[a].bb.coor[axis][0] || comp[a].bb.coor[axis][1] < comp[b].bb.coor[axis][0]); } int starting_rec[N]; struct Interval { int hei, left, right, which_rect; Interval(int hei_, int left_, int right_, int w_) : hei(hei_), left(left_), right(right_), which_rect(w_) {} }; bool cmp(const Interval& a, const Interval& b) { return a.hei < b.hei; } Rect BB(Rect& a, Rect& b) { Rect new_r(a); REP (j, 2) { mini(new_r.coor[j][0], b.coor[j][0]); maxi(new_r.coor[j][1], b.coor[j][1]); } return new_r; } int rep[N]; int Find(int v) { if (rep[v] == v) { return v; } rep[v] = Find(rep[v]); return rep[v]; } bool Union(int, int); int cnt; void TryEnlarging(int a) { /* cnt++; if (cnt % 100000 == 0) { cerr<<cnt<<endl; } */ //cerr<<"TryEnlarging "<<a<<endl; REP (j, 2) { while (!comp[a].nearest[j][0].empty()) { PII nei = *comp[a].nearest[j][0].begin(); // tu wrzucamy z minusami ;__; int rect_num = nei.second; //assert(comp[rect_num].bb.coor[j][1] == -nei.first); if (comp[rect_num].bb.coor[j][1] < comp[a].bb.coor[j][0]) { break; } comp[a].nearest[j][0].erase(comp[a].nearest[j][0].begin()); if (Union(a, rect_num)) { return; } } // Copy-paste ;__; - ale napisac bez tego to dosc ciezko while (!comp[a].nearest[j][1].empty()) { PII nei = *comp[a].nearest[j][1].begin(); int rect_num = nei.second; //assert(comp[rect_num].bb.coor[j][0] == nei.first); if (comp[rect_num].bb.coor[j][0] > comp[a].bb.coor[j][1]) { break; } comp[a].nearest[j][1].erase(comp[a].nearest[j][1].begin()); if (Union(a, rect_num)) { return; } } } } void TryUnion(int a, int b) { //cerr<<"TryUnion "<<a<<" "<<b<<endl; a = Find(a); b = Find(b); if (a == b) { return; } REP (axis, 2) { if (!if_intersect_on_axis(a, b, 1 - axis)) { continue; } //assert(if_intersect_on_axis(v, nei, axis)); if (comp[b].bb.coor[axis][0] > comp[a].bb.coor[axis][1]) { comp[a].nearest[axis][1].insert(MP(comp[b].bb.coor[axis][0], b)); comp[b].nearest[axis][0].insert(MP(-comp[a].bb.coor[axis][1], a)); } else { comp[a].nearest[axis][0].insert(MP(-comp[b].bb.coor[axis][1], b)); // ale paskudztwo ten minus :( comp[b].nearest[axis][1].insert(MP(comp[a].bb.coor[axis][0], a)); } } TryEnlarging(a); TryEnlarging(b); } bool Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) { return false; } assert(if_intersect_on_axis(a, b, 0) && if_intersect_on_axis(a, b, 1)); //cerr<<"Unionuje "<<a<<" "<<b<<endl; //cerr<<"a "<<comp[a].bb<<endl; //cerr<<"b "<<comp[b].bb<<endl; int vs[] = {a, b}; int sizes[] = {0, 0}; REP (i, 2) { REP (j, 2) { REP (k, 2) { sizes[i] += comp[vs[i]].nearest[j][k].size(); } } } if (sizes[0] < sizes[1]) { swap(a, b); } rep[b] = a; REP (i, 2) { REP (j, 2) { for (auto p : comp[b].nearest[i][j]) { comp[a].nearest[i][j].insert(p); } comp[b].nearest[i][j].clear(); } } comp[a].bb = BB(comp[a].bb, comp[b].bb); TryEnlarging(a); return true; } int saw[N]; int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); make(int, n); RE (i, n) { REP (j, 2) { REP (k, 2) { cin>>comp[i].bb.coor[j][k]; } comp[i].bb.coor[j][1]--; } rep[i] = i; } REP (axis, 2) { // // cerr<<"Widoczne w "; // if (axis == 0) { // // cerr<<"pionie:"<<endl; // } else { // // cerr<<"poziomie:"<<endl; // } vector<Interval> ints; RE (i, n) { ints.PB(Interval(comp[i].bb.coor[1 - axis][0], comp[i].bb.coor[axis][0], comp[i].bb.coor[axis][1], i)); ints.PB(Interval(comp[i].bb.coor[1 - axis][1] + 1, comp[i].bb.coor[axis][0], comp[i].bb.coor[axis][1], i)); } sort(ALL(ints), cmp); set<int> starts; REP (i, N) { starting_rec[i] = 0; } for (auto inter : ints) { auto it = starts.lower_bound(inter.left); if (it != starts.begin()) { it--; } // cerr<<"Bede dodawac "<<inter.left<<" "<<inter.right<<" "<<inter.which_rect<<endl; vector<int> to_erase; int add_right = -1; for (; it != starts.end() && *it <= inter.right; it++) { int visible = starting_rec[*it]; if (comp[visible].bb.coor[axis][1] < inter.left) { continue; } TryUnion(visible, inter.which_rect); // cerr<<visible<<" i "<<inter.which_rect<<endl; if (*it >= inter.left) { to_erase.PB(*it); } if (comp[visible].bb.coor[axis][1] > inter.right) { add_right = visible; } } for (int a : to_erase) { starts.erase(a); } if (add_right != -1) { starts.insert(inter.right + 1); starting_rec[inter.right + 1] = add_right; } starts.insert(inter.left); starting_rec[inter.left] = inter.which_rect; //for (auto it2 = starts.begin(); it2 != starts.end(); it2++) { // cerr<<"("<<*it2<<", "<<starting_rec[*it2]<<"), "; //} // cerr<<endl; } } vector<Rect> res; RE (i, n) { int root = Find(i); if (saw[root]) { continue; } saw[root] = true; res.PB(comp[root].bb); } sort(ALL(res), cmp_rects); cout<<res.size()<<endl; for (Rect& r : res) { cout<<r<<"\n"; } return 0; }
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #ifdef LOCAL #define debug(x) { cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; void mini(int& a4, int b4){a4=min(a4, b4); } void maxi(int& a4, int b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} const int N = 1e6 + 5; struct Rect { int coor[2][2]; }; ostream& operator<<(ostream& out, Rect& r) { REP (i, 2) { REP (j, 2) { out << r.coor[i][j] + j << " "; } } return out; } bool cmp_rects(Rect a, Rect b) { REP (i, 2) { REP (j, 2) { if (a.coor[i][j] != b.coor[i][j]) { return a.coor[i][j] < b.coor[i][j]; } } } return 0; } struct Comp { Rect bb; set<PII> nearest[2][2]; }; Comp comp[N]; bool if_intersect_on_axis(int a, int b, int axis) { return !(comp[b].bb.coor[axis][1] < comp[a].bb.coor[axis][0] || comp[a].bb.coor[axis][1] < comp[b].bb.coor[axis][0]); } int starting_rec[N]; struct Interval { int hei, left, right, which_rect; Interval(int hei_, int left_, int right_, int w_) : hei(hei_), left(left_), right(right_), which_rect(w_) {} }; bool cmp(const Interval& a, const Interval& b) { return a.hei < b.hei; } Rect BB(Rect& a, Rect& b) { Rect new_r(a); REP (j, 2) { mini(new_r.coor[j][0], b.coor[j][0]); maxi(new_r.coor[j][1], b.coor[j][1]); } return new_r; } int rep[N]; int Find(int v) { if (rep[v] == v) { return v; } rep[v] = Find(rep[v]); return rep[v]; } bool Union(int, int); int cnt; void TryEnlarging(int a) { /* cnt++; if (cnt % 100000 == 0) { cerr<<cnt<<endl; } */ //cerr<<"TryEnlarging "<<a<<endl; REP (j, 2) { while (!comp[a].nearest[j][0].empty()) { PII nei = *comp[a].nearest[j][0].begin(); // tu wrzucamy z minusami ;__; int rect_num = nei.second; //assert(comp[rect_num].bb.coor[j][1] == -nei.first); if (comp[rect_num].bb.coor[j][1] < comp[a].bb.coor[j][0]) { break; } comp[a].nearest[j][0].erase(comp[a].nearest[j][0].begin()); if (Union(a, rect_num)) { return; } } // Copy-paste ;__; - ale napisac bez tego to dosc ciezko while (!comp[a].nearest[j][1].empty()) { PII nei = *comp[a].nearest[j][1].begin(); int rect_num = nei.second; //assert(comp[rect_num].bb.coor[j][0] == nei.first); if (comp[rect_num].bb.coor[j][0] > comp[a].bb.coor[j][1]) { break; } comp[a].nearest[j][1].erase(comp[a].nearest[j][1].begin()); if (Union(a, rect_num)) { return; } } } } void TryUnion(int a, int b) { //cerr<<"TryUnion "<<a<<" "<<b<<endl; a = Find(a); b = Find(b); if (a == b) { return; } REP (axis, 2) { if (!if_intersect_on_axis(a, b, 1 - axis)) { continue; } //assert(if_intersect_on_axis(v, nei, axis)); if (comp[b].bb.coor[axis][0] > comp[a].bb.coor[axis][1]) { comp[a].nearest[axis][1].insert(MP(comp[b].bb.coor[axis][0], b)); comp[b].nearest[axis][0].insert(MP(-comp[a].bb.coor[axis][1], a)); } else { comp[a].nearest[axis][0].insert(MP(-comp[b].bb.coor[axis][1], b)); // ale paskudztwo ten minus :( comp[b].nearest[axis][1].insert(MP(comp[a].bb.coor[axis][0], a)); } } TryEnlarging(a); TryEnlarging(b); } bool Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) { return false; } assert(if_intersect_on_axis(a, b, 0) && if_intersect_on_axis(a, b, 1)); //cerr<<"Unionuje "<<a<<" "<<b<<endl; //cerr<<"a "<<comp[a].bb<<endl; //cerr<<"b "<<comp[b].bb<<endl; int vs[] = {a, b}; int sizes[] = {0, 0}; REP (i, 2) { REP (j, 2) { REP (k, 2) { sizes[i] += comp[vs[i]].nearest[j][k].size(); } } } if (sizes[0] < sizes[1]) { swap(a, b); } rep[b] = a; REP (i, 2) { REP (j, 2) { for (auto p : comp[b].nearest[i][j]) { comp[a].nearest[i][j].insert(p); } comp[b].nearest[i][j].clear(); } } comp[a].bb = BB(comp[a].bb, comp[b].bb); TryEnlarging(a); return true; } int saw[N]; int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); make(int, n); RE (i, n) { REP (j, 2) { REP (k, 2) { cin>>comp[i].bb.coor[j][k]; } comp[i].bb.coor[j][1]--; } rep[i] = i; } REP (axis, 2) { // // cerr<<"Widoczne w "; // if (axis == 0) { // // cerr<<"pionie:"<<endl; // } else { // // cerr<<"poziomie:"<<endl; // } vector<Interval> ints; RE (i, n) { ints.PB(Interval(comp[i].bb.coor[1 - axis][0], comp[i].bb.coor[axis][0], comp[i].bb.coor[axis][1], i)); ints.PB(Interval(comp[i].bb.coor[1 - axis][1] + 1, comp[i].bb.coor[axis][0], comp[i].bb.coor[axis][1], i)); } sort(ALL(ints), cmp); set<int> starts; REP (i, N) { starting_rec[i] = 0; } for (auto inter : ints) { auto it = starts.lower_bound(inter.left); if (it != starts.begin()) { it--; } // cerr<<"Bede dodawac "<<inter.left<<" "<<inter.right<<" "<<inter.which_rect<<endl; vector<int> to_erase; int add_right = -1; for (; it != starts.end() && *it <= inter.right; it++) { int visible = starting_rec[*it]; if (comp[visible].bb.coor[axis][1] < inter.left) { continue; } TryUnion(visible, inter.which_rect); // cerr<<visible<<" i "<<inter.which_rect<<endl; if (*it >= inter.left) { to_erase.PB(*it); } if (comp[visible].bb.coor[axis][1] > inter.right) { add_right = visible; } } for (int a : to_erase) { starts.erase(a); } if (add_right != -1) { starts.insert(inter.right + 1); starting_rec[inter.right + 1] = add_right; } starts.insert(inter.left); starting_rec[inter.left] = inter.which_rect; //for (auto it2 = starts.begin(); it2 != starts.end(); it2++) { // cerr<<"("<<*it2<<", "<<starting_rec[*it2]<<"), "; //} // cerr<<endl; } } vector<Rect> res; RE (i, n) { int root = Find(i); if (saw[root]) { continue; } saw[root] = true; res.PB(comp[root].bb); } sort(ALL(res), cmp_rects); cout<<res.size()<<endl; for (Rect& r : res) { cout<<r<<"\n"; } return 0; } |