Inherit the Spheres

Time limit1sMemory limit128 MB

Summary
Simulate a horizontal plane sweeping through overlapping spheres and output the exact sequence of increases and decreases in the number of connected disc components using event-based union-find at critical z-values.
Level

Hard8 of 10

Topics
Union-find, Geometry, Simulation
Solved
No attempts yet

Problem

In the year 2xxx, an expedition team landing on a planet found strange objects made by an ancient species that once lived there. Each object is a transparent box containing opaque solid spheres, and the team also found many lithographs that seem to record the positions and radii of the spheres.

At first the purpose of these objects was unknown, but Professor Zambendorf discovered that the cross section cut by a horizontal plane plays an important role: as the plane slides from the bottom of an object to the top, the cross section changes.

Each cross section is a set of discs, where every disc is the cross section of one solid sphere. Discs that intersect or touch each other merge into a single connected figure. The professor found that information is encoded in how the number of connected figures changes as the plane rises.

For example, in the object described by the first example dataset below, the number of connected figures changes as 0,1,2,1,2,3,2,10, 1, 2, 1, 2, 3, 2, 1, and 00 at z=0.0000,162.0000,167.0000,173.0004,185.0000,191.9996,198.0000,203.0000z = 0.0000, 162.0000, 167.0000, 173.0004, 185.0000, 191.9996, 198.0000, 203.0000, and 205.0000205.0000, respectively. Writing 11 for each increment and 00 for each decrement, this sequence of changes is expressed by the 88-bit binary number 1101100011011000.

To help further analysis, write a program that determines these transitions as the horizontal plane slides from the bottom (z=0z = 0) to the top (z=36000z = 36000).

Input

The input is a sequence of datasets. Each dataset begins with a line containing a positive integer NN, the number of spheres. It is followed by NN lines, each describing one sphere with four positive integers Xi,Yi,ZiX_i, Y_i, Z_i, and RiR_i (i=1,…,Ni = 1, \dots, N): the center (Xi,Yi,Zi)(X_i, Y_i, Z_i) and the radius RiR_i of the ii-th sphere.

You may assume 1≤N≤1001 \le N \le 100, 1≤Ri≤20001 \le R_i \le 2000, 0<Xi−Ri<Xi+Ri<40000 < X_i - R_i < X_i + R_i < 4000, 0<Yi−Ri<Yi+Ri<160000 < Y_i - R_i < Y_i + R_i < 16000, and 0<Zi−Ri<Zi+Ri<360000 < Z_i - R_i < Z_i + R_i < 36000. The ii-th solid sphere is the set of all points (x,y,z)(x, y, z) with (x−Xi)2+(y−Yi)2+(z−Zi)2≤Ri2(x - X_i)^2 + (y - Y_i)^2 + (z - Z_i)^2 \le R_i^2.

A sphere may contain other spheres. No two spheres are mutually tangent. Every value among the Zi±RiZ_i \pm R_i and the minimum and maximum zz-coordinates of the circle formed by the intersection of any two spheres differs from every other such value by at least 0.010.01.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output two lines. The first line contains an integer MM, the number of changes in the number of connected figures. The second line contains an MM-bit binary number expressing those changes: a 11 for each increment and a 00 for each decrement, in order of increasing zz.

Examples4

  1. Example 1

    Input
    3
    95 20 180 18
    125 20 185 18
    40 27 195 10
    1
    5 5 5 4
    2
    5 5 5 4
    5 5 5 3
    2
    5 5 5 4
    5 7 5 3
    16
    2338 3465 29034 710
    1571 14389 25019 842
    1706 8015 11324 1155
    1899 4359 33815 888
    2160 10364 20511 1264
    2048 8835 23706 1906
    2598 13041 23679 618
    1613 11112 8003 1125
    1777 4754 25986 929
    2707 9945 11458 617
    1153 10358 4305 755
    2462 8450 21838 934
    1822 11539 10025 1639
    1473 11939 12924 638
    1388 8519 18653 834
    2239 7384 32729 862
    0
    
    Expected output
    8
    11011000
    2
    10
    2
    10
    2
    10
    28
    1011100100110101101000101100
    
  2. Example 2

    Input
    1
    50 50 40 30
    0
    
    Expected output
    2
    10
    
  3. Example 3

    Input
    2
    40 50 100 30
    95 50 103 30
    0
    
    Expected output
    6
    110100
    
  4. Example 4

    Input
    2
    50 50 40 20
    50 50 200 20
    0
    
    Expected output
    4
    1010