CCO '14 P6 - Gates

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 1.4s
Memory limit: 1G

Author:
Problem types
Canadian Computing Competition: 2014 Stage 2, Day 2, Problem 3

You're in an airport hallway with G (1 \le G \le 10^9) gates, numbered from 1 to G in order. The entrance to gate i is 100 \cdot i metres from the start of the hallway.

There are also N (0 \le N \le 10^5) moving walkways. The i-th walkway runs from the entrance to gate A_i (1 \le A_i \le G) to the entrance to a different gate B_i (1 \le B_i \le G) at a speed of S_i (1 \le S_i \le 10^9) metres per minute, in one direction only. At every point along the hallway, there is at most one walkway moving in each direction (towards the start of the hallway, or away from it). However, it is possible that one walkway starts at the same gate as another walkway, moving in the same direction, ends.

You can walk in either direction along the hallway at a speed of W (1 \le W \le 10^9) metres per minute. Additionally, when at the start of a walkway, you may choose to get on it, in which case it'll carry you directly to its end - you may not get on or off in between these points. While on walkway i, you will move at a speed of W + S metres per minute.

To amuse yourself while waiting for your flight (which, of course, has been delayed), you've posed Q (1 \le Q \le 10^5) queries to yourself. The i-th query involves finding the minimal time in which you can get from gate X_i (1 \le X_i \le G) to gate Y_i (1 \le Y_i \le G). To make things more interesting, you've decided that you won't board your flight unless you can correctly answer all of these queries - so you'd better not screw up!

Input Specification

The first line contains four integers: G (1 \le G \le 10^9), the number of gates; W (1 \le W \le 10^9), the walking speed; N (0 \le N \le 10^5), the number of moving walkways; and Q (1 \le Q \le 10^5), the number of queries.

The next N lines each contain three integers dealing with walkway i (i = 1 \dots N): A_i (1 \le A_i \le G), the starting gate; B_i (1 \le B_i \le G), the ending gate; S_i (1 \le S_i \le 10^9), the speed. Note that A_i \neq B_i for any i.

The next Q lines each contain two integers dealing with query i = 1 \dots Q: X_i (1 \le X_i \le G), the starting gate; and Y_i (1 \le Y_i \le G), the ending gate.

You can assume that for some test cases, at least some of G, N, and Q are small. This information may be helpful, or not.

Output Specification

The output is Q lines, each line containing one real number which is the minimal amount of time required to travel from gate X_i to gate Y_i (in minutes), for i = 1 \dots Q. The output will be judged to be correct if the outputted answer is within a factor of 10^{-4} of the correct solution: that is, if D is the correct answer, values in the range [D \cdot (1 - 10^{-4}), D \cdot (1 + 10^{-4})] will be judged as correct.

Sample Input

6 10 3 4
2 3 15
4 2 150
3 6 290
3 2
2 3
1 4
4 6

Sample Output

10.0
4.0
24.0
6.25

Explanation of Output for Sample Input

For the first query, you should simply walk from gate 3 to gate 2, covering 100 metres at a speed of 10 metres per minute.

For the second query, you should immediately get on the moving walkway going from gate 2 to gate 3, covering 100 metres at a speed of 10 + 15 = 25 metres per minute.

For the third query, you should walk to gate 2 (taking 10 minutes), then take the walkway as before (taking 4 minutes), and then walk to gate 4 (taking 10 minutes).

Finally, for the fourth query, you should take the walkway from gate 4 to gate 2 (taking 1.25 minutes), then the walkway from gate 2 to gate 3 (taking 4 minutes), and finally the walkway from gate 3 to gate 6 (taking 1 minute).


Comments

There are no comments at the moment.