#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <list>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define f first
#define s second
#define pb push_back
#define mp make_pair
const int maxn = 300500;
const int inf = 1e9;
const double eps = 1e-8;
const int base = 1073676287;
struct node {
int prior, sz, dp, add;
node *l, *r;
node ( int x ) {
prior = ( rand() << 15 ) | rand();
// sz = 1;
dp = x;
l = r = NULL;
add = 0;
}
};
typedef node * pnode;
// int getSize( pnode T ) {
// return T ? T -> sz : 0;
// }
void push( pnode T ) {
T -> dp += T -> add;
if ( T -> l )
T -> l -> add += T -> add;
if ( T -> r )
T -> r -> add += T -> add;
T -> add = 0;
}
void merge( pnode &T, pnode L, pnode R ) {
if ( !L ) {
T = R;
return;
}
if ( !R ) {
T = L;
return;
}
if ( L -> prior > R -> prior ) {
push( L );
merge( L -> r, L -> r, R );
T = L;
// T -> sz = getSize( T -> l ) + getSize( T -> r ) + 1;
return;
}
push( R );
merge( R -> l, L, R -> l );
T = R;
// T -> sz = getSize( T -> l ) + getSize( T -> r ) + 1;
}
void split( pnode T, int value, pnode &L, pnode &R ) {
if ( !T ) {
L = R = NULL;
return;
}
push( T );
if ( T -> dp >= value ) {
split( T -> l, value, L, T -> l );
R = T;
// R -> sz = 1 + getSize( R -> l ) + getSize( R -> r );
return;
}
split( T -> r, value, T -> r, R );
L = T;
// L -> sz = 1 + getSize( L -> l ) + getSize( T -> r );
}
int findBegin( pnode T ) {
push( T );
if ( !T -> l )
return T -> dp;
return findBegin( T -> l );
}
int findMax( pnode T, int n ) {
if ( !T )
return 0;
push( T );
return findMax( T -> l, n ) + findMax( T -> r, n ) + ( T -> dp <= inf ? 1 : 0 );
}
void printTree( pnode T ) {
if ( !T )
return;
push( T );
printTree( T -> l );
printf ( "%d ", T -> dp );
printTree( T -> r );
}
pair < int, int > a[maxn];
pnode T = new node( 0 );
pnode L = NULL;
pnode M = NULL;
pnode R = NULL;
pnode rubbish = NULL;
void solve() {
int n;
scanf ( "%d", &n );
for ( int j = 1; j <= n; j++ )
scanf ( "%d%d", &a[j].f, &a[j].s );
for ( int j = 1; j <= n; j++ )
merge( T, T, new node( inf + j ) );
// printTree(T);
for ( int j = 1; j <= n; j++ ) {
split( T, a[j].f, L, R );
split( R, a[j].s, M, R );
// printTree(M);
// exit(0);
if ( M )
M -> add += 1;
// printTree();
int cnt = findBegin( R );
split( R, cnt + 1, rubbish, R );
merge( T, L, new node( a[j].f ) );
merge( T, T, M );
merge( T, T, R );
}
printf ( "%d\n", findMax( T, n ) - 1 );
}
int main()
{
srand( time( NULL ) );
// ios_base::sync_with_stdio( false );
solve();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHV0aWxpdHk+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxsaXN0PgojaW5jbHVkZSA8YXNzZXJ0Lmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBsb25nIGRvdWJsZSBsZDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwogCiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHMgc2Vjb25kCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiAKY29uc3QgaW50IG1heG4gPSAzMDA1MDA7CmNvbnN0IGludCBpbmYgPSAxZTk7CmNvbnN0IGRvdWJsZSBlcHMgPSAxZS04Owpjb25zdCBpbnQgYmFzZSA9IDEwNzM2NzYyODc7CiAKc3RydWN0IG5vZGUgewogICAgaW50IHByaW9yLCBzeiwgZHAsIGFkZDsKICAgIG5vZGUgKmwsICpyOwogICAgbm9kZSAoIGludCB4ICkgewogICAgICAgIHByaW9yID0gKCByYW5kKCkgPDwgMTUgKSB8IHJhbmQoKTsKICAgICAgICAvLyBzeiA9IDE7CiAgICAgICAgZHAgPSB4OwogICAgICAgIGwgPSByID0gTlVMTDsKICAgICAgICBhZGQgPSAwOwogICAgfQp9OwogCnR5cGVkZWYgbm9kZSAqIHBub2RlOwogCi8vIGludCBnZXRTaXplKCBwbm9kZSBUICkgewovLyAgcmV0dXJuIFQgPyBUIC0+IHN6IDogMDsKLy8gfQogCnZvaWQgcHVzaCggcG5vZGUgVCApIHsKICAgIFQgLT4gZHAgKz0gVCAtPiBhZGQ7CiAgICBpZiAoIFQgLT4gbCApCiAgICAgICAgVCAtPiBsIC0+IGFkZCArPSBUIC0+IGFkZDsKICAgIGlmICggVCAtPiByICkKICAgICAgICBUIC0+IHIgLT4gYWRkICs9IFQgLT4gYWRkOwogICAgVCAtPiBhZGQgPSAwOwp9CiAKdm9pZCBtZXJnZSggcG5vZGUgJlQsIHBub2RlIEwsIHBub2RlIFIgKSB7CiAgICBpZiAoICFMICkgewogICAgICAgIFQgPSBSOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGlmICggIVIgKSB7CiAgICAgICAgVCA9IEw7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaWYgKCBMIC0+IHByaW9yID4gUiAtPiBwcmlvciApIHsKICAgICAgICBwdXNoKCBMICk7CiAgICAgICAgbWVyZ2UoIEwgLT4gciwgTCAtPiByLCBSICk7CiAgICAgICAgVCA9IEw7CiAgICAgICAgLy8gVCAtPiBzeiA9IGdldFNpemUoIFQgLT4gbCApICsgZ2V0U2l6ZSggVCAtPiByICkgKyAxOwogICAgICAgIHJldHVybjsKICAgIH0KICAgI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wbm9kZSBUICkgewogICAgaWYgKCAhVCApCiAgICAgICAgcmV0dXJuOwogICAgcHVzaCggVCApOwogICAgcHJpbnRUcmVlKCBUIC0+IGwgKTsKICAgIHByaW50ZiAoICIlZCAiLCBUIC0+IGRwICk7CiAgICBwcmludFRyZWUoIFQgLT4gciApOwp9CiAKcGFpciA8IGludCwgaW50ID4gYVttYXhuXTsKcG5vZGUgVCA9IG5ldyBub2RlKCAwICk7CnBub2RlIEwgPSBOVUxMOwpwbm9kZSBNID0gTlVMTDsKcG5vZGUgUiA9IE5VTEw7CnBub2RlIHJ1YmJpc2ggPSBOVUxMOwogCnZvaWQgc29sdmUoKSB7CiAgICBpbnQgbjsKICAgIHNjYW5mICggIiVkIiwgJm4gKTsKICAgIGZvciAoIGludCBqID0gMTsgaiA8PSBuOyBqKysgKQogICAgICAgIHNjYW5mICggIiVkJWQiLCAmYVtqXS5mLCAmYVtqXS5zICk7CiAgICBmb3IgKCBpbnQgaiA9IDE7IGogPD0gbjsgaisrICkKICAgICAgICBtZXJnZSggVCwgVCwgbmV3IG5vZGUoIGluZiArIGogKSApOwogICAgLy8gcHJpbnRUcmVlKFQpOwogICAgZm9yICggaW50IGogPSAxOyBqIDw9IG47IGorKyApIHsKICAgICAgICBzcGxpdCggVCwgYVtqXS5mLCBMLCBSICk7CiAgICAgICAgc3BsaXQoIFIsIGFbal0ucywgTSwgUiApOwogICAgICAgIC8vIHByaW50VHJlZShNKTsKICAgICAgICAvLyBleGl0KDApOwogICAgICAgIGlmICggTSApCiAgICAgICAgICAgIE0gLT4gYWRkICs9IDE7CiAgICAgICAgLy8gcHJpbnRUcmVlKCk7CiAgICAgICAgaW50IGNudCA9IGZpbmRCZWdpbiggUiApOwogICAgICAgIHNwbGl0KCBSLCBjbnQgKyAxLCBydWJiaXNoLCBSICk7CiAgICAgICAgbWVyZ2UoIFQsIEwsIG5ldyBub2RlKCBhW2pdLmYgKSApOwogICAgICAgIG1lcmdlKCBULCBULCBNICk7CiAgICAgICAgbWVyZ2UoIFQsIFQsIFIgKTsKICAgIH0KICAgIHByaW50ZiAoICIlZFxuIiwgZmluZE1heCggVCwgbiApIC0gMSApOwp9CiAKIAppbnQgbWFpbigpCiAKewogICAgc3JhbmQoIHRpbWUoIE5VTEwgKSApOwogICAgLy8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyggZmFsc2UgKTsKICAgIHNvbHZlKCk7CiAgICByZXR1cm4gMDsKfQogIA==