Under construction

Still being built. Things may change

Railway Trip

Time limit2sMemory limit512 MB

Summary
Given station levels that define which train types stop where, answer each query with the fewest intermediate stops needed to travel between two stations.
Level

Hard8 of 10

Topics
Array, Dynamic programming, Greedy
Solved
No attempts yet

Problem

JOI Railways operates one railway. The railway is a straight line of NN stations numbered from 1 to NN. For each ii (1≤i≤N−11 \le i \le N-1), station ii and station i+1i+1 are connected by a track.

JOI Railways runs KK types of trains, and each train runs in both directions. The train types are integers from 1 to KK. Each station has a level, which is an integer from 1 to KK. For each ii (1≤i≤N1 \le i \le N), station ii has level LiL_i. Stations 1 and NN both have level KK.

A train of type jj (1≤j≤K1 \le j \le K) stops at every station whose level is at least jj, and at no other station. Since stations 1 and NN have level KK, every train stops at both of them.

Many passengers use the railway every day. During a trip, a passenger may take a train heading in the opposite direction from the destination, or may pass the destination. At the end, the passenger must stop at the destination. Passengers dislike stopping, so each one takes the route with the fewest intermediate stops, regardless of how many stations are passed or how many times trains are changed. If a passenger stops at a station to change trains, that counts as one stop. The first stop at the starting station and the last stop at the destination are not intermediate stops.

Write a program that answers queries on the minimum number of intermediate stops for each passenger.

Input

The first line contains three integers NN, KK, and QQ, the number of stations, the number of train types, and the number of queries. Each of the next NN lines contains one integer, the level LiL_i of station ii. Each of the next QQ lines contains two integers AkA_k and BkB_k, the starting station and the destination of the kk-th passenger.

Output

Print QQ lines. The kk-th line contains the minimum number of intermediate stops on a route from station AkA_k to station BkB_k.

Constraints

  • 2≤N≤100 0002 \le N \le 100\,000
  • 1≤K≤N1 \le K \le N
  • 1≤Q≤100 0001 \le Q \le 100\,000
  • 1≤Li≤K1 \le L_i \le K for 1≤i≤N1 \le i \le N
  • 1≤Ak≤N1 \le A_k \le N for 1≤k≤Q1 \le k \le Q
  • 1≤Bk≤N1 \le B_k \le N for 1≤k≤Q1 \le k \le Q
  • Ak≠BkA_k \neq B_k for 1≤k≤Q1 \le k \le Q

Examples3

  1. Example 1

    Input
    9 3 3
    3
    1
    1
    1
    2
    2
    2
    3
    3
    2 4
    4 9
    6 7
    
    Expected output
    1
    3
    0
    
  2. Example 2

    Input
    5 2 1
    2
    1
    1
    1
    2
    1 4
    
    Expected output
    1
    
  3. Example 3

    Input
    15 5 15
    5
    4
    1
    2
    3
    1
    1
    2
    4
    5
    4
    1
    5
    3
    5
    8 1
    11 1
    5 3
    6 11
    9 12
    15 14
    15 2
    3 12
    2 1
    4 8
    15 5
    12 6
    1 13
    13 8
    14 9
    
    Expected output
    2
    1
    1
    3
    2
    0
    3
    4
    0
    1
    3
    4
    1
    2
    2