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
#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

struct race_t {
	int x1, x2, y1, y2;
};

struct subtree_t {
	int left, right;
	set<int> somewhere, everywhere;
	subtree_t *leftTree, *rightTree;
};

struct supertree_t {
	int left, right;
	int minSub, maxSub;
	set<int> somewhere, everywhere;
	subtree_t *value;
	supertree_t *leftTree, *rightTree;
};

const race_t REMOVED_RACE = {
	x1: -1,
	x2: -1,
	y1: -1,
	y2: -1,
};

race_t combineRace(race_t *races, int i, supertree_t *tree);
bool isRemoved(const race_t &r);
bool compareRaces(const race_t &lhs, const race_t &rhs);
supertree_t *create(int left, int right, int minSub, int maxSub);
void insert(supertree_t *tree, int x1, int x2, int y1, int y2, int value);
void remove(supertree_t *tree, int x1, int x2, int y1, int y2, int value);
int findFirst(supertree_t *tree, int x1, int x2, int y1, int y2);
race_t scaleUp(const race_t &r);
race_t scaleDown(const race_t &r);

int main() {
	int n;
	scanf("%d", &n);
	race_t *races = new race_t[n];
	int minX = 1000 * 1000 + 1, maxX = -1;
	int minY = minX, maxY = maxX;
	for(int i = 0; i < n; ++i) {
		scanf("%d %d %d %d", &races[i].x1, &races[i].x2, &races[i].y1, &races[i].y2);
		races[i] = scaleUp(races[i]);
		minX = min(races[i].x1, minX);
		maxX = max(races[i].x2, maxX);
		minY = min(races[i].y1, minY);
		maxY = max(races[i].y2, maxY);
	}
	supertree_t *tree = create(minX, maxX, minY, maxY);
	for(int i = 0; i < n; ++i)
		races[i] = combineRace(races, i, tree);
	vector<race_t> result;
	for(int i = 0; i < n; ++i)
		if(!isRemoved(races[i]))
			result.push_back(scaleDown(races[i]));
	sort(result.begin(), result.end(), compareRaces);
	printf("%d\n", result.size());
	for(vector<race_t>::iterator it = result.begin(); it != result.end(); ++it)
		printf("%d %d %d %d\n", it->x1, it->x2, it->y1, it->y2);
	return 0;
}

race_t scaleUp(const race_t &r) {
	return {
		x1: r.x1 * 2,
		x2: r.x2 * 2,
		y1: r.y1 * 2,
		y2: r.y2 * 2,
	};
}

race_t scaleDown(const race_t &r) {
	return {
		x1: r.x1 / 2,
		x2: r.x2 / 2,
		y1: r.y1 / 2,
		y2: r.y2 / 2,
	};
}

race_t combine(const race_t &r1, const race_t &r2) {
	return {
		x1: min(r1.x1, r2.x1),
		x2: max(r1.x2, r2.x2),
		y1: min(r1.y1, r2.y1),
		y2: max(r1.y2, r2.y2),
	};
}

race_t combineRace(race_t *races, int i, supertree_t *tree) {
	while(true) {
		int common = findFirst(tree, races[i].x1 + 1, races[i].x2 - 1, races[i].y1 + 1, races[i].y2 - 1);
		if(common == -1) {
			insert(tree, races[i].x1, races[i].x2, races[i].y1, races[i].y2, i);
			return races[i];
		}
		remove(tree, races[common].x1, races[common].x2, races[common].y1, races[common].y2, common);
		races[i] = combine(races[i], races[common]);
		races[common] = REMOVED_RACE;
	}
}

bool isRemoved(const race_t &r) {
	return r.x1 == -1;
}

bool compareRaces(const race_t &lhs, const race_t &rhs) {
	if(lhs.x1 == rhs.x1) {
		if(lhs.x2 == rhs.x2) {
			if(lhs.y1 == rhs.y1)
				return lhs.y2 < rhs.y2;
			return lhs.y1 < rhs.y1;
		}
		return lhs.x2 < rhs.x2;
	}
	return lhs.x1 < rhs.x1;
}

supertree_t *create(int left, int right, int minSub, int maxSub) {
	supertree_t *tree = new supertree_t;
	tree->left = left;
	tree->right = right;
	tree->minSub = minSub;
	tree->maxSub = maxSub;
	tree->value = NULL;
	tree->leftTree = tree->rightTree = NULL;
	return tree;
}

subtree_t *createSubtree(int left, int right) {
	subtree_t *tree = new subtree_t;
	tree->left = left;
	tree->right = right;
	tree->leftTree = tree->rightTree = NULL;
	return tree;
}

