#include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MAX = 100 * 1000; const int INF = (int)10e9; int N; struct Pair { int *first; int second; Pair():first(NULL), second(0){} Pair(int *first, int second):first(first), second(second){} }; vector<Pair> Sorted; struct Rect { int left, right, top, bot; int l; int r; Rect():left(0), right(0), top(0), bot(0), l(NULL), r(NULL){} Rect(int left, int right, int top, int bot) :left(left), right(right), top(top), bot(bot), l(NULL), r(NULL) {} void load(int id) { scanf("%d %d %d %d", &left, &right, &bot, &top); l = left; r = -right; Sorted[id*2] = Pair(&l,id); Sorted[id*2+1] = Pair(&r,id); } static Rect boundary(Rect a, Rect b) { return Rect(min(a.left, b.left), max(a.right, b.right), max(a.top, b.top), min(a.bot, b.bot)); } static bool less(Rect a, Rect b) { if(a.left < b.left) return true; else if(a.left > b.left) return false; else if(a.right < b.right) return true; else if(a.right > b.right) return false; else if(a.bot < b.bot) return true; else if(a.bot > b.bot) return false; else if(a.top < b.top) return true; else if(a.top > b.top) return false; } }; bool comp(Pair a, Pair b) { if((*a.first)+(*b.first) == 0 && (*a.first)<0) return true; int aa = (*a.first)<0?-(*a.first):(*a.first); int bb = (*b.first)<0?-(*b.first):(*b.first); return aa<bb?true:false; } vector<Rect> TAB; int Used[MAX]; void DeleteAndInsert(int a, int b, Rect c) { if(b<a) swap(a,b); (TAB[a].l) = c.left; (TAB[a].r) = -c.right; (TAB[b].l) = INF; (TAB[b].r) = INF; c.l = TAB[a].l; c.r = TAB[a].r; swap(TAB[b], TAB[N-1]); TAB.pop_back(); TAB[a] = c; N--; sort(Sorted.begin(), Sorted.end(), comp); Sorted.pop_back(); Sorted.pop_back(); }; void Insert() { bool change = true; vector<int> in; vector<Pair> que; sort(Sorted.begin(), Sorted.end(), comp); while(change) { change = false; try{ int inside = 0; for(int i = 0; i<2*N; i++) { if(*Sorted[i].first >= 0) { in.push_back(Sorted[i].second); Used[Sorted[i].second] = inside; inside++; } else { if(inside == 1) { inside--; in.pop_back(); } else if(inside > 1) { for(int j = 0; j<inside; j++) { que.push_back(Pair(new int(-TAB[in[j]].bot), in[j])); que.push_back(Pair(&(TAB[in[j]].top), in[j])); } sort(que.begin(), que.end(), comp); int res = 0; int id; for(int j = que.size()-1; j>=0; j--) { int cur = *que[j].first; if(cur>=0) { res++; if(res == 2) { //w id oraz w que[j].second mamy przeciecie Rect c = Rect::boundary(TAB[id], TAB[que[j].second]); DeleteAndInsert(id, que[j].second, c); throw 0; } else id = que[j].second; } else { res--; } que.pop_back(); } inside--; Used[in[inside]] = Used[Sorted[i].second]; swap(in[Used[Sorted[i].second]], in[inside]); in.pop_back(); } } } }catch(int code) { in = vector<int>(0); que = vector<Pair>(0); change = true; } } } int main() { scanf("%d", &N); TAB.resize(N); Sorted.resize(2*N); for(int i = 0; i<N; i++) TAB[i].load(i); Insert(); sort(TAB.begin(), TAB.end(), Rect::less); printf("%d\n", N); for(int i = 0; i<N; i++) { printf("%d %d %d %d\n", TAB[i].left, TAB[i].right, TAB[i].bot, TAB[i].top); } 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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MAX = 100 * 1000; const int INF = (int)10e9; int N; struct Pair { int *first; int second; Pair():first(NULL), second(0){} Pair(int *first, int second):first(first), second(second){} }; vector<Pair> Sorted; struct Rect { int left, right, top, bot; int l; int r; Rect():left(0), right(0), top(0), bot(0), l(NULL), r(NULL){} Rect(int left, int right, int top, int bot) :left(left), right(right), top(top), bot(bot), l(NULL), r(NULL) {} void load(int id) { scanf("%d %d %d %d", &left, &right, &bot, &top); l = left; r = -right; Sorted[id*2] = Pair(&l,id); Sorted[id*2+1] = Pair(&r,id); } static Rect boundary(Rect a, Rect b) { return Rect(min(a.left, b.left), max(a.right, b.right), max(a.top, b.top), min(a.bot, b.bot)); } static bool less(Rect a, Rect b) { if(a.left < b.left) return true; else if(a.left > b.left) return false; else if(a.right < b.right) return true; else if(a.right > b.right) return false; else if(a.bot < b.bot) return true; else if(a.bot > b.bot) return false; else if(a.top < b.top) return true; else if(a.top > b.top) return false; } }; bool comp(Pair a, Pair b) { if((*a.first)+(*b.first) == 0 && (*a.first)<0) return true; int aa = (*a.first)<0?-(*a.first):(*a.first); int bb = (*b.first)<0?-(*b.first):(*b.first); return aa<bb?true:false; } vector<Rect> TAB; int Used[MAX]; void DeleteAndInsert(int a, int b, Rect c) { if(b<a) swap(a,b); (TAB[a].l) = c.left; (TAB[a].r) = -c.right; (TAB[b].l) = INF; (TAB[b].r) = INF; c.l = TAB[a].l; c.r = TAB[a].r; swap(TAB[b], TAB[N-1]); TAB.pop_back(); TAB[a] = c; N--; sort(Sorted.begin(), Sorted.end(), comp); Sorted.pop_back(); Sorted.pop_back(); }; void Insert() { bool change = true; vector<int> in; vector<Pair> que; sort(Sorted.begin(), Sorted.end(), comp); while(change) { change = false; try{ int inside = 0; for(int i = 0; i<2*N; i++) { if(*Sorted[i].first >= 0) { in.push_back(Sorted[i].second); Used[Sorted[i].second] = inside; inside++; } else { if(inside == 1) { inside--; in.pop_back(); } else if(inside > 1) { for(int j = 0; j<inside; j++) { que.push_back(Pair(new int(-TAB[in[j]].bot), in[j])); que.push_back(Pair(&(TAB[in[j]].top), in[j])); } sort(que.begin(), que.end(), comp); int res = 0; int id; for(int j = que.size()-1; j>=0; j--) { int cur = *que[j].first; if(cur>=0) { res++; if(res == 2) { //w id oraz w que[j].second mamy przeciecie Rect c = Rect::boundary(TAB[id], TAB[que[j].second]); DeleteAndInsert(id, que[j].second, c); throw 0; } else id = que[j].second; } else { res--; } que.pop_back(); } inside--; Used[in[inside]] = Used[Sorted[i].second]; swap(in[Used[Sorted[i].second]], in[inside]); in.pop_back(); } } } }catch(int code) { in = vector<int>(0); que = vector<Pair>(0); change = true; } } } int main() { scanf("%d", &N); TAB.resize(N); Sorted.resize(2*N); for(int i = 0; i<N; i++) TAB[i].load(i); Insert(); sort(TAB.begin(), TAB.end(), Rect::less); printf("%d\n", N); for(int i = 0; i<N; i++) { printf("%d %d %d %d\n", TAB[i].left, TAB[i].right, TAB[i].bot, TAB[i].top); } return 0; } |