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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <set>
#include <stdlib.h>
//---===---
#include <stdio.h>
#include <unistd.h>

typedef unsigned int uint;

char buf_in[4096000];
int buf_in_head = 0;
int buf_in_tail = 0;

static int getChar(void) {
  while (buf_in_head == buf_in_tail) {
    int rv = read(0, buf_in, sizeof(buf_in));
    if (rv > 0) {
      buf_in_head = 0;
      buf_in_tail = rv;
      break;
    };
    if (rv == 0) return EOF;
  }
  return buf_in[buf_in_head++];
}

// only 1 call is safe, and only if previously getChar() had been called.
static void ungetChar(void) {
  --buf_in_head;
}

static uint getUInt() {
  uint v;
  int c;
  for (;;) {
    c = getChar();
    if (c < '0') continue;
    if (c > '9') continue;
    v = c - '0';
    break;
  };
  for (;;) {
    c = getChar();
    if (c < '0') break;
    if (c > '9') break;
    v *= 10;
    v += c - '0';
  };
  ungetChar();
  return v;
}
//---===---

#define dprintf(v...)

typedef long long quad;

int n; // 1 <= n <= 100,000

struct tt;

typedef struct tt {
  quad x1, x2, y1, y2;
  int neigh[8];
  int fnu;
} tt;

tt T[1 + 100000]; // 17i * 4b * 100000e ~ 6.5MB

int Find(int a) {
  int b = a;

  if (!a) return 0;

  // Find the ultimate parent
  while (T[b].fnu >= 0) {
    b = T[b].fnu;
  };

  // b is now the ultimate parent

  // Update everyone to point at it
  while (T[a].fnu >= 0) {
    int c = a;
    a = T[a].fnu;
    T[c].fnu = b;
  };

  return b;
}

int Union(int a, int b) {
  a = Find(a);
  b = Find(b);
  if (a == b) return 0; // false - no union needed
  if (T[a].fnu < T[b].fnu) {
    // a has high rank (stored negative)
    T[b].fnu = a; // b into a
    return a; // true - non-trivial union performed
  } else if (T[a].fnu > T[b].fnu) {
    // b has has high rank (stored negative)
    T[a].fnu = b; // a into b
    return b; // true - non-trivial union performed
  } else { // T[a].fnu == T[b].fnu
    // equal ranks
    --T[b].fnu; // rank increase
    T[a].fnu = b; // a into b
    return b; // true - non-trivial union performed
  };
}

typedef std::pair<quad,quad> p_qq;
p_qq make_pair(quad a, quad b) {
  return p_qq(a, b);
}

typedef std::pair<p_qq,int> t_qqi;
#define triple0 first.first
#define triple1 first.second
#define triple2 second
t_qqi make_triple(quad a, quad b, int c) {
  return t_qqi(p_qq(a, b),c );
}

typedef std::pair<std::pair<int,int>,std::pair<int,int> > q_iiii;
#define quad0 first.first
#define quad1 first.second
#define quad2 second.first
#define quad3 second.second
q_iiii make_quad(int a, int b, int c, int d) {
  return q_iiii(std::pair<int,int>(a, b), std::pair<int,int>(c, d));
}

typedef std::set<int> set_i;
typedef std::set<int>::iterator set_i_it;

typedef std::set<p_qq> set_qq;
typedef std::set<p_qq>::iterator set_qq_it;
set_qq SBeg;
set_qq SEnd;

typedef std::set<t_qqi> set_qqi;
typedef std::set<t_qqi>::iterator set_qqi_it;
set_qqi S3x; // 3i * 4b * 100000e * 4 ~ 5MB
set_qqi S3y; // 3i * 4b * 100000e * 4 ~ 5MB

typedef std::set<q_iiii> set_iiii;
typedef std::set<q_iiii>::iterator set_iiii_it;

