#HK4012. 「eJOI2023」Teleporters
「eJOI2023」Teleporters
Description
Anna and Beka are at different points on a coordinate line, planning to meet. Their only means of movement is through the use of teleporters.
There are teleporters, with the i-th teleporter located at coordinate and operating at a frequency denoted as . However, not all of them are currently available; only those within the frequency range can be used.
Using a teleporter takes a minute and transports its user to a coordinate that is a reflection of the original coordinate around the teleporters location. In other words, if the original coordinate was , then after using teleporter , the resulting coordinate will satisfy the equation .
Each minute, Anna and Beka must use one of the available teleporters (not necessarily different ones). They will communicate during teleportation and experience discomfort equal to the absolute difference of the frequencies of the teleporters they are using. The overall difficulty of the travel is defined as the maximum discomfort they have experienced.
You will be asked about different scenarios, and for each one, your task is to determine whether Anna and Beka can ever meet using available teleporters, and if so, what the minimum possible travel difficulty is. A single scenario is described 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
For each scenario, print the minimum travel difficulty if they can meet and otherwise. Please note that the total travel time is irrelevant for the purposes of this task.
Input Format
The first line contains two integers: and .
The second line contains integers: .
The third line contains integers: .
Each of the following lines describes one scenario with four integers: , , and .
Output Format
Print space-separated integers in a single line: answers to the scenarios .
4 3
4 6 8 10
7 1 9 4
3 11 1 50
3 11 1 5
5 7 1 1
2 3 -1

In the first scenario, if Anna uses teleporter and Beka uses teleporter , they will meet at coordinate with a discomfort of .
A better solution is if Anna uses teleporter and Beka uses teleporter instead; in this case, they meet at and experience a discomfort of .
In the second scenario, the better option is no longer available due to restriction on the frequency range.
In the third scenario, there is only one available teleporter, and meeting isn't possible.
3 3
-2 1 -1
10 1 3
-6 6 20 20
-6 6 0 20
-6 6 2 20
-1 2 7
Coordinates can be negative.
Constraints
Subtasks
- (11 points) ; for each .
- (10 points) ; ; ; for each .
- (5 points) ; ;
- (9 points) ; ; ; for each .
- (6 points) ; ; for each .
- (7 points) ; ;
- (17 points) ;
- (8 points)
- (14 points)
- (13 points) No additional constraints.