#include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #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 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 #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; 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); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; struct Rect { int x1, x2, y1, y2; bool is; bool operator<(const Rect& b) const { return y2 < b.y2; } }; const int N = 1e5+1; const int S = 1<<20; int n; Rect r[N]; struct V { PII top; PII back; vector< PII > que; }; V t[S*2]; void update(int p) { while (!t[p].que.empty() && !r[t[p].que.back().Y].is) { t[p].que.pop_back(); } if (t[p].que.empty()) { t[p].back = MP(-1, -1); } else { t[p].back = t[p].que.back(); } t[p].top = t[p].back; if (p < S) { maxi(t[p].top, t[p*2].top); maxi(t[p].top, t[p*2+1].top); } } PII query(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return MP(-1,-1); if ((a <= A) && (B <= b)) return t[p].top; return max( max(query(p*2, a, b, A, (A+B)/2), query(p*2+1, a, b, (A+B)/2, B)), t[p].back);; } PII query(int x1, int x2) { return query(1, x1, x2, 0, S); } void insert(int p, int a, int b, int A, int B, PII v) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { t[p].que.PB(v); update(p); return; } insert(p*2, a, b, A, (A+B)/2, v); insert(p*2+1, a, b, (A+B)/2, B, v); update(p); } void insert(int x1, int x2, PII v) { insert(1, x1, x2, 0, S, v); } void remove(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { update(p); return; } remove(p*2, a, b, A, (A+B)/2); remove(p*2+1, a, b, (A+B)/2, B); update(p); } void remove(int x1, int x2) { remove(1, x1, x2, 0, S); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d", &n); REP(i, n) { scanf("%d %d %d %d", &r[i].x1, &r[i].x2, &r[i].y1, &r[i].y2); r[i].is = true; } sort(r, r+n); REP(i, S*2) { t[i].top = MP(-1, -1); t[i].back = MP(-1, -1); } t[0].top = MP(0,0); REP(i, n) { // printf("%d!\n", i); do { /* REP(i, S*2) { printf("(%d,%d) ", t[i].top.X, t[i].top.Y); } printf("\n"); REP(i, S*2) { printf("(%d,%d) ", t[i].back.X, t[i].back.Y); } printf("\n"); */ PII res = query(r[i].x1, r[i].x2); // printf("query %d %d -> %d %d ? %d\n", r[i].x1, r[i].x2, res.X, res.Y, r[i].y1); if ((res.Y != -1) && (res.X > r[i].y1)) { int j = res.Y; mini(r[i].x1, r[j].x1); maxi(r[i].x2, r[j].x2); mini(r[i].y1, r[j].y1); maxi(r[i].y2, r[j].y2); r[j].is = false; remove(r[j].x1, r[j].x2); } else { break; } } while (true); // printf("Wrzucam %d %d -> %d %d\n", r[i].x1, r[i].x2, r[i].y2, i); insert(r[i].x1, r[i].x2, MP(r[i].y2, i)); } /* REP(i, S*2) { printf("(%d,%d) ", t[i].top.X, t[i].top.Y); } printf("\n"); REP(i, S*2) { printf("(%d,%d) ", t[i].back.X, t[i].back.Y); } printf("\n"); */ vector< pair<PII, PII> > res; REP(i, n) if (r[i].is) { res.PB(MP(MP(r[i].x1, r[i].x2), MP(r[i].y1, r[i].y2))); } sort(ALL(res)); printf("%d\n", SZ(res)); for (auto p: res) { printf("%d %d %d %d\n", p.X.X, p.X.Y, p.Y.X, p.Y.Y); } 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 | #include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #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 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 #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; 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); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; struct Rect { int x1, x2, y1, y2; bool is; bool operator<(const Rect& b) const { return y2 < b.y2; } }; const int N = 1e5+1; const int S = 1<<20; int n; Rect r[N]; struct V { PII top; PII back; vector< PII > que; }; V t[S*2]; void update(int p) { while (!t[p].que.empty() && !r[t[p].que.back().Y].is) { t[p].que.pop_back(); } if (t[p].que.empty()) { t[p].back = MP(-1, -1); } else { t[p].back = t[p].que.back(); } t[p].top = t[p].back; if (p < S) { maxi(t[p].top, t[p*2].top); maxi(t[p].top, t[p*2+1].top); } } PII query(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return MP(-1,-1); if ((a <= A) && (B <= b)) return t[p].top; return max( max(query(p*2, a, b, A, (A+B)/2), query(p*2+1, a, b, (A+B)/2, B)), t[p].back);; } PII query(int x1, int x2) { return query(1, x1, x2, 0, S); } void insert(int p, int a, int b, int A, int B, PII v) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { t[p].que.PB(v); update(p); return; } insert(p*2, a, b, A, (A+B)/2, v); insert(p*2+1, a, b, (A+B)/2, B, v); update(p); } void insert(int x1, int x2, PII v) { insert(1, x1, x2, 0, S, v); } void remove(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { update(p); return; } remove(p*2, a, b, A, (A+B)/2); remove(p*2+1, a, b, (A+B)/2, B); update(p); } void remove(int x1, int x2) { remove(1, x1, x2, 0, S); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d", &n); REP(i, n) { scanf("%d %d %d %d", &r[i].x1, &r[i].x2, &r[i].y1, &r[i].y2); r[i].is = true; } sort(r, r+n); REP(i, S*2) { t[i].top = MP(-1, -1); t[i].back = MP(-1, -1); } t[0].top = MP(0,0); REP(i, n) { // printf("%d!\n", i); do { /* REP(i, S*2) { printf("(%d,%d) ", t[i].top.X, t[i].top.Y); } printf("\n"); REP(i, S*2) { printf("(%d,%d) ", t[i].back.X, t[i].back.Y); } printf("\n"); */ PII res = query(r[i].x1, r[i].x2); // printf("query %d %d -> %d %d ? %d\n", r[i].x1, r[i].x2, res.X, res.Y, r[i].y1); if ((res.Y != -1) && (res.X > r[i].y1)) { int j = res.Y; mini(r[i].x1, r[j].x1); maxi(r[i].x2, r[j].x2); mini(r[i].y1, r[j].y1); maxi(r[i].y2, r[j].y2); r[j].is = false; remove(r[j].x1, r[j].x2); } else { break; } } while (true); // printf("Wrzucam %d %d -> %d %d\n", r[i].x1, r[i].x2, r[i].y2, i); insert(r[i].x1, r[i].x2, MP(r[i].y2, i)); } /* REP(i, S*2) { printf("(%d,%d) ", t[i].top.X, t[i].top.Y); } printf("\n"); REP(i, S*2) { printf("(%d,%d) ", t[i].back.X, t[i].back.Y); } printf("\n"); */ vector< pair<PII, PII> > res; REP(i, n) if (r[i].is) { res.PB(MP(MP(r[i].x1, r[i].x2), MP(r[i].y1, r[i].y2))); } sort(ALL(res)); printf("%d\n", SZ(res)); for (auto p: res) { printf("%d %d %d %d\n", p.X.X, p.X.Y, p.Y.X, p.Y.Y); } return 0; } |