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
/* 2014
 * Maciej Szeptuch
 * II UWr
 */
//#define NDEGBUG

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>

using namespace std;

typedef vector<int> Array;

#pragma pack(push)
struct Rectangle
{
    int left;
    int right;
    int top;
    int bottom;

    bool operator<(const Rectangle &rect) const;
}; // struct Rectangle
#pragma pack(pop)

int         rectangles;
Rectangle   rectangle[131072];
Array       mapping;

void renumber(void);
void derenumber(void);

void solve(void);
bool intersects(const Rectangle &A, const Rectangle &B);
void join(Rectangle &dst, const Rectangle &src);

int main(void)
{
    scanf("%d", &rectangles);
    for(int r = 0; r < rectangles; ++ r)
        scanf("%d %d %d %d", &rectangle[r].left, &rectangle[r].right, &rectangle[r].bottom, &rectangle[r].top);

    renumber();

    solve();

    derenumber();

    printf("%d\n", rectangles);
    for(int r = 0; r < rectangles; ++ r)
        printf("%d %d %d %d\n", rectangle[r].left, rectangle[r].right, rectangle[r].bottom, rectangle[r].top);

    return 0;
}

inline
void renumber(void)
{
    mapping.resize(rectangles * 4);
    memcpy(&mapping[0], &rectangle[0].left, sizeof(int) * 4 * rectangles);

    sort(begin(mapping), end(mapping));
    mapping.erase(unique(begin(mapping), end(mapping)), end(mapping));

    transform(&rectangle[0].left, &rectangle[rectangles].left, &rectangle[0].left,
        bind(distance<Array::iterator>, begin(mapping), bind(lower_bound<Array::iterator, int>, begin(mapping), end(mapping), placeholders::_1)));
}

inline
void derenumber(void)
{
    transform(&rectangle[0].left, &rectangle[rectangles].left, &rectangle[0].left,
        [&](int idx){return mapping[idx];});

    sort(rectangle, rectangle + rectangles);
}

inline
void solve(void)
{
    for(int r = 0; r < rectangles; ++ r)
        for(int t = 0; t < rectangles; ++ t)
            if(r != t && intersects(rectangle[r], rectangle[t]))
            {
                join(rectangle[r], rectangle[t]);
                rectangle[t].left = rectangle[t].right = rectangle[t].top = rectangle[t].bottom = -1000;
            }

    int t = 0;
    for(int r = 0; r < rectangles; ++ r)
    {
        if(rectangle[r].left < 0)
            continue;

        rectangle[t ++] = rectangle[r];
    }

    rectangles = t;
}

inline
bool intersects(const Rectangle &A, const Rectangle &B)
{
    return  A.left < B.right && A.right > B.left &&
            A.bottom < B.top && A.top > B.bottom;
}

inline
void join(Rectangle &dst, const Rectangle &src)
{
    dst.left    = min(dst.left,     src.left);
    dst.right   = max(dst.right,    src.right);
    dst.top     = max(dst.top,      src.top);
    dst.bottom  = min(dst.bottom,   src.bottom);
}

inline
bool Rectangle::operator<(const Rectangle &rect) const
{
    const int *a = &left;
    const int *b = &rect.left;
    for(int c = 0; c < 4; ++ a, ++ b, ++ c)
    {
        if(*a < *b)
            return true;

        if(*a > *b)
            return false;
    }

    return false;
}