This page is still under construction.

Parts of this page are still being built. What you see may change.

Regions Cut by Rectangles

Time limit5sMemory limit128 MB

Summary
Count the regions into which up to 50 axis-aligned rectangle borders divide the plane, including the outer unbounded region.
Level

Medium6 of 10

Topics
Geometry, Graph, BFS
Solved
No attempts yet

Problem

There are several rectangles on the x-y plane. Every side of every rectangle is parallel to the x-axis or the y-axis, and every rectangle lies inside the coordinate range given in the input. The coordinates have no other restriction.

The sides of the rectangles cut the plane into regions. One region can be bounded by the sides of a single rectangle, or by sides of several rectangles mixed together. In Figure 1 of the hint, three rectangles overlap one another and the plane is cut into eight regions.

Rectangles can overlap in more complicated ways. Two rectangles can share part of a side, they can touch at a single corner, and one can lie entirely inside the other. Figure 2 shows those cases.

Write a program that counts how many regions the sides of the rectangles cut the plane into.

Input

The input consists of several datasets. Each dataset has the following format.

n
l1 t1 r1 b1
l2 t2 r2 b2
...
ln tn rn bn

The first line of a dataset holds the number of rectangles on the plane, nn. (1≤n≤501 \le n \le 50)

Each of the next nn lines describes one rectangle with four integers lil_i, tit_i, rir_i, bib_i separated by single spaces. (li,ti)(l_i, t_i) is the x-y coordinate of the top left corner of the ii-th rectangle, and (ri,bi)(r_i, b_i) is the coordinate of its bottom right corner. (0≤li<ri≤1060 \le l_i < r_i \le 10^6, 0≤bi<ti≤1060 \le b_i < t_i \le 10^6)

The last line of the input holds a single 0.

Output

For each dataset, print on one line how many regions the sides of the rectangles cut the plane into. The whole plane is counted, so the unbounded region outside every rectangle counts as one region.

Hint

Figure 1. Three rectangles cut the plane into eight regions. This is the first dataset of the example. The x-axis and the y-axis are drawn only to help read the picture, so they do not cut the plane.

Figure 2. Rectangles overlapping in more complicated ways. This is the second dataset of the example.

Examples1

  1. Example 1

    Input
    3
    4 28 27 11
    15 20 42 5
    11 24 33 14
    5
    4 28 27 11
    12 11 34 2
    7 26 14 16
    14 16 19 12
    17 28 27 21
    2
    300000 1000000 600000 0
    0 600000 1000000 300000
    0
    
    Expected output
    8
    6
    6