310908: CF1907D. Jumping Through Segments

Memory Limit:256 MB Time Limit:5 S
Judge Style:Text Compare Creator:
Submit:9 Solved:0

Description

D. Jumping Through Segmentstime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Polycarp is designing a level for a game. The level consists of $n$ segments on the number line, where the $i$-th segment starts at the point with coordinate $l_i$ and ends at the point with coordinate $r_i$.

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

  • After the first move, they must be inside the first segment (from $l_1$ to $r_1$);
  • After the second move, they must be inside the second segment (from $l_2$ to $r_2$);
  • ...
  • After the $n$-th move, they must be inside the $n$-th segment (from $l_n$ to $r_n$).

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

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

Input

The first line contains a single integer $t$ ($1 \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 $n$ ($1 \le n \le 2 \cdot 10^5$)—the number of segments in the level.

The following $n$ lines.

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

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

Output

For each test case, output a single integer—the minimum value of $k$ with which it is possible to complete the level.

ExampleInput
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
Note

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

  • Move from point $0$ to point $5$ ($3 \le 5 \le 8$);
  • Move from point $5$ to point $10$ ($10 \le 10 \le 18$);
  • Move from point $10$ to point $7$ ($6 \le 7 \le 11$).

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

Output

题目大意:
Polycarp正在设计一个游戏的关卡,这个关卡由数轴上的n个线段组成,第i个线段从坐标为l_i的点开始,到坐标为r_i的点结束。玩家从坐标为0的点开始游戏。每一步,他们可以移动到距离不超过k的任何点。在他们的第i步之后,玩家必须落在第i个线段内,即坐标x满足l_i ≤ x ≤ r_i。这意味着:

- 第一步后,他们必须位于第一个线段(从l_1到r_1)内;
- 第二步后,他们必须位于第二个线段(从l_2到r_2)内;
- ...
- 第n步后,他们必须位于第n个线段(从l_n到r_n)内。

如果玩家按照上述规则到达第n个线段,则认为关卡完成。Polycarp意识到,对于某些k的值,无法完成关卡。Polycarp不希望关卡太容易,所以他要求你确定可以完成关卡的最小整数k。

输入数据格式:
第一行包含一个整数t(1 ≤ t ≤ 10^4)——测试用例的数量。接下来是t个测试用例的描述。

每个测试用例的第一行包含一个整数n(1 ≤ n ≤ 2 * 10^5)——关卡中的线段数量。

接下来n行,每行包含两个整数l_i和r_i(0 ≤ l_i ≤ r_i ≤ 10^9)——第i个线段的边界。线段可能相交。

保证所有测试用例的n之和不超过2 * 10^5。

输出数据格式:
对于每个测试用例,输出一个整数——可以完成关卡的最小k值。

示例:
输入:
```
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
```
输出:
```
7
0
5
13
```题目大意: Polycarp正在设计一个游戏的关卡,这个关卡由数轴上的n个线段组成,第i个线段从坐标为l_i的点开始,到坐标为r_i的点结束。玩家从坐标为0的点开始游戏。每一步,他们可以移动到距离不超过k的任何点。在他们的第i步之后,玩家必须落在第i个线段内,即坐标x满足l_i ≤ x ≤ r_i。这意味着: - 第一步后,他们必须位于第一个线段(从l_1到r_1)内; - 第二步后,他们必须位于第二个线段(从l_2到r_2)内; - ... - 第n步后,他们必须位于第n个线段(从l_n到r_n)内。 如果玩家按照上述规则到达第n个线段,则认为关卡完成。Polycarp意识到,对于某些k的值,无法完成关卡。Polycarp不希望关卡太容易,所以他要求你确定可以完成关卡的最小整数k。 输入数据格式: 第一行包含一个整数t(1 ≤ t ≤ 10^4)——测试用例的数量。接下来是t个测试用例的描述。 每个测试用例的第一行包含一个整数n(1 ≤ n ≤ 2 * 10^5)——关卡中的线段数量。 接下来n行,每行包含两个整数l_i和r_i(0 ≤ l_i ≤ r_i ≤ 10^9)——第i个线段的边界。线段可能相交。 保证所有测试用例的n之和不超过2 * 10^5。 输出数据格式: 对于每个测试用例,输出一个整数——可以完成关卡的最小k值。 示例: 输入: ``` 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 ``` 输出: ``` 7 0 5 13 ```

加入题单

上一题 下一题 算法标签: