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

using namespace std;

struct point
{
	int Lx, Ly, Px, Py;
	point(int a, int b, int c, int d)
	{
		this -> Lx = a;
		this -> Ly = c;
		this -> Px = b;
		this -> Py = d;
	}
};

bool compare(point a, point b)
{
	if(a.Lx == b.Lx)
		return a.Ly < b.Ly;
	return a.Lx < b.Lx;
}

vector <point> v;
vector <point> temp;

bool isCuttingThrough(point a , point b)
{
	if(b.Lx < a.Px and b.Lx >= a.Lx and b.Ly >= a.Ly and b.Ly < a.Py)
		return true;
		//cout<<"1"<<endl;
	else if(b.Lx < a.Px and b.Lx >= a.Lx and b.Py <= a.Py and b.Py > a.Ly)
		return true;
		//cout<<"2";
	else if(b.Px > a.Lx and b.Px <= a.Px and b.Py <= a.Py and b.Py > a.Ly)
		return true;
		//cout<<"3";
	else if(b.Px >= a.Lx and b.Px < a.Px and b.Ly > a.Ly and b.Ly <= a.Py)
		return true;
	else 
		return false;
	
}

bool isInside(point a, point b)
{
	if(b.Lx >= a.Lx and b.Px <= a.Px and b.Ly >= a.Ly and b.Py <= a.Py)
		return true;
	return false;
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n, x1,x2,y1,y2;
	cin >> n;
	for(int i = 0 ; i < n; ++i)
	{
		cin >> x1 >> x2 >> y1 >> y2;
		if(x1 < x2 and y1 < y2)
			v.push_back(point(x1, x2, y1, y2));
		else if(x1 > x2 and y1 > y2)
			v.push_back(point(x2, x1, y2, y1));
		else if(x1 > x2 and y1 < y2)
			v.push_back(point(x2, x1, y1, y2));
		else if(x1 < x2 and y1 > y2)
			v.push_back(point(x1, x2, y2, y1));
	}
	while(1)
	{
		//temp.clear();
		//sort(v.begin(), v.end(), compare);
		bool nothingChanged = true;
		for(int i = 0; i < v.size() and nothingChanged; ++i)
		{
			//swap(v[i],v.back());
			bool is_cut = false;
		//	cout<<v[i].Lx<<" "<<v[i].Ly<<" "<<v[i].Px<<" "<<v[i].Py<<endl;
			for (int j = 0; j<v.size() and nothingChanged; ++j)
			{
				//cout<<isCuttingThrough(v[i], v[j])<<" "<< isCuttingThrough(v[j],v[i])<<endl;
				//cout<<i<<" "<<j<<"xd"<<endl;
				if(isCuttingThrough(v[i], v[j]) and i != j)
				{
					temp.push_back(point(
					min(min(v[i].Lx, v[j].Lx), min(v[i].Px, v[j].Px)),
					max(max(v[i].Lx, v[j].Lx), max(v[i].Px, v[j].Px)),
					min(min(v[i].Ly, v[j].Ly), min(v[i].Py, v[j].Py)),
					max(max(v[i].Ly, v[j].Ly), max(v[i].Py, v[j].Py))
						));
					is_cut = true;
					//cout<<v.back().Lx<<" "<<v.back().Ly<<" "<<v.back().Px<<" "<<v.back().Py<<endl;
					
					swap(v[j],v.back());
					v.pop_back();
					swap(v[i],v.back());
					v.pop_back();
					nothingChanged = false;
				}
			}
		}
		for(int i = 0; i< temp.size(); ++i)
		{
			//cout<<temp[i].Lx<<" "<<temp[i].Ly<<" "<<temp[i].Px<<" "<<temp[i].Py<<endl;
		}
		if(nothingChanged)
		{
			if(temp.empty())
				break;
			for(int i = 0; i< v.size(); ++i)
				temp.push_back(v[i]);
			v = temp;
			
			
			temp.clear();
		}

	}

	//cout<<"asdsad"<<endl;
	sort(v.begin(), v.end(), compare);
	cout<<v.size()<<endl;
	for(int i= 0; i<v.size(); ++i)
	{
		cout<<v[i].Lx<<" "<<v[i].Px<<" "<<v[i].Ly<<" "<<v[i].Py<<endl;
	}

	

}