void insertSubtree(subtree_t *tree, int y1, int y2, int value) {
	if(tree->left == y1 && tree->right == y2)
		tree->everywhere.insert(value);
	else {
		tree->somewhere.insert(value);
		int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
		int rSubLeft = lSubRight + 1, rSubRight = tree->right;
		if(y1 <= lSubRight) { // insert to left
			if(tree->leftTree == NULL)
				tree->leftTree = createSubtree(lSubLeft, lSubRight);
			insertSubtree(tree->leftTree, y1, min(y2, lSubRight), value);
		}
		if(y2 >= rSubLeft) { // insert to right
			if(tree->rightTree == NULL)
				tree->rightTree = createSubtree(rSubLeft, rSubRight);
			insertSubtree(tree->rightTree, max(y1, rSubLeft), y2, value);
		}
	}
}

void insert(supertree_t *tree, int x1, int x2, int y1, int y2, int value) {
	if(tree->left == x1 && tree->right == x2) {
		tree->everywhere.insert(value);
		if(tree->value == NULL)
			tree->value = createSubtree(tree->minSub, tree->maxSub);
		insertSubtree(tree->value, y1, y2, value);
	} else {
		tree->somewhere.insert(value);
		int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
		int rSubLeft = lSubRight + 1, rSubRight = tree->right;
		if(x1 <= lSubRight) { // insert to left
			if(tree->leftTree == NULL)
				tree->leftTree = create(lSubLeft, lSubRight, tree->minSub, tree->maxSub);
			insert(tree->leftTree, x1, min(x2, lSubRight), y1, y2, value);
		}
		if(x2 >= rSubLeft) { // insert to right
			if(tree->rightTree == NULL)
				tree->rightTree = create(rSubLeft, rSubRight, tree->minSub, tree->maxSub);
			insert(tree->rightTree, max(x1, rSubLeft), x2, y1, y2, value);
		}
	}
}

void removeSubtree(subtree_t *tree, int y1, int y2, int value) {
	if(tree == NULL)
		return;
	if(tree->left == y1 && tree->right == y2)
		tree->everywhere.erase(value);
	else {
		tree->somewhere.erase(value);
		int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
		int rSubLeft = lSubRight + 1, rSubRight = tree->right;
		if(y1 <= lSubRight) // remove from left
			removeSubtree(tree->leftTree, y1, min(y2, lSubRight), value);
		if(y2 >= rSubLeft) // remove from right
			removeSubtree(tree->rightTree, max(y1, rSubLeft), y2, value);
	}
}

void remove(supertree_t *tree, int x1, int x2, int y1, int y2, int value) {
	if(tree == NULL)
		return;
	if(tree->left == x1 && tree->right == x2) {
		tree->everywhere.erase(value);
		removeSubtree(tree->value, y1, y2, value);
	} else {
		tree->somewhere.erase(value);
		int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
		int rSubLeft = lSubRight + 1, rSubRight = tree->right;
		if(x1 <= lSubRight) // remove from left
			remove(tree->leftTree, x1, min(x2, lSubRight), y1, y2, value);
		if(x2 >= rSubLeft) // remove from right
			remove(tree->rightTree, max(x1, rSubLeft), x2, y1, y2, value);
	}
}

int findFirstSubtree(subtree_t *tree, int y1, int y2) {
	if(tree == NULL)
		return -1;
	if(!tree->everywhere.empty())
		return *(tree->everywhere.begin());
	if(tree->somewhere.empty())
		return -1;
	int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
	int rSubLeft = lSubRight + 1, rSubRight = tree->right;
	int result = -1;
	if(y1 <= lSubRight) // find in left
		result = findFirstSubtree(tree->leftTree, y1, min(y2, lSubRight));
	if(result == -1 && y2 >= rSubLeft) // find in right
		result = findFirstSubtree(tree->rightTree, max(y1, rSubLeft), y2);
	return result;
}

int findFirst(supertree_t *tree, int x1, int x2, int y1, int y2) {
	if(tree == NULL)
		return -1;
	if(!tree->everywhere.empty()) {
		int result = findFirstSubtree(tree->value, y1, y2);
		if(result != -1)
			return result;
	}
	if(tree->somewhere.empty())
		return -1;
	int lSubLeft = tree->left, lSubRight = (tree->left + tree->right) / 2;
	int rSubLeft = lSubRight + 1, rSubRight = tree->right;
	int result = -1;
	if(x1 <= lSubRight) // find in left
		result = findFirst(tree->leftTree, x1, min(x2, lSubRight), y1, y2);
	if(result == -1 && x2 >= rSubLeft) // find in right
		result = findFirst(tree->rightTree, max(x1, rSubLeft), x2, y1, y2);
	return result;
}