This page is still under construction.

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

The Worm in the Apple

Time limit15sMemory limit128 MB

Summary
Given a convex polyhedron as the hull of n points, answer queries for the distance from an interior point to the surface.
Level

Hard8 of 10

Topics
Geometry, Binary search, Divide and conquer
Solved
No attempts yet

Problem

Willy the Worm was living happily inside an apple — until a human picked the apple and started to eat it! Now Willy must escape.

You are given a description of the apple as a convex solid in 3D space, together with several positions inside the apple where Willy might be. For each position, determine the minimum distance Willy must travel to reach the surface of the apple.

Input

The input contains several test cases.

Each test case begins with a line containing a single integer nn (4≤n≤10004 \le n \le 1000), the number of points that describe the apple.

Each of the next nn lines contains three integers xx, yy, zz (−10000≤x,y,z≤10000-10000 \le x, y, z \le 10000); the point (x,y,z)(x, y, z) lies on the surface of, or inside, the apple. The apple is the convex hull of these nn points, and no four of the points are coplanar.

The next line contains a single integer qq (1≤q≤1000001 \le q \le 100000), the number of query positions. Each of the following qq lines contains three integers xx, yy, zz (−10000≤x,y,z≤10000-10000 \le x, y, z \le 10000), a position (x,y,z)(x, y, z) where Willy might be. Every query position is guaranteed to lie inside the apple.

The input ends with a line containing a single 00.

Output

For each query, output on its own line the minimum distance Willy must travel to reach the surface of the apple. Print the value with exactly 4 digits after the decimal point, using round-half-up rounding (a next digit of 5 or more rounds up, 4 or less rounds down); for example 2.123442.12344 becomes 2.12342.1234 and 2.123452.12345 becomes 2.12352.1235. Print no extra spaces and no blank lines between answers.

Examples3

  1. Example 1

    Input
    6
    0 0 0
    100 0 0
    0 100 0
    0 0 100
    20 20 20
    30 20 10
    4
    1 1 1
    30 30 35
    7 8 9
    90 2 2
    0
    
    Expected output
    1.0000
    2.8868
    7.0000
    2.0000
    
  2. Example 2

    Input
    4
    0 0 0
    30 0 0
    0 30 0
    0 0 30
    4
    5 5 5
    1 1 1
    10 10 5
    1 14 14
    0
    
    Expected output
    5.0000
    1.0000
    2.8868
    0.5774
    
  3. Example 3

    Input
    5
    6 0 0
    -3 5 0
    -3 -5 0
    0 0 8
    0 0 -8
    4
    0 0 0
    0 0 5
    -1 0 0
    0 0 -5
    0
    
    Expected output
    2.7379
    1.0267
    1.8727
    1.0267