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
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <memory.h>
#include <utility>
#include <set>
#include <unordered_map>
#include <algorithm>

#ifdef	DEBUG
#include <iostream>
#define	DBG(x)	x
#else
#define	DBG(x)
#endif

typedef struct rect_t {
	int abcd[4];
	bool operator< (const struct rect_t &that) const {
		return std::lexicographical_compare(&this->abcd[0], &this->abcd[4], &that.abcd[0], &that.abcd[4]);
	}
} rect_t;

#define	INTERSECT_RECT(r, r1, r2)	do { \
	(r).abcd[0] = std::max((r1).abcd[0], (r2).abcd[0]); \
	(r).abcd[1] = std::min((r1).abcd[1], (r2).abcd[1]); \
	(r).abcd[2] = std::max((r1).abcd[2], (r2).abcd[2]); \
	(r).abcd[3] = std::min((r1).abcd[3], (r2).abcd[3]); \
} while (0)
#define	UNION_RECT(r, r1, r2)	do { \
	(r).abcd[0] = std::min((r1).abcd[0], (r2).abcd[0]); \
	(r).abcd[1] = std::max((r1).abcd[1], (r2).abcd[1]); \
	(r).abcd[2] = std::min((r1).abcd[2], (r2).abcd[2]); \
	(r).abcd[3] = std::max((r1).abcd[3], (r2).abcd[3]); \
} while (0)
#define	IS_RECT(r)	((r).abcd[0] < (r).abcd[1] && (r).abcd[2] < (r).abcd[3])

std::vector<rect_t> rect;

int main(void)
{
	{
		int n;
		scanf("%d", &n);
		rect.resize(n);
	}
	for (std::vector<rect_t>::iterator it = rect.begin(); it != rect.end(); ++it) {
		scanf("%d%d%d%d", &it->abcd[0], &it->abcd[1], &it->abcd[2], &it->abcd[3]);
	}

	while (rect.size() > 1) {
		for (std::vector<rect_t>::iterator it = rect.begin(); it != rect.end(); ++it) {
			for (std::vector<rect_t>::iterator jt = rect.begin(); jt != rect.end(); ++jt) {
				if (it != jt) {
					rect_t r;
					INTERSECT_RECT(r, *it, *jt);
					if (IS_RECT(r)) {
						UNION_RECT(*it, *it, *jt);
						*jt = rect.back();
						rect.pop_back();
						goto end_of_loop;
					}
				}
			}
		}
		break;
end_of_loop:
		continue;
	}

	printf("%d\n", rect.size());

	std::sort(rect.begin(), rect.end());
	//std::sort(&rect[0], &rect[n]);

	for (std::vector<rect_t>::iterator it = rect.begin(); it != rect.end(); ++it) {
		printf("%d %d %d %d\n", it->abcd[0], it->abcd[1], it->abcd[2], it->abcd[3]);
	}

	return 0;
}