Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem
【题目链接】A题链接
【题目类型】模拟
【题目大意】给你n段区间要求你求解,求解一个最小答案区间满足这个区间与这n个区间最少都有一个共同点
【解题思路】
可以看一下图片
这两个线段实际上只需要【R1,R1】就可以了
这种情况要想要答案区间最小,那么选最小的右边和最大的左边(这也就是最关键的思路)
而你最后的到的ansl值比ansr的值要搭,那就说明是第一幅图那种情况,就是存在那么一个点,刚好都在这些区间上,所以直接输出0就可以了
/**
* This code has been written by YueGuang, feel free to ask me question. Blog: http://www.yx.telstudy.xyz
* created:
*/
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define REP(i, a, b) for(int i = a; i < b; i++)
#define REP_(i, a, b) for(int i = a; i <= b; i++)
#define sl(n) scanf("%lld", &n);
#define si(n) scanf("%d", &n);
#define RepAll(a) for(auto x: a)
#define cout(ans) cout << ans << endl;
typedef long long ll;
using namespace std;
const int maxn = 1e5+50;
struct pl{
int l;
int r;
}a[maxn];
bool cmp(struct pl a, struct pl b){
return a.l < b.l;
}
int main(){
//freopen("in.txt", "r", stdin);
int t; scanf("%d", &t);
while(t--){
int l, r, ansl, ansr;
int n; scanf("%d", &n);
REP(i , 0, n){
cin >> a[i].l >> a[i].r;
}
if(n == 1){cout << "0" << '\n';continue;}
sort(a, a+n, cmp);
ansl = a[0].l, ansr = a[0].r;
for(int i = 1; i < n; i++){
ansl = max(a[i].l, ansl);
ansr = min(a[i].r, ansr);
}
if(ansr > ansl) cout << "0" << endl;
else{ cout << ansl-ansr << '\n';
}}
}