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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>

#define E_START 1
#define E_END 0

typedef long long int64;

using namespace std;

typedef struct {
	int x1, x2, y1, y2;
	bool exists;
	priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >* upper;
	priority_queue<pair<int, int> >* lower;
} rect_t;

bool compare_lex(const rect_t& r1, const rect_t& r2) {
	if (r1.x1 > r2.x1) return false;
	if (r1.x1 < r2.x1) return true;
	if (r1.x2 > r2.x2) return false;
	if (r1.x2 < r2.x2) return true;
	if (r1.y1 > r2.y1) return false;
	if (r1.y1 < r2.y1) return true;
	if (r1.y2 > r2.y2) return false;
	return true;
}

typedef struct y_node {
	int from, to;
	struct y_node* lson;
	struct y_node* rson;
	set<int> rects_partial;
	int rect_full;
} y_node;

void insert_seg(y_node* tree, int from, int to, int id) {
	int my_from = tree->from;
	int my_to = tree->to;
	if (from <= my_from && to >= my_to) {
		tree->rect_full = id;
		tree->rects_partial.insert(id);
		return;
	}
	if (my_from == my_to)
		return;
	int last_left = (my_from + my_to) / 2;
	if (from <= last_left) {
		if (tree->lson == NULL) {
			y_node* lson = new y_node;
			lson->from = my_from;
			lson->to = last_left;
			lson->lson = NULL;
			lson->rson = NULL;
			lson->rect_full = -1;
			tree->lson = lson;
		}
		insert_seg(tree->lson, from, to, id);
		tree->rects_partial.insert(id);
	}
	if (to >= last_left + 1) {
		if (tree->rson == NULL) {
			y_node* rson = new y_node;
			rson->from = last_left + 1;
			rson->to = my_to;
			rson->lson = NULL;
			rson->rson = NULL;
			rson->rect_full = -1;
			tree->rson = rson;
		}
		tree->rects_partial.insert(id);
		insert_seg(tree->rson, from, to, id);
	}
}

int intersects_seg(y_node* tree, int from, int to) {
	int my_from = tree->from;
	int my_to = tree->to;
	if (tree->rect_full != -1) {
		return tree->rect_full;
	}
	if (from <= my_from && to >= my_to) {
		if (tree->rects_partial.size() > 0)
			return *(tree->rects_partial.begin());
	}
	if (my_from == my_to)
		return -1;
	int last_left = (my_from + my_to) / 2;
	if (from <= last_left) {
		if (tree->lson != NULL) {
			int ret = intersects_seg(tree->lson, from, to);
			if (ret != -1)
				return ret;
		}
	}
	if (to >= last_left + 1) {
		if (tree->rson != NULL) {
			int ret = intersects_seg(tree->rson, from, to);
			if (ret != -1)
				return ret;
		}
	}
	return -1;
}

void delete_seg(y_node* tree, int from, int to, int id) {
	int my_from = tree->from;
	int my_to = tree->to;
	tree->rects_partial.erase(id);
	if (from <= my_from && to >= my_to) {
		tree->rect_full = -1;
		return;
	}
	if (my_from == my_to)
		return;
	int last_left = (my_from + my_to) / 2;
	if (from <= last_left) {
		delete_seg(tree->lson, from, to, id);
		if (tree->lson->rect_full == -1 && tree->lson->rects_partial.size() == 0) {
			delete tree->lson;
			tree->lson = NULL;
		}
	}
	if (to >= last_left + 1) {
		delete_seg(tree->rson, from, to, id);
		if (tree->rson->rect_full == -1 && tree->rson->rects_partial.size() == 0) {
			delete tree->rson;
			tree->rson = NULL;
		}
	}
}

