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
#include <cassert>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;

#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair

#define INF 1000000001

struct Rect {
    PII x, y;   // x.ST < x.ND && y.ST < y.ND

    Rect(int x1 = 0, int x2 = 0, int y1 = 0, int y2 = 0)
        : x(x1, x2), y(y1, y2) { }

    bool operator<(const Rect& r) const {
        return (x != r.x ? x < r.x : y < r.y);
    }
};

struct ARect {
    PII x, y;
    mutable int cnt;

    ARect(int y1 = 0, int y2 = 0, int x1 = 0, int x2 = 0, int c = 0)
        : x(x1, x2), y(y1, y2), cnt(c) { }

    bool operator<(const ARect& r) const {
        return y.ST < r.y.ST;
    }
};

struct IVal {
    int x;
    PII y;
    int t;  // 0 - left 1 - right edge
    int r;  // Rect

    IVal(int _x = 0, int y1 = 0, int y2 = 0, int _t = 0, int _r = 0)
        : x(_x), y(y1, y2), t(_t), r(_r) { }

    bool operator<(const IVal& i) const {
        if (x != i.x) return x < i.x;
        if (t != i.t) return t > i.t;
        return y < i.y;
    }
};

PII MinMax(const PII& p0, const PII& p1) {
    return MP(min(p0.ST, p1.ST), max(p0.ND, p1.ND));
}

void AddIVal(set<ARect>& s, const IVal& i, const Rect& r)
{
    ARect a(i.y.ST, i.y.ND, r.x.ST, r.x.ND, 1);
    auto v = s.upper_bound(ARect(i.y.ST));
    if (!s.empty() && v != s.begin()) {
        auto w = v;
        --w;
        if (a.y.ST < w->y.ND) v = w;
    }

    while (v != s.end() && v->y.ST < a.y.ND) {
        a.x = MinMax(a.x, v->x);
        a.y = MinMax(a.y, v->y);
        a.cnt += v->cnt;
        s.erase(v++);
    }

    s.insert(a);
}

void DelIVal(set<ARect>& s, const IVal& i, vector<Rect>* vr, vector<IVal>* vi)
{
    auto v = s.lower_bound(ARect(i.y.ND));
    assert(v != s.begin());
    --v;

    if (--v->cnt == 0) {
        int id = SIZE(*vr);
        //Rect r(-v->x.ND, -v->x.ST, -v->y.ND, -v->y.ST);
        Rect r(v->y.ST, v->y.ND, -v->x.ND, -v->x.ST);
        vr->PB(r);
        vi->PB(IVal(r.x.ST, r.y.ST, r.y.ND, 0, id));
        vi->PB(IVal(r.x.ND, r.y.ST, r.y.ND, 1, id));
        s.erase(v);
    }
}

int main()
{
    vector<Rect> R[2], *pr = &R[0], *cr = &R[1];
    vector<IVal> I[2], *pi = &I[0], *ci = &I[1];
    int n, x1, x2, y1, y2;

    scanf("%d\n", &n);
    REP(i, n) {
        scanf("%d %d %d %d\n", &x1, &x2, &y1, &y2);
        pr->PB(Rect(x1, x2, y1, y2));
        pi->PB(IVal(x1, y1, y2, 0, i));
        pi->PB(IVal(x2, y1, y2, 1, i));
    }

    REP(i, 4) {
        sort(ALL(*pi));
        set<ARect> s;
        cr->clear();
        ci->clear();

        FOREACH(it, *pi) {
            if (!it->t)
                AddIVal(s, *it, (*pr)[it->r]);
            else
                DelIVal(s, *it, cr, ci);
        }

        assert(s.empty());
        swap(pr, cr);
        swap(pi, ci);
    }

    sort(ALL(*pr));
    printf("%d\n", SIZE(*pr));
    FOREACH(it, *pr)
        printf("%d %d %d %d\n", it->x.ST, it->x.ND, it->y.ST, it->y.ND);

    return 0;
}