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
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

typedef pair<int,int> PII;

const int MaxN = 100005,
          Infty = 1010101010,
          CodeOK = -1337;


struct Rectangle {
	int x1, y1, x2, y2;
	
	Rectangle() {}
	Rectangle(int _x1, int _x2, int _y1, int _y2) :
		x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
	Rectangle(FILE *fp){
		fscanf(fp, "%d%d%d%d", &x1, &x2, &y1, &y2);
	}
	Rectangle operator|(const Rectangle R) const {
		return Rectangle(min(x1, R.x1),
		                 max(x2, R.x2),
		                 min(y1, R.y1),
		                 max(y2, R.y2));
	}
	Rectangle& operator|=(const Rectangle R){
		x1 = min(x1, R.x1);
		y1 = min(y1, R.y1);
		x2 = max(x2, R.x2);
		y2 = max(y2, R.y2);
		return *this;
	}
	bool operator<(const Rectangle R) const {
		if(x1 == R.x1){
			if(x2 == R.x2){
				return y1 < R.y1;
			} else return x2 < R.x2;
		} else return x1 < R.x1;
	}
	
	void output(){
		printf("%d %d %d %d\n", x1, x2, y1, y2);
	}
};

bool compare_ld(const Rectangle &R1, const Rectangle &R2){
	if(R1.x1 == R2.x1)
		return R1.y1 < R2.y1;
	else
		return R1.x1 < R2.x1;
}

int N;
Rectangle R[MaxN];
bool exists[MaxN];


void input(){
	scanf("%d", &N);
	
	for(int i = 0; i < N; i++)
		R[i] = Rectangle(stdin);
	
	fill(exists, exists+N+1, true);
}


/************************ DRZEWO PRZEDZIALOWE *************************/
/** (wariacja na temat; pozwala na wrzucanie i usuwanie przedzialow
 * oraz znajdowanie max na przedziale **/
struct SegTree {
	static const int MaxSize = (1<<21),
	                 Base = (1<<20);
	
	// A - liczby na przedziale bazowym; B - max na dzieciach
	// P - numer liczby maksymalnej
	set<PII> A[MaxSize];
	PII B[MaxSize];
	
	
	SegTree(){
		fill(B, B+MaxSize, PII(CodeOK, CodeOK));
	}
	
	PII get_last(int num){
		if(A[num].size())
			return *A[num].rbegin();
		else
			return {CodeOK, CodeOK};
	}
	
	// dodaj przedzial [L,R] o wartosci v; bedzie to najwieksza dotad
	// wartosc na tym przedziale
	// num - numer tej liczby (czyli potem - prostokata)
	void add(int L, int R, int v, int num){
		L += Base; R += Base;
		
		A[L].insert({v,num}); B[L] = max(B[L],PII(v, num));
		if(L != R){ A[R].insert({v,num}); B[R] = max(B[R],PII(v, num)); }
		
		while(L/2 != R/2){
			if(L % 2 == 0){ A[L+1].insert({v,num}); B[L+1] = max(B[L+1],PII(v, num)); }
			if(R % 2 == 1){ A[R-1].insert({v,num}); B[R-1] = max(B[R-1],PII(v, num)); }
			L /= 2; R /= 2;
			B[L] = max(get_last(L), max(B[2*L], B[2*L+1]));
			B[R] = max(get_last(R), max(B[2*R], B[2*R+1]));
		}
		L /= 2;
		while(L){
			B[L] = max(get_last(L), max(B[2*L], B[2*L+1]));
			L /= 2;
		}
	}
	
	// usun przedzial [L,R] (o wartosci v, numerze num); wiadomo, ze to
	// najwieksza wartosc na tym przedziale
	void del(int L, int R, int v, int num){
		L += Base; R += Base;
		
		A[L].erase({v,num}); B[L] = get_last(L);
		if(L != R){ A[R].erase({v,num}); B[R] = get_last(R); }
		
		while(L/2 != R/2){
			if(L % 2 == 0){
				A[L+1].erase({v,num}); B[L+1] = get_last(L+1);
				if(L < Base){
					B[L+1] = max(B[L+1], max(B[2*(L+1)], B[2*(L+1)+1]));
				}
			}
			if(R % 2 == 1){
				A[R-1].erase({v,num}); B[R-1] = get_last(R-1);
				if(R < Base){
					B[R-1] = max(B[R-1], max(B[2*(R-1)], B[2*(R-1)+1]));
				}
			}
			L /= 2; R /= 2;
			B[L] = max(get_last(L), max(B[2*L], B[2*L+1]));
			B[R] = max(get_last(R), max(B[2*R], B[2*R+1]));
		}
		L /= 2;
		while(L){
			B[L] = max(get_last(L), max(B[2*L], B[2*L+1]));
			L /= 2;
		}
	}
	
	// uzyskaj maksimum na przedziale [L,R]
	PII get_max(int L, int R){
		L += Base; R += Base;
		PII res = max(get_last(L), get_last(R));
		
		while(L/2 != R/2){
			if(L % 2 == 0) res = max(res, B[L+1]);
			if(R % 2 == 1) res = max(res, B[R-1]);
			L /= 2; R /= 2;
			res = max(res, get_last(L));
			res = max(res, get_last(R));
		}
		L /= 2;
		while(L){
			res = max(res, get_last(L));
			L /= 2;
		}
		
		return res;
	}
};

/**********************************************************************/



SegTree endRight; // koniec z prawej strony


int try_add_rect(int num){
	int dn = R[num].y1, up = R[num].y2 - 1,
	    left = R[num].x1, right = R[num].x2 - 1;
	
	PII maxInterval = endRight.get_max(dn, up);
	
	if(maxInterval.first >= left){
		return maxInterval.second;
	}
	
	endRight.add(dn, up, right, num);
	return CodeOK;
}

void del_rect(int num){
	int dn = R[num].y1, up = R[num].y2 - 1,
	    left = R[num].x1, right = R[num].x2 - 1;
	
	endRight.del(dn, up, right, num);
}

int main(){
	input();
	int num = N;
	sort(R, R+N, compare_ld);
	
	
	for(int i = 0; i < N; i++){
		// probujemy dorzucic kolejne prostokaty
		int res = try_add_rect(i);
		
		while(res != CodeOK){
			del_rect(res);
			num--;
			
			R[i] |= R[res];
			exists[res] = false;
			
			res = try_add_rect(i);
		}
	}
	
	
	printf("%d\n", num);
	int ptr = 0;
	for(int i = 0; i < N; i++)
		if(exists[i])
			R[ptr++] = R[i];
	
	sort(R, R+num);
	for(int i = 0; i < num; i++)
		R[i].output();
}