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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define REP(i,n) for(int i=1; i<=(n); ++i)
#define FOR(i,a,b) for(int i=(a); i!=(b); ++i)
#define FORE(it,t) for(auto& it: t)
typedef vector<int> vi;
#define pb push_back
typedef pair<int,int> pii;
#define st first
#define nd second
typedef unordered_map<int,int> hii;
typedef long long ll;
#define INF 1000000001
#define sz size()
#define ALL(t) (t).begin(),(t).end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)
#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
	{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class T> void dptab(T t, const char* f="%d ")
	{if(ISDEBUG) {FORE(el, t) dprintf(f, el); dprintf("\n"); }}

unsigned int g(unsigned int i) {return i ^ (i-1);}
int f(int i) {return ((i ^ (i-1)) +1) / 2;}

int base_len(const pii& p) {
	int len = f(p.st);
	while(len > p.nd-p.st)
		len /= 2;
	return len;
}

pii extend(const pii& p) {
	int len = p.nd - p.st;
	if(g(p.st) < g(p.nd)) return {p.st - len, p.nd};
	else return {p.st, p.nd + len};
}
pii extend_limit{0,1<<20};

pii comb(const pii& a, const pii& b) {
	return {min(a.st, b.st), max(a.nd, b.nd)};
}
bool acomb(const pii& a, const pii& b) {
	return max(a.st, b.st) < min(a.nd, b.nd);
}

void print(const pii& p) {
	printf("(%d,%d) ", p.st, p.nd);
}

struct rect {
	pii x, y;

	bool ext() {
		x = extend(x);
		y = extend(y);
		return x != extend_limit;
	}

	void combine(const rect& r) {
		dprintf("cobining:\n");
		print(); r.print();
		x = comb(x, r.x);
		y = comb(y, r.y);
		print();
	}

	bool operator<(const rect& r) const {
		if(x != r.x) return x<r.x;
		else return y < r.y;
	}

	void print(bool debug=true) const {
		if(debug) {
			dprintf("%d %d %d %d\n", x.st, x.nd, y.st, y.nd);
		}
		else
			printf("%d %d %d %d\n", x.st, x.nd, y.st, y.nd);
	}

	void inc(int d=1) {
		x.st += d; x.nd += d;
		y.st += d; y.nd += d;
	}

	bool touch(const rect& r) const {
		return acomb(x, r.x) && acomb(y, r.y);
	}
};

int n;
int last;
vector<rect> rects;
vector<bool> exists;

map<rect,set<int>> some, whole;


void add(rect, int, bool);

void collision(int j, int i) {
	dprintf("collision %d %d\n", j, i);
	exists[i] = false;
	add(rects[j], j, false);
	rects[j].combine(rects[i]);
	add(rects[j], j, true);
}

void add(rect r, int i, bool doadd=true) {
	//printf("%s %d\n", doadd? "ADD":"DEL", i);
	last = max(last, i);
	exists[i] = doadd;
	if(!doadd) return;	
	FOR(j, 1, last+1)
		if(exists[j] && j!=i && rects[j].touch(r)) {
			collision(j, i);
			return;
		}
}

int main() {
	//add({{3,11},{3,9}}, 0);

	SC(n);
	exists.resize(n+1, false);
	rects.resize(n+1);
	REP(i,n) {
		GET(x1); GET(x2); GET(y1); GET(y2);
		rects[i] = {{x1, x2}, {y1, y2}};
		//rects[i].inc();
		add(rects[i], i);
	}

	vector<rect> results;
	REP(i,n) if(exists[i])
		results.pb(rects[i]);
	sort(ALL(results));
	printf("%d\n", (int)results.sz);
	FORE(res, results) {
		//res.inc(-1);
		res.print(false);
	}
}