Description

The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The measurement is done automatically, and its result immediately printed. Unfortunately, the ink in the printer has long dried out... The employees of BIM however realised the fact only recently, when the Byteotian Organisation for Meteorology (BOM) requested access to that data.

An eager intern by the name of Byteasar saved the day, as he systematically noted down the temperatures reported by two domestic alcohol thermometers placed on the north and south outside wall of the BIM building. It was established decades ago by various BIM employees that the temperature reported by the thermometer on the south wall of the building is never lower than the actual temperature, while that reported by the thermometer on the north wall of the building is never higher than the actual temperature. Thus even though the exact temperatures for each day remain somewhat of a mystery, the range they were in is known at least.

Fortunately for everyone involved (except Byteasar and you, perhaps), BOM does not require exact temperatures. They only want to know the longest period in which the temperature was not dropping (i.e. on each successive day it was no smaller than on the day before). In fact, the veteran head of BIM knows very well that BOM would like this period as long as possible. To whitewash the negligence he insists that Byteasar determines, based on his valuable notes, the longest period in which the temperature could have been not dropping. Now this is a task that Byteasar did not quite expect on his BIM internship, and he honestly has no idea how to tackle it. He asks you for help in writing a program that determines the longest such period.

某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内。

求最长的连续的一段,满足该段内可能温度不降。

Input

In the first line of the standard input there is one integer n(1<=N<=1000000) that denotes the number of days for which Byteasar took notes on the temperature. The measurements from day are given in the line no.i+1 Each of those lines holds two integers, x and y (-109<=x<=y<=109). These denote, respectively, the minimum and maximum possible temperature on that particular day, as reported by the two thermometers.

In some of the tests, worth 50 points in total, the temperatures never drop below -50 degrees (Celsius, in case you wonder!) and never exceeds 50 degrees (-50<=x<=y<=50)

第一行n

下面n行,每行l_i,r_i

1<=n<=1000000

Output

In the first and only line of the standard output your program should print a single integer, namely the maximum number of days for which the temperature in Byteotia could have been not dropping.

一行,表示该段的长度

Sample Input

6

6 10

1 5

4 8

2 5

6 8

3 5

Sample Output

4


首先,一段子串[x,y]满足题意,当且仅当这段区间Li的最大值小于等于Ry并且[x,y-1]合法

所以我们可以用给一个单调队列进行维护,对l维护一个单调递减的队列,保证队头的l小于等于队尾的r

然后统计最大差值即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e6;
struct S1{
int l,r;
void insert(int _l,int _r){l=_l,r=_r;}
}A[N+10];
int h[N+10];
int main(){
int n=read();
for (int i=1;i<=n;i++){
int l=read(),r=read();
A[i].insert(l,r);
}
A[0].insert(-inf,0);
A[n+1].insert(0,inf);
int head=1,tail=1,Ans=0;
h[1]=1;
for (int i=1,j=1;i<=n;i++){
if (j==i) j++;
if (h[head]==i-1) head++;
while (j<=n&&A[h[head]].l<=A[j].r){
while (head<=tail&&A[h[tail]].l<=A[j].l) h[tail--]=0;
h[++tail]=j++;
}
Ans=max(Ans,j-i);
}
printf("%d\n",Ans);
return 0;
}

最新文章

  1. JAVA运行时问题诊断-工具应用篇
  2. slf4j的简单介绍
  3. Maven_dependencies 和 dependencyManagement 的区别
  4. How can I view currently running MySQL queries?( 查看正在运行的MySQL语句/脚本命令)
  5. java异常处理——题
  6. struts2 + spring + mybatis 框架整合
  7. HDU 5030 Rabbit&#39;s String
  8. 编码为multipart/form-data自定义类型(包括文件)如何自动绑定到webapi的action的参数里
  9. 第1章 Python介绍
  10. 14.2.3 InnoDB Redo Log
  11. [SQL基础教程] 3-4 对查询结果进行排序/ORDER BY
  12. matlab对文件目录进行自然排序
  13. es6第一章 continue
  14. org.apache.poi.ss.usermodel 类操作excel数据遗漏
  15. elasticsearch 学习
  16. 利用野草weedcmsuseragent盲注漏洞拿shell
  17. [转]osgconv工具简介
  18. oracle查看表中否存在某字段,数据库是否存在某张表
  19. SpringBoot2 配置
  20. lwip Packet buffers (PBUF) API 操作 集合

热门文章

  1. “约定优于配置”与Magento改造尝试四之block、helper和model载入
  2. leetCode 67.Add Binary (二进制加法) 解题思路和方法
  3. lightoj 1138 - Trailing Zeroes (III)【二分】
  4. 抓包工具Fiddler使用宝典之捕获手机报文
  5. web 界面设计---js提交表单
  6. PyTorch 高级实战教程:基于 BI-LSTM CRF 实现命名实体识别和中文分词
  7. Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
  8. Poisson distribution 泊松分布 指数分布
  9. jetty与tomcat
  10. Lightoj 1012 - Guilty Prince