1. 程式人生 > >[Poi2011]Temperature

[Poi2011]Temperature

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 (-10^9<=x<=y<=10^9). 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;
}