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

제한

  • $2 ≤ N ≤ 100\,000$.
  • $1 ≤ K ≤ N - 1$.
  • $1 ≤ M ≤ 200\,000$.
  • $1 ≤ A_j ≤ N$ ($1 ≤ j ≤ M$).
  • $1 ≤ B_j ≤ N$ ($1 ≤ j ≤ M$).
  • $A_j \ne B_j$ ($1 ≤ j ≤ M$).
  • $(A_j , B_j) \ne (A_k, B_k)$ ($1 ≤ j < k ≤ M$).
  • $1 ≤ Q ≤ 50\,000$.
  • $1 ≤ S_k ≤ N$ ($1 ≤ k ≤ Q$).
  • $1 ≤ T_k ≤ N$ ($1 ≤ k ≤ Q$).
  • $S_k \ne T_k$ ($1 ≤ k ≤ Q$).
  • $(S_k, T_k) \ne (S_l , T_l)$ ($1 ≤ k < l ≤ Q$).

서브태스크

번호배점제한
18

$N ≤ 300$, $M ≤ 300$, $Q ≤ 300$.

28

$N ≤ 2\,000$, $M ≤ 2\,000$, $Q ≤ 2\,000$.

311

$Q = 1$.

425

$K = N - 1$.

535

$A_j < B_j$ ($1 ≤ j ≤ M$), $S_k < T_k$ ($1 ≤ k ≤ Q$).

613

No additional constraints.

예제 입력 1

5 2
2
5 1
3 5
3
5 3
3 2
2 1

예제 출력 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.

예제 입력 2

6 3
2
1 6
5 1
4
5 1
6 3
3 6
2 1

예제 출력 2

1
-1
1
2

This sample input satisfies the constraints of Subtasks 1, 2, 6.

예제 입력 3

6 5
4
3 1
2 4
5 3
4 6
5
1 5
3 2
2 6
6 3
5 4

예제 출력 3

-1
1
2
-1
1

This sample input satisfies the constraints of Subtasks 1, 2, 4, 6.

예제 입력 4

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

예제 출력 4

-1
1
4
-1
2
-1
1

This sample input satisfies the constraints of Subtasks 1, 2, 5, 6.

채점 및 기타 정보

  • 예제는 채점하지 않는다.