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
/*
 *  Copyright (C) 2014  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;

//#define DEBUG 666
#ifdef DEBUG
	#define DBG(x) x
#else
	#define DBG(x)
#endif

class Rectangle {
public:
	int x1, y1;
	int x2, y2;

	Rectangle(int a, int b, int c, int d): x1(a), y1(b), x2(c), y2(d) {}

	void merge(const Rectangle& other) {
		x1 = min(x1, other.x1);
		x2 = max(x2, other.x2);
		y1 = min(y1, other.y1);
		y2 = max(y2, other.y2);
	}

	bool operator==(const Rectangle& other) const {
		return x1 == other.x1 && x2 == other.x2 && y1 == other.y1 && y2 == other.y2;
	}

	bool operator<(const Rectangle& other) const {
		if (x1 == other.x1) {
			return x2 < other.x2;
		}
		return x1 < other.x1;
	}
};


struct ListSort {
    bool operator()(const Rectangle& a, const Rectangle& b) {
		if (a.x1 == b.x1) {
			if (a.x2 == b.x2) {
				if (a.y1 == b.y1) {
					return a.y2 < b.y2;
				}
				return a.y1 < b.y1;
			}
			return a.x2 < b.x2;
		}
		return a.x1 < b.x1;
    }
};


ostream& operator<< (ostream& os, Rectangle& r) {
	 os << r.x1 << " " << r.x2 << " " << r.y1 << " " << r.y2 << endl;
	 return os;
}


int main() {
	int n;
	int x1, x2, y1, y2;
	list<Rectangle> areas;
	list<Rectangle> overlaps;

	ios::sync_with_stdio (false);
	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> x1 >> x2 >> y1 >> y2;
		areas.push_back(Rectangle(x1, y1, x2, y2));
	}

	areas.sort();

	unsigned int size = areas.size() + 1;
	while (areas.size() < size) {
		size = areas.size();

		auto current = begin(areas);
		auto iter = std::next(current);

		while (iter != end(areas)) {
			auto next = iter++;

			DBG(
			for (auto i: areas) {
				cout << "** " << i << endl;
			});

			DBG(
			cout << "current " << *current;
			cout << "next " << *next;
			);

			if (next->x1 < current->x2) {
				// if intersecting, merge rectangles
				if (next->y1 < current->y2 && next->y2 > current->y1) {
					current->merge(*next);
					areas.erase(next);

					DBG(cout << "merge " << *current;);

					// loop through overlaps to find more candidates to merge
					auto iter2 = begin(overlaps);
					while (iter2 != end(overlaps)) {
						auto r = iter2++;

						DBG(cout << "overlap " << *r;);

						if (r->y1 < current->y2 && r->y2 > current->y1) {
							current->merge(*r);

							auto area = lower_bound(current, iter, *r);
							if (*area == *r) {
								if (iter == area) {
									++iter;
								}
								areas.erase(area);
							}
							overlaps.erase(r);

							DBG(cout << "merge overlap " << *current;);
						}
					}
				}
				// if in range, but not intersecting add rectangle to overlaps list
				overlaps.push_back(*next);
			} else {
				++current;

				// update overlaps
				if (current != end(areas)) {
					auto iter2 = begin(overlaps);
					while (iter2 != end(overlaps)) {
						auto r = iter2++;
						if (r->x2 <= current->x1) {
							overlaps.erase(r);
						}
					}
				}

				iter = current;
				++iter;
			}
		}
	}

	areas.sort(ListSort());

	for (auto r: areas) {
		cout << r;
	}

	return 0;
}