| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 512 MB | 181 | 70 | 62 | 37.805% |
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.