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
#include <cstdio>
#include <map>
#include <utility>
#include <algorithm>
#include <set>
#include <tuple>
#include <stack>

typedef std::pair<int, int> P;
typedef std::pair<P, P> PP;
struct SquareCoords : public PP
{
	SquareCoords(int x1, int x2, int y1, int y2)
		: PP(P(x1,x2), P(y1, y2))
	{}
	int x1() const { return first.first; };
	int x2() const { return first.second; };
	int y1() const { return second.first; };
	int y2() const { return second.second; };
};
typedef std::set<SquareCoords> Results;

struct Sq;
typedef std::pair<bool, int> PE;
struct Edge : public PE
{
	Edge(Sq *sq, int from, int to, bool close)
		: PE(!close, from), _sq(sq), _to(to)
	{
	}
	int getFrom() const { return second; }
	int getTo() const { return _to; }
	bool getClose() const { return !first; }

	Sq *_sq;
	int _to;
};

struct Sq
{
	Sq(int id, int x1, int x2, int y1, int y2)
		: _id(id), _x1(std::min(x1,x2)), _x2(std::max(x1,x2)),
		  _y1(std::min(y1,y2)), _y2(std::max(y1,y2)),
		  _deleted(false)
	{
	}

	Edge vertical(bool close) { return Edge(this, _y1, _y2, close); }
	int leftX() const { return _x1; }
	int rightX() const { return _x2; }

	Edge horizontal(bool close) { return Edge(this, _x1, _x2, close); }
	int topY() const { return _y2; }
	int bottomY() const { return _y1; }

	void addLink(Sq *sq)
	{
		_links.insert(sq);
	}

	void link(Sq *sq)
	{
		if (sq!=this)
		{
			_links.insert(sq);
			sq->addLink(this);
		}
	}

	bool intersects(Sq *other) const
	{
		int xover = std::max(0,
			std::min(_x2, other->_x2) - std::max(_x1, other->_x1));
		int yover = std::max(0,
			std::min(_y2, other->_y2) - std::max(_y1, other->_y1));
		return xover > 0 && yover > 0;
	}

	SquareCoords getCoords() const
	{
		return SquareCoords(_x1, _x2, _y1, _y2);
	}

	void increase(SquareCoords &&sc)
	{
		_x1 = std::min(sc.x1(), _x1);
		_x2 = std::max(sc.x2(), _x2);
		_y1 = std::min(sc.y1(), _y1);
		_y2 = std::max(sc.y2(), _y2);
	}

	bool traverse(Results &results)
	{
		bool intersected = false;
		std::vector<Sq *> toRemove;
//fprintf(stderr, "checking %d\n", _id);
		for ( auto &sqnext : _links )
		{
//fprintf(stderr, "\twith %d\n", sqnext->_id);
			if (sqnext->intersects(this))
			{
//fprintf(stderr, "\t\tintersection\n");
				intersected = true;
				sqnext->_deleted = true;
				sqnext->_links.erase(this);
				increase(sqnext->getCoords());
				sqnext->increase(getCoords());
				toRemove.push_back(sqnext);
			}
		}
		
		while (!toRemove.empty())
		{
			Sq *sq = toRemove.back();
			toRemove.pop_back();
			_links.erase(sq);
//fprintf(stderr, "checking %d\n", sq->_id);
			for (auto &sqnext : sq->_links)
			{
//fprintf(stderr, "\twith %d\n", sqnext->_id);
				if (sqnext != this && !sqnext->_deleted )
				{
//fprintf(stderr, "\t\tintersection\n");
					if (sqnext->intersects(sq))
					{
						intersected = true;
						sqnext->_deleted = true;
						sqnext->_links.erase(sq);
						increase(sqnext->getCoords());
						sqnext->increase(getCoords());
						toRemove.push_back(sqnext);
					}
					else
					{
						_links.insert(sqnext);
					}
				}
			}
		}
		return intersected;
	}

	int _id;
	int _x1;
	int _x2;
	int _y1;
	int _y2;
	bool _deleted;

	std::set<Sq *> _links;
};

