Slingshot
Time limit2sMemory limit512 MB
For each of M queries (a, b), find the minimum time to move manure from a to b using the tractor (cost equals distance) plus at most one slingshot that flies from x to y in time t.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Sorting, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
Of all the chores on the farm, Farmer John dislikes hauling manure the most. To cut the work down he comes up with an odd plan: instead of moving manure between two points in a cart behind his tractor, he shoots it through the air with a giant manure slingshot. (what could possibly go wrong)
John's farm runs along one long straight road, so every location on the farm is a single coordinate along that road, a point on the number line. John builds slingshots (). Slingshot is given by three integers , , and : it shoots manure from position to position in only units of time.
John has piles of manure to move (). Pile has to go from position to position . Hauling manure a distance of with the tractor takes units of time. John hopes to cut that down by allowing up to one slingshot use for each pile. Time the tractor spends moving with no manure in it does not count.
For each of the piles of manure, find the smallest possible transport time, given that John may use at most one slingshot along the way.
Input
The first line contains and . Each of the next lines describes one slingshot with the integers , , and (). Each of the final lines describes one pile of manure that has to be moved, with the integers and .
Output
Print lines, one for each pile of manure, each holding the smallest time needed to move that pile.
Hint
In the example, the first pile of manure has to move from position 1 to position 12. Without a slingshot that takes 11 units of time. With the first slingshot it takes 1 unit of time to move the manure to position 0, the launch point, 1 unit of time to fling it through the air to position 10, the landing point, and then 2 units of time to move it to position 12, for a total of 4. The second pile is fastest with no slingshot at all, and the third pile is fastest with the second slingshot.