시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB20721168.889%

문제

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

제한

  • $2 ≤ N ≤ 100\, 000$.
  • $2 ≤ T ≤ 10^9$.
  • $M_i ≥ 1$ ($1 ≤ i ≤ N - 1$).
  • $M_1 + M_2 + \cdots + M_{N-1} ≤ 100\, 000$.
  • $0 ≤ A_{i, j} < B_{i, j} < T$ ($1 ≤ i ≤ N - 1$, $1 ≤ j ≤ M_i$).
  • $1 ≤ Q ≤ 300\, 000$.
  • $1 ≤ L_k < R_k ≤ N$ ($1 ≤ k ≤ Q$).
  • Given values are all integers.

서브태스크

번호배점제한
16

$N ≤ 2\, 000$, $M_i = 1$ ($1 ≤ i ≤ N - 1$).

28

$N ≤ 2\, 000$, $M_i ≤ 5$ ($1 ≤ i ≤ N - 1$).

317

$M_i = 1$ ($1 ≤ i ≤ N - 1$).

423

$M_i ≤ 5$ ($1 ≤ i ≤ N - 1$).

536

$N ≤ 90\, 000$, $Q ≤ 90\, 000$, $M_1 + M_2 + \cdots + M_{N-1} ≤ 90\, 000$.

610

No additional constraints.

예제 입력 1

4 10000
1
100 300
2
200 400
300 600
1
500 600
3
1 3
2 4
1 4

예제 출력 1

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:

  1. Depart from city $1$ at time $100$ on day $1$ and arrive at city $2$ at time $300$ on day $1$.
  2. Depart from city $2$ at time $300$ on day $1$ and arrive at city $3$ at time $600$ on day $1$.

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:

  1. Depart from city $2$ at time $200$ on day $1$ and arrive at city $3$ at time $400$ on day $1$.
  2. Depart from city $3$ at time $500$ on day $1$ and arrive at city $4$ at time $600$ on day $1$.

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:

  1. Depart from city $1$ at time $100$ on day $1$ and arrive at city $2$ at time $300$ on day $1$.
  2. Depart from city $2$ at time $300$ on day $1$ and arrive at city $3$ at time $600$ on day $1$.
  3. Depart from city $3$ at time $500$ on day $2$ and arrive at city $4$ at time $600$ on day $2$.

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.

예제 입력 2

6 10000
1
100 300
1
400 700
1
500 600
1
300 900
1
200 800
1
1 6

예제 출력 2

30700

This sample input satisfies the constraints of all subtasks.

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.