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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <deque>

using namespace std;

struct Rec{
	int x1;
	int x2;
	int y1;
	int y2;
};

int n;
deque <Rec> dek;
deque <Rec> temp;
vector <Rec> wyn;
vector <Rec> tem;

bool cmpr(const Rec &a, const Rec &b){
	if(a.x1<b.x1){
		return true;
	}else if(a.x1==b.x1){
		if(a.x2<b.x2){
			return true;
		}else if(a.x2==b.x2){
			if(a.y1<b.y1){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	return false;
}

bool crossing(Rec a, Rec b){
	if(a.x1==b.x1 && a.x2==b.x2 && a.y1==b.y1 && a.y2==b.y2){ return false; }
	if(max(a.x1,b.x1)>min(a.x2,b.x2)){ return false; }
	if(max(a.y1,b.y1)>min(a.y2,b.y2)){ return false; }
	return true;
}

void readIn(){
	Rec el;
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%d%d%d%d",&el.x1,&el.x2,&el.y1,&el.y2);
		dek.push_back(el);
	}
}

void procOne(){
	Rec el;
	//lykamy
	el=dek[0];
	bool change=true;
	while(change==true){
		change=false;
		int i;
		for(i=1;i<dek.size() && dek[i].x1<=el.x2;++i){
			if(crossing(dek[i],el)){
				el.x1=min(el.x1,dek[i].x1);
				el.x2=max(el.x2,dek[i].x2);
				el.y1=min(el.y1,dek[i].y1);
				el.y2=max(el.y2,dek[i].y2);
				dek[i].x1=el.x1;
				dek[i].x2=el.x2;
				dek[i].y1=el.y1;
				dek[i].y2=el.y2;
				change=true;
			}
		}
		i--;
		for(;i>0;--i){
			if(crossing(dek[i],el)){
				el.x1=min(el.x1,dek[i].x1);
				el.x2=max(el.x2,dek[i].x2);
				el.y1=min(el.y1,dek[i].y1);
				el.y2=max(el.y2,dek[i].y2);
				dek[i].x1=el.x1;
				dek[i].x2=el.x2;
				dek[i].y1=el.y1;
				dek[i].y2=el.y2;
				change=true;
			}
		}
		i++;
	}
	dek[0]=el;
	sort(dek.begin(),dek.end(),cmpr);
}

void procPref(){
	sort(dek.begin(),dek.end(),cmpr);
	int i;
	for(i=0;i<dek.size()-1;++i){
		if(crossing(dek[i],dek[i+1])){
			dek[i+1].x1=min(dek[i+1].x1,dek[i].x1);
			dek[i+1].x2=max(dek[i+1].x2,dek[i].x2);
			dek[i+1].y1=min(dek[i+1].y1,dek[i].y1);
			dek[i+1].y2=max(dek[i+1].y2,dek[i].y2);
		}else{
			temp.push_back(dek[i]);
		}
	}
	temp.push_back(dek[i]);
	dek=temp;
}

void process(){
	//sortujemy
	sort(dek.begin(),dek.end(),cmpr);
	while(!dek.empty()){
		procOne();
		while(dek.size()>1 && dek[0].x1==dek[1].x1 && dek[0].x2==dek[1].x2 && dek[0].y1==dek[1].y1 && dek[0].y2==dek[1].y2){
			dek.pop_front();
		}
		wyn.push_back(dek[0]);
		dek.pop_front();
	}
}

int main(){
	readIn();
	procPref();
	process();
	for(int i=0;i<wyn.size();++i){
		dek.push_back(wyn[i]);
	}
	wyn.clear();
	process();
	printf("%d\n",wyn.size());
	///sort(wyn.begin(),wyn.end(),cmpr);
	for(int i=0;i<wyn.size();++i){
		printf("%d %d %d %d\n",wyn[i].x1,wyn[i].x2,wyn[i].y1,wyn[i].y2);
	}
	return 0;
}