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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;

inline int Pow2(int exp) { return 1 << exp; }

template<typename T>
inline T Pow2(int exp) { return (T)1 << exp; }

int Log2(ULL x, bool ceil)
{
	unsigned long r;
#ifdef __GNUC__
	r = 63 - __builtin_clzll(x);
#else
	_BitScanReverse64(&r, x);
#endif
	return r + (ceil && x > Pow2<ULL>(r));
}

struct Tree {
	int D;
	vector<set<int> > d;

	Tree(int n) : D(Pow2(Log2(n, true))), d(D*2) { }
	void Insert(int v, int nr);
	void Erase(int v, int nr);
	VI Get(int v1, int v2);
};

void Tree::Insert(int v, int nr)
{
	int ix = v + D;
	while (ix != 0) {
		d[ix].insert(nr);
		ix /= 2;
	}
}

void Tree::Erase(int v, int nr)
{
	int ix = v + D;
	while (ix != 0) {
		d[ix].erase(nr);
		ix /= 2;
	}
}

VI Tree::Get(int v1, int v2)
{
	int ix1 = v1 + D;
	int ix2 = v2 + D;
	VI res(ALL(d[ix1]));
	if (ix1 != ix2) {
		res.insert(res.end(), ALL(d[ix2]));
	}
	while (ix1/2 != ix2/2) {
		if (ix1%2 == 0) {
			res.insert(res.end(), ALL(d[ix1+1]));
		}
		if (ix2%2 == 1) {
			res.insert(res.end(), ALL(d[ix2-1]));
		}
		ix1 /= 2;
		ix2 /= 2;
	}
	sort(ALL(res));
	res.erase(unique(ALL(res)), res.end());
	return res;
}

struct IntTree {
	int D;
	VI d;

	IntTree(int n) : D(Pow2(Log2(n, true))), d(D*2) { }
	void Add(int v1, int v2, int k);
	bool Any(int v);
};

void IntTree::Add(int v1, int v2, int k)
{
	int ix1 = v1 + D;
	int ix2 = v2 + D;
	d[ix1] += k;
	if (ix1 != ix2) d[ix2] += k;
	while (ix1/2 != ix2/2) {
		if (ix1%2 == 0) d[ix1+1] += k;
		if (ix2%2 == 1) d[ix2-1] += k;
		ix1 /= 2;
		ix2 /= 2;
	}
}

bool IntTree::Any(int v)
{
	int ix = v + D;
	while (ix != 0) {
		if (d[ix] != 0) return true;
		ix /= 2;
	}
	return false;
}

struct Rct {
	int x1,x2,y1,y2;
	Rct(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) { }
};

bool ByLex(const Rct& rct1, const Rct& rct2)
{
	if (rct1.x1 != rct2.x1) return rct1.x1 < rct2.x1;
	if (rct1.x2 != rct2.x2) return rct1.x2 < rct2.x2;
	if (rct1.y1 != rct2.y1) return rct1.y1 < rct2.y1;
	return rct1.y2 < rct2.y2;
}

bool ByX1(const Rct& rct1, const Rct& rct2) { return rct1.x1 < rct2.x1; }
bool ByX2Dec(const Rct& rct1, const Rct& rct2) { return rct1.x2 > rct2.x2; }
bool ByY1(const Rct& rct1, const Rct& rct2) { return rct1.y1 < rct2.y1; }
bool ByY2Dec(const Rct& rct1, const Rct& rct2) { return rct1.y2 > rct2.y2; }

struct Ev {
	int x,nr;
	bool bgn;
	Ev(int x, int nr, bool bgn) : x(x), nr(nr), bgn(bgn) { }
};

bool ByXInc(const Ev& ev1, const Ev& ev2)
{
	if (ev1.x != ev2.x) return ev1.x < ev2.x;
	return ev1.bgn > ev2.bgn;
}

bool ByXDec(const Ev& ev1, const Ev& ev2)
{
	if (ev1.x != ev2.x) return ev1.x > ev2.x;
	return ev1.bgn > ev2.bgn;
}

struct Problem {
	int n,W,H;
	vector<Rct> rcts;
	vector<bool> del;
	Problem(int n) : n(n), del(n) { }
	vector<Rct> Go();
	bool SweepX1();
	bool SweepX2();
	bool SweepY1();
	bool SweepY2();
	bool SweepImpl(const vector<Ev>& evs, bool inc);
	void SwapXY();
	void ElimCont();
};

vector<Rct> Problem::Go()
{
	while (1) {
		bool any = false;
		any |= SweepX1();
		any |= SweepX2();
		any |= SweepY1();
		any |= SweepY2();
		if (!any) break;
	}
	ElimCont();
	vector<Rct> res;
	REP(i,n) {
		if (!del[i]) res.push_back(rcts[i]);
	}
	return res;
}

bool Problem::SweepX1()
{
	vector<Ev> evs;
	REP(i,n) {
		if (del[i]) continue;
		evs.push_back(Ev(rcts[i].x1*2+1, i, true));
		evs.push_back(Ev(rcts[i].x2*2-1, i, false));
	}
	sort(ALL(evs), ByXInc);
	return SweepImpl(evs, true);
}

