害
```
#include <bits/stdc++.h>
#define N 3000005
using namespace std;
struct node{
int x,y;
friend bool operator <(const node &a,const node &b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
}a[N];
int f[N],n,Max,now,L;
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
while(cin>>n){
L=0;
for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,L=max(L,a[i].y);
sort(a+1,a+n+1);
memset(f,0,sizeof(f)); Max=0; now=0;
for(int i=1;i<=n;i++){
while(now<a[i].x) Max=max(f[now++],Max),f[now-1]=Max;
f[a[i].y]=max(f[a[i].y],Max+a[i].y-a[i].x+1);
}
//for(int i=0;i<=L;i++) cout<<f[i]<<" "; cout<<endl;
while(now<=L) Max=max(Max,f[now++]);
cout<<Max<<endl;
}
return 0;
}
```
AC
by RefreshinglyNaive @ 2020-11-22 21:03:56