单调队列
维护li的一个递减队列
然后当Li<=Ri时更新答案
注意:空间开两倍大
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
const int N=3e6+100;
using namespace std;
inline void read(int &x){
x=0;
char ch=getchar();
int f=1;
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
x*=f;
}
struct Node{
int sum,id;
};
//queue<Node> Qmax;//最小值最大
//queue<Node> Qmin;//最大值最小
int Q[N];
int l[N]={};
int r[N]={};
int head=1;
int tail=1;
int n;
int main(){
// freopen("p3522.cpp","r",stdin);
read(n);
for(int i=1;i<=n;i++){
read(l[i]);
read(r[i]);
}
int ans=-1;
Q[1]=1;
for(int i=1;i<=n;i++){
while(l[Q[head]]>r[i]&&head<=tail)head++;
if(head<=tail){
ans=max(ans,i-Q[head]+1);
}
int t=i;
while(l[Q[tail]]<l[i]&&head<=tail)t=Q[tail],tail--,
l[t]=l[i];
Q[++tail]=t;
// while(Q.size()&&Q.front().sum<l[i])Q.pop();
/*Q.push((Node){l[i],i});
while(Q.size()&&Q.front().sum>r[i])Q.pop();
if(Q.size()){
ans=max(ans,i-Q.front().id+1);
}
cout<<i<<" "<<Q.front().id<<'\n';*/
}
cout<<ans;
}