Under construction

Still being built. Things may change

Build a Wall!

Time limit2.5sMemory limit1024 MB

Summary
Given a convex N-gon and query points, find for each point the triangulation that maximizes how many diagonals must be crossed to reach it from outside.
Level

Hard8 of 10

Topics
Dynamic programming, Geometry, Tree
Solved
No attempts yet

Problem

After the new leader Junwon takes power in X Kingdom, he decides to build walls inside the country. X Kingdom already has a convex NN-gon of walls around its outer boundary. Even so, Junwon wants to add more walls inside the country to protect the home he plans to build there.

New walls can only be built as segments joining two vertices of the outer NN-gon. Two walls cannot cross at any point other than their endpoints. Junwon plans to build N−3N-3 additional walls. He has also chosen QQ candidate sites for his home. For each candidate site, Junwon wants to solve the following question.

Place the N−3N-3 walls so that the minimum number of walls that must be crossed to reach this site from outside X Kingdom is as large as possible. Find the maximum possible value of that minimum.

Find the answer for each candidate site.

Input

The first line contains a positive integer NN (3≤N≤50003 \le N \le 5000).

The next NN lines contain the coordinates xi,yix_i, y_i of the vertices of the outer wall in counterclockwise order. The outer walls are the NN segments from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2), from (x2,y2)(x_2, y_2) to (x3,y3)(x_3, y_3), and so on, until (xN,yN)(x_N, y_N) to (x1,y1)(x_1, y_1), forming a convex NN-gon.

The next line contains the number of candidate sites QQ (1≤Q≤50001 \le Q \le 5000).

The next QQ lines contain the coordinates ai,bia_i, b_i of each candidate site. Each site is inside X Kingdom and does not lie on the line through any two vertices.

All coordinates have at most six digits after the decimal point, and their absolute values are at most 10810^8. The answer does not change if any coordinate changes by up to 10−610^{-6}.

Output

For each of the QQ candidate sites, print the maximum possible minimum number of walls that must be crossed to reach it from outside, one per line, in input order.

Examples1

  1. Example 1

    Input
    6
    -0.5 -1.0
    0.5 -1.0
    1.0 0.0
    0.5 1.0
    -0.5 1.0
    -1.0 0.0
    2
    0.0 -0.2
    0.0 -0.8
    
    Expected output
    2
    1