int Merge(int a, int b) {
  dprintf("merge %d with %d\n", a, b);
  a = Find(a);
  b = Find(b);
  int parent = Union(a, b);
  if (!parent) return 0;
  int child = (parent == a) ? b : a;
  if (T[child].x1 < T[parent].x1) {
    T[parent].x1 = T[child].x1;
    T[parent].neigh[0] = T[child].neigh[0];
    T[parent].neigh[3] = T[child].neigh[3];
  };
  if (T[child].x2 > T[parent].x2) {
    T[parent].x2 = T[child].x2;
    T[parent].neigh[4] = T[child].neigh[4];
    T[parent].neigh[7] = T[child].neigh[7];
  };
  if (T[child].y1 < T[parent].y1) {
    T[parent].y1 = T[child].y1;
    T[parent].neigh[1] = T[child].neigh[1];
    T[parent].neigh[6] = T[child].neigh[6];
  };
  if (T[child].y2 > T[parent].y2) {
    T[parent].y2 = T[child].y2;
    T[parent].neigh[2] = T[child].neigh[2];
    T[parent].neigh[5] = T[child].neigh[5];
  };

  //int i = parent;
  dprintf("[%d] %lld.%lld %lld.%lld %lld.%lld %lld.%lld %d|%d %d|%d %d|%d %d|%d\n", i,
    T[i].x1 / 1000000LL, T[i].x1 & 1,
    T[i].x2 / 1000000LL, T[i].x2 & 1,
    T[i].y1 / 1000000LL, T[i].y1 & 1,
    T[i].y2 / 1000000LL, T[i].y2 & 1,
    T[i].neigh[0], T[i].neigh[1], T[i].neigh[2], T[i].neigh[3],
    T[i].neigh[4], T[i].neigh[5], T[i].neigh[6], T[i].neigh[7]);

  return parent;
};

set_qq ExtraMerges;

void Check(int idx1, int idx2) {
  dprintf("check %d %d\n", idx1, idx2);
  long long x1 = T[idx1].x1; if (T[idx2].x1 > x1) x1 = T[idx2].x1;
  long long x2 = T[idx1].x2; if (T[idx2].x2 < x2) x2 = T[idx2].x2;
  long long y1 = T[idx1].y1; if (T[idx2].y1 > y1) y1 = T[idx2].y1;
  long long y2 = T[idx1].y2; if (T[idx2].y2 < y2) y2 = T[idx2].y2;
  if (x1 < x2) if (y1 < y2) {
    dprintf(": %lld.%lld %lld.%lld %lld.%lld %lld.%lld !\n",
      x1 / 1000000LL, x1 & 1,
      x2 / 1000000LL, x2 & 1,
      y1 / 1000000LL, y1 & 1,
      y2 / 1000000LL, y2 & 1);
    ExtraMerges.insert(make_pair(idx1, idx2));
  };
};

