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

enum
{
	MAX = 100000,
	MAXX = 1000010
};

using namespace std;

int min(int a, int b) {return(a<b)?a:b;}
int max(int a, int b) {return(a>b)?a:b;}

map<int,map<int,int> > tree_h, tree_v;

typedef map<int,map<int,int> > tree;

void add_inter(tree& tr, int v, vector<pair<int,int> > &bt, int y, int left, int right, int over_id)
{
	tr[over_id][y] = v;
	bt.push_back(pair<int,int>(over_id,y));
}

void add_over(tree& tr, int v, vector<pair<int,int> > &bt, int y, int x1, int x2, int left = 0, int right = MAXX, int id = 0)
{
	if(x1 <= left && right <= x2)
	{
		add_inter(tr,v,bt,y, 0, MAXX, id);
		return;
	}
	if(right-left <= 1) return;
	int mid = (left+right)/2;
	if(x2 <= left) return;
	if(right <= x1) return;
	add_over(tr,v,bt,y,x1,x2,left,mid,id*2+1);
	add_over(tr,v,bt,y,x1,x2,mid,right,id*2+2);
}

void read_inter(tree& tr, int* &res, int y1, int y2, int left, int right, int over_id)
{
	for(map<int,int>::iterator it = tr[over_id].lower_bound(y1);
		it != tr[over_id].end() && it->first < y2; it++)
	{
		*res = it->second;
		res++;
	}
}

void read_over(tree& tr, int* &res, int x, int y1, int y2, int left = 0, int right = MAXX, int id = 0)
{
	if(tr.find(id) != tr.end())
		read_inter(tr, res, y1, y2, 0, MAXX, id);
	if(right-left <= 1) return;
	int mid = (left+right)/2;
	if(x < mid)
		read_over(tr, res, x, y1, y2, left, mid, id*2+1);
	else
		read_over(tr, res, x, y1, y2, mid, right, id*2+2);
}

struct rect
{
	int x1, x2, y1, y2;
	vector< pair<int, int> >
		e_h1, e_h2, e_v1, e_v2;
	bool dead;
} rects[MAX];

int res[MAX*2];

bool comp(rect a, rect b)
{
	if(a.dead != b.dead)
		return (!a.dead && b.dead);
	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;
}

std::map<int,int> other_tree;
void other_write(int x1, int x2, int v, int left = 0, int right = MAXX, int id = 0)
{
	if(x1<=left && right <= x2)
	{
		other_tree[id] += v;
		return;
	}
	if(x2<=left) return;
	if(right<=x1) return;
	int mid = (left+right)/2;
	other_write(x1,x2,v,left,mid,id*2+1);
	other_write(x1,x2,v,mid,right,id*2+2);
}

int other_read(int x1, int x2, int left = 0, int right = MAXX, int id = 0)
{
	if(x1<=left && right <= x2)
		return other_tree[id];
	if(x2<=left) return 0;
	if(right<=x1) return 0;
	int mid = (left+right)/2;
	return max( other_tree[id],
		max( other_read(x1,x2,left,mid,id*2+1),
		other_read(x1,x2,mid,right,id*2+2)));
}
struct ev
{
	int x, v, r;
} queue[MAX*2];
bool operator<(ev a, ev b)
{
	if(a.x != b.x)
		return a.x < b.x;
	if(a.v != b.v)
		return a.v < b.v;
	if(rects[a.r].x2-rects[a.r].x1 != rects[b.r].x2-rects[b.r].x1)
		return rects[a.r].x2-rects[a.r].x1 < rects[b.r].x2-rects[b.r].x1;
	return rects[a.r].y2-rects[a.r].y1 < rects[b.r].y2-rects[b.r].y1;
}

int main()
{
	int n; scanf("%d",&n);
	int dead_count = 0;
	for(int ii = 0; ii < n; ii++)
	{
		int x1, x2, y1, y2;
		scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
		int *_res;
		do
		{
			_res = res;
			if(_res == res) read_over(tree_h,_res,x1,y1,y2);
			if(_res == res) read_over(tree_h,_res,x2-1,y1,y2);
			if(_res == res) read_over(tree_v,_res,y1,x1,x2);
			if(_res == res) read_over(tree_v,_res,y2-1,x1,x2);
			
			for(int *j=res; j < _res; j++)
			{
				rect* it = rects+*j;
				if(it->dead) continue;
				dead_count++;
				it->dead = 1;
				x1 = min(x1,it->x1);
				x2 = max(x2,it->x2);
				y1 = min(y1,it->y1);
				y2 = max(y2,it->y2);
				{
					tree &tr = tree_h;
					vector<pair<int, int> > &vec = it->e_h1;
					for(int i = 0; i < vec.size(); i++)
					{
						tr[vec[i].first].erase(vec[i].second);
						if(tr[vec[i].first].empty())
							tr.erase(vec[i].first);
					}
					vec.clear();
				}
				{
					tree &tr = tree_h;
					vector<pair<int, int> > &vec = it->e_h2;
					for(int i = 0; i < vec.size(); i++)
					{
						tr[vec[i].first].erase(vec[i].second);
						if(tr[vec[i].first].empty())
							tr.erase(vec[i].first);
					}
					vec.clear();
				}
				{
					tree &tr = tree_v;
					vector<pair<int, int> > &vec = it->e_v1;
					for(int i = 0; i < vec.size(); i++)
					{
						tr[vec[i].first].erase(vec[i].second);
						if(tr[vec[i].first].empty())
							tr.erase(vec[i].first);
					}
					vec.clear();
				}
				{
					tree &tr = tree_v;
					vector<pair<int, int> > &vec = it->e_v2;
					for(int i = 0; i < vec.size(); i++)
					{
						tr[vec[i].first].erase(vec[i].second);
						if(tr[vec[i].first].empty())
							tr.erase(vec[i].first);
					}
					vec.clear();
				}
			}
		} while(_res != res);
		add_over(tree_h, ii, rects[ii].e_h1, y1, x1, x2);
		add_over(tree_h, ii, rects[ii].e_h2, y2-1, x1, x2);
		add_over(tree_v, ii, rects[ii].e_v1, x1, y1, y2);
		add_over(tree_v, ii, rects[ii].e_v2, x2-1, y1, y2);
		rects[ii].x1 = x1;
		rects[ii].x2 = x2;
		rects[ii].y1 = y1;
		rects[ii].y2 = y2;
	}
	int id = 0;
	for(int i = 0; i < n; i++)
		if(!rects[i].dead)
		{
			queue[id].x = rects[i].x1;
			queue[id].v = 1;
			queue[id].r = i;
			id++;
			queue[id].x = rects[i].x2;
			queue[id].v = -1;
			queue[id].r = i;
			id++;
		}
	std::sort(queue,queue+id);
	for(int i = 0; i < id; i++)
	{
		if(queue[i].v < 0)
		{
			other_write(rects[queue[i].r].y1, rects[queue[i].r].y2, -1);
			if(other_read(rects[queue[i].r].y1, rects[queue[i].r].y2) > 0)
			{
				rects[queue[i].r].dead = 1;
				dead_count++;
			}
		}
		else
			other_write(rects[queue[i].r].y1, rects[queue[i].r].y2, 1);
	}
	std::sort(rects,rects+n,comp);
	printf("%d\n", n-dead_count);
	for(int i = 0; i < n-dead_count; i++)
		printf("%d %d %d %d\n", rects[i].x1, rects[i].x2, rects[i].y1, rects[i].y2);
	return 0;
}