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
//Krzysztof Pieprzak
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <map>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) typeof(n)v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);

#define DEBUG if(0)

const int    INF = 1000000001;
const double EPS = 10e-9;

const int MAX_N = 100005;
const int MAX_ORYG_COORD = 1000005;
const int MAX_COORD = 200010;
const int MAX_COORD_LOG = 18;

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

    Rect() {}
    Rect(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {}
};

int n;

Rect t[MAX_N];

pair<vi,vi> tree[(1<<(MAX_COORD_LOG+1))];
int start;

int eds[MAX_N<<1];
int mapX[MAX_ORYG_COORD];
int mapY[MAX_ORYG_COORD];
int remapX[MAX_COORD];
int remapY[MAX_COORD];

int mxCoordX;
int mxCoordY;

bool del[MAX_N];
vector<Rect> sol;

void read()
{
    scanf("%d", &n);
    FOR (i, 0, n) scanf("%d %d %d %d", &t[i].x1, &t[i].x2, &t[i].y1, &t[i].y2);
}

void normalize1D(int *mp, int *rmp, int &mxCoord)
{
    sort(eds,eds+(n<<1));

    int cnt = 0;
    mp[eds[0]] = 0;
    rmp[0] = eds[0];
    FOR (i, 1, (n<<1))
    {
        if (eds[i] != eds[i-1])
        {
            mp[eds[i]] = ++cnt;
            rmp[cnt] = eds[i];
        }
    }
    mxCoord = cnt;
}

void normalize()
{
    FOR (i, 0, n)
    {
        eds[i<<1] = t[i].x1;
        eds[i<<1|1] = t[i].x2;
    }
    normalize1D(mapX,remapX,mxCoordX);

    FOR (i, 0, n)
    {
        eds[i<<1] = t[i].y1;
        eds[i<<1|1] = t[i].y2;
    }
    normalize1D(mapY,remapY,mxCoordY);
    mxCoordY <<= 1;

    FOR (i, 0, n)
    {
        t[i].x1 = mapX[t[i].x1];
        t[i].x2 = mapX[t[i].x2];
        if (t[i].x2 < t[i].x1) swap(t[i].x1,t[i].x2);

        t[i].y1 = mapY[t[i].y1]*2;
        t[i].y2 = mapY[t[i].y2]*2;
        if (t[i].y2 < t[i].y1) swap(t[i].y1,t[i].y2);
    }
}

bool cmp(const Rect &a, const Rect &b)
{
    return a.x2 < b.x2;
}

bool cmpSol(const Rect &a, const Rect &b)
{
    if (a.x1 == b.x1)
        if (a.x2 == b.x2)
            if (a.y1 == b.y1) return a.y2 < b.y2;
            else return a.y1 < b.y1;
        else return a.x2 < b.x2;
    return a.x1 < b.x1;
}

void treeInit()
{
    for (start=1; start<=mxCoordY; start<<=1);
}

void treePrint()
{
    for (int lvl = 1; lvl <= start; lvl <<= 1)
    {
        for (int i = 0; i < lvl; ++i)
        {
            printf("[(");
            FOREACH (el, tree[lvl+i].F) printf("%d,", *el);
            printf("),(");
            FOREACH (el, tree[lvl+i].S) printf("%d,", *el);
            printf(")] ");
        }
        printf("\n");
    }
}

int treeFind(int bg, int ed, int v, int vBg, int vEd)
{
    DEBUG printf("treeFind(%d,%d,%d,%d,%d)\n",bg,ed,v,vBg,vEd);
    if (tree[v].F.empty() && tree[v].S.empty()) return -1;

    if (bg == vBg && ed == vEd)
    {
        if (!tree[v].F.empty() && !tree[v].S.empty())
            return t[tree[v].F.back()].x2 < t[tree[v].S.back()].x2 ? tree[v].S.back() : tree[v].F.back();
        else if (!tree[v].F.empty())
            return tree[v].F.back();
        else return tree[v].S.back();
    }

    if (!tree[v].F.empty() && (tree[v].S.empty() || tree[v].S.back() <= tree[v].F.back()))
        return tree[v].F.back();

    int res1 = !tree[v].F.empty() ? tree[v].F.back() : -1;
    int res2 = -1;

    int vMd = (vBg+vEd)>>1;
    if (bg <= vMd)
    {
        res2 = treeFind(bg,min(ed,vMd),v<<1,vBg,vMd);
        if (res1 != -1 && res2 != -1) res1 = t[res1].x2 > t[res2].x2 ? res1 : res2;
        else if (res1 == -1 && res2 != -1) res1 = res2;
    }
    if (ed > vMd)
    {
        res2 = treeFind(max(bg,vMd+1),ed,v<<1|1,vMd+1,vEd);
        if (res1 != -1 && res2 != -1) res1 = t[res1].x2 > t[res2].x2 ? res1 : res2;
        else if (res1 == -1 && res2 != -1) res1 = res2;
    }

    return res1;
}

