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

using namespace std;

#define COMPRESS_NODES

struct rect
{
	int x0, x1, y0, y1;

	rect() {}
	rect(int x0, int x1, int y0, int y1)
	{
		this->x0 = x0;
		this->x1 = x1;
		this->y0 = y0;
		this->y1 = y1;
	}
	
	struct cmp
	{
		inline bool operator () (const rect& a, const rect& b) const
		{
			int cmp;
			if (cmp = a.x0 - b.x0) return cmp < 0;
			if (cmp = a.x1 - b.x1) return cmp < 0;
			if (cmp = a.y0 - b.y0) return cmp < 0;
			if (cmp = a.y1 - b.y1) return cmp < 0;
			return false;
		}
	};

	inline void mergewith(const rect& other)
	{
		x0 = min(x0, other.x0);
		x1 = max(x1, other.x1);
		y0 = min(y0, other.y0);
		y1 = max(y1, other.y1);
	}

	inline bool contains(const rect& other) const
	{
		return
			x0 <= other.x0 && other.x1 <= x1 &&
			y0 <= other.y0 && other.y1 <= y1;
	}

	inline bool intersects(const rect& other) const
	{
		const bool empty_intersection =
			x1 <= other.x0 || other.x1 <= x0 ||
			y1 <= other.y0 || other.y1 <= y0;
		return !empty_intersection;
	}
};

struct node
{
	node* children[4];
	int rectid;

	node()
	{
		children[0] = 0;
		children[1] = 0;
		children[2] = 0;
		children[3] = 0;
		rectid = -1;
	}
};

struct nodepool
{
	vector<node*> pool;

	node* alloc()
	{
		if (pool.empty())
		{
			const int chunksize = 10000;

			node* chunk = (node*) malloc(sizeof(node) * chunksize);
			for (int i = 0; i < chunksize; i++)
				pool.push_back(chunk + i);
		}

		node* new_node = new(pool.back()) node();
		pool.pop_back();
		return new_node;
	}

	void free(node* n)
	{
		pool.push_back(n);
	}
};

struct quadtree
{
	static const int min_coord = 0;
	static const int max_coord = 1 << 20;

	nodepool nodes;
	node* root;
	rect rootbounds;

	map<int, rect> rectmap;
	int rectidgen;

	quadtree()
	{
		root = nodes.alloc();

		rootbounds.x0 = min_coord;
		rootbounds.x1 = max_coord;
		rootbounds.y0 = min_coord;
		rootbounds.y1 = max_coord;

		rectidgen = 0;
	}

	void add()
	{
		rect r;
		cin >> r.x0 >> r.x1 >> r.y0 >> r.y1;
		merge(r);
	}

	rect curr;
	int foundid;

	void merge(rect& r)
	{
		while (1)
		{
			if (!find(r))
			{
				add(r);
				return;
			}

			const rect& found = rectmap[foundid];
			r.mergewith(found);
			remove(found);
			rectmap.erase(foundid);
		}
	}

	bool find(const rect& r)
	{
		curr = r;
		return find(root, rootbounds);
	}

	inline bool in0(int x2, int y2) const
	{
		return curr.x0 < x2 && curr.y0 < y2;
	}
	inline bool in1(int x2, int y2) const
	{
		return curr.x1 > x2 && curr.y0 < y2;
	}
	inline bool in2(int x2, int y2) const
	{
		return curr.x0 < x2 && curr.y1 > y2;
	}
	inline bool in3(int x2, int y2) const
	{
		return curr.x1 > x2 && curr.y1 > y2;
	}

	bool find(node* n, const rect& bounds)
	{
		if (n->rectid != -1)
		{
			foundid = n->rectid;
#ifdef COMPRESS_NODES
			const rect& found = rectmap[foundid];
			return found.intersects(curr);
#else
			return true;
#endif
		}

		if (bounds.x0 + 1 == bounds.x1)
			return false;

		const int x2 = (bounds.x0 + bounds.x1) >> 1;
		const int y2 = (bounds.y0 + bounds.y1) >> 1;

		if (n->children[0] && in0(x2, y2) &&
			find(n->children[0], rect(bounds.x0, x2, bounds.y0, y2)))
			return true;
		if (n->children[1] && in1(x2, y2) &&
			find(n->children[1], rect(x2, bounds.x1, bounds.y0, y2)))
			return true;
		if (n->children[2] && in2(x2, y2) &&
			find(n->children[2], rect(bounds.x0, x2, y2, bounds.y1)))
			return true;
		if (n->children[3] && in3(x2, y2) &&
			find(n->children[3], rect(x2, bounds.x1, y2, bounds.y1)))
			return true;

		return false;
	}

	void add(const rect& r)
	{
		rectidgen++;
		rectmap[rectidgen] = r;
		curr = r;
		add(root, rootbounds);
	}

