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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include<algorithm>
#include<set>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define ull long long int
using namespace std;
#define DIVIDE_TRS 11
#define PARTS 6
#define PARTS1 5
class R
{
	public:
		R(int top,int right,int bottom, int left):right(right),left(left),top(top),bottom(bottom),valid(1){}
		R(){}
		int right,left,top,bottom;
	int valid;
	int invalidate(){valid=0;}
	int isValid(){return valid;}
	void mergeWith(R r){
		top = max(top, r.top);
		right = max(right, r.right);
		bottom = min(bottom, r.bottom);
		left = min(left, r.left);
	}
};
/*ostream& operator<<(ostream& os, const R r)
{
	os<<"[left:"<<r.left<<", right:"<<r.right<<", bottom:"<<r.bottom<<", top:"<<r.top<<"]";
	return os;
}*/
struct CR
{
	int operator()(const R a, const R b){
		return a.left!=b.left ? a.left<b.left :
			a.right!=b.right ? a.right<b.right :
			a.bottom!=b.bottom ? a.bottom<b.bottom :
			a.top<b.top;
	}
};
class RT
{
	public:
		RT(int l,int r):from(l),to(r),left(NULL),right(NULL),havePartials(0){}
	RT*left,*right;
	set<int> entires;
	int havePartials;
	int from,to;
	void insert(R r,int number){
		if(!amIInterestedIn(r))return;
		if(amICoveredBy(r)){
			entires.insert(number);
			return;
		}
		ensureLeftAndRightExist(r);
		havePartials++;
		left->insert(r, number);
		right->insert(r, number);
	}
	void erase(R r,int number){
		if(empty())return;
		if(!amIInterestedIn(r))return;
		if(amICoveredBy(r)){
			entires.erase(number);
			return;
		}
		havePartials--;
		//children were iniated by insert, I hope
		left->erase(r, number);
		right->erase(r, number);
	}
	int takeAnyOverlappingWith(R r){
		if(empty())return -1;
		if(!amIInterestedIn(r))return -1;
		if(!entires.empty())
			return *(entires.begin());
		if(amICoveredBy(r)){
			return giveMeAny();
		}
		int ret=-1;
		if(left!=NULL)ret = left->takeAnyOverlappingWith(r);
		if(ret==-1 && right!=NULL)ret = right->takeAnyOverlappingWith(r);
		return ret;
	}
	int giveMeAny(){
		if(empty())return -1;
		if(!entires.empty())return *(entires.begin());
		int ret=-1;
		if(havePartials){
			if(left!=NULL)ret=left->giveMeAny();
			if(ret==-1 && right!=NULL)ret = right->giveMeAny();
		}
		return ret;
	}
	void ensureLeftAndRightExist(R r){
		if(left!=NULL && right!=NULL)return;
		int middle = calculateMiddle(r.bottom, r.top);
		if(left==NULL)left = new RT(from,middle);
		if(right==NULL)right = new RT(middle,to);
	}
	int calculateMiddle(int left, int right){
		int middle = (from+to)/2;
		int range=to-from;
		if(range>DIVIDE_TRS){
			int minMid = from + range/PARTS;
			int maxMid = from + range/PARTS*PARTS1;
			int diffWithLeft = abs(left-middle);
			int diffWithRight = abs(right-middle);
			if(diffWithLeft<diffWithRight){
				if(minMid<=left && left<=maxMid){
					middle = left;
				}
			}else{
				if(minMid<=right && right<=maxMid){
					middle = right;
				}
			}
		}
		return middle;
	}
	int amICoveredBy(R r){
		return r.bottom<=from && to<=r.top;
	}
	int amIInterestedIn(R r){
		return from<r.top  && r.bottom<to;
	}
	int empty(){
		return havePartials==0 && entires.empty();
	}
};
class AreaTree
{
	public:
		AreaTree(int from,int to):left(NULL),right(NULL),from(from),to(to),partials(0,1000000),entires(0,1000000){}
		AreaTree*left;
		AreaTree*right;
		int from,to;
		RT partials;
		RT entires;
		void insert(R r, int number){
			if(!amIInterestedIn(r))return;
			if(amICoveredBy(r)){
				entires.insert(r, number);
				return;
			}
			ensureLeftAndRightExist(r);
			partials.insert(r, number);
			left->insert(r, number);
			right->insert(r, number);
		}
		void erase(R r,int number){
			if(empty())return;
			if(!amIInterestedIn(r))return;
			if(amICoveredBy(r)){
				entires.erase(r, number);
				return;
			}
			partials.erase(r, number);
			left->erase(r, number);
			right->erase(r, number);
		}
		void ensureLeftAndRightExist(R r){
			if(left!=NULL && right!=NULL)return;
			int middle = calculateMiddle(r.left, r.right);
			if(left==NULL)left = new AreaTree(from,middle);
			if(right==NULL)right = new AreaTree(middle,to);
		}
		int calculateMiddle(int left, int right){
			int middle = (from+to)/2;
			int range=to-from;
			if(range>DIVIDE_TRS){
				int minMid = from + range/PARTS;
				int maxMid = from + range/PARTS*PARTS1;
				int diffWithLeft = abs(left-middle);
				int diffWithRight = abs(right-middle);
				if(diffWithLeft<diffWithRight){
					if(minMid<=left && left<=maxMid){
						middle = left;
					}
				}else{
					if(minMid<=right && right<=maxMid){
						middle = right;
					}
				}
			}
			return middle;
		}
		int takeAnyOverlappingWith(R r){
			if(empty())return -1;
			if(!amIInterestedIn(r))return -1;
			int ret=-1;
			if(!entires.empty()){
				ret = entires.takeAnyOverlappingWith(r);
				if(ret!=-1)return ret;
			}
			if(amICoveredBy(r)){
				if(!partials.empty()){
					return partials.takeAnyOverlappingWith(r);
				}
			}
			if(left!=NULL)ret = left->takeAnyOverlappingWith(r);
			if(ret==-1 && right!=NULL)ret = right->takeAnyOverlappingWith(r);
			return ret;
		}
		int amICoveredBy(R r){
			return r.left<=from && to<=r.right;
		}
		int amIInterestedIn(R r){
			return from<r.right && r.left<to;
		}
		int empty(){
			return entires.empty() && partials.empty();
		}
};

int main(){
	ios_base::sync_with_stdio(0);
	int n;
	cin>>n;
	AreaTree theTree(0,1000000);
	//dla każdego prostokąta
	R*r=new R[n];
	int valid=n;
	FOR(i,n){
		int x1,x2,y1,y2;
		cin>>x1>>x2>>y1>>y2;
		r[i]=R(max(y1,y2),max(x1,x2),min(y1,y2),min(x1,x2));
		int overlapping;
		//dopóki obecny nakłada się na jakiś poprzedni
		//stała większa niż mur chiński
		while((overlapping=theTree.takeAnyOverlappingWith(r[i]))!=-1){
			
			//wyjmij poprzedni
			theTree.erase(r[overlapping],overlapping);
			r[i].mergeWith(r[overlapping]);
			r[overlapping].invalidate();
			--valid;
			//scal poprzednie z obecnym
		}
		theTree.insert(r[i],i);
	}
	//posortuj
	CR rc;
	sort(r,r+n,rc);

	//wypisz, które zostały
	cout<<valid<<endl;

	FOR(i,n){
		if(r[i].isValid()){
			cout<<r[i].left<<" "<<r[i].right<<" "<<r[i].bottom<<" "<<r[i].top<<endl;
		}
	}
	return 0;
}