Under construction

Still being built. Things may change

Final Defense Line

Interview

Time limit3sMemory limit512 MB

Summary
Given up to 5 polygons with gas concentrations and spawn and facility point pairs, find the minimum total damage a creature takes on its route.
Level

Medium5 of 10

Topics
Geometry, Implementation
Solved
No attempts yet

Statement

Unidentified creatures have repeatedly invaded a certain country, so the country decided to protect its important facilities with a new type of defense weapon. This weapon can damage unidentified creatures by filling a polygonal area with a special gas. If the gas concentration changes while a creature is advancing, the creature takes damage equal to the absolute difference of the two concentrations. No damage occurs while the creature moves through an area where the concentration is the same.

With current technology, the gas concentration can only be set to a constant value, so the country decided to deploy several weapons. All weapons use the same gas concentration, and where the areas of several weapons overlap, their concentrations are added together.

Given the spawn point of an unidentified creature and the location of an important facility, find the minimum total damage the creature takes while advancing from the spawn point to the facility. The creature's route does not pass through any polygon vertex or any intersection between polygons. Also, the creature never moves along the edge of a polygon.

Input

The input is given in the following format.

Number of polygons
Number of vertices of a polygon
x y
x y
...
Number of vertices of a polygon
x y
x y
...
Number of spawn and facility pairs
spawn x spawn y facility x facility y
spawn x spawn y facility x facility y
...

Output

For each pair of a spawn point and an important facility, print on its own line the minimum damage the creature takes while advancing to the facility.

Constraints

  • Coordinates in the input are integers whose absolute values are at most 1,000.
  • The number of polygons is at least 1 and at most 5.
  • The number of vertices of each polygon is at least 3 and at most 5.
  • The number of spawn and facility pairs is at least 1 and at most 100.
  • Each polygon is formed by connecting the given vertices in order, and no polygon intersects itself.
  • Spawn points and facilities are never located on a polygon vertex or on a polygon edge.

Examples5

  1. Example 1

    Input
    2
    4
    0 4
    1 1
    3 1
    4 4
    3
    6 0
    10 0
    8 7
    1
    2 3 9 1
    
    Expected output
    2
    
  2. Example 2

    Input
    1
    4
    0 0
    10 0
    10 10
    0 10
    2
    15 5 5 5
    5 5 15 5
    
    Expected output
    1
    1
    
  3. Example 3

    Input
    2
    4
    0 0
    10 0
    10 10
    0 10
    4
    10 0
    20 0
    20 10
    10 10
    1
    5 5 15 5
    
    Expected output
    0
    
  4. Example 4

    Input
    2
    3
    0 0
    10 0
    5 10
    3
    5 0
    15 0
    10 10
    1
    5 5 10 5
    
    Expected output
    2
    
  5. Example 5

    Input
    2
    4
    0 0
    10 0
    10 10
    0 10
    4
    0 0
    10 0
    10 10
    0 10
    2
    15 5 5 5
    5 5 15 5
    
    Expected output
    2
    2