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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

bool debugFlag = false;

int main() {
	std::ios_base::sync_with_stdio (false);

	int n;

	cin >> n;

	map<long, map<long, map<long, map<long, int> > > > frob;

	long x1, x2, y1, y2;
	vector<long> x1v, x2v, y1v, y2v;
	if (n == 1) {
		cin >> x1;
		cin >> x2;
		cin >> y1;
		cin >> y2;
		cout << x1 << " " << x2 << " " << y1 << " " << y2 << endl;
		return 0;
	}		

	for (int i = 0; i < n; ++i) {
		cin >> x1;
		cin >> x2;
		cin >> y1;
		cin >> y2;
		
		x1v.push_back(x1);
		x2v.push_back(x2);
		y1v.push_back(y1);
		y2v.push_back(y2);

		if (debugFlag) cout << "read: " << x1 << ", " << x2 << ", " << y1 << ", " << y2 << endl;
	}
		
	x1 = x1v[0];
	x2 = x2v[0];
	y1 = y1v[0];
	y2 = y2v[0];
	frob[x1][x2][y1][y2] = 0;
	int i = 1;
	bool next = true;
	/*
	for (int i = 0; i < n; ++i) {
		frob[x1v[i]][x2v[i]][y1v[i]][y2v[i]] = i;
	}
	*/
	long cx1, cx2, cy1, cy2; // current elems for search
	while (i < n) {
		if (next) {
			cx1 = x1v[i];
			cx2 = x2v[i];
			cy1 = y1v[i];
			cy2 = y2v[i];
		}
		if (debugFlag) cout << "processing: " << cx1 << ", " << cx2 << ", " << cy1 << ", " << cy2 << endl;
		next = true;
		// x1 < cx2
		for (map<long, map<long, map<long, map<long, int> > > >::iterator itx1 = frob.begin(); itx1 != frob.lower_bound(cx2); ++itx1) {
			if (debugFlag) cout << itx1->first << endl;
			x1 = itx1->first;
			// x2 > cx1
			for (map<long, map<long, map<long, int> > >::iterator itx2 = itx1->second.upper_bound(cx1); itx2 != itx1->second.end(); ++itx2) {
				if (debugFlag) cout << "\t" << itx2->first << endl;
				x2 = itx2->first;
				// y1 < cy2
				for ( map<long, map<long, int> >::iterator ity1 = itx2->second.begin(); ity1 != itx2->second.lower_bound(cy2); ++ity1) {
					if (debugFlag) cout << "\t\t" << ity1->first << endl;
					y1 = ity1->first;
					// y2 < cy1, we found something overlapping
					for (map<long, int>::iterator ity2 = ity1->second.upper_bound(cy1); ity2 != ity1->second.end(); ++ity2) {
						y2 = ity2->first;
						if (ity2->second == -1)
							continue;
						ity2->second = -1;
						// enlarge current with found
						if (x1 < cx1)
							cx1 = x1;
						if (x2 > cx2)
							cx2 = x2;
						if (y1 < cy1)
							cy1 = y1;
						if (y2 > cy2)
							cy2 = y2;
						// enlist found in del
						if (debugFlag) cout << "got here" << endl;
						// set next to false
						next = false;
						if (debugFlag) cout << "\t\t\t" << ity2->first << " " << ity2->second << endl;
						ity1->second.erase(ity2);
					}
					// if empty erase current
					if (ity1->second.empty()) {
						itx2->second.erase(ity1);
					}
				}
				if (itx2->second.empty()) {
					itx1->second.erase(itx2);
				}
				// if empty erase current
			}
			// if empty erase current
			if (itx1->second.empty()) {
				frob.erase(itx1);
			}
		}
		if (next) {
			frob[cx1][cx2][cy1][cy2] = i;
			++i;
		}
	}
	int ilezz = 0;
	for (map<long, map<long, map<long, map<long, int> > > >::iterator itx1 = frob.begin(); itx1 != frob.end(); ++itx1) {
		x1 = itx1->first;
		for (map<long, map<long, map<long, int> > >::iterator itx2 = itx1->second.begin(); itx2 != itx1->second.end(); ++itx2) {
			x2 = itx2->first;
			for ( map<long, map<long, int> >::iterator ity1 = itx2->second.begin(); ity1 != itx2->second.end(); ++ity1) {
				y1 = ity1->first;
				for (map<long, int>::iterator ity2 = ity1->second.begin(); ity2 != ity1->second.end(); ++ity2) {
					y2 = ity2->first;
					if (ity2->second != -1)
					++ilezz;
				}
			}
		}
	}
	cout << ilezz << endl;
	for (map<long, map<long, map<long, map<long, int> > > >::iterator itx1 = frob.begin(); itx1 != frob.end(); ++itx1) {
		x1 = itx1->first;
		for (map<long, map<long, map<long, int> > >::iterator itx2 = itx1->second.begin(); itx2 != itx1->second.end(); ++itx2) {
			x2 = itx2->first;
			for ( map<long, map<long, int> >::iterator ity1 = itx2->second.begin(); ity1 != itx2->second.end(); ++ity1) {
				y1 = ity1->first;
				for (map<long, int>::iterator ity2 = ity1->second.begin(); ity2 != ity1->second.end(); ++ity2) {
					y2 = ity2->first;
					if (ity2->second != -1)
					cout << x1 << " " << x2 << " " << y1 << " " << y2 << endl;
				}
			}
		}
	}	
	
}