Problem:
Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

9 5
1 8
2 7
3 5
4 6
7 9

Sample Output 2

2

Sample Input 3

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Sample Output 3

4

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Scanner;

class Main{
	static int[][] yobo;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();		//sima
		int M=sc.nextInt();		//yobo

		yobo=new int[M][2];
		int[] range=new int[2];
		int hashi=1;

		for(int i=0; i<M; i++) {
			yobo[i][0]=sc.nextInt();
			yobo[i][1]=sc.nextInt();
		}

		//sort(yobo);
		quick_sort(yobo, 0, M-1);
		for(int i=0; i<M; i++) {
			if(i==0) {
				range[0]=yobo[0][0];
				range[1]=yobo[0][1];
			}
			else {
				if(Math.min(range[1],yobo[i][1])-Math.max(yobo[i][0], range[0])>=1) {
					range[0]=Math.max(yobo[i][0], range[0]);
					range[1]=Math.min(yobo[i][1], range[1]);
				}
				else {
					hashi++;
					range[0]=yobo[i][0];
					range[1]=yobo[i][1];
				}
			}
		}


		/*for(int i=0; i<M; i++) {
			System.out.println(yobo[i][0]+" "+yobo[i][1]);
		}*/
		System.out.println(hashi);
	}

	/*
	static int pivot(int[][] a,int i,int j){
		int k=i+1;
		while(k<=j && a[i]==a[k]) k++;
		if(k>j) return -1;
		if(a[i][0]>=a[k][0]) return i;
		return k;
	}


	static int partition(int[][] a,int i,int j,int x){
		int tmp1=0;
		int tmp2=0;
		int l=i,r=j;

		// 検索が交差するまで繰り返します
		while(l<=r){

			// 軸要素以上のデータを探します
			while(l<=j && a[l][0]<x)  l++;

			// 軸要素未満のデータを探します
			while(r>=i && a[r][0]>=x) r--;

			if(l>r) break;
			tmp1=a[l][1];
			tmp2=a[r][1];
			a[l][1]=tmp2;
			a[r][1]=tmp1;
			int t=a[l][0];
			a[l][0]=a[r][0];
			a[r][0]=t;
			l++; r--;
		}
		return l;
	}

	public static void quickSort(int[][] a,int i,int j){
		if(i==j) return;
		int p=pivot(a,i,j);
		if(p!=-1){
			int k=partition(a,i,j,a[p][0]);
			quickSort(a,i,k-1);
			quickSort(a,k,j);
		}
	}


	public static void sort(int[][] a){
		quickSort(a,0,a.length-1);
	}
	*/





	    // 配列dのleftからrightまでの間のデータ列をクイックソートする
	    static void quick_sort(int[][] d, int left, int right) {
	        if (left>=right) {
	            return;
	        }
	        int p = d[(left+right)/2][0];
	        int l = left, r = right, tmp,tmp1,tmp2;
	        while(l<=r) {
	            while(d[l][0] < p) { l++; }
	            while(d[r][0] > p) { r--; }
	            if (l<=r) {
	                tmp = d[l][0]; d[l][0] = d[r][0]; d[r][0] = tmp;
	                tmp1=d[l][1]; tmp2=d[r][1]; d[l][1]=tmp2; d[r][1]=tmp1;
	                l++; r--;
	            }
	        }
	        quick_sort(d, left, r);  // ピボットより左側をクイックソート
	        quick_sort(d, l, right); // ピボットより右側をクイックソート
	    }
	    // 配列内のデータ列を表示する
	    static void print_data(int[] d) {
	        for(int i = 0; i < d.length; i++) System.out.print(d[i] + " ");
	        //System.out.println();
	    }

}