#P1403A. The Potion of Great Power

    ID: 5537 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>*special problem2-satbinary searchdata structuresgraphsinteractivesortingstwo pointers*2400

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 ii between 00 and N1N-1, and an altitude value HiH_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 DD others at any given time.
They have also reconstructed a log of who trusted whom: for each night they know which pair of shamans started/stopped trusting each other.

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 xx, the Evil Shaman was yy, and the formula was whispered on day vv? What is the smallest distance the whispered formula had to travel? That is, what is the minimum distance between the apartments of some shamans xx' and yy' (i.e. min(HxHy)\min\left(\left|H_{x'} - H_{y'}\right|\right)), such that xx' was a trusted friend of xx and yy' was a trusted friend of yy on day vv?

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 NN, DD, UU and QQ (2N100000(2 \leq N \leq 100000, 1D5001 \leq D \leq 500, 0U2000000 \leq U \leq 200000, 1Q50000)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 NN space separated integers will follow, the iith (1iN)(1\leq i \leq N) of which being Hi1H_{i-1} (0Hi1109)(0\leq H_{i-1} \leq 10^9), the altitude of shaman i1i-1.

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

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

  1. Read 33 integers describing the current query: x,yx,y and vv (xyx \neq y, 0x,y<N0 \leq x,y < N and 0vU0 \leq v \leq U), where xx is the suspected Thief, yy is the suspected Evil Shaman, and vv is the suspected day..
  2. 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 xx' of xx to a trusted friend yy' of yy.
    • In case someone trusted both xx and yy (i.e. x=yx'=y'), you should print 00.
    • If xx or yy had no trusted friends, print 10910^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

SubtaskPointsConstraints10samples217Q,U1000314v=Ufor all questions418Hi{0,1}for all shamansi521U,N10000630no additional constraints \begin{array}{|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} \\ \hline 1 & 0 & \text{samples}\\ \hline 2 & 17 & Q,U \leq 1000 \\ \hline 3 & 14 & v=U \: \text{for all questions} \\ \hline 4 & 18 & H_i \in \left\{0,1\right\} \: \text{for all shamans} \: i \\ \hline 5 & 21 & U,N \leq 10000\\ \hline 6 & 30 & \text{no additional constraints}\\ \hline \end{array}

Samples

Sample Input 1

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

Sample Output 1

26
0
1000000000
14

Note

Example queries:

Evolution of friendships: