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
/**
 * @author Tadeusz Faber (SI-Consulting SA).
 * 
 * Potyczki Algorytmiczne 2014.
 * 
 * 2014-05-17 Runda 5B. Plemiona. 
 *  
 * Pamiec: 1024 MB.
 * Czas: ?s.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

final class Rectangle {
	private int x1;
	private int x2;
	private int y1;
	private int y2;

	public Rectangle(int x1, int x2, int y1, int y2) {
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}

	public int getX1() {
		return x1;
	}

	public int getX2() {
		return x2;
	}

	public int getY1() {
		return y1;
	}

	public int getY2() {
		return y2;
	}

	public boolean Merge(Rectangle r) {
		boolean xx = false;
		boolean yy = false;
		if ((x1 <= r.x1 && r.x1 < x2) || (x1 < r.x2 && r.x2 <= x2)
				|| (r.x1 <= x1 && x2 <= r.x2)) {
			xx = true;
		}
		if ((y1 <= r.y1 && r.y1 < y2) || (y1 < r.y2 && r.y2 <= y2)
				|| (r.y1 <= y1 && y2 <= r.y2)) {
			yy = true;
		}
		if (xx && yy) {
			x1 = Math.min(x1, r.x1);
			x2 = Math.max(x2, r.x2);
			y1 = Math.min(y1, r.y1);
			y2 = Math.max(y2, r.y2);
			return true;
		} else {
			return false;
		}
	}
}

final class RectangleComparator implements Comparator<Rectangle> {

	@Override
	public int compare(Rectangle x, Rectangle y) {
		if (y == null)
			return -1; // <
		if (x.getX1() > y.getX1())
			return 1; // >
		else if (x.getX1() < y.getX1())
			return -1; // <
		else if (x.getX2() > y.getX2())
			return 1; // >
		else if (x.getX2() < y.getX2())
			return -1; // <
		else if (x.getY1() > y.getY1())
			return 1; // >
		else if (x.getY1() < y.getY1())
			return -1; // <
		else if (x.getY2() > y.getY2())
			return 1; // >
		else if (x.getY2() < y.getY2())
			return -1; // <
		else
			return 0;
	}
}

public final class ple {

	private static Reader in = null;

	/**
	 * Zwroc kolejna liczbe calkowita ze strumienia in
	 * 
	 * @return wczytana liczba
	 */
	private static int nextInteger() {

		int r = 0;
		boolean minus = false;
		char c;

		try {
			do {
				c = (char) in.read();
			} while (c == ' ' || c == '\n' || c == '\r');
			if (c == '-') {
				minus = true;
				c = (char) in.read();
			}
			do {
				r = r * 10 + (int) c - 48;
				c = (char) in.read();
			} while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (minus) {
			return -r;
		} else {
			return r;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {

		in = new BufferedReader(new InputStreamReader(System.in));

		int n = nextInteger(); // 1..100 000
		ArrayList<Rectangle> rect = new ArrayList<Rectangle>(n);
		for (int i = 1; i <= n; i++) {
			rect.add(new Rectangle(nextInteger(), nextInteger(), nextInteger(),
					nextInteger()));
		}
		Collections.sort(rect, new RectangleComparator());
		boolean changed = true;
		for (; changed;) {
			changed = false;
			for (int i = 0; i < rect.size(); i++) {
				Rectangle ri = rect.get(i);
				for (int j = i + 1; j < rect.size()
						&& rect.get(j).getX1() <= ri.getX2();) {
					if (ri.Merge(rect.get(j))) {
						rect.remove(j);
						changed = true;
					} else {
						j++;
					}
				}
			}
		}

		System.out.println(rect.size());
		for (Rectangle r : rect) {
			System.out.println(r.getX1() + " " + r.getX2() + " " + r.getY1()
					+ " " + r.getY2());
		}
	}
}