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
//Jakub "Cubix651" Cisło
//Zadanie: Plemiona
#include <cstdio>
#include <algorithm>

const int MAXN = 100002;
typedef std::tuple<int, int, int, int> Four;

struct Rect
{
	int x1, x2, y1, y2;
	Rect(int a=0, int b=0, int c=0, int d=0)
	{
		x1 = a;
		x2 = b;
		y1 = c;
		y2 = d;
	}

	operator Four ()
	{
		return std::make_tuple(x1, x2, y1,y2);
	}
};

std::vector<Rect> rects;

bool intersect(const Rect& a, const Rect& b)
{
	if(a.x2 > b.x1 && b.x2 > a.x1 && a.y2 > b.y1 && b.y2 > a.y1)
		return true;
	return false;
}

Rect join(const Rect& a, const Rect& b)
{
	return Rect(std::min(a.x1, b.x1), std::max(a.x2, b.x2), std::min(a.y1, b.y1), std::max(a.y2, b.y2));
}

void swap(Rect* a, Rect* b)
{
	Rect* tmp = a;
	(*a) = (*b);
	(*b) = (*tmp);
}

int n;

int main()
{
	scanf("%d", &n);
	rects.resize(n);
	int a, b, c, d;
	for(int i = 0; i < n; ++i)
	{
		scanf("%d%d%d%d", &a, &b, &c, &d);
		rects[i] = Rect(a, b, c, d);
	}
	next:
	for(int i = 0; i < n; ++i)
	{
		for(int j = i+1; j < n; ++j)
		{
			if(intersect(rects[i], rects[j]))
			{
				rects[i] = join(rects[i], rects[j]);
				swap(&rects[j], &rects[n-1]);
				n--;
				rects.pop_back();
				goto next;
			}
		}
	}
	printf("%d\n", n);
	std::sort(rects.begin(), rects.end(), [](Rect a, Rect b){return Four(a) < Four(b);});
	for(auto r : rects)
	{
		printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2);
	}
	return 0;
}