int main (void) {
  // 1 <= n <= 100,000
  n = getUInt();

  for (int i = 1; i <= n; ++i) {
    // On Read: 0 <= x1 < x2 <= 1,000,000
    quad x1 = T[i].x1 = getUInt() * 1000000LL + 2 * i + 500001LL;
    quad x2 = T[i].x2 = getUInt() * 1000000LL + 2 * i           ;
    // On Read: 0 <= y1 < y2 <= 1,000,000
    quad y1 = T[i].y1 = getUInt() * 1000000LL + 2 * i + 500001LL;
    quad y2 = T[i].y2 = getUInt() * 1000000LL + 2 * i           ;
    T[i].fnu = -1;
    S3x.insert(make_triple(x1, y1, i));
    S3x.insert(make_triple(x1, y2, i));
    S3x.insert(make_triple(x2, y1, i));
    S3x.insert(make_triple(x2, y2, i));
    S3y.insert(make_triple(y1, x1, i));
    S3y.insert(make_triple(y1, x2, i));
    S3y.insert(make_triple(y2, x1, i));
    S3y.insert(make_triple(y2, x2, i));
  };

  for (set_qqi_it i = S3x.begin(); i != S3x.end(); /*++i*/) {
    set_qqi_it j;
    quad x = i->triple0;
dprintf("(%lld.%lld) [%lld.%lld][%d]\n", x / 1000000LL, x & 1, i->triple1 / 1000000LL, i->triple1 & 1, i->triple2);
    if (x & 1) { // begin
      for (j = i; (j != S3x.end()) && (x == j->triple0); ++j) {
        quad y = j->triple1;
        quad idx = j->triple2;
dprintf("- %lld.%lld %c\n", y / 1000000LL, y & 1, ((int)('@' + idx)));
        if (y & 1) SBeg.insert(make_pair(y, idx)); else SEnd.insert(make_pair(y, idx));
      };
      for (j = i; (j != S3x.end()) && (x == j->triple0); ++j) {
        quad y = j->triple1;
        quad idx = j->triple2;
dprintf("= %lld.%lld %c\n", y / 1000000LL, y & 1, ((int)('@' + idx)));
        if (y & 1) { // begin-begin
          set_qq_it k = SBeg.find(make_pair(y, idx));
set_qq_it z = k;
++z;
if (z != SBeg.end()) Check(idx, z->second);
          if (k != SBeg.begin()) {
            --k;
            T[idx].neigh[0] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 0, ((int)('@' + k->second)));
          };
        } else { // begin-end
          set_qq_it k = SEnd.find(make_pair(y, idx));
          ++k;
          if (k != SEnd.end()) {
            T[idx].neigh[3] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 3, ((int)('@' + k->second)));
          };
        };
      };
    } else { // end
      for (j = i; (j != S3x.end()) && (x == j->triple0); ++j) {
        quad y = j->triple1;
        quad idx = j->triple2;
dprintf("= %lld.%lld %c\n", y / 1000000LL, y & 1, ((int)('@' + idx)));
        if (y & 1) { // end-begin
          set_qq_it k = SBeg.find(make_pair(y, idx));
          if (k != SBeg.begin()) {
            --k;
            T[idx].neigh[7] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 7, ((int)('@' + k->second)));
          };
        } else { // end-end
          set_qq_it k = SEnd.find(make_pair(y, idx));
          ++k;
          if (k != SEnd.end()) {
            T[idx].neigh[4] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 4, ((int)('@' + k->second)));
          };
        };
      };
      for (j = i; (j != S3x.end()) && (x == j->triple0); ++j) {
        quad y = j->triple1;
        quad idx = j->triple2;
dprintf("- %lld.%lld %c\n", y / 1000000LL, y & 1, ((int)('@' + idx)));
        if (y & 1) SBeg.erase(make_pair(y, idx)); else SEnd.erase(make_pair(y, idx));
      };
    };
dprintf("(%lld.%lld) [%lld.%lld][%d]\n", i->triple0 / 1000000LL, i->triple0 & 1, i->triple1 / 1000000LL, i->triple1 & 1, i->triple2);
    i = j;
  };

  for (set_qqi_it i = S3y.begin(); i != S3y.end(); /*++i*/) {
    set_qqi_it j;
    quad y = i->triple0;
dprintf("(%lld.%lld) [%lld.%lld][%d]\n", y / 1000000LL, y & 1, i->triple1 / 1000000LL, i->triple1 & 1, i->triple2);
    if (y & 1) { // begin
      for (j = i; (j != S3y.end()) && (y == j->triple0); ++j) {
        quad x = j->triple1;
        quad idx = j->triple2;
dprintf("- %lld.%lld %c\n", x / 1000000LL, x & 1, ((int)('@' + idx)));
        if (x & 1) SBeg.insert(make_pair(x, idx)); else SEnd.insert(make_pair(x, idx));
      };
      for (j = i; (j != S3y.end()) && (y == j->triple0); ++j) {
        quad x = j->triple1;
        quad idx = j->triple2;
dprintf("= %lld.%lld %c\n", x / 1000000LL, x & 1, ((int)('@' + idx)));
        if (x & 1) { // begin-begin
          set_qq_it k = SBeg.find(make_pair(x, idx));
          if (k != SBeg.begin()) {
            --k;
            T[idx].neigh[1] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 1, ((int)('@' + k->second)));
          };
        } else { // begin-end
          set_qq_it k = SEnd.find(make_pair(x, idx));
          ++k;
          if (k != SEnd.end()) {
            T[idx].neigh[6] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 6, ((int)('@' + k->second)));
          };
        };
      };
    } else { // end
      for (j = i; (j != S3y.end()) && (y == j->triple0); ++j) {
        quad x = j->triple1;
        quad idx = j->triple2;
dprintf("= %lld.%lld %c\n", x / 1000000LL, x & 1, ((int)('@' + idx)));
        if (x & 1) { // end-begin
          set_qq_it k = SBeg.find(make_pair(x, idx));
          if (k != SBeg.begin()) {
            --k;
            T[idx].neigh[2] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 2, ((int)('@' + k->second)));
          };
        } else { // end-end
          set_qq_it k = SEnd.find(make_pair(x, idx));
          ++k;
          if (k != SEnd.end()) {
            T[idx].neigh[5] = k->second;
dprintf("%c %d %c\n", ((int)('@' + idx)), 5, ((int)('@' + k->second)));
          };
        };
      };
      for (j = i; (j != S3y.end()) && (y == j->triple0); ++j) {
        quad x = j->triple1;
        quad idx = j->triple2;
dprintf("- %lld.%lld %c\n", x / 1000000LL, x & 1, ((int)('@' + idx)));
        if (x & 1) SBeg.erase(make_pair(x, idx)); else SEnd.erase(make_pair(x, idx));
      };
    };
