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 C (GCC 5.4.1) (judge verdict: Accepted)


#include <stdio.h>
#include <stdlib.h>
#include <math.h>


struct kenka_t {
	int a;
	int b;
} ;

int compare_kenka(const void *c, const void *d) {
  struct kenka_t *A = (struct kenka_t *)c;
  struct kenka_t *B = (struct kenka_t *)d;
  return A->a - B->a;
}


int main(void)
{
	//変数の宣言
	int n,m;
	int i,j,k;
	int flag;
	int ans,temp;
	static int anew[100010];
	static int bnew[100010];
	struct kenka_t kenka[100010];
	
	//データの読み込み
	scanf("%d %d",&n,&m);
	
	for(i=0;i<m;i++){
		scanf("%d %d",&kenka[i].a,&kenka[i].b);
	}
	
//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理
	
	qsort(kenka, m, sizeof(struct kenka_t), compare_kenka);
	
	k=0;
	for(i=0;i<m-1;i++){
//		printf("%d番目の要素についてチェック\n",i);
		j=i+1;
		flag=1;
		while(kenka[j].a<kenka[i].b){
			if(kenka[j].b<kenka[i].b){
				flag=0;
				break;
			}
			j++;
		}
//		printf("while文終了\n");
		if(flag==1){
			anew[k]=kenka[i].a;
			bnew[k]=kenka[i].b;
			k++;
//			printf("%d %dは極小\n",kenka[i].a,kenka[i].b);
		}else{
//			printf("%d %dは極小でない\n",kenka[i].a,kenka[i].b);
		}
	}
	anew[k]=kenka[m-1].a;
	bnew[k]=kenka[m-1].b;
	k++;
	
//	printf("\n");
//	for(i=0;i<k;i++){
//		printf("%d %d\n",anew[i],bnew[i]);
//	}
	
	ans=0;
	for(i=0;i<k;i++){
		if(i==0){
			ans++;
			temp=bnew[i];
//			printf("%dの左側を切断\n",temp);
		}else if(anew[i]>=temp){
			ans++;
			temp=bnew[i];
//			printf("%dの左側を切断\n",temp);
		}
	}
	
	
	
	
//	printf("計算部分終了\n");
	
	//出力
	printf("%d",ans);
	
//	printf("結果の出力終了\n");
	
	return 0;
}