Escape Route 2
Time limit2sMemory limit1024 MB
Given daily repeating flight schedules between consecutive cities, find the minimum travel time from city L to city R for each query.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Binary search, Sorting
- Solved
- No attempts yet
Problem
The IOI Kingdom has cities in a row from west to east, numbered from to starting from the west.
The IOI Kingdom uses Byou as its unit of time. A day is divided into units of time. The moment Byou () after the start of a day is called time . So when 1 Byou passes from time on some day, it becomes time the next day.
JOI Group is one of the secret sects in the IOI Kingdom. Members must avoid the country's checkpoints, so they may only use flights operated by JOY Airlines for travel between cities.
JOY Airlines operates flights departing from city (). The -th flight () departs from city at time every day and arrives at city at time on the same day, where . These flights allow convenient transfers. A member can also leave a city right after arriving, or stay overnight at the airline's airports.
The JOI Group has members, numbered from to . Member () has an operational base in city and a living base in city . Each member wants the minimum time needed to travel from to , choosing the departure time from and the flights to take.
Given the flight information and the members' bases, write a program that finds, for each member , the minimum time to travel from city to city .
Input
Read the following data from standard input.
N T
M_1
A_{1,1} B_{1,1}
A_{1,2} B_{1,2}
...
A_{1,M_1} B_{1,M_1}
M_2
...
M_{N-1}
...
Q
L_1 R_1
L_2 R_2
...
L_Q R_Q
Output
Output lines to standard output. On the -th line, output the minimum time required for member to travel from city to city .
Constraints
- .
- .
- ().
- .
- (, ).
- .
- ().
- All given values are integers.