#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v,n) typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); typedef pair<pii, pii> Pros; const int N = 101000; const int K = 1 << 19; const int INF = 10001010; Pros inp[N]; set<Pros> tabs; int cnt[N]; int n; inline Pros lacz(const Pros& p1, const Pros& p2) { return mp( mp(min(p1.ST.ST, p2.ST.ST), max(p1.ST.ND, p2.ST.ND)), mp(min(p1.ND.ST, p2.ND.ST), max(p1.ND.ND, p2.ND.ND)) ); } struct Wyd { int kt; bool ot; Wyd() { } Wyd(int kt_, bool ot_): kt(kt_), ot(ot_) { } inline int getTime() const { if (ot) return inp[kt].ST.ST; else return inp[kt].ST.ND; } inline bool operator<(const Wyd& a) const { int t1 = getTime(), t2 = a.getTime(); if (t1 != t2) { return t1 < t2; } if (ot != a.ot) { return ot > a.ot; } return inp[kt].ND < inp[a.kt].ND; } }; Wyd kol[2 * N]; inline void zlacz(const int& dest, const int& source) { cnt[dest] += cnt[source]; cnt[source] = 0; REMIN(inp[dest].ST.ST, inp[source].ST.ST); REMAX(inp[dest].ST.ND, inp[source].ST.ND); REMIN(inp[dest].ND.ST, inp[source].ND.ST); REMAX(inp[dest].ND.ND, inp[source].ND.ND); } int xx[2 * N], yy[2 * N]; map<int, int> mapX, mapY; set<pii> drzewo[K]; set<pii> wspolne[K]; int P; void dodaj(bool op, const pii& x, int x1, int x2, int pocz = 1, int kon = P, int ind = 1) { if (op) wspolne[ind].insert(x); else wspolne[ind].erase(wspolne[ind].find(x)); int sr = (pocz + kon) / 2; if (x1 == pocz && x2 == kon) { if (op) drzewo[ind].insert(x); else drzewo[ind].erase(drzewo[ind].find(x)); } else if (x2 <= sr) dodaj(op, x, x1, x2, pocz, sr, 2*ind); else if (x1 > sr) dodaj(op, x, x1, x2, sr+1, kon, 2*ind+1); else { dodaj(op, x, x1, sr, pocz, sr, 2*ind); dodaj(op, x, sr+1, x2, sr+1, kon, 2*ind+1); } } const pii BRAK = mp(-1, -1); pii findElem(const set<pii>& zbior, const pii& range) { auto p = zbior.lower_bound(mp(range.ST, 0)); if (p != zbior.end()) { if (p->ST <= range.ND) return *p; } return BRAK; } pii getFromRange(int x1, int x2, const pii& range, int pocz = 1, int kon = P, int ind = 1) { if (wspolne[ind].empty()) return BRAK; int sr = (pocz + kon) / 2; auto x = findElem(drzewo[ind], range); if (x != BRAK) return x; if (x1 == pocz && x2 == kon) { return findElem(wspolne[ind], range); } else if (x2 <= sr) { return getFromRange(x1, x2, range, pocz, sr, 2 * ind); } else if (x1 > sr) { return getFromRange(x1, x2, range, sr+1, kon, 2 * ind + 1); } else { x = getFromRange(x1, sr, range, pocz, sr, 2 * ind); if (x != BRAK) return x; return getFromRange(sr+1, x2, range, sr+1, kon, 2 * ind + 1); } } inline bool onLeft(const pii& a, const pii& b) { return a.ND < b.ST; } inline bool emptyCross(const pii& a, const pii& b) { return onLeft(a,b) || onLeft(b, a); } int main() { scanf("%d", &n); REP(i, n) { scanf("%d %d %d %d", &inp[i].ST.ST, &inp[i].ST.ND, &inp[i].ND.ST, &inp[i].ND.ND); --inp[i].ST.ND; --inp[i].ND.ND; xx[2 * i] = inp[i].ST.ST; xx[2 * i + 1] = inp[i].ST.ND; yy[2 * i] = inp[i].ND.ST; yy[2 * i + 1] = inp[i].ND.ND; kol[2 * i] = Wyd(i, 0); kol[2 * i + 1] = Wyd(i, 1); cnt[i] = 1; } sort(xx, xx + 2 * n); sort(yy, yy + 2 * n); int dx = unique(xx, xx + 2* n) - xx, dy = unique(yy, yy + 2 * n) - yy; REP(i, dx) { mapX[xx[i]] = i + 1; } REP(i, dy) { mapY[yy[i]] = i + 1; } REP(i, n) { inp[i].ST.ST = mapX[inp[i].ST.ST]; inp[i].ST.ND = mapX[inp[i].ST.ND]; inp[i].ND.ST = mapY[inp[i].ND.ST]; inp[i].ND.ND = mapY[inp[i].ND.ND]; } sort(kol, kol + 2 * n); P = 1; while (P < dx) { P <<= 1; } set<pii> zb; REP(i, 2 * n) { int x = kol[i].kt; if (kol[i].ot) { while (!zb.empty()) { pii szuk = mp(inp[x].ND.ST, -1); auto it = zb.lower_bound(szuk); if (it == zb.end() || inp[x].ND.ND < inp[it->ND].ND.ST) break; pii p = *it; // przeciecie z punktem: number p.ND, inp[p.ND].ND.ST oraz inp[p.ND].ND.ND zb.erase(zb.find(p)); zlacz(x, p.ND); while (true) { p = getFromRange(inp[x].ST.ST, inp[x].ST.ND, inp[x].ND); if (p == BRAK) break ; zlacz(x, p.ND); dodaj(false /* delete */, p, inp[p.ND].ST.ST, inp[p.ND].ST.ND); } } zb.insert(mp(inp[x].ND.ST, x)); zb.insert(mp(inp[x].ND.ND, x)); } else { pii szuk = mp(inp[x].ND.ST, INF); auto p = zb.lower_bound(szuk); --p; if (cnt[p->ND] == 0) continue ; --cnt[p->ND]; if (cnt[p->ND] == 0) { dodaj(true, *p, inp[p->ND].ST.ST, inp[p->ND].ST.ND); auto pop = p; if (inp[p->ND].ND.ST != inp[p->ND].ND.ND) { ++pop; if (pop->ND == p->ND) { dodaj(true, *pop, inp[pop->ND].ST.ST, inp[pop->ND].ST.ND); zb.erase(pop); } } zb.erase(p); } } } for (const auto& vert : wspolne[1]) { tabs.insert(inp[vert.ND]); } printf("%d\n", SIZE(tabs)); for (const auto& pros : tabs) { printf("%d %d %d %d\n", xx[pros.ST.ST - 1], xx[pros.ST.ND - 1]+1, yy[pros.ND.ST - 1], yy[pros.ND.ND - 1]+1); } 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v,n) typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); typedef pair<pii, pii> Pros; const int N = 101000; const int K = 1 << 19; const int INF = 10001010; Pros inp[N]; set<Pros> tabs; int cnt[N]; int n; inline Pros lacz(const Pros& p1, const Pros& p2) { return mp( mp(min(p1.ST.ST, p2.ST.ST), max(p1.ST.ND, p2.ST.ND)), mp(min(p1.ND.ST, p2.ND.ST), max(p1.ND.ND, p2.ND.ND)) ); } struct Wyd { int kt; bool ot; Wyd() { } Wyd(int kt_, bool ot_): kt(kt_), ot(ot_) { } inline int getTime() const { if (ot) return inp[kt].ST.ST; else return inp[kt].ST.ND; } inline bool operator<(const Wyd& a) const { int t1 = getTime(), t2 = a.getTime(); if (t1 != t2) { return t1 < t2; } if (ot != a.ot) { return ot > a.ot; } return inp[kt].ND < inp[a.kt].ND; } }; Wyd kol[2 * N]; inline void zlacz(const int& dest, const int& source) { cnt[dest] += cnt[source]; cnt[source] = 0; REMIN(inp[dest].ST.ST, inp[source].ST.ST); REMAX(inp[dest].ST.ND, inp[source].ST.ND); REMIN(inp[dest].ND.ST, inp[source].ND.ST); REMAX(inp[dest].ND.ND, inp[source].ND.ND); } int xx[2 * N], yy[2 * N]; map<int, int> mapX, mapY; set<pii> drzewo[K]; set<pii> wspolne[K]; int P; void dodaj(bool op, const pii& x, int x1, int x2, int pocz = 1, int kon = P, int ind = 1) { if (op) wspolne[ind].insert(x); else wspolne[ind].erase(wspolne[ind].find(x)); int sr = (pocz + kon) / 2; if (x1 == pocz && x2 == kon) { if (op) drzewo[ind].insert(x); else drzewo[ind].erase(drzewo[ind].find(x)); } else if (x2 <= sr) dodaj(op, x, x1, x2, pocz, sr, 2*ind); else if (x1 > sr) dodaj(op, x, x1, x2, sr+1, kon, 2*ind+1); else { dodaj(op, x, x1, sr, pocz, sr, 2*ind); dodaj(op, x, sr+1, x2, sr+1, kon, 2*ind+1); } } const pii BRAK = mp(-1, -1); pii findElem(const set<pii>& zbior, const pii& range) { auto p = zbior.lower_bound(mp(range.ST, 0)); if (p != zbior.end()) { if (p->ST <= range.ND) return *p; } return BRAK; } pii getFromRange(int x1, int x2, const pii& range, int pocz = 1, int kon = P, int ind = 1) { if (wspolne[ind].empty()) return BRAK; int sr = (pocz + kon) / 2; auto x = findElem(drzewo[ind], range); if (x != BRAK) return x; if (x1 == pocz && x2 == kon) { return findElem(wspolne[ind], range); } else if (x2 <= sr) { return getFromRange(x1, x2, range, pocz, sr, 2 * ind); } else if (x1 > sr) { return getFromRange(x1, x2, range, sr+1, kon, 2 * ind + 1); } else { x = getFromRange(x1, sr, range, pocz, sr, 2 * ind); if (x != BRAK) return x; return getFromRange(sr+1, x2, range, sr+1, kon, 2 * ind + 1); } } inline bool onLeft(const pii& a, const pii& b) { return a.ND < b.ST; } inline bool emptyCross(const pii& a, const pii& b) { return onLeft(a,b) || onLeft(b, a); } int main() { scanf("%d", &n); REP(i, n) { scanf("%d %d %d %d", &inp[i].ST.ST, &inp[i].ST.ND, &inp[i].ND.ST, &inp[i].ND.ND); --inp[i].ST.ND; --inp[i].ND.ND; xx[2 * i] = inp[i].ST.ST; xx[2 * i + 1] = inp[i].ST.ND; yy[2 * i] = inp[i].ND.ST; yy[2 * i + 1] = inp[i].ND.ND; kol[2 * i] = Wyd(i, 0); kol[2 * i + 1] = Wyd(i, 1); cnt[i] = 1; } sort(xx, xx + 2 * n); sort(yy, yy + 2 * n); int dx = unique(xx, xx + 2* n) - xx, dy = unique(yy, yy + 2 * n) - yy; REP(i, dx) { mapX[xx[i]] = i + 1; } REP(i, dy) { mapY[yy[i]] = i + 1; } REP(i, n) { inp[i].ST.ST = mapX[inp[i].ST.ST]; inp[i].ST.ND = mapX[inp[i].ST.ND]; inp[i].ND.ST = mapY[inp[i].ND.ST]; inp[i].ND.ND = mapY[inp[i].ND.ND]; } sort(kol, kol + 2 * n); P = 1; while (P < dx) { P <<= 1; } set<pii> zb; REP(i, 2 * n) { int x = kol[i].kt; if (kol[i].ot) { while (!zb.empty()) { pii szuk = mp(inp[x].ND.ST, -1); auto it = zb.lower_bound(szuk); if (it == zb.end() || inp[x].ND.ND < inp[it->ND].ND.ST) break; pii p = *it; // przeciecie z punktem: number p.ND, inp[p.ND].ND.ST oraz inp[p.ND].ND.ND zb.erase(zb.find(p)); zlacz(x, p.ND); while (true) { p = getFromRange(inp[x].ST.ST, inp[x].ST.ND, inp[x].ND); if (p == BRAK) break ; zlacz(x, p.ND); dodaj(false /* delete */, p, inp[p.ND].ST.ST, inp[p.ND].ST.ND); } } zb.insert(mp(inp[x].ND.ST, x)); zb.insert(mp(inp[x].ND.ND, x)); } else { pii szuk = mp(inp[x].ND.ST, INF); auto p = zb.lower_bound(szuk); --p; if (cnt[p->ND] == 0) continue ; --cnt[p->ND]; if (cnt[p->ND] == 0) { dodaj(true, *p, inp[p->ND].ST.ST, inp[p->ND].ST.ND); auto pop = p; if (inp[p->ND].ND.ST != inp[p->ND].ND.ND) { ++pop; if (pop->ND == p->ND) { dodaj(true, *pop, inp[pop->ND].ST.ST, inp[pop->ND].ST.ND); zb.erase(pop); } } zb.erase(p); } } } for (const auto& vert : wspolne[1]) { tabs.insert(inp[vert.ND]); } printf("%d\n", SIZE(tabs)); for (const auto& pros : tabs) { printf("%d %d %d %d\n", xx[pros.ST.ST - 1], xx[pros.ST.ND - 1]+1, yy[pros.ND.ST - 1], yy[pros.ND.ND - 1]+1); } return 0; } |