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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

const int kMaxX = 1000000;
const int kInfinity = kMaxX + 1;

vector<int> x_1, x_2, y_1, y_2;

class Tree {
 public:
  Tree(const int tribes, const int height) : added_(tribes, false) {
    base_ = 2;
    while (base_ < height) base_ *= 2;
    elem_.resize(2 * base_, -1);
    set_.resize(2 * base_, set<pair<int, int> >());
  };

  bool Delete(const int tribe) {
    if (!added_[tribe]) return false;
    added_[tribe] = false;
    priority_queue<int> q;
    int a = base_ + y_1[tribe];
    int b = base_ + y_2[tribe];
    while (a < b) {
      if (a & 1) {
        set_[a].erase(make_pair(x_2[tribe], tribe));
        if (elem_[a] == tribe) q.push(a);
        a++;
      }
      if (b & 1) {
        b--;
        set_[b].erase(make_pair(x_2[tribe], tribe));
        if (elem_[b] == tribe) q.push(b);
      }
      a /= 2;
      b /= 2;
    }
    Recompute(&q);
    return true;
  }

  void Insert(const int tribe) {
    added_[tribe] = true;
    priority_queue<int> q;
    int a = base_ + y_1[tribe];
    int b = base_ + y_2[tribe];
    while (a < b) {
      if (a & 1) {
        set_[a].insert(make_pair(x_2[tribe], tribe));
        if (elem_[a] == -1 || x_2[tribe] > x_2[elem_[a]]) q.push(a);
        a++;
      }
      if (b & 1) {
        b--;
        set_[b].insert(make_pair(x_2[tribe], tribe));
        if (elem_[b] == -1 || x_2[tribe] > x_2[elem_[b]]) q.push(b);
      }
      a /= 2;
      b /= 2;
    }
    Recompute(&q);
  }

  int Read(const int tribe) {
    int a = base_ + y_1[tribe];
    int b = base_ + y_2[tribe];
    priority_queue<int> q;
    while (a < b) {
      if (a & 1) {
        q.push(a);
        const int elem = elem_[a];
        if (elem != -1 && x_2[elem] > x_1[tribe]) return elem;
        ++a;
      }
      if (b & 1) {
        --b;
        q.push(b);
        const int elem = elem_[b];
        if (elem != -1 && x_2[elem] > x_1[tribe]) return elem;
      }
      a /= 2;
      b /= 2;
    }
    int last = -1;
    while (!q.empty()) {
      if (q.top() != last) {
        last = q.top();
        if (!set_[last].empty()) {
          const int elem = set_[last].rbegin()->second;
          if (elem != -1 && x_2[elem] > x_1[tribe]) return elem;
        }
        if (last > 1) q.push(last / 2);
      }
      q.pop();
    }
    return -1;
  }

 private:
  void Recompute(priority_queue<int> *q) {
    int last = -1;
    while (!q->empty()) {
      if (q->top() != last) {
        last = q->top();
        int e = -1;
        if (!set_[last].empty()) if (e == -1 || x_2[set_[last].rbegin()->second] > x_2[e]) e = set_[last].rbegin()->second;
        if (last < base_) if (elem_[2 * last    ] != -1) if (e == -1 || x_2[elem_[2 * last    ]] > x_2[e]) e = elem_[2 * last    ];
        if (last < base_) if (elem_[2 * last + 1] != -1) if (e == -1 || x_2[elem_[2 * last + 1]] > x_2[e]) e = elem_[2 * last + 1];
        if (e != elem_[last]) {
          elem_[last] = e;
          if (last > 1) q->push(last / 2);
        }
      }
      q->pop();
    }
  }

  int base_;
  vector<bool> added_;
  vector<int> elem_;
  vector<set<pair<int, int> > > set_;
};

int main() {
  int n;
  scanf("%d", &n);
  map<int, int> y_compress;
  x_1.resize(n);
  x_2.resize(n);
  y_1.resize(n);
  y_2.resize(n);

  for (int i = 0; i < n; ++i) {
    scanf("%d%d%d%d", &x_1[i], &x_2[i], &y_1[i], &y_2[i]);
    y_compress[y_1[i]] = -1;
    y_compress[y_2[i]] = -1;
  }
  vector<int> y_decompress;
  for (map<int, int>::iterator i = y_compress.begin(); i != y_compress.end(); ++i) {
    i->second = y_decompress.size();
    y_decompress.push_back(i->first);
  }
  for (int i = 0; i < n; ++i) {
    y_1[i] = y_compress[y_1[i]];
    y_2[i] = y_compress[y_2[i]];
    // cerr << i << ": " << x_1[i] << ' ' << x_2[i] << ' ' << y_1[i] << ' ' << y_2[i] << endl;
  }

  vector<pair<int, int> > events(n);
  for (int i = 0; i < n; ++i) events[i] = make_pair(x_1[i], i);
  sort(events.begin(), events.end());

  vector<pair<pair<int, int>, pair<int, int> > > ret;

  Tree tree(n, y_decompress.size());
  for (int i = 0; i < events.size(); ++i) {
    const pair<int, int> &event = events[i];
    const int tribe = event.second;
    // Start territory.
    while (true) {
      // Check if any tribe occupies our area.
      const int other_tribe = tree.Read(tribe);
      // If not, break.
      if (other_tribe == -1) break;
      // cerr << "Merging " << other_tribe << " into " << tribe << '.' << endl;
      // The other tribe will be merged into ours, we can get rid of it now.
      assert(tree.Delete(other_tribe));
      assert(x_1[tribe] < x_2[other_tribe]);
      assert(x_2[tribe] > x_1[other_tribe]);
      assert(y_1[tribe] < y_2[other_tribe]);
      assert(y_2[tribe] > y_1[other_tribe]);
      // Grow our area.
      x_1[tribe] = min(x_1[tribe], x_1[other_tribe]);
      x_2[tribe] = max(x_2[tribe], x_2[other_tribe]);
      y_1[tribe] = min(y_1[tribe], y_1[other_tribe]);
      y_2[tribe] = max(y_2[tribe], y_2[other_tribe]);
    }
    // Insert into tree, once no longer fits anything.
    tree.Insert(tribe);
  }
  for (int i = 0; i < n; ++i) if (tree.Delete(i)) ret.push_back(make_pair(make_pair(x_1[i], x_2[i]), make_pair(y_decompress[y_1[i]], y_decompress[y_2[i]])));

  // Sort the results.
  sort(ret.begin(), ret.end());
  // Print the results.
  printf("%lu\n", ret.size());
  for (int i = 0; i < ret.size(); ++i) printf("%d %d %d %d\n", ret[i].first.first, ret[i].first.second, ret[i].second.first, ret[i].second.second);

  return 0;
}