#P8163. [JOI 2022 Final] 铁路旅行 2 / Railway Trip 2

[JOI 2022 Final] 铁路旅行 2 / Railway Trip 2

Problem Description

The IOI Railway Company operates routes on a single railway track. The track is a straight line, and there are NN stations on it, numbered 1N1 \sim N. Station ii and station i+1i + 1 are directly connected by a piece of track.

The IOI Railway Company is operating MM routes, numbered 1M1 \sim M. Route jj starts at AjA_j and ends at BjB_j, and the train stops at every station. That is, if Aj<BjA_j < B_j, a train on route jj stops in order at stations Aj,Aj+1,,BjA_j, A_j + 1, \ldots, B_j. If Aj>BjA_j > B_j, a train on route jj stops in order at stations Aj,Aj1,,BjA_j, A_j - 1, \ldots, B_j.

JOI is a traveler. He has QQ travel plans. In the kk-th travel plan, he plans to travel from station SkS_k to station TkT_k by taking trains.

However, JOI is exhausted from long-distance travel. He wants every train he takes to have an empty seat so that he can rest. Therefore, JOI will only board within KK stations from the starting station of a route (including the starting station). In other words, for route jj, if Aj<BjA_j < B_j, then he will only board a train on route jj at stations $A_j, A_j + 1, \ldots, \min \{ A_j + K - 1, B_j - 1 \}$. If Aj>BjA_j > B_j, then he will only board a train on route jj at stations $A_j, A_j - 1, \ldots, \max \{ A_j - K + 1, B_j + 1 \}$.

Under these conditions, JOI wants to minimize the number of train rides as much as possible.

Given the route information of the IOI Railway Company and JOI's plans, write a program to compute, for each of JOI's plans, the minimum number of rides he needs.

Input Format

The first line contains two positive integers N,KN, K.

The second line contains one positive integer MM.

The next MM lines each contain two positive integers Aj,BjA_j, B_j on the jj-th line.

The next line contains one positive integer QQ.

The next QQ lines each contain two positive integers Sk,TkS_k, T_k on the kk-th line.

Output Format

Output QQ lines. The kk-th line contains one number, representing the minimum number of rides needed for JOI to complete his kk-th plan. If it is impossible to complete the kk-th plan, output -1.

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

1
2
-1

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

1
-1
1
2

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

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

Hint

[Sample Explanation #1]

For the first plan, JOI wants to travel from station 55 to station 33. Specifically, this plan can be carried out as follows: JOI boards route 11 at station 55 and gets off at station 33. In total, JOI takes one train. Since it is impossible to complete this plan with fewer than 11 ride, output 11 on the first line.

For the second plan, JOI wants to travel from station 33 to station 22. Specifically, this plan can be carried out as follows: JOI boards route 22 at station 33 and gets off at station 44; then he boards route 11 at station 44 and gets off at station 22. In total, JOI takes two trains. Since it is impossible to complete this plan with fewer than 22 rides, output 22 on the second line.

For the third plan, JOI wants to travel from station 22 to station 11. Since it is impossible to complete this plan, output -1 on the third line.

This sample satisfies the constraints of subtasks 1,2,61, 2, 6.

[Sample Explanation #2]

This sample satisfies the constraints of subtasks 1,2,61, 2, 6.

[Sample Explanation #3]

This sample satisfies the constraints of subtasks 1,2,4,61, 2, 4, 6.

[Sample Explanation #4]

This sample satisfies the constraints of subtasks 1,2,5,61, 2, 5, 6.


[Constraints]

This problem uses bundled testdata.

For 100%100\% of the testdata, 2N1052 \le N \le {10}^5, 1KN11 \le K \le N - 1, 1M2×1051 \le M \le 2 \times {10}^5, 1Q5×1041 \le Q \le 5 \times {10}^4, 1Aj,Bj,Sk,TkN1 \le A_j, B_j, S_k, T_k \le N, AjBjA_j \ne B_j, SkTkS_k \ne T_k, (Aj,Bj)(Ak,Bk)(A_j, B_j) \ne (A_k, B_k) (jkj \ne k), (Sk,Tk)(Sl,Tl)(S_k, T_k) \ne (S_l, T_l) (klk \ne l).

  • Subtask 11 (88 points): N,M,Q300N, M, Q \le 300.
  • Subtask 22 (88 points): N,M,Q2000N, M, Q \le 2000.
  • Subtask 33 (1111 points): Q=1Q = 1.
  • Subtask 44 (2525 points): K=N1K = N - 1.
  • Subtask 55 (3535 points): Aj<BjA_j < B_j, Sk<TkS_k < T_k.
  • Subtask 66 (1313 points): No special constraints.

Translated from JOI 2022 Final T4 "鉄道旅行 2 / Railway Trip 2".

Translated by ChatGPT 5