Railway Trip
Time limit2sMemory limit512 MB
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 stations numbered from 1 to . For each (), station and station are connected by a track.
JOI Railways runs types of trains, and each train runs in both directions. The train types are integers from 1 to . Each station has a level, which is an integer from 1 to . For each (), station has level . Stations 1 and both have level .
A train of type () stops at every station whose level is at least , and at no other station. Since stations 1 and have level , 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 , , and , the number of stations, the number of train types, and the number of queries. Each of the next lines contains one integer, the level of station . Each of the next lines contains two integers and , the starting station and the destination of the -th passenger.
Output
Print lines. The -th line contains the minimum number of intermediate stops on a route from station to station .
Constraints
- for
- for
- for
- for