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
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

#define VDEBUG(...) {}
#define DEBUG(...) {}
#define ERROR(...) {}
//#define ERROR(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr);/* exit(1);*/ }
//#define DEBUG(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); }
//#define VDEBUG(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); }
//#define DEBUGPRINT

#define MAXN 100007

int N, Nret;

#define RIGHT 0
#define DOWN 1
#define LEFT 2
#define UP 3
int opposite_dir(int dir) {
	return (dir + 2) % 4;
}

struct rect {
	int x1, x2, y1, y2;
	int mergeid; /* == i if not merged. */
	vector<int> *neighb[4];
} R[MAXN];

bool rectlex(const rect &a, const rect &b) {
	/* lexcmp. */
	if (a.x1 != b.x1)
		return a.x1 < b.x1;
	if (a.x2 != b.x2)
		return a.x2 < b.x2;
	if (a.y1 != b.y1)
		return a.y1 < b.y1;
	return a.y2 < b.y2;
}

int rect_find_mergeid(int id) {
	if (R[id].mergeid != id) {
		R[id].mergeid = rect_find_mergeid(R[id].mergeid);
	}
	return R[id].mergeid;
}

////////////////////////////////////////////////////////////

bool global_merged;

struct xasc {
	bool operator()(const int &i, const int &j)  { return R[i].x1 < R[j].x1; }
};
struct xdesc {
	bool operator()(const int &i, const int &j) { return R[i].x2 > R[j].x2; }
};
struct yasc {
	bool operator()(const int &i, const int &j)  { return R[i].y1 < R[j].y1; }
};
struct ydesc {
	bool operator()(const int &i, const int &j) { return R[i].y2 > R[j].y2; }
};
set<int, xasc> right;
set<int, xdesc> left;
set<int, yasc> down;
set<int, ydesc> up;
int cur_mergeid;

void insert_neighb(rect &r) {
	int i;
#define INSERT_NEIGHB(DIR, SET) { \
	for (i = 0; i < r.neighb[DIR]->size(); i++) { \
		int id = rect_find_mergeid((*r.neighb[DIR])[i]); \
		if (id == cur_mergeid) \
			continue; \
		SET.insert(id); \
	} \
}
	INSERT_NEIGHB(RIGHT, right);
	INSERT_NEIGHB(LEFT, left);
	INSERT_NEIGHB(UP, up);
	INSERT_NEIGHB(DOWN, down);
}

void erase_from_neighb(int id) {
	right.erase(id);
	left.erase(id);
	up.erase(id);
	down.erase(id);
}

bool rect_intersect(rect &a, rect &b) {
	return a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1;
}

bool rect_merge_if_intersect(int to, int from) {
	from = rect_find_mergeid(from);
	if (R[to].mergeid != to) {
		ERROR("Target rectangle %d is already merged into %d.", to, R[to].mergeid);
	}
	VDEBUG("Try merge %d (%d %d %d %d) to %d (%d %d %d %d).\n", 
		   from, R[from].x1, R[from].x2, R[from].y1, R[from].y2,
		   to,  R[to].x1, R[to].x2, R[to].y1, R[to].y2);
	rect &a = R[to];
	rect &b = R[from];
	if (a.mergeid == b.mergeid) {
		ERROR("mergeid == %d already merged together!\n", a.mergeid);
	}
	if (rect_intersect(a, b)) {
		if (b.x1 < a.x1)
			a.x1 = b.x1;
		if (b.x2 > a.x2)
			a.x2 = b.x2;
		if (b.y1 < a.y1)
			a.y1 = b.y1;
		if (b.y2 > a.y2)
			a.y2 = b.y2;
		b.mergeid = a.mergeid;
		erase_from_neighb(from);
		insert_neighb(b);
		for (int i = 0; i < 4; i++) {
			delete b.neighb[i];
			b.neighb[i] = NULL;
		}
		Nret--;
		VDEBUG("Merged %d to %d.\n", from, to);
		return true;
	}
	return false;
}

void print_merge_neighb() {
#ifdef DEBUGPRINT
	VDEBUG("merge_neighb: ");
	for (set<int, xasc>::iterator it = right.begin(); it != right.end(); it++) {
		VDEBUG(" %d", *it);
	}
	VDEBUG("\n");
#endif
}

void rect_mergeall(int i) {
	if (rect_find_mergeid(i) != i)
		return;
	cur_mergeid = R[i].mergeid;
	insert_neighb(R[i]);
	bool merged;
	do {
		print_merge_neighb();
		merged = false;
#define SEARCH(SET, SORT) { \
			set<int, SORT>::iterator it = SET.begin(); \
			if (it != SET.end()) \
				merged |= rect_merge_if_intersect(i, *it); \
		}
		SEARCH(right, xasc);
		SEARCH(left, xdesc);
		SEARCH(down, yasc);
		SEARCH(up, ydesc);
		global_merged |= merged;
	} while (merged);
	/* new neighb after all merges. */
#define REINSERT_NEIGHB(DIR, SET) {\
	R[i].neighb[DIR]->clear(); \
	R[i].neighb[DIR]->insert(R[i].neighb[DIR]->begin(), SET.begin(), SET.end()); \
	SET.clear(); \
}
	REINSERT_NEIGHB(RIGHT, right);
	REINSERT_NEIGHB(LEFT, left);
	REINSERT_NEIGHB(UP, up);
	REINSERT_NEIGHB(DOWN, down);
}

////////////////////////////////////////////////////////////

struct seg {
	int a1, a2, b, id;
} horu[MAXN], hord[MAXN], verl[MAXN], verr[MAXN];

bool segcmp_b(const seg &a, const seg &b) {
	return a.b < b.b;
}

struct segcmp_a {
	bool operator()(const seg &a, const seg &b) {
		return a.a1 < b.a1;
	}
};

set<seg, segcmp_a> sweep;

void sweep_step(seg &cur, int dir) {
	set<seg, segcmp_a>::iterator f;
	f = sweep.lower_bound(cur);
	if (f != sweep.begin())
		f--;
	/* All segments on sweep are not intersecting. */
	while (f != sweep.end() && f->a1 < cur.a2) {
		set<seg, segcmp_a>::iterator nextf = f;
		nextf++;
		if (f->a2 > cur.a1) { /* intersect. */
			R[f->id].neighb[dir]->push_back(cur.id);
			R[cur.id].neighb[opposite_dir(dir)]->push_back(f->id);
			VDEBUG("-([%d %d]@%d from rect %d)\n", f->a1, f->a2, f->b, f->id);
			sweep.erase(f);
		}
		f = nextf;
	}
	sweep.insert(cur);
	VDEBUG("+([%d %d]@%d from rect %d)\n", cur.a1, cur.a2, cur.b, cur.id);
}

////////////////////////////////////////////////////////////

void print_all_neighb() {
#ifdef DEBUGPRINT
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 4; j++) {
			VDEBUG("neighb[%d][%s]: ", i, j == 0 ? "RIGHT" : j == 1 ? "DOWN" : j == 2 ? "LEFT" : "UP");
			for(int k = 0; k < R[i].neighb[j]->size(); k++) {
				VDEBUG(" %d", (*R[i].neighb[j])[k]);
			}
			VDEBUG("\n");
		}
	}
#endif
}

void run() {
	int i, cnt;
	
	cnt = 0;
	for (i = 0; i < N; i++) {
		if (rect_find_mergeid(i) != i)
			continue;
		horu[cnt].a1 = hord[cnt].a1 = R[i].x1; 
		horu[cnt].a2 = hord[cnt].a2 = R[i].x2;
		horu[cnt].b = R[i].y1;
		hord[cnt].b = R[i].y2;
		verl[cnt].a1 = verr[cnt].a2 = R[i].y1; 
		verl[cnt].a2 = verr[cnt].a2 = R[i].y2;
		verl[cnt].b = R[i].x1;
		verr[cnt].b = R[i].x2;
		horu[cnt].id = hord[cnt].id = verl[cnt].id = verr[cnt].id = i;
		cnt++;
	}
	if (cnt != N) {
		ERROR("Some rectangles already merged: cnt = %d Nret = %d.\n", cnt, Nret);
	}
	sort(verl, verl+N, segcmp_b);
	sort(verr, verr+N, segcmp_b);
	sort(horu, horu+N, segcmp_b);
	sort(hord, hord+N, segcmp_b);
	
	/* left -> right */
	VDEBUG("sweep left->right\n");
	sweep.clear();
	for (i = 0; i < N; i++)
		sweep_step(verl[i], RIGHT);
	print_all_neighb();
	/* right -> left */
	VDEBUG("sweep right->left\n");
	sweep.clear();
	for (i = N-1; i >= 0; i--)
		sweep_step(verr[i], LEFT);
	print_all_neighb();
	/* up -> down */
	VDEBUG("sweep up->down\n");
	sweep.clear();
	for (i = 0; i < N; i++)
		sweep_step(horu[i], DOWN);
	print_all_neighb();
	/* right -> left */
	VDEBUG("sweep down->up\n");
	sweep.clear();
	for (i = N-1; i >= 0; i--)
		sweep_step(hord[i], UP);
	print_all_neighb();
	sweep.clear();
	
	/* Merge. */
	for (i = 0; i < N; i++)
		rect_mergeall(i);
	
	/* Cleanup */
	cnt = 0;
	for (i = 0; i < N; i++) {
		int j;
		for (j = 0; j < 4; j++) {
			if (R[i].neighb[j] != NULL) {
				delete R[i].neighb[j];
				R[i].neighb[j] = NULL;
			}
		}
		if (R[i].mergeid != i)
			continue;
		R[cnt] = R[i];
		R[cnt].mergeid = cnt;
		for (j = 0; j < 4; j++) R[cnt].neighb[j] = new vector<int>();
		cnt++;
	}
	if (cnt != Nret) {
		ERROR("cnt != Nret. cnt = %d, Nret = %d\n", cnt, Nret);
	}
	N = cnt;
}

int main() {
	int i;
	scanf("%d", &N);
	for (i = 0; i < N; i++) {
		scanf("%d %d %d %d", &R[i].x1, &R[i].x2, &R[i].y1, &R[i].y2);
		R[i].mergeid = i;
		for (int j = 0; j < 4; j++)
			R[i].neighb[j] = new vector<int>();
	}
	
	int num_passes = 0;
	Nret = N;
	do {
		global_merged = false;
		run();
		num_passes++;
	} while (global_merged /*&& false /* safety net for a bug*/);
	DEBUG("num_passes=%d %s\n", num_passes, num_passes > 2 ? "> 2" : "");
	
	sort(R, R+N, rectlex);
	printf("%d\n", N);
	for (i = 0; i < N; i++) {
		printf("%d %d %d %d\n", R[i].x1, R[i].x2, R[i].y1, R[i].y2);
	}
	
	return 0;
}