A12923 | Math Problem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Your math teacher gave you the following problem:
There are $n$ segments on the $x$ -axis, $[l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]$ . The segment $[l; r]$ includes the bounds, i.e. it is a set of such $x$ that $l \le x \le r$ . The length of the segment $[l; r]$ is equal to $r - l$ .
Two segments $[a; b]$ and $[c; d]$ have a common point (intersect) if there exists $x$ that $a \leq x \leq b$ and $c \leq x \leq d$ . For example, $[2; 5]$ and $[3; 10]$ have a common point, but $[5; 6]$ and $[1; 4]$ don't have.
You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given $n$ segments.
In other words, you need to find a segment $[a; b]$ , such that $[a; b]$ and every $[l_i; r_i]$ have a common point for each $i$ , and $b-a$ is minimal.
There are $n$ segments on the $x$ -axis, $[l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]$ . The segment $[l; r]$ includes the bounds, i.e. it is a set of such $x$ that $l \le x \le r$ . The length of the segment $[l; r]$ is equal to $r - l$ .
Two segments $[a; b]$ and $[c; d]$ have a common point (intersect) if there exists $x$ that $a \leq x \leq b$ and $c \leq x \leq d$ . For example, $[2; 5]$ and $[3; 10]$ have a common point, but $[5; 6]$ and $[1; 4]$ don't have.
You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given $n$ segments.
In other words, you need to find a segment $[a; b]$ , such that $[a; b]$ and every $[l_i; r_i]$ have a common point for each $i$ , and $b-a$ is minimal.
输入格式
The first line contains integer number $t$ ( $1 \le t \le 100$ ) — the number of test cases in the input. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ( $1 \le n \le 10^{5}$ ) — the number of segments. The following $n$ lines contain segment descriptions: the $i$ -th of them contains two integers $l_i,r_i$ ( $1 \le l_i \le r_i \le 10^{9}$ ).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$ .
The first line of each test case contains one integer $n$ ( $1 \le n \le 10^{5}$ ) — the number of segments. The following $n$ lines contain segment descriptions: the $i$ -th of them contains two integers $l_i,r_i$ ( $1 \le l_i \le r_i \le 10^{9}$ ).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$ .
输出格式
For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.
输入输出样例
输入 #1
4 3 4 5 5 9 7 7 5 11 19 4 17 16 16 3 12 14 17 1 1 10 1 1 1
输出 #1
2 4 0 0
In the first test case of the example, we can choose the segment $[5;7]$ as the answer. It is the shortest segment that has at least one common point with all given segments.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted