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
//#define DEBUG
#ifdef DEBUG
#define D(...) __VA_ARGS__
#else
#define D(...) do {} while(0)
#define NDEBUG
#endif

#include <iostream>
#include <algorithm>

#include <vector>
#include <set>

#include <assert.h>

using namespace std;

typedef int TribeIdx;
typedef int Coord;
typedef long long Area;
typedef set<pair<Coord,TribeIdx>> CoordMap;
typedef set<pair<Area,TribeIdx>> AreaMap;
TribeIdx n;
struct Tribe
{
	Coord x1, x2, y1, y2;
	Area area()
	{
		return Area(x2-x1)*(y2-y1);
	}
};
vector<Tribe> tribes;
CoordMap x1s, x2s, y1s, y2s;
AreaMap areas;

bool cmp_tribes_lexicographically(const Tribe &a, const Tribe &b)
{
	if(a.x1 != b.x1) return a.x1 < b.x1;
	if(a.x2 != b.x2) return a.x2 < b.x2;
	if(a.y1 != b.y1) return a.y1 < b.y1;
	return a.y2 < b.y2;
}

void add(TribeIdx i)
{
	auto &t = tribes[i];
	x1s.insert({t.x1, i});
	y1s.insert({t.y1, i});
	x2s.insert({t.x2, i});
	y2s.insert({t.y2, i});
	areas.insert({t.area(), i});
}

void remove(TribeIdx i)
{
	auto &t = tribes[i];
	x1s.erase(make_pair(t.x1, i));
	y1s.erase(make_pair(t.y1, i));
	x2s.erase(make_pair(t.x2, i));
	y2s.erase(make_pair(t.y2, i));
	areas.erase(make_pair(t.area(), i));
}

void fuse(TribeIdx i, TribeIdx j)
{
	Tribe &I = tribes[i];
	Tribe &J = tribes[j];
	remove(i);
	remove(j);
	I.x1 = min(I.x1, J.x1);
	I.x2 = max(I.x2, J.x2);
	I.y1 = min(I.y1, J.y1);
	I.y2 = max(I.y2, J.y2);
	J.x1 = -1;
	add(i);
}
int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);

	/* input */
	cin >> n;
	assert(1 <= n);
	assert(n <= 100000);
	tribes.resize(n);
	for(int i=0; i<n; i++)
	{
		auto &t = tribes[i];
		cin >> t.x1 >> t.x2 >> t.y1 >> t.y2;
		assert(0 <= t.x1);
		assert(t.x1 < t.x2);
		assert(t.x2 <= 1000000);
		assert(0 <= t.y1);
		assert(t.y1 < t.y2);
		assert(t.y2 <= 1000000);

		add(i);
	}

	bool updated2;
	do
	{
		updated2 = false;
		for(auto a = areas.rbegin(); a != areas.rend(); a++)
		{
			TribeIdx ti = a->second;
			bool updated1;
			do
			{
				updated1 = false;
				auto &t = tribes[ti];
				auto x1sl = x1s.lower_bound({t.x1,0});   bool x1sl_end = false;
				auto y1sl = y1s.lower_bound({t.y1,0});   bool y1sl_end = false;
				auto x2sl = x2s.lower_bound({t.x1+1,0}); bool x2sl_end = false;
				auto y2sl = y2s.lower_bound({t.y1+1,0}); bool y2sl_end = false;
				D(cerr << "analyzing " << ti << "..."<< endl);
				while(!x1sl_end || !x2sl_end || !y1sl_end || !y2sl_end)
				{
	#define go(l, lc, _end, _cond1, _cond2) \
					D(cerr << "  analyzing " << ti << ": " << #lc << endl); \
					if(!_end) \
					{ \
						if(l != lc.end() && l->second != ti) \
						{ \
							auto &u = tribes[l->second]; \
							if(_cond1) \
							{ \
								if(_cond2) \
								{ \
									D(cerr << "fusing " << ti << " with " << l->second << endl); \
									fuse(ti, l->second); \
									updated1 = updated2 = true; \
									break; \
								} \
								l++; \
							} \
							else \
							{ \
								_end = true; \
							} \
						} \
						else \
						{ \
							_end = true; \
						} \
					}
					go(x1sl, x1s, x1sl_end, u.x1 < t.x2, max(t.y1, u.y1) < min(t.y2, u.y2));
					go(y1sl, y1s, y1sl_end, u.y1 < t.y2, max(t.x1, u.x1) < min(t.x2, u.x2));
					go(x2sl, x2s, x2sl_end, u.x2 <= t.x2, max(t.y1, u.y1) < min(t.y2, u.y2));
					go(y2sl, y2s, y2sl_end, u.y2 <= t.y2, max(t.x1, u.x1) < min(t.x2, u.x2));
				}
			}
			while(updated1);
			if(updated2)
			{
				break;
			}
		}
	}
	while(updated2);

	/* output */
	sort(tribes.begin(), tribes.end(), cmp_tribes_lexicographically);
	auto tb = tribes.begin();
	while(tb != tribes.end() && tb->x1 < 0)
	{
		tb++;
	}
	cout << tribes.end() - tb << endl;
	for(; tb != tribes.end(); tb++)
	{
		cout << tb->x1 << " " << tb->x2 << " " << tb->y1 << " " << tb->y2 << endl;
	}
	return 0;
}