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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * ple.c
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int n;
struct rect {
	int x1, x2, y1, y2;
	struct X *metaX2;
};
struct rect **rects, **rectstmp;

struct X {
	int isStart;
	int isDeleted;
	int possibleResult;
	int onLine;
	struct rect *r;
};
struct X **xses;

int compareEnd(const void * a, const void * b) {
	struct rect * ar = *(struct rect **) a;
	struct rect * br = *(struct rect **) b;
	int diff = (ar->x1 - br->x1);
	if (diff != 0)
		return diff;
	diff = (ar->x2 - br->x2);
	if (diff != 0)
		return diff;
	diff = (ar->y1 - br->y1);
	if (diff != 0)
		return diff;
	return (ar->y2 - br->y2);

}

int compare(const void * a, const void * b) {
	struct rect * ar = *(struct rect **) a;
	struct rect * br = *(struct rect **) b;
	int diff = (ar->x1 - br->x1);
	if (diff != 0)
		return diff;
	return ar->y1 - br->y1;

}
int compareX(const void * a, const void * b) {
	struct X * xa = *(struct X **) a;
	struct X * xb = *(struct X **) b;
	int av, bv;
	if (xa->isStart)
		av = xa->r->x1;
	else
		av = xa->r->x2;
	if (xb->isStart)
		bv = xb->r->x1;
	else
		bv = xb->r->x2;

	return (av - bv);

}
void print_rect(struct rect *r) {
	//printf("Rect: %i %i %i %i\n", r->x1, r->x2, r->y1, r->y2);
}
struct rect *ExtractIntersect(struct rect *j) {
	int i;
	for (i = 0; i < n; i++) {
		if (rectstmp[i] == 0)
			continue;
		if ((j->y2 <= rectstmp[i]->y1) || (j->y1 >= rectstmp[i]->y2))
			continue;
		/*	if (rectstmp[i]->metaX2->possibleResult && !rectstmp[i]->metaX2->isDeleted)
		 continue;
		 */
		if ((j->x2 <= rectstmp[i]->x1) || (j->x1 >= rectstmp[i]->x2))
			continue;
		j = rectstmp[i];
		//printf("deleting intersected: ");
		print_rect(j);

		rectstmp[i] = 0;
		return j;
	}
	return 0;
}
struct rect *merge(struct rect *a, struct rect *b) {
	//printf("merging");
	int x1 = a->x1, x2 = a->x2, y1 = a->y1, y2 = a->y2;

	if (x1 > b->x1) {
		x1 = b->x1;

	}

	if (y1 > b->y1) {
		y1 = b->y1;

	}

	if (y2 < b->y2) {
		y2 = b->y2;
	}

	if (x2 < b->x2) {
		x2 = b->x2;
		a->metaX2->isDeleted = 1;
		b->x2 = x2;
		b->x1 = x1;
		b->y1 = y1;
		b->y2 = y2;
		//printf("Deleting ");
		//print_rect(a);
		return b;

	} else {
		b->metaX2->isDeleted = 1;
		a->x2 = x2;
		a->x1 = x1;
		a->y1 = y1;
		a->y2 = y2;
		//printf("Deleting ");
		//print_rect(b);
		return a;
	}
}

void Insert(struct rect *j) {

	int i;
	for (i = 0; i < n; i++)
		if (rectstmp[i] == 0) {
			rectstmp[i] = j;
			return;
		}
	assert(0);

}
int only_one(struct rect *r) {
	int k;
	for (k = 0; k < n; k++)
		if (rectstmp[k] != 0 && rectstmp[k] != r)
			return 0;
	return 1;

}


int main() {
	int i;
	struct rect *cur, *prev;

	scanf("%i", &n);
	rects = (struct rect**) malloc(n * sizeof(struct rect *));
	xses = (struct X**) malloc(2 * n * sizeof(struct X *));
	rectstmp = (struct rect**) malloc(n * sizeof(struct rect *));
	for (i = 0; i < n; i++) {
		rectstmp[i] = 0;
		rects[i] = (struct rect *) malloc(sizeof(struct rect));
		xses[2 * i] = (struct X *) malloc(sizeof(struct X));
		xses[2 * i + 1] = (struct X *) malloc(sizeof(struct X));
		scanf("%i %i %i %i", &rects[i]->x1, &rects[i]->x2, &rects[i]->y1,
				&rects[i]->y2);
		//rects->metaX1 = xses[2 * i];
		rects[i]->metaX2 = xses[2 * i + 1];
		xses[2 * i]->isStart = 1;
		xses[2 * i]->isDeleted = 0;
		xses[2 * i]->r = rects[i];
		xses[2 * i]->possibleResult = 0;

		xses[2 * i + 1]->isStart = 0;
		xses[2 * i + 1]->isDeleted = 0;
		xses[2 * i + 1]->r = rects[i];
		xses[2 * i + 1]->possibleResult = 0;
	}
	qsort(xses, n * 2, sizeof(struct X *), compareX);
	qsort(rects, n, sizeof(struct rect *), compare);
	for (i = 0; i < n * 2; i++) {

		if (xses[i]->isStart) {
			//fprintf(stderr, "isStart)");
			prev = xses[i]->r;
			while ((cur = ExtractIntersect(prev)) != 0) {
				//removeFromBst(cur);
				cur = merge(prev, cur);
				prev = cur;
			}
			Insert(prev);
		} else if (!(xses[i]->isDeleted)) {

			xses[i]->possibleResult = 1;

			//for (k = 0; k < n; k++)
			//if (rectstmp[k] == xses[i]->r) {

			//printf("deleting not on line: ");
			//print_rect(rectstmp[k]);
			//rectstmp[k] = 0;
			//}

		}

	}
	int k = 0;
	for (i = 0; i < n * 2; i++) {
		if (xses[i]->possibleResult && !xses[i]->isDeleted) {
			struct rect *r = xses[i]->r;
			rectstmp[k++] = r;
			//printf("Result: %i %i %i %i\n", r->x1, r->x2, r->y1, r->y2);
		}
	}

	qsort(rectstmp, k, sizeof(struct rect *), compareEnd);
	printf("%i\n",k);
	for (i = 0; i < k; i++)
		printf("%i %i %i %i\n", rectstmp[i]->x1, rectstmp[i]->x2,
				rectstmp[i]->y1, rectstmp[i]->y2);
	/*for (i = 0; i < n; i++)
	 if (rectstmp[i] != 0)
	 printf("Result: %i %i %i %i\n", rectstmp[i]->x1, rectstmp[i]->x2,
	 rectstmp[i]->y1, rectstmp[i]->y2);
	 */
	return 0;
}