#include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define x first #define y second using namespace std; typedef pair<int, int> PII; struct rectangle{ int x1, y1, x2, y2; rectangle(){} rectangle(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {} }; inline rectangle operator+(const rectangle &a, const rectangle &b){ return rectangle(min(a.x1, b.x1), min(a.y1, b.y1), max(a.x2, b.x2), max(a.y2, b.y2)); } inline bool cmpFinal(const rectangle &a, const rectangle &b){ if(a.x1 == b.x1){ if(a.x2 == b.x2){ if(a.y1 == b.y1) return a.y2 < b.y2; return a.y1 < b.y1; } return a.x2 < b.x2; } return a.x1 < b.x1; } inline bool cmpSweep(const rectangle &a, const rectangle &b){ return a.x2 < b.x2; } const int d = 1024*1024; vector<rectangle> rects; vector<int> hbars[2*d+3]; vector<int> vbars[2*d+3]; vector<bool> deleted; vector<rectangle> res; inline void addhbar(int p, int i){ p += d; while(p){ hbars[p].push_back(i); p /= 2; } } inline int gethbar(int p, int q){ p += d; q += d; int r = -1; while(p < q){ if(p&1){ while(!hbars[p].empty() && deleted[hbars[p].back()]) hbars[p].pop_back(); if(!hbars[p].empty()) r = max(r, hbars[p].back()); ++p; } if((q&1)==0){ while(!hbars[q].empty() && deleted[hbars[q].back()]) hbars[q].pop_back(); if(!hbars[q].empty()) r = max(r, hbars[q].back()); --q; } p /= 2; q /= 2; } if(p == q){ while(!hbars[p].empty() && deleted[hbars[p].back()]) hbars[p].pop_back(); if(!hbars[p].empty()) r = max(r, hbars[p].back()); } return r; } inline void addvbar(int p, int q, int i){ p += d; q += d; while(p < q){ if(p&1){ vbars[p].push_back(i); ++p; } if((q&1)==0){ vbars[q].push_back(i); --q; } p /= 2; q /= 2; } if(p == q) vbars[p].push_back(i); } inline int getvbar(int p){ p += d; int r = -1; while(p){ while(!vbars[p].empty() && deleted[vbars[p].back()]) vbars[p].pop_back(); if(!vbars[p].empty()) r = max(r, vbars[p].back()); p /= 2; } return r; } inline void usun(int i){ deleted[i] = 1; } inline void wrzuc(int i){ deleted.push_back(0); addhbar(rects[i].y1, i); addhbar(rects[i].y2, i); addvbar(rects[i].y1, rects[i].y2, i);; } void sweep(){ sort(rects.begin(), rects.end(), cmpSweep); FWD(i,0,(int)rects.size()){ for(;;){ //printf("%d: %d %d %d %d\n", i, rects[i].x1, rects[i].x2, rects[i].y1, rects[i].y2); int s = gethbar(rects[i].y1, rects[i].y2); s = max(s, getvbar(rects[i].y1)); s = max(s, getvbar(rects[i].y2)); if(s != -1 && rects[s].x2 >= rects[i].x1){ //printf("merge z %d\n", s); usun(s); rects[i] = rects[i] + rects[s]; }else break; } wrzuc(i); } FWD(i,0,(int)rects.size()) if(!deleted[i]) res.push_back(rects[i]); } int main(){ int n; scanf("%d", &n); FWD(i,0,n){ rectangle r; scanf("%d %d %d %d", &r.x1, &r.x2, &r.y1, &r.y2); assert(r.x1 < r.x2); assert(r.y1 < r.y2); --r.x2; --r.y2; rects.push_back(r); } sweep(); for(rectangle &r : res){ ++r.x2; ++r.y2; } sort(res.begin(), res.end(), cmpFinal); printf("%d\n", (int)res.size()); for(rectangle r : res) printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2); 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 | #include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define x first #define y second using namespace std; typedef pair<int, int> PII; struct rectangle{ int x1, y1, x2, y2; rectangle(){} rectangle(int a, int b, int c, int d) : x1(a), y1(b), x2(c), y2(d) {} }; inline rectangle operator+(const rectangle &a, const rectangle &b){ return rectangle(min(a.x1, b.x1), min(a.y1, b.y1), max(a.x2, b.x2), max(a.y2, b.y2)); } inline bool cmpFinal(const rectangle &a, const rectangle &b){ if(a.x1 == b.x1){ if(a.x2 == b.x2){ if(a.y1 == b.y1) return a.y2 < b.y2; return a.y1 < b.y1; } return a.x2 < b.x2; } return a.x1 < b.x1; } inline bool cmpSweep(const rectangle &a, const rectangle &b){ return a.x2 < b.x2; } const int d = 1024*1024; vector<rectangle> rects; vector<int> hbars[2*d+3]; vector<int> vbars[2*d+3]; vector<bool> deleted; vector<rectangle> res; inline void addhbar(int p, int i){ p += d; while(p){ hbars[p].push_back(i); p /= 2; } } inline int gethbar(int p, int q){ p += d; q += d; int r = -1; while(p < q){ if(p&1){ while(!hbars[p].empty() && deleted[hbars[p].back()]) hbars[p].pop_back(); if(!hbars[p].empty()) r = max(r, hbars[p].back()); ++p; } if((q&1)==0){ while(!hbars[q].empty() && deleted[hbars[q].back()]) hbars[q].pop_back(); if(!hbars[q].empty()) r = max(r, hbars[q].back()); --q; } p /= 2; q /= 2; } if(p == q){ while(!hbars[p].empty() && deleted[hbars[p].back()]) hbars[p].pop_back(); if(!hbars[p].empty()) r = max(r, hbars[p].back()); } return r; } inline void addvbar(int p, int q, int i){ p += d; q += d; while(p < q){ if(p&1){ vbars[p].push_back(i); ++p; } if((q&1)==0){ vbars[q].push_back(i); --q; } p /= 2; q /= 2; } if(p == q) vbars[p].push_back(i); } inline int getvbar(int p){ p += d; int r = -1; while(p){ while(!vbars[p].empty() && deleted[vbars[p].back()]) vbars[p].pop_back(); if(!vbars[p].empty()) r = max(r, vbars[p].back()); p /= 2; } return r; } inline void usun(int i){ deleted[i] = 1; } inline void wrzuc(int i){ deleted.push_back(0); addhbar(rects[i].y1, i); addhbar(rects[i].y2, i); addvbar(rects[i].y1, rects[i].y2, i);; } void sweep(){ sort(rects.begin(), rects.end(), cmpSweep); FWD(i,0,(int)rects.size()){ for(;;){ //printf("%d: %d %d %d %d\n", i, rects[i].x1, rects[i].x2, rects[i].y1, rects[i].y2); int s = gethbar(rects[i].y1, rects[i].y2); s = max(s, getvbar(rects[i].y1)); s = max(s, getvbar(rects[i].y2)); if(s != -1 && rects[s].x2 >= rects[i].x1){ //printf("merge z %d\n", s); usun(s); rects[i] = rects[i] + rects[s]; }else break; } wrzuc(i); } FWD(i,0,(int)rects.size()) if(!deleted[i]) res.push_back(rects[i]); } int main(){ int n; scanf("%d", &n); FWD(i,0,n){ rectangle r; scanf("%d %d %d %d", &r.x1, &r.x2, &r.y1, &r.y2); assert(r.x1 < r.x2); assert(r.y1 < r.y2); --r.x2; --r.y2; rects.push_back(r); } sweep(); for(rectangle &r : res){ ++r.x2; ++r.y2; } sort(res.begin(), res.end(), cmpFinal); printf("%d\n", (int)res.size()); for(rectangle r : res) printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2); return 0; } |