#include #include #include using namespace std; typedef long long lint; typedef vectorvi; typedef pairpii; #define rep(i,n)for(int i=0;i<(int)(n);++i) int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vi l(n),r(n); rep(i,n)cin>>l[i]>>r[i]; int ml=0; vectorm(n+1,1e7); lint cc=0; bool ab=0; rep(i,n){ if(cc>=1.3e9){ab=1;break;} int last=-1; for(int j=ml+1;j>=1;--j){ int pre=j==1?0:m[j-1]; if(m[j]<=l[i]){last=j;break;} if(pre+1<=r[i])m[j]=min(m[j],max(l[i],pre+1)); } cc+=ml+1-last; if(m[ml+1]<1e7)ml++; } if(ab){ ml=0; rep(i,n+1)m[i]=1e7; rep(i,n){ int p=0,f=ml+1; while(f-p>1){ int x=(f+p)/2; int cur=m[x]; if(max(l[i],cur+1)<=r[i]) p=x; else f=x; } ml=max(ml,p+1); //UPDATE m[p+1]=min(m[p+1],max(l[i],p==0?0:m[p]+1)); } } cout<