代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,fan[4000001],hou[4000001];
deque<int> z;
struct wendy{
int wz,z,s;
}x[4000001];
struct babatuosi{
int l,r;
}y[4000001];
struct kafuka{
int l,r,z,laze,ma;
}tree[10000001];
kafuka ans;
void sb(int c){
if(tree[c].l!=tree[c].r){
if(tree[c*2].z>=tree[c*2+1].z){
tree[c].z=tree[c*2].z,tree[c].ma=tree[c*2].ma;
}
else tree[c].z=tree[c*2+1].z,tree[c].ma=tree[c*2+1].ma;
}
}
void buid(int wz){
int mid=(tree[wz/2].l+tree[wz/2].r)/2;
if(wz%2==1) tree[wz].l=mid+1,tree[wz].r=tree[wz/2].r;
else tree[wz].l=tree[wz/2].l,tree[wz].r=mid;
if(tree[wz].l!=tree[wz].r)buid(wz*2),buid(wz*2+1);
else tree[wz].ma=tree[wz].l;
}
void la(int wz){
if(tree[wz].laze!=0){
if(tree[wz].l!=tree[wz].r){
tree[wz*2].laze+=tree[wz].laze;
tree[wz*2+1].laze+=tree[wz].laze;
tree[wz*2].z+=tree[wz].laze;
tree[wz*2+1].z+=tree[wz].laze;
}
tree[wz].laze=0;
}
}
void add(int l,int r,int c){
la(c),sb(c);
tree[c].z++;
if(tree[c].z==0)tree[c].ma=max(l,tree[c].l);
if(tree[c].l>=l&&tree[c].r<=r){
tree[c].laze++;
return;
}
if(tree[c].l==tree[c].r) return;
int mid=(tree[c].r+tree[c].l)/2;
if(l<=mid) add(l,r,c*2);
if(r>=mid+1) add(l,r,c*2+1);
la(c),sb(c);
//c,out<<l<<' '<<r<<' '<<tree[c].z<<' '<<tree[c].l<<' '<<tree[c].r<<' '<<tree[c].ma<<endl;
}
void ql(int l,int r,int c){
la(c),sb(c);
if(tree[c].l>=l&&tree[c].r<=r){
tree[c].z--;
if(tree[c].z==0) tree[c].ma=tree[c].l;
tree[c].laze--;
return;
}
if(tree[c].l==tree[c].r) return;
int mid=(tree[c].r+tree[c].l)/2;
if(l<=mid) ql(l,r,c*2);
if(r>=mid+1) ql(l,r,c*2+1);
la(c),sb(c);
}
kafuka out(int l,int r,int c){
la(c),sb(c);
//c,out<<tree[c].z<<' '<<tree[c].l<<' '<<tree[c].r<<' '<<tree[c].ma<<endl;
if(tree[c].l>=l&&tree[c].r<=r){
la(c),sb(c);
if(tree[c].ma==0) tree[c].ma=tree[c].l;
//c,out<<tree[c].z<<' '<<tree[c].l<<' '<<tree[c].r<<' '<<tree[c].ma<<endl;
return tree[c];
}
kafuka cnt,o,p;
cnt.l=0;cnt.laze=0;cnt.r=0;cnt.z=0;cnt.ma=0;
if(tree[c].l==tree[c].r) return tree[c];
o.l=0;o.laze=0;o.r=0;o.z=0;o.ma=0;
p.l=0;p.laze=0;p.r=0;p.z=0;p.ma=0;
int mid=(tree[c].r+tree[c].l)/2;
if(l<=mid) o=out(l,r,c*2);
if(r>=mid+1) p=out(l,r,c*2+1);
la(c),sb(c);
if(o.z>cnt.z)cnt=o;
else if(o.z==cnt.z&&cnt.ma>o.ma&&o.ma!=0) cnt=o;
if(p.z>cnt.z)cnt=p;
else if(p.z==cnt.z&&cnt.ma>p.ma&&p.ma!=0) cnt=p;
return cnt;
}
bool cmp(wendy a,wendy b){
return a.z<b.z;
}
bool cmpp(babatuosi a,babatuosi b){
return a.l<b.l;
}
signed main(){
cin>>n;
tree[1].l=0,tree[1].r=3e5+5;
buid(2),buid(3);
for(int i=1;i<=n;i++){
cin>>y[i].l>>y[i].r;
if(y[i].l==y[i].r-1) z.push_back(i);
}
while(z.size()!=0){
int u=z.front();
swap(y[n],y[u]);
n--;
z.pop_front();
}
if(n==0){
cout<<0<<' '<<1;
return 0;
}
for(int i=1;i<=n;i++){
x[i].z=y[i].l,x[i].wz=i,x[i].s=1;
x[i+n].z=y[i].r,x[i+n].wz=i,x[i+n].s=2;
}
sort(x+1,x+n+n+1,cmp);
int k=0;
for(int i=1;i<=n*2;i++){
if(x[i].z!=x[i-1].z||i==1){
if(x[i].z==x[i-1].z+1)k++;
else k+=2;
fan[k]=x[i].z;//c,out<<fan[k]<<' '<<k<<endl;
}
if(x[i].s==1) y[x[i].wz].l=k;
else y[x[i].wz].r=k;
}
sort(y+1,y+n+1,cmpp);
for(int i=1;i<=n;i++){
hou[i]=y[i].r;
if(fan[y[i].l]!=0)add(y[i].l+1,y[i].r-1,1);
}
sort(hou+1,hou+n+1);
kafuka o=out(0,3e5,1);
int su=1;
//c,out<<fan[y[1].l]<<endl;
while(fan[y[su].l]==0&&su<=n){
//c,out<<su<<' '<<"114514"<<endl;
add(y[su].l+1,y[su].r-1,1);
su++;
}
su=1;
ans.z=o.z,ans.l=1,ans.r=o.ma;
if(o.z==0) ans.r=1;
fan[0]=-1;
int shu=1,hu=1;
for(int i=1;i<=n;i++){
while(su<i&&fan[hou[su]]<=fan[y[i].l]+1&&su<=n){
ql(hou[su]+1,3e5,1);
su++;
}
o=out(y[i].l+1,3e5,1);
//c,out<<o.z<<' '<<o.l<<' '<<o.ma<<" 1145141919810"<<endl;
while(fan[y[shu].l]<=fan[y[i].l]+1&&shu<=n)
ql(y[shu].l+1,y[shu].r-1,1),shu++;
while(fan[y[hu].l]<fan[y[i].l]+1&&hu<=n)
add(y[hu].r+1,3e5,1),hu++;
o=out(y[i].l+1,3e5,1);
//c,out<<o.z<<' '<<y[i].l<<' '<<o.ma<<endl;
if(o.z>ans.z) ans.z=o.z,ans.l=y[i].l+1,ans.r=o.ma;
}
if(ans.z==0||fan[ans.r-1]+1>1000000000||ans.r-1<0) cout<<0<<' '<<1;
else cout<<fan[ans.l-1]+1<<' '<<fan[ans.r-1]+1;
return 0;
}