시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 20 | 15 | 13 | 86.667% |
JOI Railways is a company operating one railway. In the railway of JOI Railways, there are N stations on a straight line numbered from 1 to N. For each i (1 ≤ i ≤ N − 1), the station i and the station i + 1 are connected by a railway track.
JOI Railways has K types of trains running in both directions. The types of trains are expressed as integers between 1 and K inclusive. Each station has a level which is an integer between 1 and K inclusive. For each i (1 ≤ i ≤ N), the station i has level Li . The stations in both ends, namely the station 1 and the station N, have level K.
A train of type j (1 ≤ j ≤ K) stops at every station whose level is greater than or equal to j, and it does not stop at any other stations. Since the stations in both ends, namely the station 1 and the station N, have level K, every train stops at these stations.
Many passengers use JOI Railways every day. During travel, they can take a train whose direction is opposite to the destination, or they can pass the destination. In the end of the travel, they have to stop at the destination. They do not like to stop at stations very much. Hence they try to take a route with minimum number of intermediate stops regardless of the number of passed stations or the number of connections. If a passenger stops at a station to change trains, we count it as one stop. The first stop at the starting station and the last stop at the destination are not considered as intermediate stops.
Your task is to write a program which answers queries on the minimum number of intermediate stops for each passenger.
Given information of the railway of JOI Railways and the starting station and the destination of each passenger, write a program which answers queries on the minimum number of intermediate stops for each passenger.
Read the following data from the standard input.
Write Q lines to the standard output. The k-th line (1 ≤ k ≤ Q) of output contains the minimum number of intermediate stops of a route from the station Ak to the station Bk.
There are no additional constraints.
9 3 3 3 1 1 1 2 2 2 3 3 2 4 4 9 6 7
1 3 0
In this sample input, three queries about travels between two stations are given.
5 2 1 2 1 1 1 2 1 4
1
Note that passengers can pass the destination during travel.
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
2 1 1 3 2 0 3 4 0 1 3 4 1 2 2