Teleporters
Time limit2sMemory limit1024 MB
Given teleporters with frequencies in [L, R], find the minimum possible maximum frequency gap for Anna and Beka to meet, or report that they cannot.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Union-find
- Solved
- No attempts yet
Problem
Anna and Beka are at different points on a coordinate line and want to meet. Their only way to move is by teleporters.
There are teleporters. The -th one is located at coordinate and operates at frequency . Only the teleporters with frequency in the range are available.
Using a teleporter takes one minute and moves the user to the reflection of their current coordinate across the teleporter's location. If the coordinate before use is and the coordinate after use is , then .
Every minute, Anna and Beka must each use one of the available teleporters (not necessarily the same one). While they teleport, they feel discomfort equal to the absolute difference between the frequencies of the two teleporters they use. The travel difficulty is the maximum discomfort felt over the whole trip.
There are scenarios. For each one, determine whether Anna and Beka can ever meet using the available teleporters. If they can, print the minimum possible travel difficulty. The total travel time does not affect the answer.
Each scenario is given by four integers:
- : Anna's starting coordinate
- : Beka's starting coordinate
- : the minimum frequency of the available teleporters
- : the maximum frequency of the available teleporters
Input
The first line contains two integers and .
The second line contains integers .
The third line contains integers .
Each of the following lines contains four integers , , , and describing one scenario. It is guaranteed that .
Output
Print space-separated integers on a single line. The -th integer is the answer for scenario .