#P6804. [CEOI 2020] 权力药水
[CEOI 2020] 权力药水
Description
A long time ago, on the Island of Shamans, everyone lived on a beanstalk as tall as the sky. Each shaman has a unique ID in the range from to . The height where shaman lives is denoted by . The distance between two shamans is defined as the absolute value of the difference of their heights.
All shamans originally lived in harmony, until one day the recipe of the Power Potion was stolen. To cover up his actions, the thief cast a curse on the whole island, and most residents no longer trusted each other.
Although the situation is very complex, after investigation, an organization obtained the following information:
- When the curse first took effect, all shamans stopped trusting each other.
- The curse itself is unstable. At the end of each day (to be exact, at midnight), a certain pair of shamans will either start trusting each other or stop trusting each other.
- Unfortunately, at any moment, a shaman trusts at most other shamans.
The organization also reconstructed the trust-change log between shamans. The log records, at each midnight, which pair of shamans had their trust relationship changed (from not trusting to trusting, or from trusting to not trusting).
The organization members believe that the thief also leaked the recipe to an evil shaman via a “remote whisper” (pinyin: gekong chuanhua). To avoid being discovered, they each visited the home of one shaman they trusted. During the visit, the thief leaked the recipe to the evil shaman through the window. Note that the trusted friend did not have to be at home at that time; in fact, the two shamans might even visit each other’s homes. After all, shamans are strange.
Luckily, because shamans have limited hearing, sound cannot travel too far. This means the distance between the two friends (the thief’s friend and the evil shaman’s friend) must be as small as possible.
Now the organization asks you to help with the investigation. They want to verify their guess: when the thief is , the evil shaman is , and the leak happened on day , what is the minimum distance the whisper needs to travel? That is, on day , you need to find a shaman among all shamans trusted by , and a shaman among all shamans trusted by , such that the distance between and is minimized.
You have obtained all information needed to compute the answers, but you must answer each query online.
Input Format
Interactive IO only.
The first line contains four integers , representing the number of shamans, the maximum number of friends a shaman can trust, the number of days included in the log, and the number of queries.
The next line contains integers separated by single spaces. The -th integer represents the height where shaman lives.
The next lines: the -th line contains two integers , meaning that at the end of day , the trust status between shamans with IDs and changes. That is, if and trusted each other on day , then starting from day they no longer trust each other, and vice versa.
Then you need to answer queries. Queries must be answered online, i.e., you must finish answering one query before you can read the next one.
Each query contains three integers , meaning the thief is , the evil shaman is , and the leak happens on day .
Output Format
For each query, on day , find a shaman among all shamans trusted by , and a shaman among all shamans trusted by , such that the distance between and is minimized, and output this minimum value.
In particular:
- If there exists a shaman trusted by both and , output .
- If or has no trusted friends on day , output ().
After answering a query, you must flush the buffer immediately.
For C++ users, you can flush the buffer using fflush(stdout); or cout.flush().
6 5 11 4
2 42 1000 54 68 234
0 1
2 0
3 4
3 5
3 5
1 3
5 3
0 5
3 0
1 3
3 5
0 3 4
3 0 8
0 5 5
3 0 11
26
0
1000000000
14
Hint
Sample Explanation
Below are the queries:

Below is how the trust relationships change each day:

Subtasks
All testdata satisfy: , , , .
The constraints for each subtask are as follows:
| Subtask ID | Points | Constraints |
|---|---|---|
| Sample | ||
| All queries satisfy | ||
| , | ||
| No special constraints |
Translated by ChatGPT 5
京公网安备 11011102002149号