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
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <stack>
#include <list>
#include <set>
#include <map>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      ;
#define deb2(X)                     ;

#define M 1000000007
#define INF 1000000007

#define stack vector
#define push PB
#define pop pop_back
#define top back
#define empty empty

using namespace std;

int n, m, a, b, S, wynik;
map <int, int> mapa;

struct prost
{
    int g, d, l, p;
    int org_l, org_p;
    bool byl;
};
prost tab[200007];

struct drzewo
{
    pair <int, int> maks;
    stack <pair<int, int> > st;
};
drzewo d[520007];

static inline bool comp(const prost &p1, const prost &p2)
{
    return p1.g < p2.g;
}

static inline void wkladaj(const int &nr, const int &pocz, const int &kon, const int &p, const int &k, const int &ile, const int &elem)
{
    if(pocz == p && kon == k)
    {
        d[nr].st.push(MP(ile, elem));
    }
    else
    {
        int s = (pocz + kon) >> 1;
        if(k <= s)
            wkladaj(nr*2, pocz, s, p, k, ile, elem);
        else
        {
            if(s < p)
                wkladaj(nr*2+1, s+1, kon, p, k, ile, elem);
            else
            {
                wkladaj(nr*2,  pocz,   s,   p, s, ile, elem);
                wkladaj(nr*2+1, s+1, kon, s+1, k, ile, elem);
            }
        }
    }

    if(nr >= S)
        d[nr].maks = (d[nr].st.empty() ? MP(-1, -1) : d[nr].st.top());
    else
    {
        d[nr].maks = max(d[nr*2].maks, d[nr*2+1].maks);
        if(!d[nr].st.empty())
            d[nr].maks = max(d[nr].maks, d[nr].st.top());
    }
}

static inline void zdejmij(const int &nr, const int &pocz, const int &kon, const int &p, const int &k)
{
    if(pocz == p && kon == k)
    {
        if(d[nr].st.empty())
        {
            printf("ERORR !!!\n");
            return ;
        }
        d[nr].st.pop();
    }
    else
    {
        int s = (pocz + kon) >> 1;
        if(k <= s)
            zdejmij(nr*2, pocz, s, p, k);
        else
        {
            if(s < p)
                zdejmij(nr*2+1, s+1, kon, p, k);
            else
            {
                zdejmij(nr*2,  pocz,   s,   p, s);
                zdejmij(nr*2+1, s+1, kon, s+1, k);
            }
        }
    }

    if(nr >= S)
        d[nr].maks = (d[nr].st.empty() ? MP(-1, -1) : d[nr].st.top());
    else
    {
        d[nr].maks = max(d[nr*2].maks, d[nr*2+1].maks);
        if(!d[nr].st.empty())
            d[nr].maks = max(d[nr].maks, d[nr].st.top());
    }
}

static inline pair<int, int> jaki_maks(const int &nr, const int &pocz, const int &kon, const int &p, const int &k)
{
    //cout << ">";// << nr << "," << p << "," << k << "," << pocz << "," << kon << endl;
    if(pocz == p && kon == k)
    {
        //cout << " jaki maks dla " << pocz << "-" << kon << ": " << d[nr].maks.first << endl;
        return d[nr].maks;
    }

    int s = (pocz + kon) >> 1;
    pair <int, int> moj_maks;
    if(k <= s)
        moj_maks = jaki_maks(nr*2, pocz, s, p, k);
    else
    {
        if(s < p)
            moj_maks = jaki_maks(nr*2+1, s+1, kon, p, k);
        else
        {
            moj_maks =  max(jaki_maks(nr*2,  pocz,   s,   p, s),
                            jaki_maks(nr*2+1, s+1, kon, s+1, k));
        }
    }

    deb(cout << "> " << nr << "\n");
    if(!d[nr].st.empty())
    {
        deb(cout << "tu\n");
        deb(cout << d[nr].st.top().first << " - " << d[nr].st.top().second << "\n");
        moj_maks = max(moj_maks, d[nr].st.top());
    }
    deb(cout << "<\n");

    //cout << "<" << nr << endl;
    return moj_maks;
}

int main()
{
    scanf("%d", &n);
    forr(i, n)
    {
        scanf("%d %d %d %d", &tab[i].l, &tab[i].p, &tab[i].d, &tab[i].g);
        tab[i].org_l = tab[i].l;
        tab[i].org_p = tab[i].p;
        if(tab[i].l >= tab[i].p || tab[i].d >= tab[i].g)
        {
            printf("zle wejscie!\n");
            return 0;
        }
        mapa[tab[i].l] = 1;
        mapa[tab[i].p] = 1;
    }

    int ii = 0;
    FOREACH(it, mapa)
        it->second = ii++;
    forr(i, n)
    {
        tab[i].l = mapa[tab[i].l];
        tab[i].p = mapa[tab[i].p]-1;
    }

    S = 1;
    while(S < ii)
        S *= 2;

    FOR(i, 1, 2*S)
    {
        d[i].maks = MP(-1, -1);
        //d[i].st.push(MP(-1, -1));
    }

    sort(tab, tab+n, comp);

    pair <int, int> p, p2; // <maks, nr_elementu>
    forr(i, n)
    {
        deb(cout << "teraz wklada " << tab[i].l << "-" << tab[i].p << endl);
        deb(cout << "jaki maks\n");
        p = jaki_maks(1, 0, S-1, tab[i].l, tab[i].p);
        deb(cout << "<\n");
        deb(cout << "   maks: " << p.first << ", elem: " << p.second << endl);
        while(p.first > tab[i].d)
        {
            p2 = jaki_maks(1, 0, S-1, tab[p.second].l, tab[p.second].p);
            while(p2.second != p.second)
            {
                p = p2;
                p2 = jaki_maks(1, 0, S-1, tab[p.second].l, tab[p.second].p);
            }

            deb2(cout << "jaki_maks znalazl " << p.first << ", " << p.second << endl);
            deb(cout << "zdejmij\n");
            zdejmij(1, 0, S-1, tab[p.second].l, tab[p.second].p);
            tab[p.second].byl = 1;
            deb(cout << "<\n");

            tab[i].l = min(tab[i].l, tab[p.second].l);
            tab[i].p = max(tab[i].p, tab[p.second].p);
            tab[i].d = min(tab[i].d, tab[p.second].d);
            tab[i].g = max(tab[i].g, tab[p.second].g);

            tab[i].org_l = min(tab[i].org_l, tab[p.second].org_l);
            tab[i].org_p = max(tab[i].org_p, tab[p.second].org_p);


            deb(cout << "jaki maks\n");
            p = jaki_maks(1, 0, S-1, tab[i].l, tab[i].p);
            deb(cout << "<\n");
        }

        deb(cout << "wkladaj\n");
        wkladaj(1, 0, S-1, tab[i].l, tab[i].p, tab[i].g, i);
        deb(cout << "<\n");

        deb(forr(i, 2*S)
            cout << " drzewo[" << i << "] ma maksa " << d[i].maks.first << endl);
    }

    set <pair<pair<int, int>, pair<int, int> > > res;
    forr(i, n)
        if(!tab[i].byl)
        {
            deb(printf("%d %d %d %d\n", tab[i].org_l, tab[i].org_p, tab[i].d, tab[i].g));
            res.insert(MP(MP(tab[i].org_l, tab[i].org_p), MP(tab[i].d, tab[i].g)));
        }

    printf("%d\n", res.size());
    FOREACH(it, res)
    {
        printf("%d %d %d %d\n", it->first.first, it->first.second, it->second.first, it->second.second);
    }

    return 0;
}