#P5998. Merging Tribes

    ID: 19222 Type: Default 1000ms 256MiB

Merging Tribes

Merging Tribes

In ancient times, there were $n$ tribes in the Kingdom of Jili. Each tribe was represented by an axis-aligned rectangle. Note that some regions might belong to multiple tribes.

Over time, if two tribes have a common overlapping area (i.e. intersection area strictly greater than zero), they merge into a new tribe. The new tribe is represented by the smallest axis-aligned rectangle that contains both original tribes. This process continues until no two tribes can merge.

Input Format: The first line contains an integer $n$. Each of the next $n$ lines contains four integers $x_1$, $y_1$, $x_2$, $y_2$, representing the bottom-left and top-right coordinates of a tribe's rectangle (with $x_1 < x_2$ and $y_1 < y_2$).

Output Format: First, output a single integer denoting the number of tribes remaining after all mergers. Then, output the coordinates of each tribe's rectangle on a separate line as four integers: $x_{\min}$, $y_{\min}$, $x_{\max}$, $y_{\max}$. The order of the tribes does not matter.

inputFormat

The first line contains an integer nn (1n1051\le n\le 10^5). Each of the following nn lines contains four integers x1x_1, y1y_1, x2x_2, y2y_2 (109x1,y1,x2,y2109-10^9 \le x_1,y_1,x_2,y_2 \le 10^9 with x1<x2x_1 < x_2 and y1<y2y_1 < y_2), representing a tribe's rectangle.

outputFormat

First, output an integer representing the number of remaining tribes. Then, for each tribe, output a line with four integers: xminx_{\min}, yminy_{\min}, xmaxx_{\max}, ymaxy_{\max}, which are the coordinates of the merged rectangle that covers all rectangles in that tribe.

sample

4
1 1 3 3
2 2 4 4
5 5 6 6
3 1 4 2
2

1 1 4 4 5 5 6 6

</p>