typedef std::multiset<Edge> Edges;
struct M : public std::map<int, Edges>
{
	void add(int pos, Edge &&e)
	{
		auto it = find(pos);
		if (it == end())
		{
			it = insert(std::make_pair(pos, Edges())).first;
		}
		it->second.insert(e);
	}
};

typedef std::map<P, Sq *> SweepSet;
typedef std::vector<P> SquaresToRemove;

void closeEdge(P xy, SweepSet &sweepSet, SquaresToRemove &squaresToRemove)
{
	auto found = sweepSet.lower_bound(xy);
	if (found != sweepSet.end() && xy.first != found->first.first)
	{
		++found;
	}
	// closing the edge - link squares
	if (found != sweepSet.end() && xy.first == found->first.first)
	{
			squaresToRemove.push_back(found->first);
			auto next = found;
			++next;
			if (next != sweepSet.end())
			{
//fprintf(stderr, "Linking %d-%d\n", next->second->_id, found->second->_id);
					next->second->link(found->second);
			}
			if (found != sweepSet.begin())
			{
					auto prev = found;
					--prev;
//fprintf(stderr, "Linking %d-%d\n", prev->second->_id, found->second->_id);
					prev->second->link(found->second);
			}
	}
}

void sweep( M &edges, bool vertical )
{
	SweepSet sweepSet;
	for (auto it = edges.begin(); it != edges.end(); ++it)
	{
		SquaresToRemove squaresToRemove;
		int x = it->first;
		for (auto iit = it->second.begin(); iit != it->second.end(); ++iit)
		{
			int y1 = iit->getFrom();
			int y2 = iit->getTo();
			bool close = iit->getClose();
//fprintf(stderr, "[%d] %c%d-%d\n", it->first, close ? 'X':'O', y1,y2);
			if (!close)
			{
				P p1(y1, x);
				P p2(y2, x);
			
			//fprintf(stderr, "Adding %d-%d\n", p1.first, p1.second);
				sweepSet[p1] = iit->_sq;
			//fprintf(stderr, "Adding %d-%d\n", p2.first, p2.second);
				sweepSet[p2] = iit->_sq;
			}
			else
			{
				int x1 = vertical ? iit->_sq->_x1 : iit->_sq->_y1;
				P p1(y1, x1);
				P p2(y2, x1);
//fprintf(stderr, "close %d\n", y1);
				closeEdge(p1, sweepSet, squaresToRemove);
//fprintf(stderr, "close %d\n", y2);
				closeEdge(p2, sweepSet, squaresToRemove);
			}
		}
		for (auto &it : squaresToRemove)
		{
			//fprintf(stderr, "Removing %d-%d\n", it.first, it.second);
			sweepSet.erase(it);
		}
	}
}

int main()
{
	int N;
	scanf("%d ", &N);

	std::vector<Sq> v;
	v.reserve(N);

	M vedges;
	M hedges;

	for (int n = 0; n < N; ++n)
	{
		int x1, x2, y1, y2;
		scanf("%d %d %d %d ", &x1, &x2, &y1, &y2);

		v.push_back(Sq(n+1, x1, x2, y1, y2));
		Sq &sq = *(v.rbegin());
		vedges.add(sq.leftX(), sq.vertical(false));
		vedges.add(sq.rightX(), sq.vertical(true));
		hedges.add(sq.bottomY(), sq.horizontal(false));
		hedges.add(sq.topY(), sq.horizontal(true));
	}

//fprintf(stderr, "sweep v\n");
	sweep(vedges, true);
//fprintf(stderr, "sweep h\n");
	sweep(hedges, false);

	Results &results = *(new Results);
	
	bool intersected = false;
	do {
		intersected = false;
		for (Sq &sq : v)
		{
			if (!sq._deleted) {
				bool res = sq.traverse(results);
				if (res) intersected = true;
			}
		};
	} while (intersected);

	for (Sq &sq : v)
	{
		if (!sq._deleted) results.insert(sq.getCoords());
	}

	printf("%lu\n", results.size());
	std::for_each(results.begin(), results.end(),
		[](const SquareCoords &elem) {
			printf("%d %d %d %d\n",
				elem.x1(), elem.x2(),
				elem.y1(), elem.y2());
		});

	return 0;
}