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
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 1E5;

struct Field
{
   int x1, y1, x2, y2;
};

Field a[N];

inline int readnum()
{
   int a = 0, c;
   while((c = getchar() - '0') < 0);
   for(; c >= 0; c = getchar() - '0') a = 10*a + c;
   return a;
}

inline int compare(const Field &p, const Field &q)
{
   const int dx1 = p.x1 - q.x1;
   const int dx2 = p.x2 - q.x2;
   const int dy1 = p.y1 - q.y1;
   return dx1 ? dx1 : dx2 ? dx2 : dy1 ? dy1 : p.y2 - q.y2;
}

inline bool compareLess(const Field &p, const Field &q)
{
   return compare(p, q) < 0;
}

inline bool intersect(const Field &p, const Field &q)
{
   return p.x1 < q.x2 && q.x1 < p.x2 && p.y1 < q.y2 && q.y1 < p.y2;
}

inline void update(Field &p, const Field &q)
{
   p.x1 = min(p.x1, q.x1);
   p.x2 = max(p.x2, q.x2);
   p.y1 = min(p.y1, q.y1);
   p.y2 = max(p.y2, q.y2);
}

void brute(int n)
{
   for(bool found = true; found; )
   {
      found = false;

      for(int i = 0; i < n; i++)
      {
         for(int j = 0; j < n; j++)
         {
            if (i != j && intersect(a[i], a[j]))
            {
               update(a[i], a[j]);
               a[j] = a[--n];
               found = true;
            }
         }
      }
   }

   sort(a, a + n, compareLess);

   printf("%d", n);

   for(int i = 0; i < n; i++)
   {
      printf("\n%d %d %d %d", a[i].x1, a[i].x2, a[i].y1, a[i].y2);
   }
}

int main()
{
   const int n = readnum();

   for(int i = 0; i < n; i++)
   {
      Field &p = a[i];
      p.x1 = readnum();
      p.x2 = readnum();
      p.y1 = readnum();
      p.y2 = readnum();
   }

   brute(n);
}