题意: 给你一个n个区间[l,r],问当你删掉某1个区间后,剩下的n-1个区间所能够相交的长度的最大值。
求线段的最大交集只需要用最靠左边的右端点减去最靠右边的左端点就行了(如果是负数的话,就是0)。
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<cstdlib>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define repp(i,n,a) for(int i=n;i>=a;i--)
#define ll long long
#define ull unsigned long long
#define mem(a,x) memset((a),(x),sizeof ((a)))//x只能是0或-1或false或true
#define debug(x) cout<<"X: "<<(x)<<endl
#define de cout<<"************"<<endl
#define lowbit(x) ((x)&(-x))
#define lson rt<<1
#define rson rt<<1|1
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/(__gcd(a,b))
#define inf 0x3f3f3f3f//1e9+6e7
#define eps 1e-8
#define mod 1e9+7
#define N 100010
const double pi=acos(-1.0);
using namespace std;
int a[300010],b[300010];
bool aa[300010],bb[300010];
int main()
{
int n;
cin>>n;
int lmax=-10,lmaxx=-10,rmin=inf,rminn=inf;
rep(i,0,n-1)
{
scanf("%d%d",&a[i],&b[i]);
lmax=max(lmax,a[i]);
rmin=min(rmin,b[i]);
}
mem(aa,false);
mem(bb,false);
rep(i,0,n-1)
{
if(a[i]==lmax)
aa[i]=true;
if(b[i]==rmin)
bb[i]=true;
}
int f1=0,f2=0;
rep(i,0,n-1)
{
if(aa[i]&&f1==0)
f1++;
else
lmaxx=max(lmaxx,a[i]);
if(bb[i]&&f2==0)
f2++;
else
rminn=min(rminn,b[i]);
}
int ans=0;
rep(i,0,n-1)
{
if(aa[i]==true&&bb[i]==true)
{ans=max(ans,rminn-lmaxx);continue;}
if(aa[i])
{ans=max(ans,rmin-lmaxx);continue;}
if(bb[i])
{ans=max(ans,rminn-lmax);continue;}
ans=max(ans,rmin-lmax);
}
cout<<ans<<endl;
return 0;
}