https://www.luogu.org/record/lists?pid=P1204
维护区间
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
int n,max1=0,max2=0,maxto=0;
struct H{
int head,tail;
int mto;
}kk[5009];
int my_comp(const H&a,const H&b)
{
if(a.head<b.head) return 1;
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&kk[i].head,&kk[i].tail);
}
sort(kk+1,kk+n+1,my_comp);
kk[0].head=kk[0].tail=kk[1].head;
int n1=kk[1].head,n2=kk[1].tail;/n1为头,n2为尾
max1=n2-n1;
for(int i=1;i<=n;i++)
{
if(kk[i].head>n2)//区间间断
{
max2=max(max2,kk[i].head-n2);
max1=max(max1,n2-n1);
n1=kk[i].head,n2=kk[i].tail;//跳到下一个区间
}
if(kk[i].tail>n2)
n2=kk[i].tail;//更新尾
}
printf("%d %d",max1,max2);
}