Departure
Time limit4sMemory limit512 MB
Given bus routes whose speed depends on distance, find for each home the earliest arrival time from the stadium at 0, as an exact fraction of days.
- Level
Hard9 of 10
- Topics
- Shortest path, Sorting, Math
- Solved
- No attempts yet
Problem
The final match of the prestigious national football championship has just ended. It is midnight, and football fans from across the country have gathered at the national stadium, the largest sports venue in Mouseland, and are leaving to head home by bus.
Mouseland is modelled as a very long straight east-west road, and its inhabitants live in houses along it. The national stadium is the most important building in Mouseland, so it sits at kilometre zero. Every location is given as a single number relative to the stadium. Locations east of the stadium are positive, and locations west are negative. For example, 10 is ten kilometres east of the stadium, and -4 is four kilometres west. The stadium is at location 0.
There are N public bus routes along this road. Each route runs one bus each day. The i-th bus travels from S_i to E_i at a constant speed and direction, leaving at midnight and reaching its destination at midnight the next day. Every midnight, an identical bus leaves each route's start location for the same destination. The speed of a bus depends on the distance it has to cover. Routes are one-directional, so there may be no bus running the opposite way.
People can board a bus, alight from it, and transfer between buses at the same location instantly, at any point along a route, even while the bus is moving.
M people left the national stadium at midnight. The j-th person lives at P_j. For each person, find the minimum time needed to get home by bus.
Input
The first line contains N and M, the number of bus routes and the number of people. Each of the next N lines contains S_i and E_i, the start and destination locations of the i-th bus route. The last line contains M integers, where the j-th integer is the location of the j-th person's home.
Output
Print exactly M lines. On line j, print two integers A_j and B_j such that is in lowest terms (gcd(A_j, B_j) = 1) and equals the minimum number of days the j-th person needs to get home by bus. Report the exact value, including any fractional part. For example, if the earliest arrival is noon on the fourth day, then 3.5 days have passed, so the answer is , printed as '7 2'. It is guaranteed that every person can eventually get home by bus.
Constraints
- for all
- for all
- for all
- for all
For any integer , is 1 if is positive, 0 if is zero, and -1 if is negative.