Math Problem(求与n个区间中每一个区间都至少有一个公共点的最短区间的长度)

A. Math Problem

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Your math teacher gave you the following problem:

There are nn segments on the xx-axis, [l1;r1],[l2;r2],…,[ln;rn][l1;r1],[l2;r2],…,[ln;rn]. The segment [l;r][l;r] includes the bounds, i.e. it is a set of such xx that l≤x≤rl≤x≤r. The length of the segment [l;r][l;r] is equal to r−lr−l.

Two segments [a;b][a;b] and [c;d][c;d] have a common point (intersect) if there exists xx that a≤x≤ba≤x≤b and c≤x≤dc≤x≤d. For example, [2;5][2;5] and [3;10][3;10] have a common point, but [5;6][5;6] and [1;4][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 nn segments.

In other words, you need to find a segment [a;b][a;b], such that [a;b][a;b] and every [li;ri][li;ri] have a common point for each ii, and b−ab−a is minimal.

Input

The first line contains integer number tt (1≤t≤1001≤t≤100) — the number of test cases in the input. Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤1051≤n≤105) — the number of segments. The following nn lines contain segment descriptions: the ii-th of them contains two integers li,rili,ri (1≤li≤ri≤1091≤li≤ri≤109).

The sum of all values nn over all the test cases in the input doesn't exceed 105105.

Output

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.

Example

input

Copy

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

output

Copy

2
4
0
0

Note

In the first test case of the example, we can choose the segment [5;7][5;7] as the answer. It is the shortest segment that has at least one common point with all given segments.

题意:求与给定的n个区间中每一个区间都至少有一个公共点的最短区间的长度

思路:显然该最短区间长度len=max(0,maxl-minr);其中maxl代表n个区间的左端点的最大值,minr代表n个区间右端点的最小值;

注:当maxl<minr时(即:maxl-minr<0),即代表n个区间有交叉,此时取其交叉部分的一点作为所求最短区间即可,此时因为是点,所以len=0。.

完整代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t,l,r;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int N=n,lmax=-1,rmin=0x3f3f3f3f;
        while(N--){
            cin>>l>>r;
            lmax=max(lmax,l);
            rmin=min(rmin,r);
        }
        int ans=0;
        if(lmax-rmin>0){
            ans=lmax-rmin;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值