int treeFind(int y1, int y2, int x)
{
    DEBUG printf("treeFind(%d,%d,%d)\n",y1,y2,x);
    int id = treeFind(y1+1,y2-1,1,0,start-1);

    if (id == -1) return -1;
    return t[id].x2 > x ? id : -1;
}

void treeAdd(int id, int bg, int ed, int v, int vBg, int vEd)
{
    DEBUG printf("treeAdd(%d,%d,%d,%d,%d,%d)\n",id,bg,ed,v,vBg,vEd);

    if (bg == vBg && ed == vEd)
    {
        tree[v].F.PUB(id);
        return;
    }

    tree[v].S.PUB(id);

    int vMd = (vBg+vEd)>>1;
    if (bg <= vMd) treeAdd(id,bg,min(ed,vMd),v<<1,vBg,vMd);
    if (ed > vMd) treeAdd(id,max(bg,vMd+1),ed,v<<1|1,vMd+1,vEd);
}

void treeAdd(int id)
{
    DEBUG printf("treeAdd(%d)\n",id);
    treeAdd(id,t[id].y1,t[id].y2,1,0,start-1);
}

void treeRemove(int bg, int ed, int v, int vBg, int vEd)
{
    DEBUG printf("treeRemove(%d,%d,%d,%d,%d)\n",bg,ed,v,vBg,vEd);
    while (!tree[v].F.empty() && del[tree[v].F.back()]) tree[v].F.POB();
    while (!tree[v].S.empty() && del[tree[v].S.back()]) tree[v].S.POB();

    if (bg == vBg && ed == vEd) return;

    int vMd = (vBg+vEd)>>1;
    if (bg <= vMd) treeRemove(bg,min(ed,vMd),v<<1,vBg,vMd);
    if (ed > vMd) treeRemove(max(bg,vMd+1),ed,v<<1|1,vMd+1,vEd);
}

void treeRemove(int id)
{
    DEBUG printf("treeRemove(%d)\n",id);
    treeRemove(t[id].y1,t[id].y2,1,0,start-1);
}

inline void update(int id1, int id2)
{
    t[id1].x1 = min(t[id1].x1,t[id2].x1);
    t[id1].x2 = max(t[id1].x2,t[id2].x2);

    t[id1].y1 = min(t[id1].y1,t[id2].y1);
    t[id1].y2 = max(t[id1].y2,t[id2].y2);
}

void process()
{
    sort(t,t+n,cmp);

    DEBUG
    {
        FOR (i, 0, n) printf("[(%d,%d),(%d,%d)], ", t[i].x1, t[i].y1, t[i].x2, t[i].y2);
        printf("\n\n");
    }

    treeInit();
    DEBUG {treePrint();printf("\n");}

    int id;
    FOR (i, 0, n)
    {
        id = treeFind(t[i].y1,t[i].y2,t[i].x1);
        if (id == -1) treeAdd(i);
        else
        {
            del[id] = true;
            treeRemove(id);
            update(i,id);
            --i;
        }
        DEBUG {treePrint();printf("\n");}
    }
}

void getSol()
{
    FOR (i, 0, n)
        if (!del[i])
            sol.PUB(Rect(remapX[t[i].x1],remapX[t[i].x2],remapY[t[i].y1/2],remapY[t[i].y2/2]));

    sort(ALL(sol),cmpSol);
}

void print()
{
    printf("%d\n", Size(sol));
    FOREACH (el, sol) printf("%d %d %d %d\n", el->x1, el->x2, el->y1, el->y2);
}

void rob()
{
    read();
    normalize();
    process();
    getSol();
    print();
}

int main()
{
    int test = 1;
    //scanf("%d", &test);

    while (test--) rob();

    return 0;
}