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

static const int MAX_N = 100005;
static const int MAX = 262144;

//static const int MAX_N = 1005;
//static const int MAX = 8;

static const int verbose = false;
static const int verbose2 = false;

int n;
struct tribe {
	int x[2];
	int y[2];
	int father;
	
	void join(tribe & a) {
		a.x[0] = x[0] = std::min(x[0], a.x[0]);
		a.y[0] = y[0] = std::min(y[0], a.y[0]);
		a.x[1] = x[1] = std::max(x[1], a.x[1]);
		a.y[1] = y[1] = std::max(y[1], a.y[1]);
		
	}
} tribes[MAX_N];

int x[MAX_N * 2];
int y[MAX_N * 2];

std::map<int, int> renum[2]; 

void renumImpl(int num, int * values, std::map<int, int> & m1, std::map<int, int> & m2) {
	int prev = -1;
	int k = 1;
	for (int i = 0; i < num; ++i)
		if (values[i] != prev) {
			m2[k] = values[i];
			m1[values[i]] = k++;
			prev = values[i];
		}
}

void renumPositions() {
	for (int i = 0; i < n; ++i) {
		x[i * 2] = tribes[i].x[0];
		x[i * 2 + 1] = tribes[i].x[1];
		//y[i * 2] = tribes[i].y[0];
		//y[i * 2 + 1] = tribes[i].y[1];
	}
	
	std::sort(x, x + n * 2);
	//std::sort(y, y + n * 2);
	
	renumImpl(2 * n, x, renum[0], renum[1]);
	//renumImpl(2 * n, y, renum[1], renum[3]);
	
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 2; ++j) {
			tribes[i].x[j] = renum[0][tribes[i].x[j]];
			//tribes[i].y[j] = renum[1][tribes[i].y[j]];
		}
}

inline int rightNode(int x){ return x * 2 + 1; }
inline int leftNode(int x){ return x * 2; }


int find(int x) {
	if (tribes[x].father == x)
		return x;
	return tribes[x].father = find(tribes[x].father);
}

bool joinDoneGlobal;
void join(int x, int y) {
	if (verbose) printf("join! %d %d\n", y, x);
	int f1 = find(x);
	int f2 = find(y);
	if (f1 != f2)
		joinDoneGlobal = true;
	tribes[f1].join(tribes[f2]);
	tribes[f1].father = f2;
}


struct Column {
	/*std::set<std::pair<int, std::pair<int, int> > > odcinki;
	
	std::pair<int,int> check(int y1, int y2, int value) {
		if (verbose) for(auto iter=odcinki.begin(); iter!=odcinki.end(); ++iter) {
			printf("   (%d %d) ", iter->first, iter->second.first);
		}
		if (verbose) printf("\n	Check %d %d %d\n", y1, y2, value);
	
		bool job = true;
		bool joinDone = false;
		while(job) {
			job = false;
			auto beg = odcinki.lower_bound(std::make_pair(y1, std::make_pair(1000009, 0)));
			if (verbose); if (beg == odcinki.end())
				printf("beg problem!\n");
			else
				printf("(%d %d) \n", beg->first, beg->second.first);
			if (beg != odcinki.end() && (beg->second.first < y2 && beg->first > y1))
			{
				joinDone = true;
				odcinki.erase(beg);
				join(beg->second.second, value);
				job = true;
			}
		}
		if (joinDone) {
			int node = find(value);
			odcinki.insert(std::make_pair(tribes[node].y[1], std::make_pair(tribes[node].y[0], node)));
		}
		return std::make_pair(joinDone, find(value));
	}
	
	void insert(int y1, int y2, int value) {
		if (verbose) for(auto iter=odcinki.begin(); iter!=odcinki.end(); ++iter) {
			printf("   (%d %d) ", iter->first, iter->second.first);
		}
		if (verbose) printf("\n	Insert %d %d %d\n", y1, y2, value);
		odcinki.insert(std::make_pair(y2, std::make_pair(y1, value)));
	}*/
	std::vector<std::tuple<int, int, int> > odcinki;
	std::pair<int,int> check(int y1, int y2, int value) {
		
		bool joinDone = false;
		for(int i = 0; i < odcinki.size(); ++i) 
			if (std::get<0>(odcinki[i]) < y2 && std::get<1>(odcinki[i]) > y1)
				join(std::get<2>(odcinki[i]), value);
		
		return std::make_pair(joinDone, find(value));
	}
	
