#P1403A. The Potion of Great Power
The Potion of Great Power
No submission language available for this problem.
Description
Once upon a time, in the Land of the Shamans, everyone lived on the Sky-High Beanstalk. Each shaman had a unique identifying number between and , and an altitude value , representing how high he lived above ground level. The distance between two altitudes is the absolute value of their difference.
All shamans lived together in peace, until one of them stole the formula of the world-famous Potion of Great Power. To cover his/her tracks, the Thief has put a Curse on the land: most inhabitants could no longer trust each other...
Despite the very difficult circumstances, the Order of Good Investigators have gained the following information about the Curse:
- When the Curse first takes effect, everyone stops trusting each other.
- The Curse is unstable: at the end of each day (exactly at midnight), one pair of shamans will start or stop trusting each other.
- Unfortunately, each shaman will only ever trust at most others at any given time.
They believe the Thief has whispered the formula to an Evil Shaman. To avoid detection, both of them visited the home of one of their (respective) trusted friends. During the visit, the Thief whispered the formula to the Evil Shaman through the window. (Note: this trusted friend did not have to be home at the time. In fact, it's even possible that they visited each other's houses – shamans are weird.)
Fortunately, whispers only travel short distances, so the Order knows the two trusted friends visited (by the Thief and the Evil Shaman) must live very close to each other.
They ask you to help with their investigation. They would like to test their suspicions: what if the Thief was , the Evil Shaman was , and the formula was whispered on day ? What is the smallest distance the whispered formula had to travel? That is, what is the minimum distance between the apartments of some shamans and (i.e. ), such that was a trusted friend of and was a trusted friend of on day ?
They will share all their information with you, then ask you a number of questions. You need to answer each question immediately, before receiving the next one.
Interaction
The interaction will begin with a line containing , , and , , , – the number of shamans, the maximum number of trusted friends a shaman can have at any given point, the number of days, and the number of questions.
On the next line space separated integers will follow, the th of which being , the altitude of shaman .
On the next lines there will be two integers each, on the th () and and , which represents a pair of shamans who started or stopped trusting each other at the end of day . That is, if and trusted each other on day , they did not trust each other on day , or vice versa. Read all of these integers.
The interactor now will ask you question, so the following interaction should happen times:
- Read integers describing the current query: and (, and ), where is the suspected Thief, is the suspected Evil Shaman, and is the suspected day..
- Then print the answer to this query on a single line, i.e. you should print the minimum distance the whispered formula had to travel from some trusted friend of to a trusted friend of .
- In case someone trusted both and (i.e. ), you should print .
- If or had no trusted friends, print .
After printing each line do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
Scoring
Samples
Note
Example queries:
Evolution of friendships: