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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <stdio.h>


/*******************************
 * Struktury danych
 *******************************/


struct C
{
    C()
        : x1(-1)
        , x2(-1)
        , y1(-1)
        , y2(-1)
    {}

    C(int x1, int x2, int y1, int y2)
        : x1(x1)
        , x2(x2)
        , y1(y1)
        , y2(y2)
    {}

    int x1;
    int x2;
    int y1;
    int y2;
};


struct x2cmp
{
    bool operator()(const C &a, const C &b) const
    {
        if (a.x2 < b.x2)
            return true;
        return false;
    }
};


struct y2cmp
{
    bool operator()(const C &a, const C &b) const
    {
        if (a.y2 < b.y2)
            return true;
        return false;
    }
};


struct lexcmp
{
    bool operator()(const C &a, const C &b) const
    {
        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;
    }
};


/******************************
 *
 ******************************/


typedef std::multiset<C, x2cmp> SetByX2;
typedef SetByX2::const_iterator SetByX2ConstIter;

typedef std::multiset<C, y2cmp> SetByY2;
typedef SetByX2::const_iterator SetByY2ConstIter;

typedef std::map<int, std::map<int, C> > Map;
typedef Map::iterator MapIter;
typedef Map::const_iterator MapConstIter;


/******************************
 * Prototypy funkcji
 ******************************/


void read_data(int n, std::vector<C> &plemiona);
void build_sets(std::vector<C> &plemiona, SetByX2 &setByX2, SetByY2 &setByY2);

void get_spaces(const SetByX2 &setByX2, Map &map);
void get_spaces(const SetByY2 &setByY2, Map &map);
void print_map(const Map &map);

int process(int n, const std::vector<C> &plemiona, Map &map);
void updateC(C &c, const C &dd);


/******************************
 * Main
 ******************************/


int main()
{
    std::ios_base::sync_with_stdio(0);

    //
    int n = 0;
    std::cin >> n;

    std::vector<C> plemiona(n);
    read_data(n, plemiona);

    //
    SetByX2 setByX2;
    SetByY2 setByY2;

    build_sets(plemiona, setByX2, setByY2);

    //
    Map map;
    get_spaces(setByX2, map);
    get_spaces(setByY2, map);

    //
    int liczba_panstw = process(n, plemiona, map);
    std::cout << liczba_panstw << std::endl;

    //
    print_map(map);

    //
    return 0;
}


/*******************************
 * Funkcje
 *******************************/


/**
 *
 */
void read_data(int n, std::vector<C> &plemiona)
{
    for (int i = 0; i < n; ++i)
    {
        C temp;

        std::cin >> temp.x1;
        std::cin >> temp.x2;
        std::cin >> temp.y1;
        std::cin >> temp.y2;

        plemiona[i] = temp;
    }
}

/**
 *
 */
void build_sets(std::vector<C> &plemiona, SetByX2 &setByX2, SetByY2 &setByY2)
{
    size_t n = plemiona.size();

    for (size_t i = 0; i < n; ++i)
    {
        setByX2.insert(plemiona[i]);
        setByY2.insert(plemiona[i]);
    }
}

/**
 *
 */
void get_spaces(const SetByX2 &setByX2, Map &map)
{
    int prev_x2 = 0;
    SetByX2ConstIter iter = setByX2.begin(), end = setByX2.end();

    //
    prev_x2 = iter->x2;
    ++iter;

    //
    for ( ; iter != end; ++iter)
    {
        if (iter->x1 >= prev_x2)
        {
            map.insert(std::make_pair(prev_x2, std::map<int, C>()));
        }

        prev_x2 = iter->x2;
    }

    //
    map.insert(std::make_pair(prev_x2, std::map<int, C>()));
}

/**
 *
 */
void get_spaces(const SetByY2 &setByY2, Map &map)
{
    int prev_y2 = 0;

    SetByX2ConstIter iter = setByY2.begin(), end = setByY2.end();

    //
    prev_y2 = iter->y2;
    ++iter;

    //
    for ( ; iter != end; ++iter)
    {
        if (iter->y1 >= prev_y2)
        {
            MapIter iter = map.begin(), end = map.end();
            for ( ; iter != end; ++iter)
            {
                iter->second.insert(std::make_pair(prev_y2, C()));
            }
        }

        prev_y2 = iter->y2;
    }

    //
    {
        MapIter iter = map.begin(), end = map.end();
        for ( ; iter != end; ++iter)
        {
            iter->second.insert(std::make_pair(prev_y2, C()));
        }
    }
}

/**
 *
 */
void print_map(const Map &map)
{
    MapConstIter iter = map.begin(), end = map.end();
    std::set<C, lexcmp> result;

    //
    for ( ; iter != end; ++iter)
    {
        std::map<int, C>::const_iterator i = iter->second.begin(), e = iter->second.end();

        for ( ; i != e; ++i)
        {
            // DEBUG
            //printf("%d %d\n", iter->first, i->first);
            //printf("--- %d %d %d %d\n", i->second.x1, i->second.x2, i->second.y1, i->second.y2);

            if (i->second.x1 != -1)
            {
                result.insert(i->second);
            }
        }
    }

    //
    {
        std::set<C, lexcmp>::const_iterator iter = result.begin(), end = result.end();

        for ( ; iter != end; ++iter)
        {
            std::cout
                    << iter->x1 << " " << iter->x2 << " "
                    << iter->y1 << " " << iter->y2
                    << std::endl
                    ;
        }
    }
}

/**
 *
 */
int process(int n, const std::vector<C> &plemiona, Map &map)
{
    int liczba_panstw = 0;

    for (int i = 0; i < n; ++i)
    {
        //
        MapIter iter1 = map.lower_bound(plemiona[i].x1);
        if (iter1->first == plemiona[i].x1)
        {
            iter1++;
        }

        //
        std::map<int, C>::iterator iter2 = iter1->second.lower_bound(plemiona[i].y1);
        if (iter2->first == plemiona[i].y1)
        {
            iter2++;
        }

        // DEBUG
        //printf("lower: %d %d\n", iter1->first, iter2->first);

        if (iter2->second.x1 == -1)
        {
            liczba_panstw++;

            iter2->second.x1 = plemiona[i].x1;
            iter2->second.x2 = plemiona[i].x2;
            iter2->second.y1 = plemiona[i].y1;
            iter2->second.y2 = plemiona[i].y2;
        }
        else
        {
            updateC(iter2->second, plemiona[i]);
        }
    }

    return liczba_panstw;
}

/**
 *
 */
void updateC(C &c, const C &dd)
{
    if (c.x1 > dd.x1) c.x1 = dd.x1;
    if (c.x2 < dd.x2) c.x2 = dd.x2;

    if (c.y1 > dd.y1) c.y1 = dd.y1;
    if (c.y2 < dd.y2) c.y2 = dd.y2;
}