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
// Krzysztof Baranski
////////////////////////////////////////////////////////////////////
//                                                                //
//                           HEADERS                              //
//                                                                //
//////////////////////////////////////////////////////////////////// 
// includes
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <complex>
#include <iterator>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cstdlib>
#include <ctime>
using namespace std;
// defines
#define FOR(x,b,e) for(int x=b; x<=(e); ++x)
#define FORD(x,b,e) for(int x=(b); x>=(e); --x)
#define FORK(x,b,e,k) for(int x=b; x<=(e); x+=k)
#define REP(x,n) for(int x=0; x<(n); ++x)
#define VAR(v,n) decltype(n) v=(n)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) (int)x.size()
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define CLEAR(x) while(!(x).empty())(x).pop()
#define PB push_back
#define MP make_pair
#define ND second
#define ST first
#define POP(q) q.top(); q.pop()

#define __START__ int __z_;scanf("%d\n",&__z_);while(__z_--){
#define __STOP__ }return 0;
#define _INT(x) int x; scanf("%d",&x) 
// typedef
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef stack<int> SI;
typedef pair<int,int> PII;
typedef stack<PII> SPII;
typedef vector<char> VC;
typedef vector<VC> VVC;
// consts
const int INF = 1000000001;
const long long LINF = 1000000000000000001;
const double EPS = 10e-9;
////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------//
////////////////////////////////////////////////////////////////////

inline void READ(int* a)
{
    register char c=0;
    while((c=getchar_unlocked())<33);
    *a=0;
    do *a=*a*10+c-'0'; while((c=getchar_unlocked())>33);
}

struct Tri {
	int id;
	int x1,x2,y1,y2;
	
	
	Tri(int id,int x1=-1,int x2=-1,int y1=-1,int y2=-1):id(id),x1(x1),x2(x2),y1(y1),y2(y2){}
	
	inline Tri& Load() {
		READ(&x1);READ(&x2);READ(&y1);READ(&y2);
		//cout<<"<< wczytalem: "<<*this<<endl;
		return *this;
	}
	
	/*friend ostream& operator <<(ostream& out,const Tri&t) {
		out<<"PLEMIE nr "<<t.id<<"\t("<<t.x1<<","<<t.x2<<","<<t.y1<<","<<t.y2<<")";
		return out;
	}*/
};
// po wspolrzednych
bool operator<(const Tri&a,const Tri&b){
	return (a.x1==b.x1?(a.x1==b.x2?(a.y1==b.y1?a.y2<b.y2:a.y1<b.y1):a.x2<b.x2):a.x1<b.x1);
}
bool inter(const Tri&a,const Tri&b){
	if(	a.x2<=b.x1 || b.x2<=a.x1 ||
		a.y2<=b.y1 || b.y2<=a.y1) return false;
	return true;
}


// x1
struct Comparator1 {
	bool operator()(const Tri&a,const Tri&b) const {
		return (a.x1==b.x1?a.id<b.id:a.x1<b.x1);
	}
};

struct Area {
	set<Tri,Comparator1> tribes1;
	
	void AddTri(const Tri&t) {
		
		
		int min_x1 = t.x1;
		int min_y1 = t.y1;
		
		int max_x2 = t.x2;
		int max_y2 = t.y2;
		
			
		auto a = tribes1.upper_bound(Tri(0,t.x2,0,0,0));
		
		/*
		cout<<"Szukam przeciecia od poczatku do prostokata: "<<*a<<"czyli:"<<endl;
		for(auto it=tribes1.begin();it!=a;++it) cout<<"    "<<*it<<endl;
		*/
		
		bool intersec=false;
		for(auto it=tribes1.begin();it!=a;){
			
		//while(it!=tribes1.end()) {
			if(inter(t,*it)) {
				intersec=true;
				//cout<<"  Ale znalazlem przeciecie z: "<<*it<<"\n";
				min_x1 = min(min_x1,it->x1);
				min_y1 = min(min_y1,it->y1);
				max_x2 = max(max_x2,it->x2);
				max_y2 = max(max_y2,it->y2);
				auto tmp = it;
				++it;
				tribes1.erase(tmp);
			} else ++it;
		}
		
		if(intersec) {
			/*
			cout<<"\n    < wiec szukam ekstremow\n";
			cout<<"      znalazlem takie: ("<<min_x1<<","<<max_x2<<","<<min_y1<<","<<max_y2<<")\n";
			
			cout<<"    usuwam teraz tamte prostokaty i probuje wstawic ten duzy\n";
			cout<<"    START__________________________________________________\n";
			*/
			AddTri(Tri(t.id,min_x1,max_x2,min_y1,max_y2));
			
			//cout<<"    STOP___________________________________________________\n";

			
		} else {
			tribes1.insert(t);
			//cout<<"  udalo sie\nTERAZ JEST W SECIE:\n";
			//FOREACH(v,tribes1) cout<<"      "<<*v<<endl;
			
		}
		
	}
	
	
};






//==================================================================| MAIN
int main() {	
	srand(time(NULL));
	_INT(n);

	Area a;
	
	FOR(i,1,n) {
		Tri t(i);
		a.AddTri(t.Load());
		
	}
	
	set<Tri> res;
	FOREACH(it,a.tribes1) res.insert(*it);
	printf("%d\n",SIZE(res));
	FOREACH(it,res) printf("%d %d %d %d\n",it->x1,it->x2,it->y1,it->y2);
	

	return 0;
}