#P11355. [eJOI 2023] Teleporters
[eJOI 2023] Teleporters
Problem Description
dXqwq and Haitang are at two different points on the number line. They want to meet, but they can only move using teleporters.
There are teleporters. The -th teleporter is located at position on the number line and has frequency . For some reason, only teleporters with frequency can be used.
Using a teleporter will send a person to the point symmetric with respect to its coordinate. Formally, if a person's positions before and after teleportation are , and the teleporter position is , then .
dXqwq and Haitang will repeatedly simultaneously choose a teleporter (not necessarily the same), teleport, and incur fatigue of , until they reach the same position. The total fatigue of the whole process is the maximum fatigue incurred in any single step.
Given queries, each query provides an interval . Find the minimum possible total fatigue for dXqwq and Haitang to meet, or report that it is impossible for them to meet using these teleporters.
Input Format
The first line contains two integers .
The second line contains integers .
The third line contains integers .
The next lines each contain four integers , and it is guaranteed that .
Output Format
Output one line containing integers, representing the minimum total fatigue for each query. In particular, if it is impossible for them to meet, output -1.
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
3 3
-2 1 -1
10 1 3
-6 6 20 20
-6 6 1 20
-6 6 2 20
-1 2 7
Hint
[Sample Explanation]
The following explains the first sample.

In the first query, if dXqwq chooses the second teleporter and Haitang chooses the fourth teleporter, they can meet at , with fatigue . But if dXqwq chooses the first teleporter and Haitang chooses the third teleporter, they can meet at , with fatigue .
In the second query, the second method above is invalid due to the restriction of .
In the third query, there is only one available teleporter, so meeting is impossible.
Note that coordinates may be negative.
[Constraints]
This problem uses bundled testdata.
- Subtask 1 (11 pts): , .
- Subtask 2 (10 pts): , , , .
- Subtask 3 (5 pts): , , .
- Subtask 4 (9 pts): , , , .
- Subtask 5 (6 pts): , , .
- Subtask 6 (7 pts): , , .
- Subtask 7 (17 pts): , .
- Subtask 8 (8 pts): .
- Subtask 9 (14 pts): .
- Subtask 10 (13 pts): No special constraints.
For of the data, it is guaranteed that , , , , .
Translated by ChatGPT 5