This page is still under construction.

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

GRAD

Time limit2sMemory limit256 MB

Summary
Cities join the road network one by one with two roads each, and each query asks the shortest road distance between two cities.
Level

Hard9 of 10

Topics
Shortest path, Graph, Dynamic programming, Geometry
Solved
No attempts yet

Problem

The road network has cities in the plane and roads as straight segments between pairs of cities. Roads may intersect, but you can switch roads only at the two cities that road connects. Road length is Euclidean distance.

Initially cities 1 and 2 are connected. Each step adds one city and connects it by two new roads to two existing cities A and B that are already directly connected.

Support commands:

  • d X Y A B: add a city at (X,Y)(X,Y) and connect it to A and B.
  • u A B: print the shortest road distance between A and B.

Input

Coordinates of cities 1 and 2, then NN commands in the forms above. No two cities share coordinates.

Output

For each u command, print the distance on its own line. Absolute error at most 0.10.1 from the official answer is accepted.

Examples2

  1. Example 1

    Input
    6 4
    10 4
    9
    d 6 7 2 1
    u 1 2
    u 3 2
    d 10 2 1 2
    u 3 4
    d 12 7 2 4
    u 5 3
    u 4 5
    u 1 5
    
    Expected output
    4.000000
    5.000000
    7.000000
    8.605551
    5.385165
    7.605551
    
  2. Example 2

    Input
    1 1
    3 8
    10
    d 8 2 1 2
    u 2 1
    d 2 9 1 3
    u 1 4
    d 4 7 3 4
    u 4 3
    d 6 1 5 4
    u 6 5
    d 0 0 4 6
    u 7 3
    
    Expected output
    7.280110
    8.062258
    9.219544
    6.324555
    18.439089