Central-European Olympiad in Informatics, CEOI 2020, Day 2 (IOI, Unofficial Mirror Contest, Unrated) |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

*special problem

2-sat

binary search

data structures

graphs

interactive

sortings

two pointers

*2400

No tag edit access

The problem statement has recently been changed. View the changes.

×
A. The Potion of Great Power

time limit per test

3 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputOnce upon a time, in the Land of the Shamans, everyone lived on the Sky-High Beanstalk. Each shaman had a unique identifying number $$$i$$$ between $$$0$$$ and $$$N-1$$$, and an altitude value $$$H_i$$$, 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 $$$D$$$ 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 $$$x$$$, the Evil Shaman was $$$y$$$, and the formula was whispered on day $$$v$$$? What is the smallest distance the whispered formula had to travel? That is, what is the minimum distance between the apartments of some shamans $$$x'$$$ and $$$y'$$$ (i.e. $$$\min\left(\left|H_{x'} - H_{y'}\right|\right)$$$), such that $$$x'$$$ was a trusted friend of $$$x$$$ and $$$y'$$$ was a trusted friend of $$$y$$$ on day $$$v$$$?

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 $$$N$$$, $$$D$$$, $$$U$$$ and $$$Q$$$ $$$(2 \leq N \leq 100000$$$, $$$1 \leq D \leq 500$$$, $$$0 \leq U \leq 200000$$$, $$$1 \leq Q \leq 50000)$$$ – 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 $$$N$$$ space separated integers will follow, the $$$i$$$th $$$(1\leq i \leq N)$$$ of which being $$$H_{i-1}$$$ $$$(0\leq H_{i-1} \leq 10^9)$$$, the altitude of shaman $$$i-1$$$.

On the next $$$U$$$ lines there will be two integers each, on the $$$i$$$th ($$$1 \leq i \leq U$$$) $$$A_i$$$ and $$$B_i$$$ $$$(0 \leq A_i, B_i < N$$$ and $$$A_i \neq B_i)$$$, which represents a pair of shamans who started or stopped trusting each other at the end of day $$$i-1$$$. That is, if $$$A_i$$$ and $$$B_i$$$ trusted each other on day $$$i-1$$$, they did not trust each other on day $$$i$$$, or vice versa. Read all of these integers.

The interactor now will ask you $$$Q$$$ question, so the following interaction should happen $$$Q$$$ times:

- Read $$$3$$$ integers describing the current query: $$$x,y$$$ and $$$v$$$ ($$$x \neq y$$$, $$$0 \leq x,y < N$$$ and $$$0 \leq v \leq U$$$), where $$$x$$$ is the suspected Thief, $$$y$$$ is the suspected Evil Shaman, and $$$v$$$ 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 $$$x'$$$ of $$$x$$$ to a trusted friend $$$y'$$$ of $$$y$$$.
- In case someone trusted both $$$x$$$ and $$$y$$$ (i.e. $$$x'=y'$$$), you should print $$$0$$$.
- If $$$x$$$ or $$$y$$$ had no trusted friends, print $$$10^9$$$.

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

Example

Input

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

Output

26 0 1000000000 14

Note

Example queries:

Evolution of friendships:

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/25/2024 18:03:12 (l3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|