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
#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
using namespace std;
//const int MAXX = (1<<20);
const int MAXX = (1<<5);
const int MAXN = (1<<17);
typedef pair<int, int> PII;
typedef pair<PII, PII> P4I;
const int NIL = -1;

struct Rect {
  int x1, x2, y1, y2;
  void read() {
    assert(4 == scanf("%d%d%d%d", &x1, &x2, &y1, &y2));
  }
  Rect(int xi1=0, int xi2=0, int yi1=0, int yi2=0) : x1(xi1), x2(xi2), y1(yi1), y2(yi2) {}
  inline Rect sum(const Rect& other) const {
    return Rect(min(x1, other.x1), max(x2, other.x2), min(y1, other.y1), max(y2, other.y2));
  }
  inline bool empty() const {
    return x1 >= x2 or y1 >= y2;
  }
  inline Rect intersect(const Rect& other) const {
    return Rect(max(x1, other.x1), min(x2, other.x2), max(y1, other.y1), min(y2, other.y2));
  }
  inline bool operator<(const Rect& other) const {
    return P4I(PII(x1, x2), PII(y1, y2)) < P4I(PII(other.x1, other.x2), PII(other.y1, other.y2));
  }
  void print() {
    printf("%d %d %d %d\n", x1, x2, y1, y2);
  }
  inline pair<Rect, Rect> splitX(int mid) {
    return pair<Rect, Rect>(Rect(x1, min(x2, mid), y1, y2), Rect(max(x1, mid), x2, y1, y2));
  }
  inline pair<Rect, Rect> split(bool doSplitX) const {
    if (doSplitX) {
      int midX = (x1 + x2) / 2;
      return pair<Rect, Rect>(Rect(x1, midX, y1, y2), Rect(midX, x2, y1, y2));
    } else {
      int midY = (y1 + y2) / 2;
      return pair<Rect, Rect>(Rect(x1, x2, y1, midY), Rect(x1, x2, midY, y2));
    }
  }
  inline bool operator==(const Rect& other) const {
    return P4I(PII(x1, x2), PII(y1, y2)) == P4I(PII(other.x1, other.x2), PII(other.y1, other.y2));
  }
};

typedef pair<Rect, Rect> PRR;

/*struct Node1D {
  int beg, end;
  int all_value;
  int some_value;
  Node1D* left;
  Node1D* right;
  Node1D(int b=0, int e=0, int v=NIL, Node1D* l=nullptr, Node1D* r=nullptr) :
    beg(b), end(e), all_value(v), some_value(v), left(l), right(r) {}
  int getMax(int x1, int x2) {
    if (end <= x1 or x2 <= beg or x2 <= x1) {
      return NIL;
    }
    if ((x1 == beg and x2 == end) or (left == nullptr)) {
      return some_value;
    }
    int mid = (beg + end) / 2;
    return max(left->getMax(x1, min(x2, mid)), right->getMax(max(x1, mid), x2));
  }
  void setValue(int x1, int x2, int v) {
    // printf("setValue[%d, %d](%d, %d, %d): ", beg, end, x1, x2, v);
    if (end <= x1 or x2 <= beg or x2 <= x1) {
      // printf("bad\n");
      return;
    }
    if (x1 == beg and x2 == end) {
      all_value = v;
      deleteChildren();
      updateSomeValue();
      // printf("end\n");
      return;
    }
    // printf("normal\n");
    int mid = (beg + end) / 2;
    if (left == nullptr) {
      left = new Node1D(beg, mid, all_value);
      right = new Node1D(mid, end, all_value);
    }
    all_value = NIL;
    left->setValue(x1, min(x2, mid), v);
    right->setValue(max(x1, mid), x2, v);
    updateSomeValue();
  }
  void updateSomeValue() {
    if (left == nullptr) {
      some_value = all_value;
    } else {
      some_value = max(all_value, max(left->some_value, right->some_value));
    }
  }
  void deleteChildren() {
    if (left != nullptr) {
      left->deleteChildren();
      right->deleteChildren();
      delete left;
      delete right;
      left = nullptr;
      right = nullptr;
    }
  }
}; // */

/*
struct Node1D {
  vector<int> val;
  int beg;
  Node1D(int b=0, int e=0, int v=NIL) : val(e-b), beg(b) {
    for (int i = 0; i < e-b; ++i) {
      val[i] = v;
    }
  }
  int getMax(int x1, int x2) {
    x1 -= beg;
    x2 -= beg;
    int res = NIL;
    for (int i = x1; i < x2; ++i) {
      res = max(res, val[i]);
    }
    return res;
  }
  void setValue(int x1, int x2, int v) {
    x1 -= beg;
    x2 -= beg;
    for (int i = x1; i < x2; ++i) {
      val[i] = v;
    }

  }
}; // */

/*
struct Node2D {
  int beg, end;
  Node1D* all_values;
  Node1D* some_values;
  Node2D* left;
  Node2D* right;
  Node2D(int b=0, int e=0, Node2D* l=nullptr, Node2D* r=nullptr) :
    beg(b), end(e), all_values(new Node1D(0, MAXX, NIL)), some_values(new Node1D(0, MAXX, NIL)), left(l), right(r) {}
  int getMax(Rect r) {
    // printf("Node2D::getMax()\n");
    if (r.empty()) {
      return NIL;
    }
    int v = all_values->getMax(r.y1, r.y2);
    if ((r.x1 == beg and r.x2 == end) or left == nullptr) {
      return max(v, some_values->getMax(r.y1, r.y2));
    } 
    int mid = (beg + end) / 2;
    PRR rs = r.splitX(mid);
    return max(v, max(left->getMax(rs.first), right->getMax(rs.second)));
  }
  void setValue(Rect r, int v) {
    // printf("Node2D::setValue[%d, %d]([%d, %d): %d): ", beg, end, r.x1, r.x2, v);
    if (r.empty()) {
      // printf("bad\n");
      return;
    }
    if (r.x1 == beg and r.x2 == end) {
      all_values->setValue(r.y1, r.y2, v);
      some_values->setValue(r.y1, r.y2, v);
      // printf("end\n");
    } else {
      // printf("normal\n");
      int mid = (beg + end) / 2;
      if (left == nullptr) {
        left = new Node2D(beg, mid);
        right = new Node2D(mid, end);
      }
      PRR rs = r.splitX(mid);
      left->setValue(rs.first, v);
      right->setValue(rs.second, v);
    }
    if (left != nullptr and v == NIL) {
      v = max(all_values->getMax(r.y1, r.y2),
          max(left->some_values->getMax(r.y1, r.y2),
              right->some_values->getMax(r.y1, r.y2)));
      some_values->setValue(r.y1, r.y2, v);
    }
    // printf(" (updating some_values of [%d, %d) ([%d, %d) -> %d))\n", beg, end, r.y1, r.y2, v);
  }
}; // */

/*
struct Node2D {
  int beg;
  vector<Node1D> values;
  Node2D(int b=0, int e=0) : beg(b), values(e-b, Node1D(0, MAXX, NIL)) {}
  int getMax(Rect r) {
    int res = NIL;
    for (int i = r.x1 - beg; beg + i < r.x2; ++i) {
      res = max(res, values[i].getMax(r.y1, r.y2));
    }
    return res;
  }
  void setValue(Rect r, int v) {
    for (int i = r.x1 - beg; i < r.x2 - beg; ++i) {
      values[i].setValue(r.y1, r.y2, v);
    }
  }
}; // */

//Node2D tree(0, MAXX);

Rect rects[MAXN];

struct RectSet {
  map<Rect, int> m;
  RectSet() : m() {}
  int getMax(Rect r) {
    for (auto p : m) {
      if (!p.first.intersect(r).empty())
        return p.second;
    }
    return NIL;
  }

  void setValue(Rect r, int v) {
    if (v != NIL) {
      m[r] = v;
    } else {
      m.erase(r);
    }
  }
};
RectSet tree;

void printTree() {
  printf("   ");
  for (int i = 0; i < 30; ++i) {
    printf("%02d ", i);
  }
  printf("\n");
  for (int i = 0; i < 30; ++i) {
    printf("%02d ", i);
    for (int j = 0; j < 30; ++j) {
      int v = tree.getMax(Rect(j, j+1, i, i+1));
      if (v != NIL) {
        printf("%02d ", v);
      }
      else {
        printf("   ");
      }
    }
    printf("\n");
  }
  printf("\n");
}

int main() {
  int n;
  assert(1 == scanf("%d", &n));
  for (int i = 0; i < n; ++i) {
    Rect r;
    r.read();

    /*
    printf("i: %2d  ", i);
    r.print();
    printTree(); // */
    for (int v = tree.getMax(r); v != NIL; v = tree.getMax(r)) {
    /*
      printf("\tv:%d, rect: ", v); rects[v].print();
      printTree(); // */

      tree.setValue(rects[v], NIL);
      r = r.sum(rects[v]);
      rects[v] = Rect();
      
    /*
      printf("\tremoving rect, r: "); r.print();
      printTree(); //*/
    }
    tree.setValue(r, i);
    rects[i] = r;
  }
  vector<Rect> result;
  for (int i = 0; i < n; ++i) {
    if (not rects[i].empty()) {
      result.push_back(rects[i]);
    }
  }
  sort(result.begin(), result.end());
  printf("%d\n", (int)result.size());
  for (Rect r : result) {
    r.print();
  }
  return 0;
}