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
// Grzegorz Bukowiec
// PA 2014 - Plemiona

#include <iostream>
#include <algorithm>
using namespace std;

struct Point
{
	int x, y;
};

struct Tribe
{
	Point a, b;
};

bool active(Tribe A)
{
	if (A.a.x == -1) {
		return false;
	} else {
		return true;
	}
}

void setInactive(Tribe &A)
{
	A.a.x = -1;
}

int min(int a, int b)
{
	if (a < b) {
		return a;
	} else {
		return b;
	}
}

int max(int a, int b)
{
	if (a < b) {
		return b;
	} else {
		return a;
	}
}

bool compareTribes(const Tribe &A, const Tribe &B)
{
	if (min(A.a.x, A.a.y) != min(B.a.x, B.a.y)) {
		return min(A.a.x, A.a.y) < min(B.a.x, B.a.y);
	} else {
		return max(A.a.x, A.a.y) < max(B.a.x, B.a.y);
	}
}

bool compareCountries(const Tribe &A, const Tribe &B)
{
	if (A.a.x == -1) {
		return false;
	}
	if (B.a.x == -1) {
		return true;
	}
	if (A.a.x != B.a.x) {
		return A.a.x < B.a.x;
	} else if (A.b.x != B.b.x) {
		return A.b.x < B.b.x;
	} else  if (A.a.y != B.a.y) {
		return A.a.y < B.a.y;
	} else {
		return A.b.y < B.b.y;
	}
}

bool intersecting(Tribe A, Tribe B)
{
	if (active(A) && active(B)) {
		return ((A.b.x > B.a.x && A.b.y > B.a.y && A.a.x < B.b.x && A.a.y < B.b.y) || (A.a.x > B.b.x && A.a.y > B.b.y && A.b.x < B.a.x && A.b.y < B.a.y) || 
			(B.b.x > A.a.x && B.b.y > A.a.y && B.a.x < A.b.x && B.a.y < A.b.y) || (B.a.x > A.b.x && B.a.y > A.b.y && B.b.x < A.a.x && B.b.y < A.a.y));
	} else {
		return false;
	}
}

bool tooFar(Tribe A, Tribe B)
{
	if (min(A.b.x, A.b.y) < min(B.a.x, B.a.y)) {
		return true;
	} else {
		return false;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	int n;
	cin >> n;
	Tribe *tribes = new Tribe[n];
	for (int i = 0; i < n; ++i) {
		cin >> tribes[i].a.x;
		cin >> tribes[i].b.x;
		cin >> tribes[i].a.y;
		cin >> tribes[i].b.y;
	}
	sort(tribes, tribes + n, compareTribes);
	
	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			if (tooFar(tribes[i], tribes[j])) {
				break; // not sure if I can do it
			}
			if (intersecting(tribes[i], tribes[j])) {
				tribes[j].a.x = min(tribes[i].a.x, tribes[j].a.x);
				tribes[j].a.y = min(tribes[i].a.y, tribes[j].a.y);
				tribes[j].b.x = max(tribes[i].b.x, tribes[j].b.x);
				tribes[j].b.y = max(tribes[i].b.y, tribes[j].b.y);

				setInactive(tribes[i]);

				for (int k = 0; k < i; ++k) {
					if (intersecting(tribes[j], tribes[k])) {
						tribes[j].a.x = min(tribes[k].a.x, tribes[j].a.x);
						tribes[j].a.y = min(tribes[k].a.y, tribes[j].a.y);
						tribes[j].b.x = max(tribes[k].b.x, tribes[j].b.x);
						tribes[j].b.y = max(tribes[k].b.y, tribes[j].b.y);

						setInactive(tribes[k]);
						k = -1;
					}
				}
				break;
			}
		}
	}

	sort(tribes, tribes + n, compareCountries);

	int countries = 0;
	for (int i = 0; i < n; ++i) {
		if (active(tribes[i])) {
			++countries;
		} else {
			break;
		}
	}

	cout << countries << endl;
	for (int i = 0; i < n; ++i) {
		if (active(tribes[i])) {
			cout << tribes[i].a.x << " " << tribes[i].b.x << " " << tribes[i].a.y << " " << tribes[i].b.y << endl;
		}
	}

	return 0;
}