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

#define REP(i, n) for(int i = 0; i < n; i++)
#define FWD(i, a, b) for(int i = a; i < b; i++)
#define ALL(u) (u).begin(), (u).end()

using namespace std;

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

const int INF = 1000000000;

struct rect {
    int id, x1, x2, y1, y2;
    rect(int _id, int _x1, int _x2, int _y1, int _y2) : id(_id), x1(_x1), x2(_x2), y1(_y1), y2(_y2) { }
    rect max_with(rect r) const {
        return rect(-1, min(x1, r.x1), max(x2, r.x2), min(y1, r.y1), max(y2, r.y2));
    }
    bool operator<(rect a) const {
        if (x1 != a.x1) return x1 < a.x1;
        if (x2 != a.x2) return x2 < a.x2;
        if (y1 != a.y1) return y1 < a.y1;
        return y2 < a.y2;
    }
    void print() const {
        printf("rect(id=%d): [x1=%d, x2=%d], [y1=%d, y2=%d]\n", id, x1, x2, y1, y2);
    }
};

struct Renamer {
    vector<int> values;

    void insert(int val) {
        values.push_back(val);
    }

    void done() {
        sort(ALL(values));
        values.resize(unique(ALL(values)) - values.begin());
    }

    int lookup(int val) {
        return lower_bound(ALL(values), val) - values.begin();
    }

    int rlookup(int val) {
        return values[val];
    }

    int range() {
        return values.size();
    }
};

class TwoDTree {
    vector<set<PII>> up, down;
    int pot;

    public:

    void init(int size) {
        pot = 1;
        while(pot < size) pot *= 2;
        up.resize(2 * pot);
        down.resize(2 * pot);
    }

    TwoDTree(int xrange) {
        init(xrange);
    }

    void insert(rect r) {
        //printf("insert:\n");
        //r.print();
        insert_rek(1, 0, pot-1, r);
    }

    void insert_rect(int u, rect r) {
        up[u].insert(make_pair(r.y1, r.id));
        up[u].insert(make_pair(r.y2, r.id));
    }

    void down_insert_rect(int u, rect r) {
        down[u].insert(make_pair(r.y1, r.id));
        down[u].insert(make_pair(r.y2, r.id));
    }

    void print_node(int u, int low, int high) {
        printf("%d [%d, %d]: set size: %d\n", u, low, high, (int)up[u].size());
    }

    //TODO: zawieranie prostokatow - wystarczy isc iteratorem w tyl
    void insert_rek(int u, int low, int high, rect r) {
        insert_rect(u, r);
        //print_node(u, low, high);
        if (r.x1 <= low and high <= r.x2) {
            down_insert_rect(u, r);
            return;
        }
        if (r.x1 <= (low + high) / 2) {
            insert_rek(2 * u, low, (low + high)/2, r);
        }
        if (r.x2 >= (low + high) / 2 + 1) {
            insert_rek(2 * u + 1, (low + high)/2 + 1, high, r);
        }
    }

    void erase(rect r) {
        erase_rek(1, 0, pot-1, r);
    }

    void erase_rect(int u, rect r) {
        up[u].erase(make_pair(r.y1, r.id));
        up[u].erase(make_pair(r.y2, r.id));
    }

    void down_erase_rect(int u, rect r) {
        down[u].erase(make_pair(r.y1, r.id));
        down[u].erase(make_pair(r.y2, r.id));
    }

    //TODO: zawieranie prostokatow - wystarczy isc iteratorem w tyl
    void erase_rek(int u, int low, int high, rect r) {
        erase_rect(u, r);
        if (r.x1 <= low and high <= r.x2) {
            down_erase_rect(u, r);
            return;
        }
        if (r.x1 <= (low + high) / 2) {
            erase_rek(2 * u, low, (low + high)/2, r);
        }
        if (r.x2 >= (low + high) / 2 + 1) {
            erase_rek(2 * u + 1, (low + high)/2 + 1, high, r);
        }
    }

    void query(rect r, vector<int>& answer) {
        //printf("query:\n");
        //r.print();
        answer.clear();
        query_rek(1, 0, pot-1, r, answer);
    }

    void query_rect(set<PII>& the_set, int type, int u, rect r, vector<int>& answer) {
        set<PII>::iterator first = the_set.lower_bound(PII(r.y1, -1)),
            last = the_set.upper_bound(PII(r.y2, INF)); //!
        set<PII>::iterator first_b = first, last_b = last;
        if (first_b != the_set.begin() and last_b != the_set.end()) {
            first_b--;
            if (first_b->second == last_b->second) {
                //printf("query(%d) - surround : %d (%d, %d)\n", type, first_b->second, first_b->first, last_b->first);
                answer.push_back(first_b->second);
            }
        }
        while(first != last) {
            answer.push_back(first->second);
            //printf("query(%d) - inside : %d\n", type, first->second);
            // TODO: is it safe to remove here?
            first++;
        }
    }

    //TODO: zawieranie prostokatow - wystarczy isc iteratorem w tyl
    void query_rek(int u, int low, int high, rect r, vector<int>& answer) {
        query_rect(down[u], 0, u, r, answer);
        if (r.x1 <= low and high <= r.x2) {
            query_rect(up[u], 1, u, r, answer);
            return;
        }
        if (r.x1 <= (low + high) / 2) {
            query_rek(2 * u, low, (low + high)/2, r, answer);
        }
        if (r.x2 >= (low + high) / 2 + 1) {
            query_rek(2 * u + 1, (low + high)/2 + 1, high, r, answer);
        }
    }
};

class Sol {
    public:
    int n;

    void sol() {
        scanf("%d", &n);
        vector<rect> rects, start_rects;
        vector<rect> countries;
        Renamer renamer;
        for(int i = 0; i < n; i++) {
            int x1, x2, y1, y2;
            scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
            if (x1 > x2) swap(x1, x2);
            if (y1 > y2) swap(y1, y2);
            x2--;
            y2--;
            start_rects.push_back(rect({-1, x1, x2, y1, y2}));
            renamer.insert(x1);
            renamer.insert(x2);
        }
        renamer.done();
        for (auto& r : start_rects) {
            r.x1 = renamer.lookup(r.x1);
            r.x2 = renamer.lookup(r.x2);
        }
        TwoDTree tree(renamer.range());
        vector<int> adj;
        // TODO: stykanie prostokatow
        for (auto& r : start_rects) {
            rect stable = r;
            while(true) {
                tree.query(stable, adj);
                if (adj.size() == 0) {
                    stable.id = rects.size();
                    rects.push_back(stable);
                    tree.insert(stable);
                    break;
                }
                r.id = -1;
                for (int rect_id : adj) {
                    //printf("rect_id: %d\n", rect_id);
                    stable = stable.max_with(rects[rect_id]);
                    tree.erase(rects[rect_id]);
                    rects[rect_id].id = -1;
                }
            }
        }
        for (auto r : rects) {
            if (r.id != -1) {
                r.x1 = renamer.rlookup(r.x1);
                r.x2 = renamer.rlookup(r.x2);
                r.x2++;
                countries.push_back(r);
            }
        }
        sort(ALL(countries));
        printf("%d\n", (int)countries.size());
        for (auto& r : countries) {
            printf("%d %d %d %d\n", r.x1, r.x2, r.y1, r.y2+1);
        }
    }
};

int main() {
    Sol s;
    s.sol();
    return 0;
}