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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

int n;
#include <set>

#define SI set<int>

struct Rect {
  int x1,x2,y1,y2;
  Rect() {}
  Rect(int x1_, int x2_, int y1_, int y2_) : x1(x1_), x2(x2_), y1(y1_), y2(y2_) {}

  bool empty() const {
    return x1 >= x2 || y1 >= y2;
  }

  bool isIncludedIn(const Rect &other) const {
    return (other.x1 <= x1 && x2 <= other.x2 &&
            other.y1 <= y1 && y2 <= other.y2);
  }
  bool intersects(const Rect &other) const {
    return (max(x1, other.x1) < min(x2, other.x2) &&
            max(y1, other.y1) < min(y2, other.y2));

  }

  Rect extend(const Rect &other) {
    x1 = min(x1, other.x1);
    x2 = max(x2, other.x2);
    y1 = min(y1, other.y1);
    y2 = max(y2, other.y2);
  }

  bool disjoint(const Rect &other) const {
    return !intersects(other);
  }
  /*
  virtual void print() const {
    //printf("x1: %d, x2: %d, y1: %d, y2: %d.\n", x1, x2, y1, y2);
  }*/

};

class Rectangle : public Rect {
public:
  int nr;
  bool deleted;
  Rectangle() {
    deleted = false;
  }
  void print() const {
    //printf("nr: %d, deleted: %s ", nr, deleted?"true":"false");
    //Rect::print();
  }
};

struct XRectangleComparator {
  bool operator() (const Rectangle &lhs, const Rectangle &rhs) {
    return lhs.x1 < rhs.x1;
  }
} xCmp;

struct YRectangleComparator {
  bool operator() (const Rectangle &lhs, const Rectangle &rhs) {
    return lhs.y1 < rhs.y1;
  }
} yPosCmp;

struct LexRectangleComparator {
  bool operator() (const Rectangle &lhs, const Rectangle &rhs) {
    return MP(MP(lhs.x1, lhs.x2), MP(lhs.y1, lhs.y2)) <
           MP(MP(rhs.x1, rhs.x2), MP(rhs.y1, rhs.y2));
  }
} lexPosCmp;


vector<Rectangle> rects;

struct IsDeletedRectangleNr {
  bool operator() (int nr) {
    return rects[nr].deleted;
  }
} isDeletedRectangleNr;
struct qtree;
vector<qtree*> qtreeDfsStack;

struct qtree {
  Rect r;

  int rectNrCoveringWhole;
  int rectsCoveringPartCount;
  VI rectsVectorCoveringPart;

  qtree *leftTop, *rightTop, *leftBottom, *rightBottom; 
  qtree() : r(), leftTop(NULL), rightTop(NULL), leftBottom(NULL), rightBottom(NULL), rectNrCoveringWhole(-1), rectsCoveringPartCount(0) {}
  qtree(const Rect &r_) : r(r_), leftTop(NULL), rightTop(NULL), leftBottom(NULL), rightBottom(NULL), rectNrCoveringWhole(-1), rectsCoveringPartCount(0) {
    //printf("qtree constructor with: ");
    //r.print();
  }
  void init(const Rect &r_) {
    r = r_;
  }

  void fillIntersectingRectsNumbers(const Rect &rect, SI& intersectingRectsNumbers) {
    qtreeDfsStack.PB(this);
    qtree* curr;
    while (!qtreeDfsStack.empty()) {
      curr = qtreeDfsStack.back();
      qtreeDfsStack.pop_back();

      if (curr->r.disjoint(rect)) {
        continue;
      }
      if (curr->rectNrCoveringWhole >= 0) intersectingRectsNumbers.insert(curr->rectNrCoveringWhole);
      if (curr->rectsCoveringPartCount == 0) {
        continue;
      }
     
      if (curr->r.isIncludedIn(rect)) {
        if (curr->rectsCoveringPartCount != rectsVectorCoveringPart.size()) {
          curr->rectsVectorCoveringPart.erase(remove_if(curr->rectsVectorCoveringPart.begin(), curr->rectsVectorCoveringPart.end(), isDeletedRectangleNr), curr->rectsVectorCoveringPart.end());
        }
        intersectingRectsNumbers.insert(curr->rectsVectorCoveringPart.begin(), curr->rectsVectorCoveringPart.end());
        continue;
      }
      qtreeDfsStack.PB(curr->leftTop);
      qtreeDfsStack.PB(curr->rightTop);
      qtreeDfsStack.PB(curr->leftBottom);
      qtreeDfsStack.PB(curr->rightBottom);
    }
  }

  void deleteFromStructure(const Rect &rect, int nr) {
    qtreeDfsStack.PB(this);
    qtree* curr;
    while (!qtreeDfsStack.empty()) {
      curr = qtreeDfsStack.back();
      qtreeDfsStack.pop_back();

      if (curr->r.isIncludedIn(rect)) {
        curr->rectNrCoveringWhole = -1;
        //nie powinno go byc w Part
        continue;
      }
      if (curr->r.disjoint(rect)) {
        continue;
      }
      curr->rectsCoveringPartCount--;
      qtreeDfsStack.PB(curr->leftTop);
      qtreeDfsStack.PB(curr->rightTop);
      qtreeDfsStack.PB(curr->leftBottom);
      qtreeDfsStack.PB(curr->rightBottom);

      //TODO jesli puste rectsCoveringPart to mozna zwolnic childow, ale jest ryzyko, ze za chwile i tak bedziemy dodawac
    }
  }

  void addToStructure(const Rect &rect, int nr) {
    qtreeDfsStack.PB(this);
    qtree* curr;
    while (!qtreeDfsStack.empty()) {
      curr = qtreeDfsStack.back();
      qtreeDfsStack.pop_back();


      if (curr->r.isIncludedIn(rect)) {
        curr->rectNrCoveringWhole = nr;
        continue;
      }
      if (curr->r.disjoint(rect)) {
        continue;
      }
      curr->rectsVectorCoveringPart.PB(nr);
      curr->rectsCoveringPartCount++;
      //uwaga na za male przedzialy przedzialu 1 x 1 nie dzielimy
      //ale tu mamy cos nierozlacznego i nie zawierajacego sie w rect
      int xMiddle = (curr->r.x1+curr->r.x2)/2;
      int yMiddle = (curr->r.y1+curr->r.y2)/2;
      if (!curr->leftTop)         curr->leftTop = new qtree(Rect(curr->r.x1, xMiddle, yMiddle, curr->r.y2));
      if (!curr->rightTop)       curr->rightTop = new qtree(Rect(xMiddle, curr->r.x2, yMiddle, curr->r.y2));
      if (!curr->leftBottom)   curr->leftBottom = new qtree(Rect(curr->r.x1, xMiddle, curr->r.y1, yMiddle));
      if (!curr->rightBottom) curr->rightBottom = new qtree(Rect(xMiddle, curr->r.x2, curr->r.y1, yMiddle));

      qtreeDfsStack.PB(curr->leftTop);
      qtreeDfsStack.PB(curr->rightTop);
      qtreeDfsStack.PB(curr->leftBottom);
      qtreeDfsStack.PB(curr->rightBottom);

    }
  }
};

void sortAndUniq(VI &vec) {
  sort( vec.begin(), vec.end() );
  vec.erase( unique( vec.begin(), vec.end() ), vec.end() );
}

int toOldX[200009];
int toOldY[200009];
int toNewX[1000009];
int toNewY[1000009];



int main() {
  scanf("%d", &n);
  rects.assign(n, Rectangle());
  VI oldXes, oldYes;
  oldXes.reserve(2*n);
  oldYes.reserve(2*n);
  REP(i,n) {
    Rectangle &r = rects[i];
    r.nr = i;
    scanf("%d%d%d%d", &r.x1, &r.x2, &r.y1, &r.y2);
    oldXes.PB(r.x1);
    oldXes.PB(r.x2);
    oldYes.PB(r.y1);
    oldYes.PB(r.y2);
  }
  sortAndUniq(oldXes);
  sortAndUniq(oldYes);

  int maxX = oldXes.size()+5;
  int maxY = oldYes.size()+5;

  REP(i, oldXes.size()) {
    toOldX[i] = oldXes[i];
    toNewX[oldXes[i]] = i;
  }

  REP(i, oldYes.size()) {
    toOldY[i] = oldYes[i];
    toNewY[oldYes[i]] = i;
  }

  REP(i,n) {
    Rectangle &r = rects[i];
    r.x1 = toNewX[r.x1];
    r.x2 = toNewX[r.x2];
    r.y1 = toNewY[r.y1];
    r.y2 = toNewY[r.y2];
    r.print();
  }

  int notDeletedRects = n;

  int xQtreeSize = 1;
  while (maxX > xQtreeSize) xQtreeSize<<=1;
  int yQtreeSize = 1;
  while (maxY > yQtreeSize) yQtreeSize<<=1;

  qtree q(Rect(0, xQtreeSize, 0, yQtreeSize));

  SI intersectingRectsNumbers;
  REP(i,n) {
    Rectangle &r = rects[i];
    q.fillIntersectingRectsNumbers(r, intersectingRectsNumbers);
    while(!intersectingRectsNumbers.empty()) {
      FOREACH(it, intersectingRectsNumbers) {
        int rectNr = *it;
        Rectangle &intersectingRect = rects[rectNr];
        q.deleteFromStructure(intersectingRect, rectNr);
        intersectingRect.deleted = true;
        notDeletedRects--;
        r.extend(intersectingRect);
      }
      intersectingRectsNumbers.clear();
      q.fillIntersectingRectsNumbers(r, intersectingRectsNumbers);
    }
    q.addToStructure(r, r.nr);
  }

  sort(rects.begin(), rects.end(), lexPosCmp);
  printf("%d\n", notDeletedRects);
  REP(i,n) {
    const Rectangle &r = rects[i];
    if (r.deleted) continue;
    printf("%d %d %d %d\n", toOldX[r.x1], toOldX[r.x2], toOldY[r.y1], toOldY[r.y2]);
  }

  return 0;
}