	inline void addchild(node* n, int childindex, const rect& bounds)
	{
		if (!n->children[childindex])
			n->children[childindex] = nodes.alloc();
		add(n->children[childindex], bounds);
	}

	void add(node* n, const rect& bounds)
	{
#ifdef COMPRESS_NODES
		const int x2 = (bounds.x0 + bounds.x1) >> 1;
		const int y2 = (bounds.y0 + bounds.y1) >> 1;

		if (n->rectid != -1)
		{
			const rect prevcurr = curr;
			const int prevrectidgen = rectidgen;

			curr = rectmap[n->rectid];
			rectidgen = n->rectid;
			if (in0(x2, y2)) addchild(n, 0, rect(bounds.x0, x2, bounds.y0, y2));
			if (in1(x2, y2)) addchild(n, 1, rect(x2, bounds.x1, bounds.y0, y2));
			if (in2(x2, y2)) addchild(n, 2, rect(bounds.x0, x2, y2, bounds.y1));
			if (in3(x2, y2)) addchild(n, 3, rect(x2, bounds.x1, y2, bounds.y1));
			curr = prevcurr;
			rectidgen = prevrectidgen;

			n->rectid = -1;
		}
		else if (!n->children[0] && !n->children[1] && !n->children[2] && !n->children[3])
		{
			n->rectid = rectidgen;
			return;
		}

		if (in0(x2, y2)) addchild(n, 0, rect(bounds.x0, x2, bounds.y0, y2));
		if (in1(x2, y2)) addchild(n, 1, rect(x2, bounds.x1, bounds.y0, y2));
		if (in2(x2, y2)) addchild(n, 2, rect(bounds.x0, x2, y2, bounds.y1));
		if (in3(x2, y2)) addchild(n, 3, rect(x2, bounds.x1, y2, bounds.y1));
#else
		if (curr.contains(bounds))
		{
			n->rectid = rectidgen;
			return;
		}

		const int x2 = (bounds.x0 + bounds.x1) >> 1;
		const int y2 = (bounds.y0 + bounds.y1) >> 1;

		if (in0(x2, y2)) addchild(n, 0, rect(bounds.x0, x2, bounds.y0, y2));
		if (in1(x2, y2)) addchild(n, 1, rect(x2, bounds.x1, bounds.y0, y2));
		if (in2(x2, y2)) addchild(n, 2, rect(bounds.x0, x2, y2, bounds.y1));
		if (in3(x2, y2)) addchild(n, 3, rect(x2, bounds.x1, y2, bounds.y1));
#endif
	}

	void remove(const rect& r)
	{
		curr = r;
		remove(root, rootbounds);
	}
	
	inline void removechild(node* n, int childindex, const rect& bounds)
	{
		if (n->children[childindex])
		{
			if (remove(n->children[childindex], bounds))
				n->children[childindex] = 0;
		}
	}

	bool remove(node* n, const rect& bounds)
	{
		bool doremove = false;
		if (n->rectid != -1)
		{
			doremove = true;
		}
		else
		{
			const int x2 = (bounds.x0 + bounds.x1) >> 1;
			const int y2 = (bounds.y0 + bounds.y1) >> 1;

			if (n->children[0] && in0(x2, y2))
				removechild(n, 0, rect(bounds.x0, x2, bounds.y0, y2));
			if (n->children[1] && in1(x2, y2))
				removechild(n, 1, rect(x2, bounds.x1, bounds.y0, y2));
			if (n->children[2] && in2(x2, y2))
				removechild(n, 2, rect(bounds.x0, x2, y2, bounds.y1));
			if (n->children[3] && in3(x2, y2))
				removechild(n, 3, rect(x2, bounds.x1, y2, bounds.y1));
			
			doremove = true;
			for (int i = 0; i < 4; i++)
				if (n->children[i])
				{
					doremove = false;
					break;
				}
		}

		if (n == root)
		{
			n->rectid = -1;
			return false;
		}

		if (doremove)
			nodes.free(n);
		return doremove;
	}
};

quadtree tree;

int main()
{
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
//		if (!(i % 10000))
//			printf("%d...\n", i);
		tree.add();
	}

	vector<rect> rvec;
	rvec.reserve(tree.rectmap.size());
	for (map<int, rect>::iterator it = tree.rectmap.begin(), end = tree.rectmap.end(); it != end; ++it)
		rvec.push_back(it->second);
	sort(rvec.begin(), rvec.end(), rect::cmp());

	cout << rvec.size() << endl;
	for (vector<rect>::iterator it = rvec.begin(), end = rvec.end(); it != end; ++it)
		cout << it->x0 << " " << it->x1 << " " << it->y0 << " " << it->y1 << endl;

	return 0;
}