Line Segments
Difficulty: Hard

You are given $N$ line segments. All of them lie on the same line. We will describe the $i^{th}$ line segment with two integers, $A_i$ and $B_i$. This means it starts at $A_i$ and ends at $B_i$. It will be denoted as $(A_i;B_i)$.  

Let's say a pair of line segments is ***good***, if one of them is not covering the other one. For example, the pair of $(1; 4)$ and $(2; 5)$ is ***good***. But the pair of $(1; 6)$ and $(2; 5)$ is ***bad*** because $(1; 6)$ covers $(2; 5)$.

Let's assume that we have the $i^{th}$ and $j^{th}$ line segments. If $A_i \le A_j$ and $B_i \ge B_j$, we can say "the $i^{th}$ line segment covers $j^{th}$ line segment."  

You must find the size of the biggest subset of these line segments that contains __no__ ***bad*** pairs.  

**Input Format**  
In the first line you are given an integer $N$, the total number of line segments.  
The next $N$ lines contain two integers, $A_i ~and ~B_i$.   

**Constraints**  
$1 \le N \le 10^5$  
$1 \le $ $A_i \le B_i \le 10^9$  

**Output Format**  
You must print one integer denoting the size of the biggest subset.  

**Sample Input#0**  

	4
    2 5
    1 4
    1 6
    3 8

**Sample Output#0**  

    3
    
**Explanation#0**  
We can only choose the first, second, and fourth line segments. We can not choose the third line segment because it covers the first and second line segments.  

**Sample Input#1**  

    5
    5 8
    3 7
    4 10
    12 15
    1 7

**Sample Output#1**  

    3


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	#include <bits/stdc++.h>
	using namespace std;

	typedef pair<int,int> ii;	
	const int MAXN = 100010;
	int seg[5*MAXN];
	ii LineS[MAXN];
	ii subseq[MAXN];
	int N;
		
	int comparator( ii a , ii b ){
		if ( a.first != b.first )	return a.first < b.first;
		return a.second > b.second;
	}
	 
	int update( int k , int b , int e , int x , int y ){
		int m = (b+e)/2;
		
		if ( b>x || e<x )	return seg[k];
		if ( b==e )	return seg[k] = y;
		
		return seg[k] = max( update( 2*k , b , m , x , y )  ,  update( 2*k+1 , m+1 , e , x , y ) );
	}
	 
	int find( int k , int b , int e , int x , int y ){
		int m = (b+e)/2;
		
		if ( b>y || e<x )	return 0;
		if ( b>=x && e<=y )	return seg[k];
		
		return max( find( 2*k , b , m , x , y ) , find( 2*k+1 , m+1 , e , x , y ) );
	}	

	int main(){
		
		scanf(" %d",&N);
		
		for ( int i=1 ; i<=N ; i++ )
			scanf(" %d %d",&LineS[i].first , &LineS[i].second);
		
		sort( LineS + 1 , LineS + N + 1 );
			
		for ( int i=1 ; i<=N ; i++ ){
				
			subseq[i].first = LineS[i].second;
			subseq[i].second = i;
			
		}
		
		sort( subseq + 1 , subseq + N + 1 , comparator );	
        // CARE : INCREASING ORDER , NOT NON-DECREASING ORDER
	
		for ( int i=1 ; i<=N ; i++ )
			update( 1 , 1 , N , subseq[i].second , find( 1 , 1 , N , 1 , subseq[i].second )+1 );
	
		printf("%d\n",seg[1]);
		
		return 0;
	
	}