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. java transient关键字作用,使用场景

    transient的作用及使用方法,官方解释为: Variables may be marked transient to indicate that they are not part of the ...

  2. dispaly:none 和visibility :hidden的区别

    display:none 通常被 JavaScript 用来在不删除元素的情况下隐藏或显示元素. 它和 visibility 属性不一样.把 display 设置成 none 元素不会占据它本来应该显 ...

  3. nodejs 快要变成爬虫界的王者

    nodejs 快要变成爬虫界的王者 爬虫这东西是很多数据采集必须要的东西. 但是现在随着网页不断发展,已经出现了出单纯的网页,到 ajax 网页, 再到 spa , 再到 websocket 应用,一 ...

  4. IBatis.net特性展示代码

    最近公司计划设计新业务平台架构.数据访问层框架要使用ibatis.net.头让我做些例子给其他同事演示下 ibatis的基本特性.然后评估下看是否使用.本来以后上官方下载NPetshop演示下就行了那 ...

  5. css制作三分圆形

    效果图展示: 原理很简单,主要运用transform这个样式,通过斜切和旋转达成 html: css: 怎样,是不是很简单

  6. Android 如何通过Retrofit提交Json格式数据

    本文将介绍如何通过retrofit库post一串json格式的数据.首先post的json数据格式如下: { "Id": "string", "Dev ...

  7. 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)

    查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...

  8. SP2-0734: 未知的命令开头 "imp scott/..." - 忽略了剩余的行。

    Oracle数据导入报错:SP2-0734: 未知的命令开头 "imp scott/..." - 忽略了剩余的行. 原因:进入sqlplus里是不能执行imp的(sqlplus不认 ...

  9. iOS:让UIView覆盖导航栏

    当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...

  10. 分布式定时任务的redis锁实现

    一个web项目如果部署为分布式时,平时常见的定时服务在一定的间隔时间内,可能出现多次重复调用的问题.而此时由于是不同容器之间的竞争,因此需要容器级别的锁 Redis为单进程单线程模式,采用队列模式将并 ...