| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 1024 MB | 152 | 15 | 14 | 10.687% |
The IOI Kingdom consists of $N$ cities lined up from west to east, with cities numbered from 1ドル$ to $N$ in order from west.
In the IOI Kingdom, they use Byou as the unit of time. A day in the IOI Kingdom is divided into $T$ units of time. The moment $x$ Byous (0ドル ≤ x < T$) after the beginning of a day is called time $x$. Therefore, when 1ドル$ Byou passes from time $T - 1$ of a certain day, it becomes time 0ドル$ of the next day.
JOI Group is one of the secret sects in the IOI Kingdom. Since it is a secrect sect, members must navigate around the country’s checkpoints. Consequently, JOI Group members are restricted to using only flights operated by JOY Airlines for intercity travel.
JOY Airlines operate $M_i$ flights departing from city $i$ (1ドル ≤ i ≤ N - 1$). The $j$-th flight (1ドル ≤ j ≤ M_i$) departs from city $i$ at time $A_{i, j}$ every day and arrives at city $i + 1$ at time $B_{i, j}$ on the same day. Here, $A_{i, j} < B_{i, j}$ holds. These flights allow convenient transfers, and it is also possible to depart from a city immediately upon arrival or stay overnight at the company’s airports.
The JOI Group has $Q$ members, numbered from 1ドル$ to $Q$. Member $k$ (1ドル ≤ k ≤ Q$) places their operational base in city $L_k$ and their living base in city $R_k$. Therefore, they want to know the minimum time required to travel from city $L_k$ to city $R_k$ by selecting the departure time from city $L_k$ and flights to use appropriately.
Given information about the flights operated by JOY Airlines and the members of the JOI Group, create a program to find the minimum time required for each member $k$ to travel from city $L_k$ to city $R_k$.
Read the following data from the standard input.
$N$ $T$
$M_1$
$A_{1,1}$ $B_{1,1}$
$A_{1,2}$ $B_{1,2}$
$\vdots$
$A_{1,M_1}$ $B_{1,M_1}$
$M_2$
$A_{2,1}$ $B_{2,1}$
$A_{2,2}$ $B_{2,2}$
$\vdots$
$A_{2,M_2}$ $B_{2,M_2}$
$\vdots$
$M_{N-1}$
$A_{N-1,1}$ $B_{N-1,1}$
$A_{N-1,2}$ $B_{N-1,2}$
$\vdots$
$A_{N-1,M_{N-1}}$ $B_{N-1,M_{N-1}}$
$Q$
$L_1$ $R_1$
$L_2$ $R_2$
$\vdots$
$L_Q$ $R_Q$
Output $Q$ lines to the standard output. On the $k$-th line (1ドル ≤ k ≤ Q$), output the minimum time required for the member $k$ to travel from city $L_k$ to city $R_k$.
| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 6 | $N ≤ 2,円 000,ドル $M_i = 1$ (1ドル ≤ i ≤ N - 1$). |
| 2 | 8 | $N ≤ 2,円 000,ドル $M_i ≤ 5$ (1ドル ≤ i ≤ N - 1$). |
| 3 | 17 | $M_i = 1$ (1ドル ≤ i ≤ N - 1$). |
| 4 | 23 | $M_i ≤ 5$ (1ドル ≤ i ≤ N - 1$). |
| 5 | 36 | $N ≤ 90,円 000,ドル $Q ≤ 90,円 000,ドル $M_1 + M_2 + \cdots + M_{N-1} ≤ 90,円 000$. |
| 6 | 10 | No additional constraints. |
4 10000 1 100 300 2 200 400 300 600 1 500 600 3 1 3 2 4 1 4
500 400 10500
As a demonstration, let us designate the day on which member $k$ departs from city $L_k$ as day 1ドル$.
Member 1ドル$ can travel from city 1ドル$ to city 3ドル$ in 500ドル$ Byou by following actions:
Since there is no faster way to travel, output 500ドル$ on line 1ドル$.
Member 2ドル$ can travel from city 2ドル$ to city 4ドル$ in 400ドル$ Byou by following actions:
Since there is no faster way to travel, output 400ドル$ on line 2ドル$.
Member 3ドル$ can travel from city 1ドル$ to city 4ドル$ in 10500ドル$ Byou by following actions:
Since there is no faster way to travel, output 10500ドル$ on line 3ドル$.
This sample input satisfies the constraints of subtasks 2, 4, 5, 6.
6 10000 1 100 300 1 400 700 1 500 600 1 300 900 1 200 800 1 1 6
30700
This sample input satisfies the constraints of all subtasks.