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

#define MAX 100003
#define MAX4 400003

using namespace std;

struct Rect {
  int x1;
  int x2;
  int y1;
  int y2;
  int d;
};

enum EType
{
  eLL = 0,
  eLU = 1,
  eRL = 2,
  eRU = 3
};

struct Point
{
  int r;
  int x;
  int y;
  int d;
  EType t;
};

Rect R[MAX];
Point P[MAX4];

typedef set<int, bool(*)(const int&, const int&)> SetI;
typedef SetI::iterator SetIit;

bool CompareR(const int& a, const int& b)
{
  if (R[a].x1 != R[b].x1)
    return R[a].x1<R[b].x1;
  else if (R[a].x2 != R[b].x2)
    return R[a].x2<R[b].x2;
  if (R[a].y1 != R[b].y1)
    return R[a].y1<R[b].y1;
  else
    return R[a].y2<R[b].y2;
};

bool CompareP(const int& a, const int& b)
{
  if (P[a].x != P[b].x)
    return P[a].x<P[b].x;
  else if(P[a].t>1 && P[b].t<2)
    return 1;
  else if(P[a].t<2 && P[b].t>1)
    return 0;
  else if(P[a].y != P[b].y)
    return P[a].y<P[b].y;
  else if (P[a].t != P[b].t)
    return P[a].t>P[b].t;
  else
    return P[a].r<P[b].r;
};

bool CompareY(const int& a, const int& b)
{  
  if(P[a].r==P[b].r && P[a].t==P[b].t)
    return 0;
  else if (P[a].y != P[b].y)
    return P[a].y < P[b].y;
  else if(P[a].t==eLL && (P[b].t==eLL || P[b].t==eRL) && P[a].x != P[b].x)
    return P[a].x < P[b].x;
  else if(P[a].t==eLL && P[b].t==eLL)
    return P[a].r < P[b].r;
  else if(P[a].t==eLL && P[b].t==eRL)
    return 0;
  else if(P[a].t==eLL && (P[b].t==eRU || P[b].t==eLU))
    return 0;
  else if(P[a].t==eLU && (P[b].t==eLL || P[b].t==eRL))
    return 1;
  else if(P[a].t==eLU && (P[b].t==eLU || P[b].t==eRU))
    return 1;
  else if((P[a].t==eRL || P[a].t==eRU) && (P[b].t==eLL || P[b].t==eRL))
    return 1;
  else if((P[a].t==eRL || P[a].t==eRU) && (P[b].t==eLU || P[b].t==eRU))
    return 0;
  
  return 1;
};

SetI all(CompareP);
SetI sY(CompareY);
SetI sR(CompareR);

int n, n4;

void join(int r1, int r2)
{
  int p1,p2;
  
  p1 = r1*4;
  p2 = r2*4;
 
  all.erase(p1);
  all.erase(p1+1);
  all.erase(p1+2);
  all.erase(p1+3);
  all.erase(p2);
  all.erase(p2+1);
  all.erase(p2+2);
  all.erase(p2+3);

  R[r1].x1 = min(R[r1].x1, R[r2].x1);
  R[r1].x2 = max(R[r1].x2, R[r2].x2);
  R[r1].y1 = min(R[r1].y1, R[r2].y1);
  R[r1].y2 = max(R[r1].y2, R[r2].y2);
  
  R[r2].d = 0;
  
  P[p1].x = R[r1].x1;
  P[p1].y = R[r1].y1;
  P[p1+1].x = R[r1].x1;
  P[p1+1].y = R[r1].y2;
  P[p1+2].x = R[r1].x2;
  P[p1+2].y = R[r1].y1;
  P[p1+3].x = R[r1].x2;
  P[p1+3].y = R[r1].y2;
  
  P[p2].d =P[p2+1].d = P[p2+2].d = P[p2+3].d = 0;

  all.insert(p1);
  all.insert(p1+1);
  all.insert(p1+2);
  all.insert(p1+3);
}

void doAll()
{
  SetIit it = all.begin();
  SetIit itf;
  int p, p2;
  while(it!=all.end())
  {
    p = *it;
    
    if(sY.empty())
    {
      sY.insert(p);
      ++it;
      continue;
    }
    if((itf = sY.lower_bound(p)) == sY.begin())
    {
      sY.insert(p);
      ++it;
      continue;
    }
    --itf;
    p2 = *itf;
    switch(P[p].t)
    {
      case eLL:
        if(P[p2].t==eLL)
        {
          join(P[p].r, P[p2].r);
          sY.clear();
          it=all.begin();
          continue;
        }
        else
         sY.insert(p);
        break;
      case eLU:
        if(P[p2].t==eLL && P[p].r == P[p2].r)
        {
          sY.insert(p);
        }
        else
        {
          join(P[p].r, P[p2].r);
          sY.clear();
          it=all.begin();
          continue;
        }
        break;
      case eRL:
        if(P[p2].t==eLL && P[p].r != P[p2].r)
        {
          join(P[p].r, P[p2].r);
          sY.clear();
          it=all.begin();
          continue;
        }
        break;
      case eRU:
        if(P[p2].t==eLL && P[p].r != P[p2].r)
        {
          join(P[p].r, P[p2].r);
          sY.clear();
          it=all.begin();
          continue;
        }
        else
        {
          sY.erase(4*(p/4));
          sY.erase(4*(p/4)+1);
        }
        break;
    }
    ++it;
  }
}

int main()
{
  ios_base::sync_with_stdio(0);
  
  int x1, y1, x2, y2;
  
  cin >> n;
  n4 = n*4;
  for(int i=0; i<n; ++i)
  {
    cin >> x1 >> x2 >> y1 >> y2;
    
    R[i].x1 = x1;
    R[i].x2 = x2;
    R[i].y1 = y1;
    R[i].y2 = y2;
    R[i].d = 1;
    
    P[4*i].x = x1;
    P[4*i].y = y1;
    P[4*i].t = eLL;
    
    P[4*i+1].x = x1;
    P[4*i+1].y = y2;
    P[4*i+1].t = eLU;
    
    P[4*i+2].x = x2;
    P[4*i+2].y = y1;
    P[4*i+2].t = eRL;
    
    P[4*i+3].x = x2;
    P[4*i+3].y = y2;
    P[4*i+3].t = eRU;
    
    P[4*i].r = P[4*i+1].r = P[4*i+2].r = P[4*i+3].r = i;
    
    all.insert(4*i);
    all.insert(4*i+1);
    all.insert(4*i+2);
    all.insert(4*i+3);
  }
  
  doAll();
 
  for(int i=0; i<n; ++i)
  {
    if(R[i].d)
      sR.insert(i);
  }
 
 cout << sR.size()<<endl;
 for(SetIit it = sR.begin(); it!=sR.end(); ++it)
 {
   cout << R[*it].x1<<" "<< R[*it].x2<<" "<< R[*it].y1<<" "<< R[*it].y2<<endl;
 }
 return 0;
}