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

struct Rect {
    Rect(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {
    }
    
    int x1;
    int x2;
    int y1;
    int y2;
};

class YTree {
private:
    struct Vert {
	Vert() {
	    s[0] = s[1] = NULL;
	}
	std::set<Rect*> whole, partial;
	Vert *s[2];  
    };
    
    void ad(int p, int k, Vert *v, bool add) {
	if (kk<=p || pp>=k) return;
	if (p>=pp && k<=kk) {
	    if (add) v->whole.insert(r);
	    else v->whole.erase(r);
	} else {
	    int c = (p+k)/2;
	    if (add) v->partial.insert(r);
	    else v->partial.erase(r);
	    if (pp<=c) {
		if (!v->s[0]) v->s[0] = new Vert();
		ad(p,c,v->s[0],add);
	    }
	    if (kk>=c) {
		if (!v->s[1]) v->s[1] = new Vert();
		ad(c,k,v->s[1],add);
	    }
	}
    }
    
    Rect* fi(int p, int k, Vert *v) {
	if (!v || p>=kk || k<=pp) return NULL;
	if (!v->whole.empty()) return *(v->whole.begin());
	
	if (p>=pp && k<=kk) {
	    if (!v->partial.empty()) return *(v->partial.begin());
	    else return NULL;
	}
	
	int c = (p+k)/2;
	Rect *tmp = fi(p,c,v->s[0]);
	if (tmp) return tmp;
	else return fi(c,k,v->s[1]);
    }
  
    Vert *root;
    int zp,zk;
    int pp,kk;
    Rect *r;
  
public:
    YTree(int maxy) : zp(0), zk(maxy) {
	root = new Vert();
    }
    
    void add(int p, int k, Rect* re) {
	pp = p;
	kk = k;
	r = re;
	ad(zp,zk,root,true);
    }
    
    void rem(int p, int k, Rect* re) {
	pp = p;
	kk = k;
	r = re;
	ad(zp,zk,root,false);
    }
    
    Rect* find(int a, int b) {
	pp = a;
	kk = b;
	return fi(zp, zk, root);
    }
};

class XTree {
private:
    struct Vert {
	Vert(int maxy) : whole(maxy), partial(maxy) {
	    s[0] = s[1] = NULL;
	}
	YTree whole,partial;
	Vert *s[2];  
    };
    
    void ad(int p, int k, Vert *v, bool add) {
	if (kk<=p || pp>=k) return;
	if (p>=pp && k<=kk) {
	    if (add) v->whole.add(pp2,kk2,r);
	    else v->whole.rem(pp2,kk2,r);
	} else {
	    int c = (p+k)/2;
	    if (add) v->partial.add(pp2,kk2,r);
	    else v->partial.rem(pp2,kk2,r);
	    if (pp<=c) {
		if (!v->s[0]) v->s[0] = new Vert(zk);
		ad(p,c,v->s[0],add);
	    }
	    if (kk>=c) {
		if (!v->s[1]) v->s[1] = new Vert(zk);
		ad(c,k,v->s[1],add);
	    }
	}
    }
    
    Rect* fi(int p, int k, Vert *v) {
	if (!v || p>=kk || k<=pp) return NULL;
	Rect* tmp = v->whole.find(pp2,kk2);
	if (tmp) return tmp;
	if (p>=pp && k<=kk) {
	    return (v->partial.find(pp2,kk2));
	}
	
	int c = (p+k)/2;
	tmp = fi(p,c,v->s[0]);
	if (tmp) return tmp;
	else return fi(c,k,v->s[1]);
    }
  
    Vert *root;
    int zp,zk;
    int pp,kk,pp2,kk2;
    Rect* r;
  
public:
    XTree(int maxx) : zp(0), zk(maxx) {
	root = new Vert(zk);
    }
    
    void add(int p, int k, int p2, int k2, Rect* re) {
	pp=p;
	kk=k;
	pp2 = p2;
	kk2 = k2;
	r = re;
	ad(zp,zk,root,true);
    }
    
    void rem(int p, int k, int p2, int k2, Rect* re) {
	pp=p;
	kk=k;
	pp2 = p2;
	kk2 = k2;
	r = re;
	ad(zp,zk,root,false);
    }
    
    Rect* find(int a, int b, int p2, int k2) {
	pp = a;
	kk = b;
	pp2 = p2;
	kk2 = k2;
	return fi(zp, zk, root);
    }
};

bool lexCmp(const Rect *lhs, const Rect *rhs) {
    if (lhs->x1 != rhs->x1) return lhs->x1 < rhs->x1;
    if (lhs->x2 != rhs->x2) return lhs->x2 < rhs->x2;
    if (lhs->y1 != rhs->y1) return lhs->y1 < rhs->y1;
    return lhs->y2 < rhs->y2;
}

bool lexEqual(const Rect *lhs, const Rect *rhs) {
    return !lexCmp(lhs,rhs) && !lexCmp(rhs,lhs);
}

int main() {
    std::vector<Rect> input;
    std::vector<int> xmapping,ymapping;
    std::set<Rect*> result;
    int N;
    scanf("%d",&N);
    while (N--) {
	int x1,x2,y1,y2;
	scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
	input.push_back(Rect(x1,x2,y1,y2));
	xmapping.push_back(x1);
	xmapping.push_back(x2);
	ymapping.push_back(y1);
	ymapping.push_back(y2);
    }
    
    std::sort(xmapping.begin(), xmapping.end());
    std::sort(ymapping.begin(), ymapping.end());
    xmapping.erase(std::unique(xmapping.begin(), xmapping.end()), xmapping.end());
    ymapping.erase(std::unique(ymapping.begin(), ymapping.end()), ymapping.end());
    
    XTree tree(std::max(xmapping.size(), ymapping.size()));
    for (int i=0; i<input.size(); ++i) {
	int x1 = std::lower_bound(xmapping.begin(), xmapping.end(), input[i].x1) - xmapping.begin();
	int x2 = std::lower_bound(xmapping.begin(), xmapping.end(), input[i].x2) - xmapping.begin();
	int y1 = std::lower_bound(ymapping.begin(), ymapping.end(), input[i].y1) - ymapping.begin();
	int y2 = std::lower_bound(ymapping.begin(), ymapping.end(), input[i].y2) - ymapping.begin();
	Rect *current = new Rect(x1,x2,y1,y2);

	while (current) {
	    Rect* tmp = tree.find(current->x1, current->x2, current->y1, current->y2);
	    if (tmp) {
		result.erase(tmp);
		tree.rem(tmp->x1,tmp->x2,tmp->y1,tmp->y2,tmp);
		current = new Rect(std::min(current->x1,tmp->x1), std::max(current->x2,tmp->x2), std::min(current->y1,tmp->y1), std::max(current->y2,tmp->y2));
	    } else {
		tree.add(current->x1,current->x2,current->y1,current->y2,current);
		result.insert(current);
		current = NULL;
	    }
	}
    }
    
    std::vector<Rect*> resArray(result.begin(), result.end());
    std::sort(resArray.begin(), resArray.end(), lexCmp);
    resArray.erase(std::unique(resArray.begin(), resArray.end(), lexEqual), resArray.end());
    
    printf("%d\n",(int)resArray.size());
    for (int i=0; i<resArray.size(); ++i) {
	printf("%d %d %d %d\n",xmapping[resArray[i]->x1],xmapping[resArray[i]->x2],ymapping[resArray[i]->y1],ymapping[resArray[i]->y2]);
    }
    
    return 0;
}