int main () {
	int n, i, id, id_before, id_after, x, x1, x2, y1, y2, intersects, join;
	scanf("%d", &n);
	vector<rect_t> rects;
	rect_t r;
	vector<rect_t> queue;
	map<int, int> xs, ys;
	for (i = 0; i < n; ++i) {
		scanf("%d %d %d %d", &r.x1, &r.x2, &r.y1, &r.y2);
		r.exists = true;
		r.upper = new priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >;
		r.lower = new priority_queue<pair<int, int> >;
		rects.push_back(r);
		xs[r.x1] = -1;
		xs[r.x2] = -1;
		ys[r.y1] = -1;
		ys[r.y2] = -1;
	}
	i = 0;
	vector<int> rxs, rys;
	for (map<int, int>::iterator it = xs.begin(); it != xs.end(); it++) {
		(*it).second = i;
		rxs.push_back((*it).first);
		++i;
	}
	i = 0;
	for (map<int, int>::iterator it = ys.begin(); it != ys.end(); it++) {
		(*it).second = i;
		rys.push_back((*it).first);
		++i;
	}
	for (i = 0; i < n; ++i) {
		rects[i].x1 = xs[rects[i].x1];
		rects[i].x2 = xs[rects[i].x2];
		rects[i].y1 = ys[rects[i].y1];
		rects[i].y2 = ys[rects[i].y2];
	}
	y_node* tree = new y_node;
	tree->from = 0;
	tree->to = 1;
	while (tree->to < (*(ys.rbegin())).second) {
		tree->to <<= 1;
	}
	tree->lson = NULL;
	tree->rson = NULL;
	tree->rect_full = -1;
	set<pair<int, int> > order;
	set<pair<pair<int, int>, int> > events;
	for (i = 0; i < rects.size(); ++i) {
		events.insert(make_pair(make_pair(rects[i].x1, E_START), i));
		events.insert(make_pair(make_pair(rects[i].x2, E_END), i));
	}
	while (events.size()) {
		x = (*(events.begin())).first.first;
		vector<int> to_delete;
		vector<int> to_link;
		while (true) {
			set<pair<pair<int, int>, int> >::iterator it = events.begin();
			if (it == events.end() || (*it).first.first != x || (*it).first.second != E_END) {
				break;
			}
			id = (*it).second;
			to_delete.push_back(id);
			set<pair<int, int> >::iterator it2 = order.find(make_pair(rects[id].y1, id));
			it2++;
			if (it2 != order.end()) {
				to_link.push_back((*(it2)).second);
			}
			it2--;
			if (it2 != order.begin()) {
				it2--;
				to_link.push_back((*(it2)).second);
			}
			events.erase(it);
		}
		for (i = 0; i < to_delete.size(); ++i) {
			id = to_delete[i];
			order.erase(make_pair(rects[id].y1, id));
			delete_seg(tree, rects[id].y1, rects[id].y2 - 1, id);
		}
		while (true) {
			set<pair<pair<int, int>, int> >::iterator it = events.begin();
			if (it == events.end() || (*it).first.first != x || (*it).first.second != E_START) {
				break;
			}
			id = (*it).second;
			y1 = rects[id].y1;
			y2 = rects[id].y2;
			x1 = rects[id].x1;
			x2 = rects[id].x2;
			to_link.push_back(id);
			if (intersects_seg(tree, y1, y2 - 1) == -1) {
				order.insert(make_pair(y1, id));
				insert_seg(tree, y1, y2 - 1, id);
				events.erase(it);
				continue;
			}
			events.erase(make_pair(make_pair(rects[id].x1, E_START), id));
			events.erase(make_pair(make_pair(rects[id].x2, E_END), id));
			while (1) {
				intersects = intersects_seg(tree, y1, y2 - 1);
				if (intersects == -1) {
					break;
				}
				vector<int> stack;
				stack.push_back(intersects);
				while (stack.size()) {
					join = stack.back();
					stack.pop_back();
					if (!rects[join].exists) {
						continue;
					}
					if (order.find(make_pair(rects[join].y1, join)) != order.end()) {
						order.erase(make_pair(rects[join].y1, join));
						delete_seg(tree, rects[join].y1, rects[join].y2 - 1, join);
					}
					rects[join].exists = false;
					y1 = min(y1, rects[join].y1);
					y2 = max(y2, rects[join].y2);
					x1 = min(x1, rects[join].x1);
					x2 = max(x2, rects[join].x2);
					events.erase(make_pair(make_pair(rects[join].x1, E_START), join));
					events.erase(make_pair(make_pair(rects[join].x2, E_END), join));
					priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >* qfrom = rects[join].upper;
					priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >* qto = rects[id].upper;
					if (qfrom->size() > qto->size()) {
						priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >* tmp = qfrom;
						qfrom = qto;
						qto = tmp;
					}
					while (!qfrom->empty()) {
						qto->push(qfrom->top());
						qfrom->pop();
					}
					delete qfrom;
					while (true) {
						if (qto->empty()) {
							break;
						}
						pair<int, int> item = qto->top();
						if (item.first < y2) {
							qto->pop();
							stack.push_back(item.second);
						}
						else {
							break;
						}
					}
					rects[id].upper = qto;
					priority_queue<pair<int, int> >* qlfrom = rects[join].lower;
					priority_queue<pair<int, int> >* qlto = rects[id].lower;
					if (qlfrom->size() > qlto->size()) {
						priority_queue<pair<int, int> >* tmp = qlfrom;
						qlfrom = qlto;
						qlto = tmp;
					}
					while (!qlfrom->empty()) {
						qlto->push(qlfrom->top());
						qlfrom->pop();
					}
					delete qlfrom;
					while (true) {
						if (qlto->empty()) {
							break;
						}
						pair<int, int> item = qlto->top();
						if (item.first > y1) {
							qlto->pop();
							stack.push_back(item.second);
						}
						else {
							break;
						}
					}
					rects[id].lower = qlto;
				}
			}
			rects[id].x1 = x1;
			rects[id].x2 = x2;
			rects[id].y1 = y1;
			rects[id].y2 = y2;
			order.insert(make_pair(y1, id));
			insert_seg(tree, y1, y2 - 1, id);
			events.insert(make_pair(make_pair(x2, E_END), id));
		}
		for (i = 0; i < to_link.size(); ++i) {
			id = to_link[i];
			if (!rects[id].exists) {
				continue;
			}
			set<pair<int, int> >::iterator it = order.find(make_pair(rects[id].y1, id));
			if (it == order.end()) {
				continue;
			}
			if (it != order.begin()) {
				--it;
				id_before = (*it).second;
				rects[id].lower->push(make_pair(rects[id_before].y2, id_before));
				rects[id_before].upper->push(make_pair(rects[id].y1, id));
				++it;
			}
			++it;
			if (it != order.end()) {
				id_after = (*it).second;
				rects[id].upper->push(make_pair(rects[id_after].y1, id_after));
				rects[id_after].lower->push(make_pair(rects[id].y2, id));
			}
		}
	}
	vector<rect_t> res;
	for (i = 0; i < rects.size(); ++i) {
		if (!rects[i].exists) continue;
		res.push_back(rects[i]);
	}
	sort(res.begin(), res.end(), compare_lex);
	printf("%d\n", (int)res.size());
	for (i = 0; i < res.size(); ++i) {
		printf("%d %d %d %d\n", rxs[res[i].x1], rxs[res[i].x2], rys[res[i].y1], rys[res[i].y2]);
	}
	return 0;
}