#include<cstdio> #include<algorithm> #include<cassert> #include<complex> #include<map> #include<iomanip> #include<sstream> #include<queue> #include<set> #include<string> #include<vector> #include<iostream> #include<cstring> #include<stack> #include<list> #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define fup FOR #define fdo FORD #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 siz SZ #define CLR(x) memset((x), 0, sizeof(x)) #define PB push_back #define MP make_pair #define X first #define Y second #define FI X #define SE Y #define SQR(a) ((a)*(a)) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }} using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int>P; typedef vector<int>VI; const int INF=1E9+7; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } #define DBG(a) a struct R {int x1; int y1; int x2; int y2;bool removed; R():x1(0),y1(0),x2(0),y2(0),removed(false){} friend bool operator<(const R& a, const R& b){ if (a.removed && !b.removed) return false; if (!a.removed && b.removed) return true; if (a.x1>b.x1) return false; if (a.x1<b.x1) return true; if (a.x2>b.x2) return false; if (a.x2<b.x2) return true; if (a.y1>b.y1) return false; if (a.y1<b.y1) return true; if (a.y2>b.y2) return false; if (a.y2<b.y2) return true; return true; } }; ostream& operator<<(ostream& o, const R& r) { o<< r.x1 << ' ' << r.x2 << ' ' << r.y1 << ' ' << r.y2; return o; } const int MAX_R = 102*1000; vector<R> r; typedef pair<pair<int,int>, int> XYI; struct QT { R mybounds; QT* subs[4]; vector<XYI> v; void init() {mybounds.x1=-1; mybounds.x2=1000*1010; mybounds.y1=-1; mybounds.y2=1000*1010; subs[0]=subs[1]=subs[2]=subs[3]=0; } bool inside(int x, int y) { return (x>=mybounds.x1 && x<mybounds.x2 && y>=mybounds.y1 && y<mybounds.y2); } bool insert(int x, int y, int i) { if (!inside(x,y)) return false; if (v.size()<16) {v.PB(MP(MP(x,y),i)); return true;} if (subs[0]==NULL) divide(); FOR(i,0,3) if (subs[i]->insert(x,y,i)); return true; return false; } bool strict_within(int x, int y, const R& a) { return (x>a.x1 && x<a.x2 && y>a.y1 && y<a.y2); } template <typename T> void gather(const R& a, T& res) { if (!cross(a, mybounds)) return; FORE(i, v) { int x = i->FI.FI; int y = i->FI.SE; if (strict_within(x,y,a)) res.PB(i->SE); } if (subs[0]) FOR(i,0,3) { subs[i]->gather(a, res); } } void divide() { FOR(i,0,3) subs[i]=new QT; R a; int x = (mybounds.x2-mybounds.x1)/2+mybounds.x1; int y = (mybounds.y2-mybounds.y1)/2+mybounds.y1; a.x1=mybounds.x1; a.x2=x; a.y1=mybounds.y1; a.y2=y; subs[0]->mybounds=a; subs[0]->subs[0]=NULL; a.x1=mybounds.x1; a.x2=x; a.y1=y; a.y2=mybounds.y2; subs[1]->mybounds=a; subs[1]->subs[0]=NULL; a.x1=x; a.x2=mybounds.x2; a.y1=y; a.y2=mybounds.y2; subs[2]->mybounds=a; subs[2]->subs[0]=NULL; a.x1=x; a.x2=mybounds.x2; a.y1=mybounds.y1; a.y2=y; subs[3]->mybounds=a; subs[3]->subs[0]=NULL; } bool cross(int a1, int a2, int b1, int b2) { // czy [a1 .. a2] i [b1..b2] maja czesc wspolna? if (a2<=b1 || a1 >= b2) return false; else return true; } bool cross(const R& a, const R& b) { return (cross(a.x1,a.x2, b.x1, b.x2) && cross(a.y1,a.y2, b.y1, b.y2)); } list<int> fallinto(const R& a) { list<int> l; // cerr<<"|"; FORE(i,r) if (!i->removed && cross(a,*i)) { // cerr<<i-r.begin()<<'|'; l.PB(i-r.begin()); } return l; } void add(int index) { insert(r[index].x1,r[index].y1, index); insert(r[index].x2,r[index].y1, index); insert(r[index].x1,r[index].y2, index); insert(r[index].x2,r[index].y2, index); } void remove(int index) { } }; struct RT { list<int> p; void remove(int index) { FORE(i, p) if (*i==index) { p.erase(i); return; }; } list<int> iscrossing(int index){ int iy1 = r[index].y1; int iy2 = r[index].y2; list<int> res; FORE(i, p) { int y1=r[*i].y1; int y2=r[*i].y2; // czy [iy1 .. iy2] i [y1..y2] maja czesc wspolna? if (iy2<=y1 || iy1 >= y2) ; else res.PB(*i); } return res; } void add(int index) { p.PB(index); } }; QT qt; RT rt; set<P> ixy; void remove_r(int index) { // usun [index] ze wszystkich struktur: r, rt, ixy, QT; rt.remove(index); r[index].removed = true; // ixy nie trzeba usuwac qt.remove(index); } void add_r(const R& ar) { int index = SZ(r); r.PB(ar); rt.add(index); ixy.insert(MP(ar.x2, index)); qt.add(index); } bool isclosing(R& r, int x) { return (r.x2 == x); } void grow(R& a, const R& b) { mini(a.x1,b.x1); mini(a.y1,b.y1); maxi(a.x2,b.x2); maxi(a.y2,b.y2); } template <typename A> R deal_with_intersection(int index, A& li) { #if 0 cerr<<"("<<r[index].y1 <<"," <<r[index].y2<<"):"; FORE(j,li) cerr<<'['<<r[*j].y1<<',' <<r[*j].y2<<']'; #endif R super = r[index]; remove_r(index); FORE(j,li) { grow(super, r[*j]); remove_r(*j); } DBG(cerr<<"<-["<<super<<']';) VAR(ki, qt.fallinto(super)); // DBG(cerr<<'+'<<SZ(ki)<<'+';) while(SZ(ki)>0) { FORE(k,ki) { grow(super, r[*k]); remove_r(*k); } ki = qt.fallinto(super); } DBG(cerr<<"<-["<<super<<']';) return super; } void scan() { FORE(i,ixy) { DBG(cerr<<":";) int x = i->FI; int index = i->SE; if (isclosing(r[index], x)) rt.remove(index); else { if (!r[index].removed) { DBG(cerr<<"open{"<<x<<"}";) VAR(li, rt.iscrossing(index)); rt.add(index); if (!li.empty()) { // found intersection R ar = deal_with_intersection(index, li); add_r(ar); } } } DBG(cerr<<endl;) } } int main(){ ios_base::sync_with_stdio(false); cout << setprecision(15) << fixed; int n; cin >> n; r.reserve(n*2+7); //ixy.reserve(n*2+7); FOR(i,1,n) { R a; cin >> a.x1 >> a.x2 >> a.y1 >> a.y2; a.x1=a.x1*2+1; a.x2=a.x2*2; ixy.insert(MP(a.x1,SZ(r))); ixy.insert(MP(a.x2,SZ(r))); r.PB(a); } // qt.construct(); qt.init(); scan(); int count=0; FORE(i,r) { i->x1 /= 2; i->x2 /= 2; if (!i->removed) count++;} sort(ALL(r)); cout<<count<<endl; FORE(i,r) if (!i->removed) cout<<*i<<endl; 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 292 293 294 295 296 297 | #include<cstdio> #include<algorithm> #include<cassert> #include<complex> #include<map> #include<iomanip> #include<sstream> #include<queue> #include<set> #include<string> #include<vector> #include<iostream> #include<cstring> #include<stack> #include<list> #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define fup FOR #define fdo FORD #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 siz SZ #define CLR(x) memset((x), 0, sizeof(x)) #define PB push_back #define MP make_pair #define X first #define Y second #define FI X #define SE Y #define SQR(a) ((a)*(a)) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }} using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int>P; typedef vector<int>VI; const int INF=1E9+7; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } #define DBG(a) a struct R {int x1; int y1; int x2; int y2;bool removed; R():x1(0),y1(0),x2(0),y2(0),removed(false){} friend bool operator<(const R& a, const R& b){ if (a.removed && !b.removed) return false; if (!a.removed && b.removed) return true; if (a.x1>b.x1) return false; if (a.x1<b.x1) return true; if (a.x2>b.x2) return false; if (a.x2<b.x2) return true; if (a.y1>b.y1) return false; if (a.y1<b.y1) return true; if (a.y2>b.y2) return false; if (a.y2<b.y2) return true; return true; } }; ostream& operator<<(ostream& o, const R& r) { o<< r.x1 << ' ' << r.x2 << ' ' << r.y1 << ' ' << r.y2; return o; } const int MAX_R = 102*1000; vector<R> r; typedef pair<pair<int,int>, int> XYI; struct QT { R mybounds; QT* subs[4]; vector<XYI> v; void init() {mybounds.x1=-1; mybounds.x2=1000*1010; mybounds.y1=-1; mybounds.y2=1000*1010; subs[0]=subs[1]=subs[2]=subs[3]=0; } bool inside(int x, int y) { return (x>=mybounds.x1 && x<mybounds.x2 && y>=mybounds.y1 && y<mybounds.y2); } bool insert(int x, int y, int i) { if (!inside(x,y)) return false; if (v.size()<16) {v.PB(MP(MP(x,y),i)); return true;} if (subs[0]==NULL) divide(); FOR(i,0,3) if (subs[i]->insert(x,y,i)); return true; return false; } bool strict_within(int x, int y, const R& a) { return (x>a.x1 && x<a.x2 && y>a.y1 && y<a.y2); } template <typename T> void gather(const R& a, T& res) { if (!cross(a, mybounds)) return; FORE(i, v) { int x = i->FI.FI; int y = i->FI.SE; if (strict_within(x,y,a)) res.PB(i->SE); } if (subs[0]) FOR(i,0,3) { subs[i]->gather(a, res); } } void divide() { FOR(i,0,3) subs[i]=new QT; R a; int x = (mybounds.x2-mybounds.x1)/2+mybounds.x1; int y = (mybounds.y2-mybounds.y1)/2+mybounds.y1; a.x1=mybounds.x1; a.x2=x; a.y1=mybounds.y1; a.y2=y; subs[0]->mybounds=a; subs[0]->subs[0]=NULL; a.x1=mybounds.x1; a.x2=x; a.y1=y; a.y2=mybounds.y2; subs[1]->mybounds=a; subs[1]->subs[0]=NULL; a.x1=x; a.x2=mybounds.x2; a.y1=y; a.y2=mybounds.y2; subs[2]->mybounds=a; subs[2]->subs[0]=NULL; a.x1=x; a.x2=mybounds.x2; a.y1=mybounds.y1; a.y2=y; subs[3]->mybounds=a; subs[3]->subs[0]=NULL; } bool cross(int a1, int a2, int b1, int b2) { // czy [a1 .. a2] i [b1..b2] maja czesc wspolna? if (a2<=b1 || a1 >= b2) return false; else return true; } bool cross(const R& a, const R& b) { return (cross(a.x1,a.x2, b.x1, b.x2) && cross(a.y1,a.y2, b.y1, b.y2)); } list<int> fallinto(const R& a) { list<int> l; // cerr<<"|"; FORE(i,r) if (!i->removed && cross(a,*i)) { // cerr<<i-r.begin()<<'|'; l.PB(i-r.begin()); } return l; } void add(int index) { insert(r[index].x1,r[index].y1, index); insert(r[index].x2,r[index].y1, index); insert(r[index].x1,r[index].y2, index); insert(r[index].x2,r[index].y2, index); } void remove(int index) { } }; struct RT { list<int> p; void remove(int index) { FORE(i, p) if (*i==index) { p.erase(i); return; }; } list<int> iscrossing(int index){ int iy1 = r[index].y1; int iy2 = r[index].y2; list<int> res; FORE(i, p) { int y1=r[*i].y1; int y2=r[*i].y2; // czy [iy1 .. iy2] i [y1..y2] maja czesc wspolna? if (iy2<=y1 || iy1 >= y2) ; else res.PB(*i); } return res; } void add(int index) { p.PB(index); } }; QT qt; RT rt; set<P> ixy; void remove_r(int index) { // usun [index] ze wszystkich struktur: r, rt, ixy, QT; rt.remove(index); r[index].removed = true; // ixy nie trzeba usuwac qt.remove(index); } void add_r(const R& ar) { int index = SZ(r); r.PB(ar); rt.add(index); ixy.insert(MP(ar.x2, index)); qt.add(index); } bool isclosing(R& r, int x) { return (r.x2 == x); } void grow(R& a, const R& b) { mini(a.x1,b.x1); mini(a.y1,b.y1); maxi(a.x2,b.x2); maxi(a.y2,b.y2); } template <typename A> R deal_with_intersection(int index, A& li) { #if 0 cerr<<"("<<r[index].y1 <<"," <<r[index].y2<<"):"; FORE(j,li) cerr<<'['<<r[*j].y1<<',' <<r[*j].y2<<']'; #endif R super = r[index]; remove_r(index); FORE(j,li) { grow(super, r[*j]); remove_r(*j); } DBG(cerr<<"<-["<<super<<']';) VAR(ki, qt.fallinto(super)); // DBG(cerr<<'+'<<SZ(ki)<<'+';) while(SZ(ki)>0) { FORE(k,ki) { grow(super, r[*k]); remove_r(*k); } ki = qt.fallinto(super); } DBG(cerr<<"<-["<<super<<']';) return super; } void scan() { FORE(i,ixy) { DBG(cerr<<":";) int x = i->FI; int index = i->SE; if (isclosing(r[index], x)) rt.remove(index); else { if (!r[index].removed) { DBG(cerr<<"open{"<<x<<"}";) VAR(li, rt.iscrossing(index)); rt.add(index); if (!li.empty()) { // found intersection R ar = deal_with_intersection(index, li); add_r(ar); } } } DBG(cerr<<endl;) } } int main(){ ios_base::sync_with_stdio(false); cout << setprecision(15) << fixed; int n; cin >> n; r.reserve(n*2+7); //ixy.reserve(n*2+7); FOR(i,1,n) { R a; cin >> a.x1 >> a.x2 >> a.y1 >> a.y2; a.x1=a.x1*2+1; a.x2=a.x2*2; ixy.insert(MP(a.x1,SZ(r))); ixy.insert(MP(a.x2,SZ(r))); r.PB(a); } // qt.construct(); qt.init(); scan(); int count=0; FORE(i,r) { i->x1 /= 2; i->x2 /= 2; if (!i->removed) count++;} sort(ALL(r)); cout<<count<<endl; FORE(i,r) if (!i->removed) cout<<*i<<endl; return 0; } |