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 ;
}

cf 1029 C的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. python入门之三元运算,存址方式,深浅拷贝

    三元运算 格式: name = 值1 if 条件 else 值2 如果条件为True,那么将值1赋值给name,条件为False,那么将值2赋值给name 存址方式 不同的数据类型在内存中的存址方式不 ...

  2. Java中,数值比较大小,以及数值判断相等

    Java中,数值比较大小,以及数值判断相等

  3. linux学习笔记汇总

    linux 文件系统是采用级层树状的目录结构,采用"/"根目录的方式 目录结构: / 根目录           |---root: 存放root用户相关的文件          ...

  4. js中函数的理解

     在JavaScript中,函数是一个极容易引起误解或引发歧义的数据类型,它可以是独立的函数类型,又可以作为对象的方法,也可以被称为类或构造器,还可以作为函数对象而存在等. 

  5. 自定义Mega菜单的巧妙实现

    查看了<云制造>官网源码,为其mega菜单的巧妙实现打call. 其另辟蹊径,采取父级主控分支的方法,仅对父级“增加/删除”控制标识,从而控制子层显示. <!DOCTYPE html ...

  6. Android Theme.Dialog 到光 AppCompatDialog

    我用在我的 style.xml 作为主要应用程序主题 <style name="AppTheme" parent="Theme.AppCompat.Light&qu ...

  7. sql server Cannot resolve the collation conflict between "Chinese_PRC_BIN" and "Chinese_PRC_CI_AS" in the equal to operation

    今天查询二个db,出现这个错误,二种方法,一种是把db里的collation改成一样的:如果不方便可以直接在sql语句后面转一下: select * from table where crm_mscr ...

  8. Windows64+Python27下配置matplotlib

    注:转载请注明原作者并附上原文链接! 网上看了很多方法,均遇到这样或者那样的问题导致安装失败,最后自己摸索一条方法,最终安装成功了. 1,首先安装numpy,这个可以选择install安装包,很简单, ...

  9. PCL点云处理可视化——法向显示错误“no override found for vtk actor”解决方法

    转:https://blog.csdn.net/bflong/article/details/79137692 参照:https://blog.csdn.net/imsaws/article/deta ...

  10. -[UPAInitViewController startAPPay] in libUPAPayPlugin.a(UPAInitViewController.o)

    问题 Undefined symbols for architecture arm64: "_PKPaymentNetworkChinaUnionPay", referenced ...