CF BUDDY
← Problems·

1907D · Jumping Through Segments

1400 · binary search, constructive algorithms

Problem: Polycarp is designing a level for a game. The level consists of nn segments on the number line, where the ii-th segment starts at the point with coordinate lil_i and ends at the point with coordinate rir_i.

The player starts the level at the point with coordinate 00. In one move, they can move to any point that is within a distance of no more than kk. After their ii-th move, the player must land within the ii-th segment, that is, at a coordinate xx such that lixril_i \le x \le r_i. This means:

  • After the first move, they must be inside the first segment (from l1l_1 to r1r_1);
  • After the second move, they must be inside the second segment (from l2l_2 to r2r_2);
  • ...
  • After the nn-th move, they must be inside the nn-th segment (from lnl_n to rnr_n).

The level is considered completed if the player reaches the nn-th segment, following the rules described above. After some thought, Polycarp realized that it is impossible to complete the level with some values of kk.

Polycarp does not want the level to be too easy, so he asks you to determine the minimum integer kk with which it is possible to complete the level.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4)—the number of test cases. Descriptions of the test cases follow.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5)—the number of segments in the level.

The following nn lines.

The ii-th line contain two integers lil_i and rir_i (0liri1090 \le l_i \le r_i \le 10^9)—the boundaries of the ii-th segment. Segments may intersect.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single integer—the minimum value of kk with which it is possible to complete the level.

Note: In the third example, the player can make the following moves:

  • Move from point 00 to point 55 (3583 \le 5 \le 8);
  • Move from point 55 to point 1010 (10101810 \le 10 \le 18);
  • Move from point 1010 to point 77 (67116 \le 7 \le 11).

Note that for the last move, the player could have chosen not to move and still complete the level.

Sample Cases

Case 1

Input

4
5
1 5
3 4
5 6
8 10
0 1
3
0 2
0 1
0 3
3
3 8
10 18
6 11
4
10 20
0 5
15 17
2 2

Output

7
0
5
13

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback