Codeforces Round 857 (Div. 1) B

130 阅读2分钟

Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city.

There are n departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from 1 to n. It is known that gifts in the first store of the i department cost ai rubles, and in the second store of the i department — bi rubles.

Entering the mall, Sasha will visit each of the n departments of the mall, and in each department, he will enter exactly one store. When Sasha gets into the i-th department, he will perform exactly one of two actions:

  1. Buy a gift for the first friend, spending ai rubles on it.
  2. Buy a gift for the second friend, spending bi rubles on it.

Sasha is going to buy at least one gift for each friend. Moreover, he wants to pick up gifts in such a way that the price difference of the most expensive gifts bought for friends is as small as possible so that no one is offended.

More formally: let m1  be the maximum price of a gift bought to the first friend, and m2  be the maximum price of a gift bought to the second friend. Sasha wants to choose gifts in such a way as to minimize the value of |m1−m2|.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤500000) — the number of departments in the mall.

Each of the following n lines of each test case contains two integers ai and bi (0≤ai,bi≤109) — the prices of gifts in the first and second store of the i department, respectively.

It is guaranteed that the sum of n over all test cases does not exceed 500000.

Output

Print one integer — the minimum price difference of the most expensive gifts bought to friends.

Example

input

Copy

2
2
1 2
2 1
5
1 5
2 7
3 3
4 10
2 5

output

Copy

0
1

Note

In the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend  — in the second department, or vice versa. In the first case, m1=m2=1, and in the second case — m1=m2=2. In both cases, the answer is 00. In the second test case, you can buy gifts for the first friend in the 2, 4 and 5 departments, and for the second friend  — in the 1 and 3 departments.So m1=max(2,4,2)=4, m2=max(5,3)=5. The answer is |4−5|=1.

思路:

做两个排序,一个是m1优先,一个是m2优先,然后从后往前二分查找大于等于的数,再进行比较

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n,m;
struct NOW{
    int m1;
    int m2;
}seg[N];
bool cmp(NOW a,NOW b)
{
    if(a.m1 == b.m1)
    return a.m2 < b.m2;
    else
    return a.m1 < b.m1;
}

bool cmp1(NOW a,NOW b)
{
    if(a.m2 == b.m2)
    return a.m1 < b.m1;
    else
    return a.m2 < b.m2;
}
int a[N];
set<int>se;
unordered_map<int,int>mp;
void solve()
{
    cin >> n;
    se.clear();
    mp.clear();
    for(int i = 1;i <= n;i ++)
    {
        int x,y;
        cin >> x >> y;
        se.insert(y);
        mp[y]++;
        seg[i] = {x,y};
    }
    for(int i = 1;i <= n+1;i ++)
    a[i] = 0;
    sort(seg+1,seg+n+1,cmp);
    for(int i = n;i >= 1;i --)
    {
        a[i] = max(a[i+1],seg[i].m2);
    }
    int ans = 1e18;
    for(int i = n;i > 1;i --)
    {
        if(mp[seg[i].m2] == 1)
        se.erase(seg[i].m2);
        int pos = *se.lower_bound(seg[i].m1);
        if(pos == *se.end())
        pos = *(--se.end());
        if(i<n)
        {
            if(pos < a[i+1])
            pos = abs(a[i+1]-seg[i].m1);
            else
            pos = min(abs(pos -seg[i].m1) ,abs(a[i+1]-seg[i].m1));
        }
        else
            pos = abs(pos -seg[i].m1);
        ans = min(ans,pos);
        mp[seg[i].m2] --;
    }
    ans = min(ans,abs(a[2] - seg[1].m1));
    sort(seg+1,seg+n+1,cmp1);
    for(int i = 1;i <= n+1;i ++)
    a[i] = 0;
    se.clear();
    mp.clear();
    for(int i = n;i >= 1;i --)
    {
        a[i] = max(a[i+1],seg[i].m1);
        se.insert(seg[i].m1);
        mp[seg[i].m1]++;
    }
    for(int i = n;i > 1;i --)
    {
        if(mp[seg[i].m1] == 1)
        se.erase(seg[i].m1);
        int pos = *se.lower_bound(seg[i].m2);
        if(pos == *se.end())
        pos = *(--se.end());
        if(i<n)
        {
        if(pos < a[i+1])
        pos = abs(a[i+1]-seg[i].m2);
        else
        pos = min(abs(pos -seg[i].m2) ,abs(a[i+1]-seg[i].m2));
        }
        else
        pos = abs(pos - seg[i].m2);
        ans = min(ans,pos);
        mp[seg[i].m1] --;
    }
    ans = min(ans,abs(a[2] - seg[1].m2));
    cout << ans << '\n';
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
    cin >> tt;
    while(tt--)
    {
        solve();
    }
}