Logo
(追記) (追記ここまで)

24477번 - Railway Trip 2 서브태스크다국어

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

제한

  • 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.

힌트

출처

Olympiad > Japanese Olympiad in Informatics > JOI 2021/2022 4번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /