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
#include<cstdio>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;

const int _N = 100003;
int n;

struct rect
{
    int x1, x2, y1, y2;
    void read() { scanf("%d%d%d%d", &x1, &x2, &y1, &y2); }
    void print() { printf("%d %d %d %d\n", x1, x2, y1, y2); }
    void remove() { x1 = -1; }
    bool valid() const { return x1 >= 0; }
    bool resize(const rect &r)
    {
         bool b = (r.x1 < x1) || (r.x2 > x2) || (r.y1 < y1) || (r.y2 > y2);
         x1 = min(x1, r.x1); x2 = max(x2, r.x2); y1 = min(y1, r.y1); y2 = max(y2, r.y2);
         return b;
    }
} p[_N];

bool intersect(const rect &r1, const rect &r2)
{
    return ((r1.x1 >= r2.x1 && r1.x1 < r2.x2) || (r2.x1 >= r1.x1 && r2.x1 < r1.x2))
        && ((r1.y1 >= r2.y1 && r1.y1 < r2.y2) || (r2.y1 >= r1.y1 && r2.y1 < r1.y2));
}

const int _X = 1000001;
int x[_X], y[_X];

struct node
{
    vector<int> v1, v2;
};

const int _T = 262150;
int MX, MY;
struct hash_func { size_t operator()(int i) const { return i; } };
typedef unordered_map<int, node, hash_func> hashmap;
hashmap t1[_T], t2[_T];
bool resized;

void resize(vector<int> &v, int i)
{
     for (int j=0; j<v.size(); j++)
     {
         if (p[v[j]].valid())
         {
             if (intersect(p[i], p[v[j]]))
             {
                 if (p[i].resize(p[v[j]])) resized = true;
                 p[v[j]].remove();
             }
         }
         else
         {
             v[j] = v.back();
             v.pop_back();
             j--;
         }
     }
}
void resize1(node& v, int i) { resize(v.v1, i); }
void resize2(node& v, int i) { resize(v.v2, i); }

void insert1(node& v, int i)
{
     v.v2.push_back(i);
}

void insert2(node& v, int i)
{
     v.v1.push_back(i);
}

void inserty(hashmap *t, int v, int i)
{
    int v1 = MY + y[p[i].y1], v2 = MY + y[p[i].y2];
    insert1(t[v][v1], i);
    insert1(t[v][v2], i);
    while ((v1>>1) != (v2>>1))
    {
        if ((v1&1)==0) insert1(t[v][v1+1], i);
        if ((v2&1)==1) insert1(t[v][v2-1], i);
        v1 >>= 1; v2 >>= 1;
        insert2(t[v][v1], i);
        insert2(t[v][v2], i);
    }
    do
    {
        v1 >>= 1;
        insert2(t[v][v1], i);
    }
    while (v1 != 1);
}

void inserty1(int v, int i)
{
     inserty(t2, v, i);
}

void inserty2(int v, int i)
{
     inserty(t2, v, i);
}

void insert(int i)
{
    int v1 = MX + x[p[i].x1], v2 = MX + x[p[i].x2];
    inserty1(v1, i);
    inserty1(v2, i);
    while ((v1>>1) != (v2>>1))
    {
        if ((v1&1)==0) inserty1(v1+1, i);
        if ((v2&1)==1) inserty1(v2-1, i);
        v1 >>= 1; v2 >>= 1;
        inserty2(v1, i);
        inserty2(v2, i);
    }
    do
    {
        v1 >>= 1;
        inserty2(v1, i);
    }
    while (v1 != 1);
}

void query1(node &v, int i)
{
    resize1(v, i);
    resize2(v, i);
}

void query2(node &v, int i)
{
    resize2(v, i);
}

void queryy(hashmap *t, int v, int i)
{
    int v1 = MY + y[p[i].y1], v2 = MY + y[p[i].y2];
    query1(t[v][v1], i);
    query1(t[v][v2], i);
    while ((v1>>1) != (v2>>1))
    {
        if ((v1&1)==0) query1(t[v][v1+1], i);
        if ((v2&1)==1) query1(t[v][v2-1], i);
        v1 >>= 1; v2 >>= 1;
        query2(t[v][v1], i);
        query2(t[v][v2], i);
    }
    do
    {
        v1 >>= 1;
        query2(t[v][v1], i);
    }
    while (v1 != 1);
}

void queryy1(int v, int i)
{
    queryy(t1, v, i);
    queryy(t2, v, i);
}

void queryy2(int v, int i)
{
    queryy(t2, v, i);
}

void query(int i)
{
    int v1 = MX + x[p[i].x1], v2 = MX + x[p[i].x2];
    queryy1(v1, i);
    queryy1(v2, i);
    while ((v1>>1) != (v2>>1))
    {
        if ((v1&1)==0) queryy1(v1+1, i);
        if ((v2&1)==1) queryy1(v2-1, i);
        v1 >>= 1; v2 >>= 1;
        queryy2(v1, i);
        queryy2(v2, i);
    }
    do
    {
        v1 >>= 1;
        queryy2(v1, i);
    }
    while (v1 != 1);
}

void init()
{
    scanf("%d", &n);
    vector<int> vx, vy;
    vx.reserve(2*n);
    vy.reserve(2*n);
    for (int i=0; i<n; i++)
    {
        p[i].read();
        vx.push_back(p[i].x1);
        vx.push_back(p[i].x2);
        vy.push_back(p[i].y1);
        vy.push_back(p[i].y2);
    }
    sort(vx.begin(), vx.end());
    vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
    sort(vy.begin(), vy.end());
    vy.resize(unique(vy.begin(), vy.end()) - vy.begin());
    for (int i=0; i<vx.size(); i++) x[vx[i]] = i;
    for (int i=0; i<vy.size(); i++) y[vy[i]] = i;
    MX = MY = 1;
    while (MX < vx.size()) MX <<= 1;
    while (MY < vy.size()) MY <<= 1;
}

void perform()
{
    for (int i=0; i<n; i++)
    {
        do { resized = false; query(i); } while (resized);
        insert(i);
    }
}

bool cmp_rect(int i1, int i2)
{
    const rect &r1 = p[i1];
    const rect &r2 = p[i2];
    if (r1.x1 != r2.x1) return r1.x1 < r2.x1;
    if (r1.x2 != r2.x2) return r1.x2 < r2.x2;
    if (r1.y1 != r2.y1) return r1.y1 < r2.y1;
    return r1.y2 < r2.y2;
}

void print()
{
    vector<int> r;
    r.reserve(n);
    for (int i=0; i<n; i++) if (p[i].valid()) r.push_back(i);
    sort(r.begin(), r.end(), cmp_rect);
    printf("%d\n", r.size());
    for (int i=0; i<r.size(); i++) p[r[i]].print();
}

int main()
{
    init();
    perform();
    print();
    return 0;
}