#P10772. City Bus Transfers

    ID: 12810 Type: Default 1000ms 256MiB

City Bus Transfers

City Bus Transfers

In a city, there are N bus routes. Every day at midnight, a bus starts from its route’s starting point and arrives at its endpoint at the next midnight. For the i-th bus, the starting coordinate is Si and the ending coordinate is Ei. The bus travels at a constant speed along the straight line from Si to Ei, so that the trip always lasts 24 hours regardless of the distance.

Passengers are allowed to board, disembark, or transfer at any location along a bus route – not only at its endpoints. The city is arranged on a horizontal line with a central stadium at coordinate 0. Points east of the stadium have positive coordinates and points west have negative coordinates.

There are M people, each starting at the stadium at time 0, who wish to go home. For each person, given the coordinate of their home, determine the minimum time (in hours) required to reach home using the bus network. If a person cannot reach their home, output -1.

Notes on the bus schedule and riding:

  • Each bus departs its starting point at midnight (time 0 of that day) and arrives at its destination at midnight the next day.
  • A bus’s position at time t (in hours) can be given by a linear function. For an eastbound bus (i.e. when Si < Ei), if a passenger boards at a coordinate x (with Si ≤ x ≤ Ei), then the bus passes that point at time t = 24 * (x - Si)/(Ei - Si). Similarly, for a westbound bus (i.e. when Si > Ei), if Ei ≤ x ≤ Si, then the bus passes x at time t = 24 * (Si - x)/(Si - Ei).
  • A passenger arriving at some point at time T may have to wait for the next bus that passes that point. Specifically, if for a bus route the bus passes the point at time d (computed as above), then if the current time modulo 24 is less than or equal to d the waiting time is d - (T mod 24); otherwise, the passenger must wait until the bus’s next departure, that is, (24 - (T mod 24) + d) hours.

Your task is to compute, for each of the M people, the minimum total travel time (possibly spanning multiple days) to go from the stadium (coordinate 0 at time 0) to their home.

inputFormat

The first line contains two integers N and M – the number of bus routes and the number of people, respectively.

Each of the next N lines contains two integers Si and Ei representing the starting point and the ending point of the i-th bus route.

Each of the following M lines contains one integer representing the coordinate of a person’s home.

You may assume that all coordinates are given in kilometers and that the stadium is at coordinate 0. It is guaranteed that |Si|, |Ei| and the home coordinates do not exceed 109.

outputFormat

Output M lines. The i-th line should contain the minimum time (in hours) required for the i-th person to get home. The answer should be a floating–point number with at least 6 digits after the decimal point. If a person cannot reach home, output -1 instead for that person.

sample

2 2
-5 5
5 10
10
-5
48.000000

-1

</p>