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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include<cstdio>
#include<tuple>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;


/**
    Segment Tree:
        Structure for storing segments of form (x, y1)--(x,y2) and
        quering for segments intersecting segment (x1, y)--(x2, y)
*/
class SegmentTree{
    private:
        map<int, pair<int, pair<int,int> > > idToSegment;
        map<pair<int, pair<int,int> >, pair<int, int> >segmentToId;
        
        int nextid;
        
        class Node{
            int a, b;
            Node *left, *right;//left = [a, s], right = [s+1, b]
            set<pair<int, int> > items;
            
            //void query(int x1, int x2, set<int> *S);
            void query(int x1, int x2, set<int> *S);
            void queryOrt(int x1, int x2, set<int> *S);
            bool test(int x1, int x2);
            public:
                Node(int a, int b);
                ~Node();
                
                void insert(int id, int x, int y1, int y2);
                void remove(int id, int x, int y1, int y2);
                
                //void query(int x, int y1, int y2, set<int> *S);
                void queryOrt(int x1, int x2, int y, set<int> *S);
                
                //bool test(int x, int y1, int y2);
                bool testOrt(int x1, int x2, int y);
        };
        
        Node *root;
    public:
        SegmentTree(int y0, int y1);//create structure for storing elements 
        ~SegmentTree();
        void insert(int x, int y1, int y2);
        void remove(int x, int y1, int y2);
        void removeAll(int x, int y1, int y2);
        
        //set<pair<int, pair<int, int> > > query(int x, int y1, int y2);
        set<pair<int, pair<int, int> > > queryOrt(int x1, int x2, int y);
        
        //bool test(int x, int y1, int y2);
        bool testOrt(int x1, int x2, int y);
};

//---- INNER NODE -----

SegmentTree::Node::Node(int a, int b):left(NULL), right(NULL){
    this->a = a;
    this->b = b;
    //this->s = (a+b)/2;
}

SegmentTree::Node::~Node(){
    if(this->left != NULL)
        delete this->left;
    
    if(this->right != NULL)
        delete this->right;
}

void SegmentTree::Node::insert(int id, int x, int y1, int y2){
    y1 = max(this->a, y1);
    y2 = min(this->b, y2);
    if(y2<y1)
        return;

    if(y1 == this->a && y2 == this->b){
        this->items.insert(make_pair(x, id));
        return ;
    }
    
    int s = (this->a+this->b)/2;
    
    if(y1<=s){
        if(this->left == NULL)
            this->left = new Node(this->a, s);
        this->left->insert(id, x, y1, min(y2,s));
    }
    if(s+1<=y2){
        if(this->right == NULL)
            this->right = new Node(s+1, this->b);
        this->right->insert(id, x, max(s+1, y1), y2);
    }
}

void SegmentTree::Node::remove(int id, int x, int y1, int y2){
    y1 = max(this->a, y1);
    y2 = min(this->b, y2);
    if(y2<y1)
        return;
    
    if(y1 == this->a && y2 == this->b){
        
        this->items.erase(make_pair(x, id));
        return ;
    }
    
    int s = (this->a+this->b)/2;
    
    if(y1<=s && this->left != NULL)
        this->left->remove(id, x, y1, min(y2,s));
        
    if(s+1<=y2 && this->right != NULL)
        this->right->remove(id, x, max(s+1,y1), y2);
}

bool SegmentTree::Node::test(int x1, int x2){
    for(set<pair<int, int> >::iterator it = this->items.lower_bound(make_pair(x1, -1));it!=this->items.end();it++){
        return (*it).first <= x2;
    }
    return false;
}


bool SegmentTree::Node::testOrt(int x1, int x2, int y){
    if(this->a <=y && y<=this->b)
        return this->test(x1, x2);
    
    int s = (this->a+this->b)/2;
    if(y<=s && this->left != NULL && this->left->testOrt(x1, x2, y))
        return true;
    else if(y>s && this->right != NULL && this->right->testOrt(x1, x2, y))
        return true;
    return false;
}

void SegmentTree::Node::query(int x1, int x2, set<int> *res){
    for(set<pair<int, int> >::iterator it = this->items.lower_bound(make_pair(x1, -1));it!=this->items.end();it++){
        if(x1 <= (*it).first && (*it).first <= x2){
            res->insert((*it).second);
        }else
            break;
    }
}



void SegmentTree::Node::queryOrt(int x1, int x2, int y, set<int> *res){
    if(this->a <=y && y<=this->b)
        this->query(x1, x2, res);
    
    int s = (this->a+this->b)/2;
    if(y<=s && this->left != NULL)
        this->left->queryOrt(x1, x2, y, res);
    else if(y>s && this->right != NULL)
        this->right->queryOrt(x1, x2, y, res);
}

//--------------------------------------

SegmentTree::SegmentTree(int x0, int x1):nextid(0){
    if(x0 > x1)
        throw "Initalized SegmentTree with empty range";
    //assert(x0 < x1);
    root = new Node(x0, x1);
}

SegmentTree::~SegmentTree(){
    delete this->root;
}

void SegmentTree::insert(int x, int y1, int y2){
    map<pair<int, pair<int,int> >, pair<int,int> >::iterator it = this->segmentToId.find(make_pair(x, make_pair(y1, y2)));
    if(it != this->segmentToId.end()){
        (*it).second.second += 1;//increase count
        return ;
    }
    this->segmentToId[make_pair(x, make_pair(y1, y2))] = make_pair(this->nextid, 1);
    this->idToSegment[this->nextid] = make_pair(x, make_pair(y1, y2));
        
    this->root->insert(this->nextid, x, y1, y2);
    this->nextid++;
}

///Remove one copy of segment (x,y1)--(x,y2)
void SegmentTree::remove(int x, int y1, int y2){
    map<pair<int, pair<int,int> >, pair<int,int> >::iterator it = this->segmentToId.find(make_pair(x, make_pair(y1, y2)));
    
    if(it != this->segmentToId.end()){
        if((*it).second.second>1)
            (*it).second.second -= 1;//increase count
        else{
            int id = (*it).second.first;
            this->root->remove(id, x, y1, y2);
            this->idToSegment.erase(id);
            this->segmentToId.erase(it);
        }
    }
}

///Remove all copies of segment (x, y1)--(x, y2)
void SegmentTree::removeAll(int x, int y1, int y2){
    map<pair<int, pair<int,int> >, pair<int,int> >::iterator it = this->segmentToId.find(make_pair(x, make_pair(y1, y2)));
    
    if(it != this->segmentToId.end()){
        int id = (*it).second.first;
        this->root->remove(id, x, y1, y2);
        this->idToSegment.erase(id);
        this->segmentToId.erase(it);
    }
}

set<pair<int, pair<int,int> > > SegmentTree::queryOrt(int x1, int x2, int y){
    set<int> res;
    set<pair<int, pair<int, int> > > segments;
    
    this->root->queryOrt(x1, x2, y, &res);
    for(set<int>::iterator it=res.begin();it!=res.end();it++)
        segments.insert(this->idToSegment[*it]);
        
    return segments;
}

bool SegmentTree::testOrt(int x1, int x2, int y){
    return this->root->testOrt(x1, x2, y);
}


/// ---COUNTER-TREE
class CounterTree{
	private:
		int *T, n;
		int FRAME;

	public:
		CounterTree(int n);
		
		void set(int k, int v);
		void add(int k, int v);
		void inc(int k);
		void dec(int k);

		int query(int k);
		int query(int k0, int k1);
		int less_than(int k);
		int greater_than(int k);

		void clear();
		void erase(int k);
};

CounterTree::CounterTree(int _n):n(_n){
	this->FRAME = 1;
	while(this->FRAME < n)
		this->FRAME<<=1;
	this->T = new int[2*this->FRAME];
	for(int i = 0; i < 2*this->FRAME; i++)
		this->T[i] = 0;
}

void CounterTree::add(int k, int v){
	if(k<0 || k>=this->n)return;
	k += this->FRAME;
	while(k>0){
		this->T[k] += v;
		k/=2;
	}
}

void CounterTree::set(int k, int v){
	this->add(k, v-this->query(k));
}

void CounterTree::inc(int k){
	this->add(k, 1);
}

void CounterTree::dec(int k){
	this->add(k, -1);
}

int CounterTree::less_than(int k){
	if(k<0)return 0;
	if(k>=this->n)return this->T[1];

	k += this->FRAME;

	int res = 0;
	while(k > 0){
		if(2*(k/2)+1 == k)
			res+=this->T[k-1];
		k/=2;
	}
	return res;
}

int CounterTree::greater_than(int k){
	if(k>=this->n)return 0;
	if(k<0)return this->T[1];

	k += this->FRAME;

	int res = 0;
	while(k > 0){
		if(2*(k/2) == k)
			res += this->T[k+1];
		k/=2;
	}
	return res;
}

int CounterTree::query(int k){
	return this->T[k+this->FRAME];
}

int CounterTree::query(int k0, int k1){
	return this->less_than(k1+1)-this->less_than(k0);
}

void CounterTree::clear(){
	for(int i=0;i<2*this->FRAME;i++)
		this->T[i] = 0;
}

void CounterTree::erase(int k){
	this->set(k, 0);
}


/// ------------------------------

SegmentTree Pionowe(0, 1000000), Poziome(0, 1000000);

typedef pair<int, pair<int, int> > segment;

int n, nextR=0;
map<segment, int> Mv, Mh;//Segments -> Rectangles
map<int, tuple<int,int,int,int> > R;//id -> cords [x1,y1,x2,y2]

void query(int x1, int y1, int x2, int y2){
    
    while(true){
        //printf("Query: %d,%d,%d,%d\n",x1,y1,x2,y2);
        set<int> res;
        
        //Znajdz "ramki" prostokątów przecinających się z ramką aktualnego
        for(segment s: Pionowe.queryOrt(x1, x2, y1)){
            //printf("1:[%d, %d]--[%d,%d] = %d\n", s.second.first,s.first,s.second.second,s.first, Mv[s]);
            res.insert(Mv[s]);
        }
            
        for(segment s: Pionowe.queryOrt(x1, x2, y2)){
            //printf("2:[%d, %d]--[%d,%d] = %d\n", s.second.first,s.first,s.second.second,s.first, Mv[s]);
            res.insert(Mv[s]);
        }
            
        for(segment s: Poziome.queryOrt(y1, y2, x1)){
            //printf("3:[%d, %d]--[%d,%d] = %d\n", s.first,s.second.first,s.first,s.second.second, Mh[s]);
            res.insert(Mh[s]);
        }
        
        for(segment s: Poziome.queryOrt(y1, y2, x2)){
            //printf("4:[%d, %d]--[%d,%d] = %d\n", s.first,s.second.first,s.first,s.second.second, Mh[s]);
            res.insert(Mh[s]);
        }

        if(res.empty()){//z niczym się nie przecina
            Mv[make_pair(x1, make_pair(y1, y2))] = nextR;
            Mv[make_pair(x2, make_pair(y1, y2))] = nextR;
            Mh[make_pair(y1, make_pair(x1, x2))] = nextR;
            Mh[make_pair(y2, make_pair(x1, x2))] = nextR;
            R[nextR] = make_tuple(x1, y1, x2, y2);
            Pionowe.insert(x1, y1, y2);
            Pionowe.insert(x2, y1, y2);
            Poziome.insert(y1, x1, x2);
            Poziome.insert(y2, x1, x2);
            nextR++;
            return ;
        }
        
     
        //Usuń znalezione prostokąty
        for(int i: res){
            tuple<int,int,int,int> P = R[i];
            Mv.erase(make_pair(get<0>(P), make_pair(get<1>(P), get<3>(P))));
            Mv.erase(make_pair(get<2>(P), make_pair(get<1>(P), get<3>(P))));
            Mh.erase(make_pair(get<1>(P), make_pair(get<0>(P), get<2>(P))));
            Mh.erase(make_pair(get<3>(P), make_pair(get<0>(P), get<2>(P))));
            R.erase(i);
            
            Pionowe.remove(get<0>(P), get<1>(P), get<3>(P));
            Pionowe.remove(get<2>(P), get<1>(P), get<3>(P));
            
            Poziome.remove(get<1>(P), get<0>(P), get<2>(P));
            Poziome.remove(get<3>(P), get<0>(P), get<2>(P));
            x1 = min(x1, get<0>(P));
            y1 = min(y1, get<1>(P));
            x2 = max(x2, get<2>(P));
            y2 = max(y2, get<3>(P));
        }
        
        //printf("BOX: [%d,%d,%d,%d]\n",get<0>(B),get<1>(B)
        //query(get<0>(B), get<1>(B), get<2>(B), get<3>(B));
        //  x1 = get<0>(B); y1 = get<1>(B); x2 = get<2>(B); g
    }
}

int main(){
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
        x2--;y2--;
        query(x1,y1,x2,y2);
    }
    
    //Usun prostokaty zawarte wewnatrz innych
    vector<tuple<int,int, int,int, int> > V;
    CounterTree intervals(1000005);
    
    for(auto r: R){
        V.push_back(make_tuple(get<0>(r.second), 1, get<1>(r.second), get<3>(r.second), get<2>(r.second)));
        V.push_back(make_tuple(get<2>(r.second)+1, -1, get<1>(r.second), get<3>(r.second), get<0>(r.second)));
        
        //printf("[%d, %d, %d, %d]\n",get<0>(r.second), get<1>(r.second), get<2>(r.second)+1, get<3>(r.second)+1);
    }
    
    
    set<tuple<int,int,int,int> > results;
    sort(V.begin(), V.end());

    for(auto x: V){
        if(get<1>(x) == -1 && results.find(make_tuple(get<4>(x), get<0>(x), get<2>(x), get<3>(x)+1)) != results.end()){
            intervals.dec(get<2>(x));
            intervals.inc(get<3>(x)+1);
        }else{
            if(intervals.less_than(get<2>(x)+1) == 0){
                intervals.inc(get<2>(x));
                intervals.dec(get<3>(x)+1);
                //if(get<0>(x)<=693609 && get<4>(x)+1 >=694170 && get<2>(x) <= 166206 && get<3>(x)+1 >=166820)
                  //  fprintf(stderr, "TAK: %d %d %d %d\n",get<0>(x), get<4>(x)+1, get<2>(x), get<3>(x)+1);
                results.insert(make_tuple(get<0>(x), get<4>(x)+1, get<2>(x), get<3>(x)+1));
            }//else
              //  fprintf(stderr, "%d-> %d %d %d %d\n",intervals.less_than(get<2>(x)+1), get<0>(x), get<4>(x)+1, get<2>(x), get<3>(x)+1);
        }
    }
    //fprintf(stderr, "%d\n",intervals.less_than(1000000));
    //Posortuj i wypisz
    printf("%d\n",results.size());
    for(auto r: results)
        printf("%d %d %d %d\n",get<0>(r), get<1>(r), get<2>(r), get<3>(r));
    return 0;
}