C. Maximal Intersection
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given nn segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or 00 in case the intersection is an empty set.

For example, the intersection of segments [1;5][1;5] and [3;10][3;10] is [3;5][3;5] (length 22), the intersection of segments [1;5][1;5] and [5;7][5;7] is [5;5][5;5] (length 00) and the intersection of segments [1;5][1;5] and [6;6][6;6] is an empty set (length 00).

Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining (n−1)(n−1) segments has the maximal possible length.

Input

The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of segments in the sequence.

Each of the next nn lines contains two integers lili and riri (0≤li≤ri≤1090≤li≤ri≤109) — the description of the ii-th segment.

Output

Print a single integer — the maximal possible length of the intersection of (n−1)(n−1) remaining segments after you remove exactly one segment from the sequence.

Examples
input

Copy
4
1 3
2 6
0 4
3 3
output

Copy
1
input

Copy
5
2 6
1 3
0 4
1 20
0 4
output

Copy
2
input

Copy
3
4 5
1 2
9 20
output

Copy
0
input

Copy
2
3 10
1 5
output

Copy
7
Note

In the first example you should remove the segment [3;3][3;3], the intersection will become [2;3][2;3] (length 11). Removing any other segment will result in the intersection [3;3][3;3] (length 00).

In the second example you should remove the segment [1;3][1;3] or segment [2;6][2;6], the intersection will become [2;4][2;4] (length 22) or [1;3][1;3] (length 22), respectively. Removing any other segment will result in the intersection [2;3][2;3] (length 11).

In the third example the intersection will become an empty set no matter the segment you remove.

In the fourth example you will get the intersection [3;10][3;10] (length 77) if you remove the segment [1;5][1;5] or the intersection [1;5][1;5] (length 44) if you remove the segment [3;10][3;10].

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define N 300009
int n,a[N],b[N],c[N],d[N];
map<int,int>mp;
int maxx=-,minn=;
bool check(int x,int y)
{
if(x!=y||x==y&&mp[y]>)
return true;
return false;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
c[i]=a[i];d[i]=b[i];
mp[a[i]]++;mp[b[i]]++;
maxx=max(maxx,a[i]);
minn=min(minn,b[i]);
}
//cout<<maxx<<" "<<minn<<endl;
sort(a+,a++n);
sort(b+,b++n);
int l=;
for(int i=;i<=n;i++){
//cout<<maxx<<" "<<minn<<" "<<c[i]<<" "<<d[i]<<endl;
if(c[i]==maxx&&check(d[i],minn)){
l=max(l,minn-a[n-]);
}
if(d[i]==minn&&check(c[i],maxx)){
l=max(l,b[]-maxx);
}
if(c[i]==maxx&&d[i]==minn){//去掉某一个区间,那么区间的左右要同时去掉
//cout<<l<<endl;
l=max(l,b[]-a[n-]);
//cout<<b[2]<<" "<<a[n-1]<<endl;
}
}
/*
2
1 5
2 4
去掉2 4
2 4要同时去掉
*/
printf("%d\n",l);
return ;
}

最新文章

  1. AFNetworking报错&quot;_UTTypeCopyPreferredTagWithClass&quot;, referenced from: _AFContentTypeForPathExtens
  2. RPN-逆波兰计算器-中缀表达式转后缀表达式-javascript
  3. 超级强大的formValidator
  4. VB6.0对鼠标滚轮不支持的解决方法[转]已经测试work
  5. Codeforces 85D Sum of Medians
  6. 修改ptrace_scope
  7. spring exception--No unique bean of type
  8. CSS样式权值
  9. Linux - How To Set Up an NFS Mount on CentOS 6
  10. leetcode Roman to Integer python
  11. JSF教程(9)——生命周期之Process Validations Phase
  12. C语言实现进制转换——超图面试题
  13. MySQL分区表的局限和限制
  14. MySQL 索引管理与执行计划
  15. 使用zii.widgets.CDetailView显示内容
  16. EntityFrameWork实现部分字段获取和修改(含源码)
  17. 12 SharedPreferences
  18. 其他综合-fdisk一键分区操作-无需脚本
  19. Lua中__index和__newindex实践
  20. Red-Gate.NET.Reflector.v8.0.1.308(内含注册机Keygen与注册图解)

热门文章

  1. 基于阿里云SLB/ESS/EIP/ECS/VPC的同城高可用方案演练
  2. Java的Cloneable接口还有深浅复制
  3. freertos之任务
  4. json数据有换行符时提交不成功的坑
  5. POJ - 3020  Antenna Placement 二分图最大匹配
  6. G. Of Zorcs and Axes 二分 + 贪心 —— STL的用法
  7. centos7 更换jdk版本
  8. display flex在部分低级android中的支付宝窗口表现
  9. systemback-----做你折腾的后盾
  10. RStudio的Markdown