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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>	
#include <set>
using namespace std;

class R{
	public:
		int x1, x2, y1, y2;
		R(int x1, int x2, int y1, int y2):x1(x1), x2(x2), y1(y1), y2(y2){};
		R(){};
		/*bool operator< (R const &q){
			if (x1<q.x1) return true;
			if ((x1==q.x1) && (x2<q.x2)) return true;
			if ((x1==q.x1) && (x2==q.x2) && (y1<q.y1)) return true;
			if ((x1==q.x1) && (x2==q.x2) && (y1==q.y1)) return y2 < q.y2;
			return false;
		}*/
};


bool sortFun(R a, R b){
	if (a.x1<b.x1) return true;
	if ((a.x1==b.x1) && (a.x2<b.x2)) return true;
	if ((a.x1==b.x1) && (a.x2==b.x2) && (a.y1<b.y1)) return true;
	if ((a.x1==b.x1) && (a.x2==b.x2) && (a.y1==b.y1)) return a.y2 < b.y2;
	return false;
}

R robDuzy(R a, R b){
	return R(min(a.x1, b.x1), max(a.x2, b.x2), min(a.y1, b.y1), max(a.y2, b.y2));
}


bool czyOdcinkiMajaCzWsp(int a1, int a2, int b1, int b2){
	if (a1<b1) return a2 > b1;
	else if (a1>b1) return a1<b2;
	else return true;		
}

bool czyPrzecinaja(R a, R b){
	bool first = czyOdcinkiMajaCzWsp(a.x1, a.x2, b.x1, b.x2);
	bool second = czyOdcinkiMajaCzWsp(a.y1, a.y2, b.y1, b.y2);	
	return first && second;
}

int main(){
	int n;
	scanf("%d", &n);
	vector<R> ple;
	set<R> pleSet;
	for (int i=0 ; i<n ; i++){
		int x1, x2, y1, y2;
		scanf("%d", &x1); scanf("%d", &x2); scanf("%d", &y1); scanf("%d", &y2);
		R r = R(x1, x2, y1, y2);
		//vector<R>::iterator low=lower_bound(ple.begin(), ple.end(), r);
		//cout<<low - ple.begin()<<endl;
		//ple.insert(low, r);		
		ple.push_back(r);		
	}
	while(true){
		bool zmiana = false;
		for (int i=0 ; i<ple.size() ; i++){
			for (int j=0 ; j<ple.size() ; j++){
				if (i<j){
					bool przecinajaSie = czyPrzecinaja(ple[i], ple[j]);
					if (przecinajaSie){
						zmiana = true;
						R tmp = robDuzy(ple[i], ple[j]);
						ple.erase(ple.begin()+j);
						ple.erase(ple.begin()+i);
						ple.push_back(tmp);
					}					
				}
			}
		}
		if (!zmiana) break;
	}
	sort(ple.begin(), ple.end(), sortFun);	
	cout<<ple.size()<<endl;	
	for (int i=0 ; i<ple.size() ; i++){
		cout<<ple[i].x1<<" "<<ple[i].x2<<" "<<ple[i].y1<<" "<<ple[i].y2<<endl;
	}
	return 0;
}