#P10439. [JOIST 2024] 逃生路线 2 / Escape Route 2

[JOIST 2024] 逃生路线 2 / Escape Route 2

Problem Description

The IOI Kingdom consists of NN cities arranged from west to east. The cities are numbered from 11 to NN in order from west to east.

In the IOI Kingdom, they use Byou as the unit of time. One day in the IOI Kingdom is divided into TT time units. The time xx Byous (0x<T0 \leq x < T) after the start of some day is called time xx. Therefore, when 11 Byou passes starting from time T1T - 1 of some day, it becomes time 00 of the next day.

The JOI Organization is one of the secret cults in the IOI Kingdom. Since it is a secret cult, its members must bypass the kingdom’s checkpoints. Therefore, JOI Organization members can only travel between cities using flights operated by JOY Airlines.

JOY Airlines offers MiM_i flights in city ii (1iN11 \leq i \leq N - 1). Flight jj (1jMi1 \leq j \leq M_i) departs from city ii every day at time Ai,jA_{i,j} and arrives at city i+1i + 1 at time Bi,jB_{i,j} on the same day. Here, Ai,j<Bi,jA_{i,j} < B_{i,j} holds.

These flights provide convenient connections: you may depart immediately after arriving, or stay overnight at the airline’s airport.

The JOI Organization has QQ members, numbered from 11 to QQ. Member kk (1kQ1 \leq k \leq Q) has their operation base in city LkL_k and their living base in city RkR_k. Therefore, they want to know the shortest time needed to travel from city LkL_k to city RkR_k, by choosing a departure time from city LkL_k and taking suitable flights.

Given the flights operated by JOY Airlines and the information about the JOI Organization members, write a program to find, for each member kk, the shortest time to travel from city LkL_k to city RkR_k.

Input Format

Read the following data from standard input.

  • NN TT
  • M1M_1
  • A1,1A_{1,1} B1,1B_{1,1}
  • A1,2A_{1,2} B1,2B_{1,2}
  • ...
  • A1,M1A_{1,M_1} B1,M1B_{1,M_1}
  • M2M_2
  • A2,1A_{2,1} B2,1B_{2,1}
  • A2,2A_{2,2} B2,2B_{2,2}
  • ...
  • A2,M2A_{2,M_2} B2,M2B_{2,M_2}
  • ...
  • MN1M_{N-1}
  • AN1,1A_{N-1,1} BN1,1B_{N-1,1}
  • AN1,2A_{N-1,2} BN1,2B_{N-1,2}
  • ...
  • AN1,MN1A_{N-1,M_{N-1}} BN1,MN1B_{N-1,M_{N-1}}
  • QQ
  • L1L_1 R1R_1
  • L2L_2 R2R_2
  • ...
  • LQL_Q RQR_Q

Output Format

Output QQ lines to standard output. On line kk (1kQ1 \leq k \leq Q), output the shortest time required for member kk to travel from city LkL_k to city RkR_k.

4 10000
1
100 300
2
200 400
300 600
1
500 600
3
1 3
2 4
1 4
500
400
10500
6 10000
1
100 300
1
400 700
1
500 600
1
300 900
1
200 800
1
1 6

30700

Hint

Sample Explanation 1

For demonstration, let us call the first day on which member kk departs from city LkL_k Day 11. Member 11 can travel from city 11 to city 33 in 500500 Byou as follows.

  1. Depart from city 11 at time 100100 on Day 11, and arrive at city 22 at time 300300 on Day 11.
  2. Depart from city 22 at time 300300 on Day 11, and arrive at city 33 at time 600600 on Day 11.

Since there is no faster way to travel, output 500500 on line 11.

Member 22 can travel from city 22 to city 44 in 400400 Byou as follows.

  1. Depart from city 22 at time 200200 on Day 11, and arrive at city 33 at time 400400 on Day 11.
  2. Depart from city 33 at time 500500 on Day 11, and arrive at city 44 at time 600600 on Day 11.

Since there is no faster way to travel, output 400400 on line 22.

Member 33 can travel from city 11 to city 44 in 1050010500 Byou as follows.

  1. Depart from city 11 at time 100100 on Day 11, and arrive at city 22 at time 300300 on Day 11.
  2. Depart from city 22 at time 300300 on Day 11, and arrive at city 33 at time 600600 on Day 11.
  3. Depart from city 33 at time 500500 on Day 22, and arrive at city 44 at time 600600 on Day 22.

Since there is no faster way to travel, output 1050010500 on line 33.

This sample input satisfies the constraints for Subtasks 2,4,5,62,4,5,6.

Sample Explanation 2

This sample input satisfies the constraints for all subtasks.

Constraints

  • 2N100,0002 \leq N \leq 100,000
  • 2T1092 \leq T \leq 10^9
  • Mi1M_i \geq 1 (1iN11 \leq i \leq N - 1)
  • M1+M2++MN1100,000M_1 + M_2 + \cdots + M_{N-1} \leq 100,000
  • 0Ai,j<Bi,j<T0 \leq A_{i,j} < B_{i,j} < T (1iN1,1jMi1 \leq i \leq N - 1, 1 \leq j \leq M_i)
  • 1Q300,0001 \leq Q \leq 300,000
  • 1Lk<RkN1 \leq L_k < R_k \leq N (1kQ1 \leq k \leq Q)
  • All given values are integers.

Subtasks

  1. (6 points) N2,000N \leq 2,000, Mi=1M_i = 1 (1iN11 \leq i \leq N - 1).
  2. (8 points) N2,000N \leq 2,000, Mi5M_i \leq 5 (1iN11 \leq i \leq N - 1).
  3. (17 points) Mi=1M_i = 1 (1iN11 \leq i \leq N - 1).
  4. (23 points) Mi5M_i \leq 5 (1iN11 \leq i \leq N - 1).
  5. (36 points) N90,000N \leq 90,000, Q90,000Q \leq 90,000, M1+M2++MN190,000M_1 + M_2 + \cdots + M_{N-1} \leq 90,000.
  6. (10 points) No additional constraints.

Translated by ChatGPT 5