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
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <deque>
#include <cmath>

#define REP(i, n) for (int i = 0; i < (n); ++i) 
#define FOR(i, a, b) for (int i = (a); i < (b); ++i) 

using namespace std;

int n;
const int MAX_N = 2000100;
const int MAX = 1000000007;

struct rect {
    int x1, y1, x2, y2;
    bool still_exists;
    int first;

    rect() : still_exists(true), first(-1) {}

    void get() {
        scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
    }

    static bool sort_by_x(const rect& a, const rect& b) {
        if (a.x1 != b.x1) return a.x1 < b.x1;
        if (a.x2 != b.x2) return a.x2 < b.x2;
        if (a.y1 != b.y1) return a.y1 < b.y1;
        return a.y2 < b.y2;
    }
};

rect d[MAX_N];

struct set_sorter {
    bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {
        if (x.first != y.first) return x.first < y.first;
        return x.second < y.second;
    }
};

struct seg {
    set<pair<int, int>, set_sorter> poczatki;
    set<pair<int, int>, set_sorter> wszystkie;
    int start, stop;

    seg() : start(MAX), stop(-1) {}

    void update_start_stop(int x) {
        start = min(start, d[x].x1);
        stop = max(stop, d[x].x1);
    }

    void dodaj_poczatek(int x) {
        poczatki.insert(make_pair(d[x].y1, x));
        poczatki.insert(make_pair(d[x].y2, x));
    }

    void dodaj_wszystkie(int x) {
        wszystkie.insert(make_pair(d[x].y1, x));
        wszystkie.insert(make_pair(d[x].y2, x));
    }

    void get_internal(int a, set<int>& is, set<pair<int, int>, set_sorter>& data) {
        set<pair<int, int>, set_sorter>::iterator it = data.lower_bound(make_pair(d[a].y1, -1));
        while (it != data.end()) {
            if (it->first > d[a].y2) break;
            int b = it->second;
          
            // TODO: nie potrzebne, przynajmniej nie zawsze
            if ((d[a].y1 >= d[b].y1 and d[a].y1 < d[b].y2) or (d[b].y1 >= d[a].y1 and d[b].y1 < d[a].y2)) {
                // czy sie przecinaja na X
                if ((d[a].x1 >= d[b].x1 and d[a].x1 < d[b].x2) or (d[b].x1 >= d[a].x1 and d[b].x1 < d[a].x2)) {
                    is.insert(b);
                }
            }
            
            it++;
        }
    }

    void get_is(int x, set<int>& is) {
        get_internal(x, is, poczatki);
    }

    void get_is_all(int x, set<int>& is) {
//        printf("%lu\n", wszystkie.size());
        get_internal(x, is, wszystkie);
    }

    bool usun(int x) {
        poczatki.erase(make_pair(d[x].y1, x));
        poczatki.erase(make_pair(d[x].y2, x));
        wszystkie.erase(make_pair(d[x].y1, x));
        return wszystkie.erase(make_pair(d[x].y2, x));
    }
};

int num_segs;
seg s[MAX_N / 100 + 5];

void find_is_or_add(int x, set<int>& is) {
    int first = -1;
    REP(i, num_segs) {
        if (s[i].start >= d[x].x2) break;
        if (s[i].stop < d[x].x1) continue;
//        printf("  i = %d\n", i);

        if (s[i].start <= d[x].x2) {
//            printf("    sprawdzam!\n");
            if (first == -1) {
//                printf("get all dla %d\n", i);
                s[i].get_is_all(x, is);
                first = i;
            } else {
                s[i].get_is(x, is);
            }
        }
    }
    
    d[x].first = first;
    if (!is.empty()) return;

    // Dodaj 
    s[first].dodaj_poczatek(x);
    FOR(i, first, num_segs) {
        if (s[i].start > d[x].x2) break;
        s[i].dodaj_wszystkie(x);
    }
}

void create_new_rect(set<int>& is) {
    rect out = d[*is.begin()];

    out.still_exists = true;
    out.first = -1;

    for (set<int>::iterator it = is.begin(); it != is.end(); it++) {
        int a = *it;
        out.x1 = min(out.x1, d[a].x1);
        out.x2 = max(out.x2, d[a].x2);
        out.y1 = min(out.y1, d[a].y1);
        out.y2 = max(out.y2, d[a].y2);
    }

    d[n++] = out;
}

void usun_ze_wszystkich(int x) {
    d[x].still_exists = false;
    FOR(i, d[x].first, num_segs) {
        if (!s[i].usun(x)) break;
    }
}

struct in_sorter {
    bool operator()(const int& a, const int& b) const {
        int a_len = d[a].y2 - d[a].y1;
        int b_len = d[b].y2 - d[b].y1;
        if (a_len != b_len) return a_len < b_len;
        return a < b;
    }
};

int main() {
    scanf("%d", &n);
    REP(i, n) d[i].get(); 
    sort(d, d+n, rect::sort_by_x);

    int limit = max(int(sqrt(n)) + 1, 100);

    // Dodaj poczatki
    num_segs = 0;
    int cur_size = 0;
    REP(i, n) {
        if (s[num_segs].stop != d[i].x1 and cur_size >= limit) {
            num_segs += 1;
            cur_size = 0;
        }
        cur_size += 1;
        s[num_segs].update_start_stop(i);
    }
    num_segs += 1;
//    REP(i, num_segs) printf("seg %d: start = %d, stop = %d\n", i, s[i].start, s[i].stop);

    set<int, in_sorter> in;
    REP(i, n) in.insert(i);

    while (!in.empty()) {
        int i = *in.begin();
        in.erase(in.begin());
//        printf("przerabiam %d, czyli: %d %d %d %d\n", i, d[i].x1, d[i].x2, d[i].y1, d[i].y2);
        set<int> is;
        find_is_or_add(i, is);
       
        if (is.empty()) continue;
//        printf("tworze nowy!\n");

        is.insert(i);
        create_new_rect(is);
        in.insert(n-1);
        
        for (set<int>::iterator it = is.begin(); it != is.end(); it++) {
            usun_ze_wszystkich(*it);
        }
    }

    vector<rect> out;
    REP(i, n) if (d[i].still_exists) out.push_back(d[i]);
    sort(out.begin(), out.end(), rect::sort_by_x);

//    REP(i, num_segs) printf("seg %d: wszystkie.size() = %lu\n", num_segs, s[i].wszystkie.size());

    printf("%d\n", (int)out.size());
    REP(i, out.size()) {
        printf("%d %d %d %d\n", out[i].x1, out[i].x2, out[i].y1, out[i].y2);
    }

    return 0;
}