#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 stations on it, numbered . Station and station are directly connected by a piece of track.
The IOI Railway Company is operating routes, numbered . Route starts at and ends at , and the train stops at every station. That is, if , a train on route stops in order at stations . If , a train on route stops in order at stations .
JOI is a traveler. He has travel plans. In the -th travel plan, he plans to travel from station to station 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 stations from the starting station of a route (including the starting station). In other words, for route , if , then he will only board a train on route at stations $A_j, A_j + 1, \ldots, \min \{ A_j + K - 1, B_j - 1 \}$. If , then he will only board a train on route 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 .
The second line contains one positive integer .
The next lines each contain two positive integers on the -th line.
The next line contains one positive integer .
The next lines each contain two positive integers on the -th line.
Output Format
Output lines. The -th line contains one number, representing the minimum number of rides needed for JOI to complete his -th plan. If it is impossible to complete the -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 to station . Specifically, this plan can be carried out as follows: JOI boards route at station and gets off at station . In total, JOI takes one train. Since it is impossible to complete this plan with fewer than ride, output on the first line.
For the second plan, JOI wants to travel from station to station . Specifically, this plan can be carried out as follows: JOI boards route at station and gets off at station ; then he boards route at station and gets off at station . In total, JOI takes two trains. Since it is impossible to complete this plan with fewer than rides, output on the second line.
For the third plan, JOI wants to travel from station to station . Since it is impossible to complete this plan, output -1 on the third line.
This sample satisfies the constraints of subtasks .
[Sample Explanation #2]
This sample satisfies the constraints of subtasks .
[Sample Explanation #3]
This sample satisfies the constraints of subtasks .
[Sample Explanation #4]
This sample satisfies the constraints of subtasks .
[Constraints]
This problem uses bundled testdata.
For of the testdata, , , , , , , , (), ().
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): , .
- Subtask ( points): No special constraints.
Translated from JOI 2022 Final T4 "鉄道旅行 2 / Railway Trip 2".
Translated by ChatGPT 5