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
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <algorithm>
//#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
//#include <cmath>
#include <set>
#include <map>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b);i>=(e);--i)

#define PB push_back
#define ALL(V) (V).begin(),(V).end()
#define SIZE(V) ((int)(V).size())

#define MP make_pair
#define ST first
#define ND second

#define DBG

#ifdef DBG
	#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
	#define debug(...)
#endif

int __stmp;
#define scanf __stmp=scanf


const int INF = 1000000001;
const int MAX = 100010;
const int MAXW = 1000010;

struct rect {
	int x1, x2, y1, y2;
	bool operator<(const rect &rhs) const {
		if(x1 != rhs.x1) return x1 < rhs.x1;
		if(x2 != rhs.x2) return x2 < rhs.x2;
		if(y1 != rhs.y1) return y1 < rhs.y1;
		return y2 < rhs.y2;
	}
};

rect arr[MAX];
bool del[MAX];

class tree {
public:
	void init(int n) {
		for(size=1;size<n;size*=2);
		mx.assign(2*size+3, -1);
		R.assign(2*size+3, VI());
	}
	int get(int b, int e) {
		return _get(1, 0, size, b, e);
	}
	void push(int b, int e, int id) {
		_push(1, 0, size, b, e, id);
	}
	void pop(int id) {
		del[id] = true;
		_pop(1, 0, size, arr[id].y1, arr[id].y2);
	}
private:
	int size;
	VI mx;
	VVI R;
	
	void _push(int v, int b, int e, int k, int l, int id) {
		if(e <= k || l <= b) return;
		if(k <= b && e <= l) {
			R[v].PB(id);
			mx[v] = id;
			return;
		}
		_push(2*v  , b, (b+e)/2, k, l, id);
		_push(2*v+1, (b+e)/2, e, k, l, id);
		_update(v);
	}
	
	void _pop(int v, int b, int e, int k, int l) {
		if(e <= k || l <= b) return;
		if(k <= b && e <= l) {
			_update(v);
			return;
		}
		_pop(2*v  , b, (b+e)/2, k, l);
		_pop(2*v+1, (b+e)/2, e, k, l);
		_update(v);
	}
	
	int _get(int v, int b, int e, int k, int l) {
		if(e <= k || l <= b) return -1;
		if(k <= b && e <= l) return mx[v];
		int ret = _op(
			_get(2*v  , b, (b+e)/2, k, l),
			_get(2*v+1, (b+e)/2, e, k, l)
		);
		if(!R[v].empty()) ret = _op(ret, R[v].back());
		return ret;
	}
	
	inline int _op(int a, int b) {
		if(a == -1 || b == -1) return a+b+1;
		if(arr[a].x2 < arr[b].x2) return b;
		if(arr[a].x2 > arr[b].x2) return a;
		return min(a, b);
	}
	
	inline void _update(int v) {
		while(!R[v].empty() && del[R[v].back()]) R[v].pop_back();
		mx[v] = R[v].empty() ? -1 : R[v].back();
		if(v < size)
			mx[v] = _op(mx[v], _op(mx[2*v], mx[2*v+1]));
	}
};

tree T;

int n;

int main(int argc, char *argv[]) {
	T.init(MAXW);
	scanf("%d", &n);
	REP(i,n) scanf("%d %d %d %d", &arr[i].x1, &arr[i].x2, &arr[i].y1, &arr[i].y2);
	sort(arr, arr+n);
	REP(i,n)
	{
		int x1 = arr[i].x1, x2 = arr[i].x2;
		int y1 = arr[i].y1, y2 = arr[i].y2;
		for(int id=T.get(y1, y2);id!=-1 && x1<arr[id].x2;id=T.get(y1, y2))
		{
			x1 = min(x1, arr[id].x1);
			x2 = max(x2, arr[id].x2);
			y1 = min(y1, arr[id].y1);
			y2 = max(y2, arr[id].y2);
			T.pop(id);
			del[id] = true;
		}
		arr[i] = rect{x1, x2, y1, y2};
		T.push(y1, y2, i);
	}
	vector<rect> res;
	res.reserve(n);
	REP(i,n)
		if(!del[i])
			res.PB(arr[i]);
	sort(ALL(res));
	printf("%d\n", SIZE(res));
	for(rect i : res)
		printf("%d %d %d %d\n", i.x1, i.x2, i.y1, i.y2);
	return 0;
}