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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Quadtree adapted from: https://sites.google.com/site/indy256/algo/quad_tree
 */
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

#define FOR(i,a,b)  for(int i=(a);i<(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,a)    FOR(i,0,a)
#define MP          make_pair
#define PB          push_back
#define ST          first
#define ND          second

using namespace std;

typedef vector<int>             VI;
typedef vector<VI>              VVI;
typedef pair<int, int>          PII;
typedef vector<PII>             VII;
typedef long long int           LL;
typedef unsigned long long int  ULL;

const int MAXN = 100005;
const int MAXXY = 1<<20;

struct Rect {
    Rect() : left(0), right(0), bottom(0), top(0), id(0) {}

    int left, right, bottom, top, id;

    void print() {
        printf("%d %d %d %d\n", left, right, bottom, top);
    }
};

inline bool operator<(const Rect & r1, const Rect & r2) {
    if (r1.left != r2.left) {
        return r1.left < r2.left;
    }
    else if (r1.right != r2.right) {
        return r1.right < r2.right;
    }
    else if (r1.bottom != r2.bottom) {
        return r1.bottom < r2.bottom;
    }
    else {
        return r1.top < r2.top;
    }
}

inline bool operator!=(const Rect & r1, const Rect & r2) {
    return r1.id != r2.id;
}

inline bool intersect(Rect r1, Rect r2) {
    if (min(r1.right, r2.right) <= max(r1.left, r2.left)) {
        return false;
    }
    if (min(r1.top, r2.top) <= max(r1.bottom, r2.bottom)) {
        return false;
    }
    return true;
}

Rect join(Rect r1, Rect r2) {
    Rect res;
    res.left   = min(r1.left,   r2.left);
    res.right  = max(r1.right,  r2.right);
    res.bottom = min(r1.bottom, r2.bottom);
    res.top    = max(r1.top,    r2.top);
    res.id     = r1.id;
    return res;
}

struct Point {
    Point(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
    int x, y, id;
};

struct Line {
    Line(Rect r, bool bottom) :
            left(r.left), right(r.right), id(r.id), isBottom(bottom) {
        if (isBottom) y = r.bottom;
        else y = r.top;
    }

    int y, left, right;
    int id;
    bool isBottom;
};

inline bool operator<(const Line & l1, const Line & l2) {
    if (l1.y != l2.y) {
        return l1.y < l2.y;
    }
    else if (l1.isBottom != l2.isBottom) {
        return l2.isBottom;
    }
    else if (l1.left != l2.left) {
        return l1.left < l2.left;
    }
    else if (l1.right != l2.right) {
        return l1.right < l2.right;
    }
    else {
        return l1.id < l2.id;
    }
}

struct Node {
    Node() : topLeft(nullptr), topRight(nullptr), bottomLeft(nullptr), bottomRight(nullptr) {}

    Node *topLeft, *topRight, *bottomLeft, *bottomRight;
    set<int> pointIds;
};

struct QuadTree {
    QuadTree() : root(nullptr) {}

    void insert(Rect r) {
        insert(Point(r.left, r.bottom, r.id));
        insert(Point(r.left, r.top, r.id));
        insert(Point(r.right, r.bottom, r.id));
        insert(Point(r.right, r.top, r.id));
    }

    void remove(Rect r) {
        remove(Point(r.left, r.bottom, r.id));
        remove(Point(r.left, r.top, r.id));
        remove(Point(r.right, r.bottom, r.id));
        remove(Point(r.right, r.top, r.id));
    }

    void clear() {
        root = clear(root);
    }

    Node* clear(Node* node) {
        if (node == nullptr) {
            return node;
        }

        node->bottomLeft = clear(node->bottomLeft);
        node->topLeft = clear(node->topLeft);
        node->bottomRight = clear(node->bottomRight);
        node->topRight = clear(node->topRight);
        delete node;
        node = nullptr;
        return node;
    }

    void insert(Point p) {
//cerr << "quad insert: " << p.x << " " << p.y << " " << p.id << "\n";
        root = insert(root, 0, 0, MAXXY-1, MAXXY-1, p);
    }

    Node* insert(Node* node, int x1, int y1, int x2, int y2, Point p) {
        if (x1 > p.x || p.x > x2 || y1 > p.y || p.y > y2) {
            return node;
        }
        if (node == nullptr) {
            node = new Node();
        }
        if (x1 == x2) {
//if (p.id == 3) cerr << "rec insert: " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
            node->pointIds.insert(p.id);
            return node;
        }

        int mx = (x1 + x2) >> 1;
        int my = (y1 + y2) >> 1;

        node->bottomLeft = insert(node->bottomLeft, x1, y1, mx, my, p);
        node->topLeft = insert(node->topLeft, x1, my + 1, mx, y2, p);
        node->bottomRight = insert(node->bottomRight, mx + 1, y1, x2, my, p);
        node->topRight = insert(node->topRight, mx + 1, my + 1, x2, y2, p);

        return node;
    }

    void remove(Point p) {
//cerr << "quad remove: " << p.x << " " << p.y << " " << p.id << "\n";
        root = remove(root, 0, 0, MAXXY-1, MAXXY-1, p);
    }

    Node* remove(Node* node, int x1, int y1, int x2, int y2, Point p) {
        if (node == nullptr || x1 > p.x || p.x > x2 || y1 > p.y || p.y > y2) {
            return node;
        }

        if (x1 == x2) {
            node->pointIds.erase(p.id);
            if (node->pointIds.size() == 0) {
                node = clear(node);
            }
            return node;
        }

        int mx = (x1 + x2) >> 1;
        int my = (y1 + y2) >> 1;

        node->bottomLeft = remove(node->bottomLeft, x1, y1, mx, my, p);
        node->topLeft = remove(node->topLeft, x1, my + 1, mx, y2, p);
        node->bottomRight = remove(node->bottomRight, mx + 1, y1, x2, my, p);
        node->topRight = remove(node->topRight, mx + 1, my + 1, x2, y2, p);
        if (node->bottomLeft == nullptr && node->topLeft == nullptr
                && node->bottomRight == nullptr && node->topRight == nullptr) {
            delete node;
            node = nullptr;
        }
        return node;
    }

    int find(Rect r) {
//cerr << "quad find: " << r.left+1 << " " << r.bottom+1 << " " << r.right-1 << " " << r.top-1 << "\n";
        return find(root, 0, 0, MAXXY-1, MAXXY-1, r.left+1, r.bottom+1, r.right-1, r.top-1);
    }

    int find(Node* node, int ax, int ay, int bx, int by, int x1, int y1, int x2, int y2) {
//cerr << "quad rec find: " << ax << " " << ay << " " << bx << " " << by << "\n";
        if (node == nullptr || ax > x2 || x1 > bx || ay > y2 || y1 > by) {
//if (node == nullptr) cerr << "nullptr\n";
            return -1;
        }
        if (ax == bx) {
//cerr << "quad rec find contain\n";
            if (node->pointIds.size() == 0) {
                return -1;
            }
            else {
//cerr << "quad rec find contain\n";
                return *(node->pointIds.begin());
            }
        }

        int mx = (ax + bx) >> 1;
        int my = (ay + by) >> 1;
        int res = -1;
        res = find(node->bottomLeft, ax, ay, mx, my, x1, y1, x2, y2);
        if (res == -1) res = find(node->topLeft, ax, my + 1, mx, by, x1, y1, x2, y2);
        if (res == -1) res = find(node->bottomRight, mx + 1, ay, bx, my, x1, y1, x2, y2);
        if (res == -1) res = find(node->topRight, mx + 1, my + 1, bx, by, x1, y1, x2, y2);
        return res;
    }

    Node *root;
};

int n;
Rect rects[MAXN], result[MAXN];
bool dead[MAXN];

struct ActiveRectangles {
    inline void remove(int id) {
        active.erase(rects[id]);
    }

    inline void insert(int id) {
        active.insert(rects[id]);
    }

    inline bool isActive(int id) {
        return active.find(rects[id]) != active.end();
    }

    inline bool intersects(Line l) {
        Rect mock;
        mock.left = l.right;
        mock.right = l.right;
        auto it = active.upper_bound(mock);
        if (it == active.begin()) {
            return false;
        }
        --it;
        if (it->right <= l.left) {
            return false;
        }
        else {
            return true;
        }
    }

    inline Line getIntersecting(Line l) {
        Rect mock;
        mock.left = l.right;
        mock.right = l.right;
        auto it = active.upper_bound(mock);
        --it;
        return Line(*it, true);
    }

    set<Rect> active;
};

struct LineSet {
    inline void remove(Line l) {
        lines.erase(l);
    }

    inline void insert(Line l) {
        lines.insert(l);
    }

    inline void update(Line prev, Line curr) {
        lines.erase(prev);
        lines.insert(curr);
    }

    set<Line> lines;
};

QuadTree tree;
LineSet linesSet;
ActiveRectangles activeSet;

void killRect(int id) {
    if (activeSet.isActive(id)) {
        activeSet.remove(id);
    }
    linesSet.remove(Line(rects[id], false));
    tree.remove(rects[id]);
    dead[id] = true;
}

Rect mergeRects(int idFrom, int idTo) {
    killRect(idFrom);

    Rect joined = join(rects[idTo], rects[idFrom]);
    linesSet.update(Line(rects[idTo], false), Line(joined, false));
    tree.remove(rects[idTo]);
    rects[idTo] = joined;
    tree.insert(rects[idTo]);
    return joined;
}

void backwardJoin(int id, int y) {
    Rect query = rects[id];
    query.top = y+1;
    int mergeId = tree.find(query);
    while (mergeId != -1) {
        query = mergeRects(mergeId, id);
        query.top = y+1;
        mergeId = tree.find(query);
    }
}

void checkIntersections(Line l) {
    int y = l.y;
    bool intersectionFound = false;
    while (activeSet.intersects(l)) {
        intersectionFound = true;
        Line tmp = activeSet.getIntersecting(l);
        l = Line(mergeRects(tmp.id, l.id), true);
    }
    if (intersectionFound) {
        backwardJoin(l.id, y);
    }
    activeSet.insert(l.id);
}

void sweep() {
    auto it = linesSet.lines.begin();
    while (it != linesSet.lines.end()) {
        Line l = *it;
        if (l.isBottom) {
            checkIntersections(l);
            tree.insert(rects[l.id]);
        }
        else {
            activeSet.remove(l.id);
        }
        ++it;
    }
}

int main() {
    scanf("%d", &n);
    Rect r;
    REP (i, n) {
        scanf("%d %d %d %d", &r.left, &r.right, &r.bottom, &r.top);
        r.id = i;
        rects[i] = r;
        linesSet.insert(Line(r, true));
        linesSet.insert(Line(r, false));
    }

    sweep();

    int res_cnt = 0;
    REP (i, n) {
        if (!dead[i]) {
            result[res_cnt] = rects[i];
            ++res_cnt;
        }
    }
    sort(result, result + res_cnt);

    printf("%d\n", res_cnt);
    REP (i, res_cnt) {
        result[i].print();
    }

    tree.clear();

    return 0;
}