bool Problem::SweepX2()
{
	vector<Ev> evs;
	REP(i,n) {
		if (del[i]) continue;
		evs.push_back(Ev(rcts[i].x1*2+1, i, false));
		evs.push_back(Ev(rcts[i].x2*2-1, i, true));
	}
	sort(ALL(evs), ByXDec);
	return SweepImpl(evs, false);
}

bool Problem::SweepY1()
{
	SwapXY();
	bool res = SweepX1();
	SwapXY();
	return res;
}

bool Problem::SweepY2()
{
	SwapXY();
	bool res = SweepX2();
	SwapXY();
	return res;
}

void Problem::SwapXY()
{
	REP(i,n) {
		if (del[i]) continue;
		swap(rcts[i].x1, rcts[i].y1);
		swap(rcts[i].x2, rcts[i].y2);
	}
	swap(W, H);
}

bool Problem::SweepImpl(const vector<Ev>& evs, bool inc)
{
	bool res = false;
	Tree tree(2*H);
	FOREACH(it1,evs) {
		if (del[it1->nr]) continue;
		Rct rct = rcts[it1->nr];
		bool ins = it1->bgn;
		if (!it1->bgn) {
			tree.Erase(rct.y1*2+1, it1->nr);
			tree.Erase(rct.y2*2-1, it1->nr);
		}
		VI r = tree.Get(rct.y1*2+1, rct.y2*2-1);
		del[it1->nr] = true;
		int nr = it1->nr;
		FOREACH(it2,r) {
			if (inc && rcts[*it2].x2 >= rct.x2) nr = *it2;
			else if (!inc && rcts[*it2].x1 <= rct.x1) nr = *it2;
			rct.x1 = min(rct.x1, rcts[*it2].x1);
			rct.x2 = max(rct.x2, rcts[*it2].x2);
			rct.y1 = min(rct.y1, rcts[*it2].y1);
			rct.y2 = max(rct.y2, rcts[*it2].y2);
			del[*it2] = true;
			tree.Erase(rcts[*it2].y1*2+1, *it2);
			tree.Erase(rcts[*it2].y2*2-1, *it2);
			ins = true;
			res = true;
		}
		rcts[nr] = rct;
		del[nr] = false;
		if (ins) {
			tree.Insert(rct.y1*2+1, nr);
			tree.Insert(rct.y2*2-1, nr);
		}
	}
	return res;
}

struct ECEv {
	int x,y1,nr;
	bool bgn;
	ECEv(int x, int y1, int nr, bool bgn) : x(x), y1(y1), nr(nr), bgn(bgn) { }
};

bool operator<(const ECEv& ev1, const ECEv& ev2)
{
	if (ev1.x != ev2.x) return ev1.x < ev2.x;
	if (ev1.bgn != ev2.bgn) return ev1.bgn > ev2.bgn;
	return ev1.y1 < ev2.y1;
}

void Problem::ElimCont()
{
	vector<ECEv> evs;
	REP(i,n) {
		if (del[i]) continue;
		evs.push_back(ECEv(rcts[i].x1*2+1, rcts[i].y1*2+1, i, true));
		evs.push_back(ECEv(rcts[i].x2*2-1, rcts[i].y1*2+1, i, false));
	}
	sort(ALL(evs));
	IntTree tree(2*H);
	FOREACH(it,evs) {
		Rct rct = rcts[it->nr];
		if (it->bgn) {
			if (tree.Any(it->y1)) del[it->nr] = true;
			else tree.Add(rct.y1*2+1, rct.y2*2-1, 1);
		}
		else if (!del[it->nr]) tree.Add(rct.y1*2+1, rct.y2*2-1, -1);
	}
}

int main(int argc, char** argv)
{
	int n;
	scanf("%d", &n);
	Problem p(n);
	REP(i,n) {
		int x1,x2,y1,y2;
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		p.rcts.push_back(Rct(x1,x2,y1,y2));
	}
	VI xs(2*n), ys(2*n);
	REP(i,n) {
		xs[2*i] = p.rcts[i].x1;
		xs[2*i+1] = p.rcts[i].x2;
		ys[2*i] = p.rcts[i].y1;
		ys[2*i+1] = p.rcts[i].y2;
	}
	sort(ALL(xs));
	xs.erase(unique(ALL(xs)), xs.end());
	p.W = SIZE(xs);
	sort(ALL(ys));
	ys.erase(unique(ALL(ys)), ys.end());
	p.H = SIZE(ys);
	REP(i,n) {
		p.rcts[i].x1 = lower_bound(ALL(xs), p.rcts[i].x1) - xs.begin();
		p.rcts[i].x2 = lower_bound(ALL(xs), p.rcts[i].x2) - xs.begin();
		p.rcts[i].y1 = lower_bound(ALL(ys), p.rcts[i].y1) - ys.begin();
		p.rcts[i].y2 = lower_bound(ALL(ys), p.rcts[i].y2) - ys.begin();
	}
	vector<Rct> res = p.Go();
	sort(ALL(res), ByLex);
	printf("%d\n", SIZE(res));
	FOREACH(it,res) printf("%d %d %d %d\n", xs[it->x1], xs[it->x2], ys[it->y1], ys[it->y2]);

#ifdef _DEBUG
	system("pause");
#endif
	return 0;
}