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. Qt 进程和线程之一:运行一个进程和进程间通信

    Qt提供了对进程和线程的支持.本节讲述了怎样在Qt应用程序中启动一个进程,以及几种常用的进程间通信方法.如果对进程和线程的概念不是很了解,可以看我的另一篇博客:[多进程和多线程的概念. 设计应用程序时 ...

  2. 117 Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    这是“每个节点的右向指针”问题的进阶.如果给定的树可以是任何二叉树,该怎么办?你以前的解决方案仍然有效吗?注意:    你只能使用恒定的空间.例如,给定以下二叉树,         1       / ...

  3. 字符串与C51的格式化输出

    一字符数组和字符指针: 字符指针可以用字符串对其直接初始化和随时赋值:而字符数组可以用字符串进行初始化,但不能用字符串对其进行随时赋值(但此时可以定义一个字符串指针指向字符数组,然后用字符串对指针随时 ...

  4. 推荐一个VPS

    有日本节点,不贵,用了两个月,感觉不错 http://www.vultr.com/?ref=6847480

  5. Windows忘记mysql的密码

    1.查看mysql的安装路径 show variables like "%char%"; 路径:C:\Program Files\MySQL\MySQL Server 5.7\ 2 ...

  6. 初始Mybatis,好累,自己感觉自己快坚持不了了

    Mybatis1.持久化 持久化,就是内存数据和硬盘数据状态的转换 2.ORM思想Object Relation Mapping 对象关系映射 3.MyBatis入门案例 3.1导入jar包 依赖 & ...

  7. asp也玩三层架构(有源代码)

    实体类 <% Class UserInfo Private mintId Public Property Let UserId(intUserId) mintId = intUserId End ...

  8. TCP简单程序

    服务器段: package com.dcz.socket; import java.io.IOException; import java.io.OutputStream; import java.n ...

  9. IE下的圆角

    元素{ position: relative;/*必须*/ z-index: 10;/*必须*/ border-radius: 8px; -moz-border-radius: 8px; -webki ...

  10. Android ScrollView嵌套RecyclerView导致在三星s8曲面屏显示不全问题

    当RecyclerView适配显示不全时可以单独给其嵌套一个相对布局!!!(必须是相对布局),这样在曲面屏手机就可以全部显示出来如下图所示 <RelativeLayout android:lay ...