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
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <array>
#include <map>
#define scanf(args...) (scanf(args) ? : 0)
const int MAXN = 1 << 20;
const int INF = 1e9;

using std::array;
int n, count;

struct Quad {
    int low, high, begin, end;

    void fix() {
        if (low > high)
            std::swap(low, high);
        if (begin > end)
            std::swap(begin, end);
    }

    bool operator < (const Quad& q) const {
        array<int, 4> v1 = { begin, end, low, high };
        array<int, 4> v2 = { q.begin, q.end, q.low, q.high };
        return v1 < v2;
    }

    void print() const {
        printf("%d %d %d %d\n", begin, end, low, high);
    }
} quad[MAXN], result[MAXN];

Quad* getQuad() {
    static int allocated;
    static Quad array[2*MAXN];
    return &array[allocated++];
}

struct Tree {
    struct Node {
        const Quad *max, *added, *maxw, *maxh;
        int visited;
        Node(): max(), added(), maxw(), maxh() {
        }
    };

    std::unordered_map<int, Node> m_tree[2*MAXN];
    
    int a1, b1, a2, b2, cast;
    const Quad* val;
    
    void add(int cur1A, int cur1B, int node1, int cur2A, int cur2B, int node2) {
        if (cur1A > b1 || cur1B < a1)
            return;
        if (cur2A > b2 || cur2B < a2)
            return;
        
        Node &node = m_tree[node1][node2];
        if (node.visited == cast)
            return;

        node.visited = cast;
        if (cur1A >= a1 && cur1B <= b1) {
            if (cur2A >= a2 && cur2B <= b2) {
                node.max = val;
                node.added = val;
                return;
            }
        }

        const Quad *r1 = 0, *r2 = 0;
        if (!(cur1A >= a1 && cur1B <= b1)) {
            add(cur1A, (cur1A+cur1B)/2, 2*node1, cur2A, cur2B, node2);
            add((cur1A+cur1B)/2+1, cur1B, 2*node1+1, cur2A, cur2B, node2);
            r1 = std::max(m_tree[2*node1][node2].max, m_tree[2*node1+1][node2].max);
        }
        else 
            node.maxw = std::max(node.maxw, val);

        if (!(cur2A >= a2 && cur2B <= b2)) {
            add(cur1A, cur1B, node1, cur2A, (cur2A+cur2B)/2, 2*node2);
            add(cur1A, cur1B, node1, (cur2A+cur2B)/2+1, cur2B, 2*node2+1);
            r2 = std::max(m_tree[node1][2*node2].max, m_tree[node1][2*node2+1].max);
        }
        else 
            node.maxh = std::max(node.maxh, val);

        node.max = std::max(std::max(r1, r2), node.added);
    }

    void add(int a1, int b1, int a2, int b2, const Quad* val) {
        this->a1 = a1;
        this->b1 = b1;
        this->a2 = a2;
        this->b2 = b2;
        this->val = val;
        this->cast++;
        add(0, MAXN-1, 1, 0, MAXN-1, 1);
    }

    const Quad* query(int cur1A, int cur1B, int node1, int cur2A, int cur2B, int node2) {
        if (cur1A > b1 || cur1B < a1)
            return 0;
        if (cur2A > b2 || cur2B < a2)
            return 0;
        Node& node = m_tree[node1][node2];
        if (node.visited == cast)
            return 0;
        node.visited = cast;
        count++;

        if (cur1A >= a1 && cur1B <= b1)
            if (cur2A >= a2 && cur2B <= b2)
                return node.max;
        
        const Quad* r = node.added;
        if (!(cur1A >= a1 && cur1B <= b1)) {
            const Quad* q1 = query(cur1A, (cur1A+cur1B)/2, 2*node1, cur2A, cur2B, node2);
            r = std::max(r, q1);
            const Quad* q2 = query((cur1A+cur1B)/2+1, cur1B, 2*node1+1, cur2A, cur2B, node2);
            r = std::max(r, q2);
        }
        else
            r = std::max(r, node.maxh);
        
        if (!(cur2A >= a2 && cur2B <= b2)) {
            const Quad* q1 = query(cur1A, cur1B, node1, cur2A, (cur2A+cur2B)/2, 2*node2);
            r = std::max(r, q1);
            const Quad* q2 = query(cur1A, cur1B, node1, (cur2A+cur2B)/2+1, cur2B, 2*node2+1);
            r = std::max(r, q2);
        }
        else
            r = std::max(r, node.maxw);

        return r;
    }

    const Quad* query(int a1, int b1, int a2, int b2) {
        this->a1 = a1;
        this->b1 = b1;
        this->a2 = a2;
        this->b2 = b2;
        this->cast++;
        return query(0, MAXN-1, 1, 0, MAXN-1, 1);
    }
} tree;

bool byEnd(const Quad& q1, const Quad& q2) {
    return q1.end < q2.end;
}

void run() {
    std::sort(quad, quad+n, byEnd);
    for (int i=0; i<n; i++) {
        const Quad& q = quad[i];
        int minlow = q.low, maxhigh = q.high, minbegin = q.begin, maxend = q.end;
        
        while (true) {
            const Quad* q = tree.query(minbegin, maxend-1, minlow, maxhigh-1);
            if (q == nullptr)
                break;
            minlow = std::min(minlow, q->low);
            maxhigh = std::max(maxhigh, q->high);
            minbegin = std::min(minbegin, q->begin);
            maxend = std::max(maxend, q->end);
            
            tree.add(q->begin, q->end-1, q->low, q->high-1, nullptr);
        }
        
        Quad* t = getQuad();
        *t = { minlow, maxhigh, minbegin, maxend };
        tree.add(minbegin, maxend-1, minlow, maxhigh-1, t);
    }
}

int main() {
    scanf("%d", &n);

    for (int i=0; i<n; i++) {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
        quad[i] = { y1, y2, x1, x2 };
        quad[i].fix();
    }
    
    run();
    
    int size = 0;
    while (true) {
        const Quad* q = tree.query(0, MAXN-1, 0, MAXN-1);
        if (q == nullptr)
            break;

        result[size++] = *q;
        tree.add(q->begin, q->end-1, q->low, q->high-1, nullptr);
    }
    
    //printf("%d\n", count);
    //return 0;
    std::sort(result, result+size);
    
    printf("%d\n", size);
    for (int i=0; i<size; i++) {
        result[i].print();
    }
}