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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
class Rect
{
public:
    Rect() {}
    Rect(int x1, int y1, int x2, int y2, int num = 0) : x1(x1), y1(y1), x2(x2), y2(y2), num(num) {}
    int x1, y1, x2, y2, num;
};

inline bool cmp(const Rect& a, const Rect& b)
{
    if(a.x1 == b.x1)
    {
        if(a.x2 == b.x2)
        {
            if(a.y1 == b.y1)
            {
                return a.y2 < b.y2;
            }
            return a.y1 < b.y1;
        }
        return a.x2 < b.x2;
    }
    return a.x1 < b.x1;
}

bool cross(Rect a, Rect b)
{
    if(a.x1 > b.x2 || a.x2 < b.x1 || a.y1 > b.y2 || a.y2 < b.y1)
        return false;
    return true;
}

int main()
{
    int n;
    scanf("%d", &n);
    vector<Rect> vr;
    Rect r;
    for(int i = 0; i < n; ++i)
    {
        scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2);
        --r.x2;
        --r.y2;
        while(1)
        {
            bool canInsert = true;
            for(int j = 0; j < vr.size(); ++j)
            {
                if(cross(r, vr[j]))
                {
                    r = Rect(min(vr[j].x1, r.x1), min(vr[j].y1, r.y1), max(vr[j].x2, r.x2), max(vr[j].y2, r.y2));
                    vr.erase(vr.begin()+j);
                    canInsert = false;
                    break;
                }
            }
            if(canInsert)
            {
                vr.push_back(r);
                break;
            }
        }
    }
    sort(vr.begin(), vr.end(), cmp);
    printf("%d\n", (int)(vr.size()));
    for(int i = 0; i < vr.size(); ++i)
    {
        printf("%d %d %d %d\n", vr[i].x1, vr[i].x2+1, vr[i].y1, vr[i].y2+1);
    }
    return 0;
}