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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
using namespace std;

const int INF = 1000000000;

struct Interval
{
	int a, b, from, to;
	
	Interval(int _a, int _b, int _from, int _to)
	{
		a = _a; b = _b; from = _from; to = _to;
	}
	
	string toString() const
	{
		char buffer[128];
		sprintf (buffer, "(%d,%d; %d,%d)", a, b, from, to);
		string s = buffer;
		return s;
	}
	
	bool operator< (const Interval &I) const
	{
		if ( a != I.a ) return a<I.a;
		if ( b != I.b ) return b<I.b;
		return make_pair(from,to) < make_pair(I.from,I.to);
	}
};

struct Node
{
	int from, to, a, b;
	bool beginning;
	
	Node() {} 
	Node(int _from, int _to, int _a, int _b, bool _beg)
	{
		from = _from;
		to = _to;
		a = _a;
		b = _b;
		beginning = _beg;
	}
	
	bool operator< (const Node &N) const
	{
		if ( from != N.from ) return from < N.from;
		if ( beginning != N.beginning ) return beginning < N.beginning;
		if ( to != N.to ) return to > N.to;
		return make_pair(a,b) < make_pair(N.a,N.b);
	}
};

bool cmpout ( const Node &A, const Node &B )
{
	if ( A.beginning != B.beginning ) return A.beginning > B.beginning;
	if ( A.from != B.from ) return A.from < B.from;
	if ( A.to != B.to ) return A.to < B.to;
	return make_pair(A.a,A.b) < make_pair(B.a,B.b);
}

vector<Node> V,W;

inline Interval join(const Interval A, const Interval B)
{
	return Interval( min(A.a,B.a), max( A.b, B.b ),
					 min(A.from,B.from), max(A.to,B.to) );
}

set<Interval> S;

void insert(Interval I)
{
	//fprintf(stderr, "insert %s\n", I.toString().c_str());
	S.insert(I);
}

int counter = 0;
void erase(set<Interval>::iterator i, bool notify=false)
{
	//fprintf(stderr, "erase %s\n", i->toString().c_str());
	if (notify)
	{
		//fprintf(stderr, "country!!!\n");
		W.push_back( Node(-i->b, -i->a, i->from, i->to, 1) );
		W.push_back( Node(-i->a, -i->b, i->from, i->to, 0) );
	}
	S.erase(i);
}


void beginning(const Node &D)
{
	Interval I(D.a, D.b, D.from, D.to);
	//fprintf(stderr, "Beginning %s\n", I.toString().c_str());
	if ( S.empty() ) { insert(I); return; }
	
	set<Interval>::iterator it1, it2;
	
	it1 = S.lower_bound(Interval(I.a,INF,0,0));
	it2 = S.lower_bound(Interval(I.b,INF,0,0) );
	
	if ( it2 == S.begin() ) { insert(I); return; }
	if ( it1 == S.end() ) // odcinek I jest największy
	{
		it1--;
		Interval X = *it1;
		
		if ( X.b <= I.a ) insert(I);
		else {
			erase( it1 );
			insert( join(X,I) );
		}
		return;
	}	
	
	it2--;
	I.b = max( I.b, it2->b );
	if ( it1 != S.begin() )
	{
		set<Interval>::iterator tmp = it1;
		tmp--;
		if ( tmp->b > I.a ) it1 = tmp;
	}
	I.a = min( I.a, it1->a );
	it2++;
	while (it1 != it2)
	{
		I.from = min( I.from, it1->from );
		I.to   = max( I.to  , it1->to   );
		set<Interval>::iterator tmp = it1;
		it1++;
		erase(tmp);
	}
	insert(I);
}

void ending(const Node D)
{
	Interval I(D.a, D.b, D.to, D.from);
	//fprintf(stderr, "Ending %s\n", I.toString().c_str());
	set<Interval>::iterator it = S.lower_bound( Interval(I.a,INF,0,0) );
	
	if ( it == S.begin() ) return;
	it--;
	if ( it->b <= I.a )  return;
	if ( it->to > I.to ) return;
	erase(it, true);
}


int main()
{
	int n;
	scanf("%d", &n);
	for (int i=0; i<n; i++)
	{
		int x1,x2,y1,y2;
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		V.push_back( Node(x1,x2,y1,y2,1) );
		V.push_back( Node(x2,x1,y1,y2,0) );
	}
	
	n = V.size();
	int m = n+1;
	counter=0;
	while (n<m || counter%4 != 0)
	{
		//fprintf(stderr, "***************  counter=%d (#countries=%d)\n", counter, m/2);
		m = n;
		sort( V.begin(), V.end() ); 
		S.clear(); 
		for (auto D : V) if (D.beginning) beginning(D); else ending(D);
		swap(V,W);
		W.resize(0);
		n = V.size();
		counter++;
		//if ( n==m && counter%4==0) break;
	}
	sort( V.begin(), V.end() , cmpout );
	printf("%d\n", (int) V.size()/2);
	for (int i=0; i<(int) V.size()/2; i++)
	{
		printf("%d %d %d %d\n", V[i].from, V[i].to, V[i].a, V[i].b);
	}
	return 0;
}