dprintf("(%lld.%lld) [%lld.%lld][%d]\n", i->triple0 / 1000000LL, i->triple0 & 1, i->triple1 / 1000000LL, i->triple1 & 1, i->triple2);
    i = j;
  };

  for (set_qq_it i = ExtraMerges.begin(); i != ExtraMerges.end(); ++i) {
    int idx1 = i->first;
    int idx2 = i->second;
    dprintf("extra merge %d %d\n", idx1, idx2);
    Merge(idx1, idx2);
  };

again:
  int changes = 0;
  set_i S;
  for (int i = 1; i <= n; ++i) S.insert(i);

  while (!S.empty()) {
    int i = *(S.begin());
    S.erase(S.begin());
    if (Find(i) != i) continue;
    int merges = 0;
    for (int j = 0; j < 8; ++j) {
      int f = Find(T[i].neigh[j]);
      T[i].neigh[j] = (f != i) ? f : 0;
    };
    for (int j = 0; j < 8; j += 2) {
      if (T[i].neigh[j]) if (T[i].neigh[j] == T[i].neigh[j + 1]) merges = T[i].neigh[j];
    };
    dprintf("%d (%d) %d|%d %d|%d %d|%d %d|%d\n", i, merges,
      T[i].neigh[0], T[i].neigh[1], T[i].neigh[2], T[i].neigh[3],
      T[i].neigh[4], T[i].neigh[5], T[i].neigh[6], T[i].neigh[7]);
    if (merges) {
      merges = Merge(i, merges);
      if (merges) S.insert(merges);
      ++changes;
    };
  };

  if (changes) goto again;

//==
  {
    ExtraMerges.clear();
    set_i S2;
    for (int i = 1; i <= n; ++i) if (Find(i) == i) S2.insert(i);
    if (S2.size() < 50) {
      for (set_i_it i = S2.begin(); i != S2.end(); ++i) {
        for (set_i_it j = i; j != S2.end(); ++j) {
          int a = *i;
          int b = *j;
          if (a != b) Check(a, b);
        };
      };

      for (set_qq_it i = ExtraMerges.begin(); i != ExtraMerges.end(); ++i) {
        int idx1 = i->first;
        int idx2 = i->second;
        dprintf("extra merge %d %d\n", idx1, idx2);
        Merge(idx1, idx2);
      };

    };
  };
//==/

  set_iiii S4;

  for (int i = 1; i <= n; ++i) if (Find(i) == i) {
    int x1 = T[i].x1 / 1000000LL;
    int x2 = T[i].x2 / 1000000LL;
    int y1 = T[i].y1 / 1000000LL;
    int y2 = T[i].y2 / 1000000LL;
    S4.insert(make_quad(x1, x2, y1, y2));
    dprintf("%d %d %d %d\n", x1, x2, y1, y2);
    dprintf("%d: %lld.%lld %lld.%lld %lld.%lld %lld.%lld\n", i,
      T[i].x1 / 1000000LL, T[i].x1 & 1,
      T[i].x2 / 1000000LL, T[i].x2 & 1,
      T[i].y1 / 1000000LL, T[i].y1 & 1,
      T[i].y2 / 1000000LL, T[i].y2 & 1
    );
  };

  printf("%lu\n", S4.size());
  for (set_iiii_it i = S4.begin(); i != S4.end(); ++i) {
    int x1 = i->quad0;
    int x2 = i->quad1;
    int y1 = i->quad2;
    int y2 = i->quad3;
    printf("%d %d %d %d\n", x1, x2, y1, y2);
  };

  return 0;
}