	void insert(int y1, int y2, int value) {
		odcinki.push_back(std::make_tuple(y1, y2, value));
	}
};

Column tree[MAX_N*4];
Column treeFull[MAX_N*4];

int insert(int x1, int x2, int y1, int y2, int value, int node = 1, int left = 0, int right = MAX)
{
	if (verbose) printf("Insert %d %d in %d %d\n", x1, x2, left, right);

	if (verbose) printf("	Full:");
	treeFull[node].check(y1, y2, value);
		
	if (x1 == left && x2 == right)
	{
		if (verbose) printf("	Full:");
		treeFull[node].insert(y1, y2, value);
		
		if (verbose) printf("	Part:");
		auto result = tree[node].check(y1, y2, value);
		return result.second;
	}

	if (verbose) printf("	Part:");
	tree[node].insert(y1, y2, value);
	
	int centre = (left + right) / 2;
	if (left == right)
		return -1;
	if (x1 >= centre) {
		return insert(x1, x2, y1, y2, value, rightNode(node), centre, right);
	} else if ( x2 <= centre) {
		return insert(x1, x2, y1, y2, value, leftNode(node), left, centre);
	} else if (x1 > left || x2 < right){
		int r1 = insert(centre, x2, y1, y2, value, rightNode(node), centre, right);
		int r2 = insert(x1, centre, y1, y2, value, leftNode(node), left, centre);
		if (r1 == value)
			return r2;
		else
			return r1;
	}
}

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

	for (int i = 0; i < n; ++i) {
		scanf("%d%d%d%d", tribes[i].x,
			  tribes[i].x + 1,
			  tribes[i].y,
		      tribes[i].y + 1);
		tribes[i].father = i;
	}
	
	renumPositions();
	
	if (verbose) for (int i = 0; i < n; ++i) {
		printf("%d %d %d %d\n", tribes[i].x[0], tribes[i].x[1], tribes[i].y[0], tribes[i].y[1]);
	}
	
	for (int i = 0; i < n; ++i) {
		if (verbose2) printf("%d %d %d %d\n", tribes[i].x[0], tribes[i].x[1],
			       tribes[i].y[0], tribes[i].y[1]);
			
		insert(tribes[i].x[0], tribes[i].x[1],
			       tribes[i].y[0], tribes[i].y[1], i);
		while (joinDoneGlobal) {
			joinDoneGlobal = false;
			int current = find(i);
			if (verbose) printf("Joined!\n");
			if (verbose2) printf("->%d %d %d %d\n", tribes[current].x[0], tribes[current].x[1],
			       tribes[current].y[0], tribes[current].y[1]);
			insert(tribes[current].x[0], tribes[current].x[1],
			       tribes[current].y[0], tribes[current].y[1], current);
			
		}
	}
	
	std::vector<std::tuple<int,int,int,int> > out;
	for (int i = 0; i < n; ++i)
		if (find(i) == i)
			out.push_back(std::make_tuple(renum[1][tribes[i].x[0]], renum[1][tribes[i].x[1]],
			       tribes[i].y[0], tribes[i].y[1]));

	std::sort(out.begin(), out.end());

	printf("%d\n", (int)out.size());
	for (int i = 0; i < out.size(); ++i) {
		printf("%d %d %d %d\n", std::get<0>(out[i]), std::get<1>(out[i]),
			   std::get<2>(out[i]), std::get<3>(out[i]));
	}
	
	return 0;
}