시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 90 | 40 | 35 | 46.053% |
IOI Railway Company is operating lines on a railway track. There are $N$ stations in a straight line, numbered from $1$ to $N$. For each $i$ ($1 ≤ i ≤ N - 1$), Station $i$ and Station $i + 1$ are connected directly by a railway track.
IOI Railway Company is operating $M$ lines, numbered from $1$ to $M$. In Line $j$ ($1 ≤ j ≤ M$), the starting station is Station $A_j$, and the terminal station is Station $B_j$. A train stops at every station. Namely, if $A_j < B_j$ a train of Line $j$ stops at Station $A_j$, Station $A_j + 1$, $\dots$, Station $B_j$, in this order. If $A_j > B_j$, a train of Line $j$ stops at Station $A_j$, Station $A_j - 1$, $\dots$, Station $B_j$, in this order.
JOI-kun is a traveler. He has $Q$ travel plans. In the $k$-th plan ($1 ≤ k ≤ Q$), he travels from Station $S_k$ to Station $T_k$ by taking lines.
However, JOI-kun is tired from a long journey. He wants to take a vacant train and get a seat. Thus, JOI-kun decided that he takes a train of a line at a station only if it is the $K$-th or earlier stop from the starting station of the line. In other words, if $A_j < B_j$, he can take a train of Line $j$ only at Station $A_j$, Station $A_j + 1$, $\dots$, Station $\min{\{A_j + K - 1, B_j - 1\}}$. If $A_j > B_j$, he can take a train of Line $j$ only at Station $A_j$, Station $A_j - 1$, $\dots$, Station $\max{\{A_j - K + 1, B_j + 1\}}$. JOI-kun will get out of the train at a station between the station next to where he takes the train and the terminal station, inclusive.
Under these conditions, JOI-kun wants to minimize the number of times of taking trains.
Given the information of the lines of IOI Railway Company and JOI-kun’s plans, write a program which calculates, for each of JOI-kun’s plans, the minimum number of times of taking trains needed for JOI-kun to achieve it.
Read the following data from the standard input. Given values are all integers.
$\begin{align*}&N\,K \\ & M \\ & A_1\,B_1 \\ & A_2\,B_2 \\ & \vdots \\ & A_M\,B_M \\ & Q \\ & S_1\,T_1 \\ & S_2\,T_2 \\ & \vdots \\ & S_Q\,T_Q\end{align*}$
Write $Q$ lines to the standard output. The $k$-th line ($1 ≤ k ≤ Q$) should contain the minimum number of times of taking trains needed for JOI-kun to achieve the $k$-th plan. If it is not possible to achieve the $k$-th plan, output -1
.
번호 | 배점 | 제한 |
---|---|---|
1 | 8 | $N ≤ 300$, $M ≤ 300$, $Q ≤ 300$. |
2 | 8 | $N ≤ 2\,000$, $M ≤ 2\,000$, $Q ≤ 2\,000$. |
3 | 11 | $Q = 1$. |
4 | 25 | $K = N - 1$. |
5 | 35 | $A_j < B_j$ ($1 ≤ j ≤ M$), $S_k < T_k$ ($1 ≤ k ≤ Q$). |
6 | 13 | No additional constraints. |
5 2 2 5 1 3 5 3 5 3 3 2 2 1
1 2 -1
In the first plan, JOI-kun travels from Station $5$ to Station $3$. For example, this plan is achieved if JOI-kun takes a train of Line $1$ at Station $5$, and get out of the train at Station $3$. In total, JOI-kun will take one train. Since it is impossible to achieve the plan by taking less than one train, output $1$ in the first line.
In the second plan, JOI-kun travels from Station $3$ to Station $2$. For example, this plan is achieved if JOI-kun takes a train of Line $2$ at Station $3$, get out of the train at Station $4$, takes a train of Line $$1 at Station $4$, and get out of the train at Station $2$. In total, JOI-kun will take two trains. Since it is impossible to achieve the plan by taking less than two trains, output $2$ in the second line.
In the third plan, JOI-kun travels from Station $2$ to Station $1$. Since it is impossible for JOI-kun to achieve this plan, output -1
in the third line.
This sample input satisfies the constraints of Subtasks 1, 2, 6.
6 3 2 1 6 5 1 4 5 1 6 3 3 6 2 1
1 -1 1 2
This sample input satisfies the constraints of Subtasks 1, 2, 6.
6 5 4 3 1 2 4 5 3 4 6 5 1 5 3 2 2 6 6 3 5 4
-1 1 2 -1 1
This sample input satisfies the constraints of Subtasks 1, 2, 4, 6.
12 1 5 1 7 10 12 3 5 8 10 5 9 7 2 11 5 8 3 12 4 6 1 9 9 10 1 4
-1 1 4 -1 2 -1 1
This sample input satisfies the constraints of Subtasks 1, 2, 5, 6.