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

using namespace std;

const int N=100001;
const int inftyCord=1000001;
const int Pow2 = 1 << 20;
int nn;

struct Intvl {
    int a, b;
    Intvl() {}
    Intvl(int a, int b) : a(a), b(b) {}
    void Join(Intvl i) { a = min(a, i.a); b = max(b, i.b); }
    Intvl Left() { return Intvl(a, (a + b) / 2); }
    Intvl Right() { return Intvl((a + b) / 2, b); }
    bool Covers(Intvl i) { return a <= i.a && i.b <= b; }
    bool Disjoint(Intvl i) { return i.b <= a || b <= i.a; }
    bool operator<(Intvl i) const { return make_pair(a, b) < make_pair(i.a, i.b); }
};

struct Rect {
    Intvl x, y;
    bool removed;
    Rect() : removed(false) {}
    void Join(Rect r) { x.Join(r.x); y.Join(r.y); }
    bool operator<(Rect r) const { return x.b < r.x.b; }
} rectangles[N];

bool lex(Rect a, Rect b) { return make_pair(a.x, a.y) < make_pair(b.x, b.y); }

struct ListElem {
    int x_max;
    int id;
    int next;
};
ListElem list_mem[N * 21];
int list_free = 0;

struct End {
    int x_max;
    int id;
    End() : x_max(-1), id(-1) {}
    End(int x, int id) : x_max(x), id(id) {}
    bool operator<(End e) const { return make_pair(x_max, id) < make_pair(e.x_max, e.id); }
};

struct Node {
    int list_idx;
    End end;
    Node() : list_idx(-1) {}
};

Node tree[Pow2 * 2];

set<End> leaf[Pow2];

void Add(int i, int id, Intvl y, Intvl base, int x_max)
{
    if (y.Covers(base)) {
        Node& n = tree[i];
        ListElem& e = list_mem[list_free];
        e.x_max = x_max;
        e.id = id;
        e.next = n.list_idx;
        n.list_idx = list_free++;
    } else if (!y.Disjoint(base)) {
        Add(2 * i, id, y, base.Left(), x_max);
        Add(2 * i + 1, id, y, base.Right(), x_max);
    }
}

void AddEnd(int y, End end)
{
    leaf[y].insert(end);
    for (int i = Pow2 + y; i > 1; i /= 2)
        tree[i].end = max(tree[i].end, end);
}

void Add(int id, Intvl y)
{
    Rect& r = rectangles[id];
    Add(1, id, y, Intvl(0, Pow2), r.x.b);
    End end(r.x.b, id);
    AddEnd(r.y.a, end);
    if (r.y.b - 1 != r.y.a)
        AddEnd(r.y.b - 1, end);
}

End GetMax(int i, Intvl y, Intvl base)
{
    if (!base.Covers(y))
        return End();
    Node& n = tree[i];
    while (n.list_idx >= 0) {
        ListElem& e = list_mem[n.list_idx];
        if (!rectangles[e.id].removed)
            break;
        n.list_idx = e.next;
    }
    End res;
    if (n.list_idx >= 0) {
        ListElem& e = list_mem[n.list_idx];
        res = End(e.x_max, e.id);
    }
    if (i < Pow2) {
        res = max(res, GetMax(2 * i, y, base.Left()));
        res = max(res, GetMax(2 * i + 1, y, base.Right()));
    }
    return res;
}

End GetMaxEnd(int i, Intvl y, Intvl base)
{
    if (y.Disjoint(base))
        return End();
    if (y.Covers(base))
        return tree[i].end;
    return max(GetMaxEnd(2 * i, y, base.Left()), GetMaxEnd(2 * i + 1, y, base.Right()));
}

int GetMax(Intvl y)
{
    End res = GetMax(1, y, Intvl(0, Pow2));
    res = max(res, GetMaxEnd(1, y, Intvl(0, Pow2)));
    return res.id;
}

void RemoveEnd(int y, End end)
{
    leaf[y].erase(end);
    int i = Pow2 + y;
    tree[i].end = leaf[y].empty() ? End() : *(--leaf[y].end());
    for (i /= 2; i > 0; i /= 2)
        tree[i].end = max(tree[2 * i].end, tree[2 * i + 1].end);
}

void RemoveEnds(int id)
{
    Rect& r = rectangles[id];
    End end(r.x.b, id);
    RemoveEnd(r.y.a, end);
    if (r.y.b - 1 != r.y.a)
        RemoveEnd(r.y.b - 1, end);
}

void Process(int id)
{
    Rect& r = rectangles[id];
    while (true) {
        int xmax_id = GetMax(r.y);
        if (xmax_id < 0)
            break;
        Rect& other = rectangles[xmax_id];
        if (other.x.b <= r.x.a)
            break;
        other.removed = true;
        nn--;
        RemoveEnds(xmax_id);
        r.Join(other);
    }
    Add(id, r.y);
}

int main()
{
    int n; scanf("%d",&n);
    for(int j=0; j<n; ++j) {
        Rect &r = rectangles[j];
        scanf("%d%d%d%d",&r.x.a,&r.x.b,&r.y.a,&r.y.b);
    }
    nn = n;

    sort(rectangles, rectangles + n);

    for(int j=0; j<n; ++j)
        Process(j);

    sort(rectangles, rectangles + n, lex);

    printf("%d\n", nn);
    for(int j=0; j<n; ++j) {
        Rect &r = rectangles[j];
        if (!r.removed)
            printf("%d %d %d %d\n",r.x.a, r.x.b, r.y.a, r.y.b);
    }

    return 0;
}