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

struct Rect {
  long x1,y1,x2,y2;
  Rect(long x1, long y1, long x2, long y2):x1(x1),y1(y1),x2(x2),y2(y2) {};  
};

bool compare(const Rect& first, const Rect& second) {
  if (first.x1 < second.x1) {
    return true;
  } else if (first.x1 > second.x1) {
  	return false;
  } else if (first.x2 < second.x2) {
  	return true;
  } else if (first.x2 > second.x2) {
  	return false;
  } else if (first.y1 < second.y1) {
  	return true;
  } else if (first.y1 > second.y1) {
  	return false;
  } else if (first.y2 < second.y2) {
 	return true;
  } else if (first.y2 > second.y2) {
  	return false;
  } else
  	return false;   
}

bool compare_rev(const Rect& first, const Rect& second) {
  if (first.x1 < second.x1) {
    return false;
  } else if (first.x1 > second.x1) {
  	return true;
  } else if (first.x2 < second.x2) {
  	return false;
  } else if (first.x2 > second.x2) {
  	return true;
  } else if (first.y1 < second.y1) {
  	return false;
  } else if (first.y1 > second.y1) {
  	return true;
  } else if (first.y2 < second.y2) {
 	return false;
  } else if (first.y2 > second.y2) {
  	return true;
  } else
  	return false;   
}


bool overlap(Rect &r1, Rect &r2) {
	if(r1.x1>=r2.x2) {
		return false;
	} else if (r1.x2<=r2.x1) {
		return false;
	} else if (r1.y1>=r2.y2) {
		return false;
	} else if (r1.y2<=r2.y1) {
		return false;
	}
	return true;
}

int main() {
	long n;
	long dir = 1;
	long x1,y1,x2,y2;
	vector<Rect> rects;
	scanf("%ld\n", &n);
	for (int i=0;i<n;i++) {
		
		scanf("%ld %ld %ld %ld\n", &x1,&x2,&y1,&y2);

		rects.push_back(Rect(x1,y1,x2,y2));
	}

	bool connected = true;
	while(connected == true) {
		connected = false;

		if(dir == 1) {
			sort(rects.begin(),rects.end(),compare);
			dir = -1;
		} else {
			sort(rects.begin(),rects.end(),compare_rev);
			dir = 1;
		}

		vector<Rect>::iterator i=rects.begin();
		while(i != rects.end()) {
	//		bool over=false;
			vector<Rect>::iterator j = i + 1;

			while(j!=rects.end()){
				Rect &r1 = *i;
				Rect &r2 = *j;

				if(i != j && overlap(r1,r2)) {
					r1.x1 = min(r1.x1,r2.x1);
					r1.y1 = min(r1.y1,r2.y1);
					r1.x2 = max(r1.x2,r2.x2);
					r1.y2 = max(r1.y2,r2.y2);
					rects.erase(j);
					j = i;
					connected = true;
					//break;
				}
				j ++;
			}
			i ++;
		}
	}

	sort(rects.begin(),rects.end(),compare);
	printf("%ld\n", rects.size());

	for(int i=0; i<rects.size(); i++) {
		Rect &p = rects[i];
		printf("%ld %ld %ld %ld\n", p.x1,p.x2,p.y1,p.y2);
	}

	return 0;
}