Spaceship Exploration

시간 제한2초메모리 제한1024 MB

요약
볼록 다각형 밖에서 두 점 사이를 이동할 때 방향을 최대 한 번만 바꿔 가는 최단 거리를 각 질의마다 구하고, 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
기하, 최단 경로, 이분 탐색
정답자
아직 제출이 없습니다

문제

In The ICPC Galaxy, there exists a zone filled with asteroids that is unsafe to enter. The map of the galaxy is represented in a 2D Cartesian coordinate system. The zone is in the shape of an NN-sided convex polygon. Each corner is numbered from 11 to NN; corner ii is located at (X_i,Y_i)(X\_i , Y\_i). At any moment, you should not be inside this polygon; however, it is safe to touch the side of the polygon.

There are QQ scenarios (numbered from 11 to QQ). In scenario jj, you want to go from a starting point at (A_j,B_j)(A\_j , B\_j ) to an ending point at (C_j,D_j)(C\_j , D\_j ). You will be riding on a special spaceship that can only travel in a straight line. First, you set the direction of the spaceship, then the spaceship will start traveling in that direction. During the travel, you are only allowed to change direction at most once. Changing direction means you stop the spaceship, set a new direction, and then start traveling again in the new direction.

For each scenario, determine the minimum distance required to travel without being inside of the zone at any moment, or report if it is impossible to reach the ending point.

입력

The first line consists of an integer NN (3≤N≤100,0003 ≤ N ≤ 100\\, 000).

Each of the next NN lines consists of two integers X_iX\_i Y_iY\_i (−109≤X_i,Y_i≤109-10^9 ≤ X\_i , Y\_i ≤ 10^9). The points form a convex polygon in counterclockwise order. There are no three points which are collinear.

The following line consists of an integer QQ (1≤Q≤100,0001 ≤ Q ≤ 100\\, 000).

Each of the next QQ lines consists of four integers A_jA\_j B_jB\_j C_jC\_j D_jD\_j (−109≤A_j,B_j,C_j,D_j≤109-10^9 ≤ A\_j , B\_j , C\_j , D\_j ≤ 10^9). There are no starting points and ending points inside the zone. However, it is possible for the starting point and the ending point to be at the side of the zone.

All the coordinates in the input are integers.

출력

For each scenario, output the answer in a single line.

If it is possible to reach the ending point without being inside the zone at any moment, then output the minimum distance required to travel. Otherwise, output -1.

Your answer is considered correct if its absolute error or relative error does not exceed 10−610^{-6}. Namely, if your answer is aa and the jury’s answer is bb, then your answer is accepted if ∣a−b∣max⁡(1,∣b∣)≤10−6\frac{|a−b|}{\max(1,|b|)} ≤ 10^{-6}.

예제3

  1. 예제 1

    입력
    5
    0 2
    2 0
    4 0
    4 4
    2 4
    5
    6 1 6 3
    2 5 0 0
    3 5 3 -1
    1 4 5 4
    3 4 3 0
    
    예상 출력
    2
    5.6055512755
    8.48528137422
    4
    -1
    
  2. 예제 2

    입력
    4
    -10 -9
    10 -9
    10 9
    -10 9
    2
    0 10 0 -10
    -10 -10 -10 -10
    
    예상 출력
    200.9975124224
    0
    
  3. 예제 3

    입력
    8
    -20 -10
    10 -20
    25 -15
    35 -5
    30 10
    15 20
    -25 15
    -30 5
    6
    -15 -15 -15 20
    -30 -5 30 15
    25 20 -5 -20
    -5 25 20 -20
    -30 10 30 -10
    -30 -50 50 0
    
    예상 출력
    59.0857761929
    103.2455532034
    94.7213595500
    101.5640991922
    164.8528137424
    94.3398113206