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 (Python):
N, M = map(int, input().split())
m = []
for i in range(M):
    a, b = map(int, input().split())
    m.append((a,b-1))
ans = 0
t = None
for l,r in sorted(m, key=lambda x:x[1]):
    if t is None or l > t:
        t = r
        ans += 1
print(ans)

Solution (Python):
n, m = list(map(int, input().split()))
l = [200000 for i in range(0, n-1)]

for i in range(0, m):
    a, b = list(map(int, input().split()))
    l[a-1] = min([l[a-1], b-2])

right = -1
count = 0

for i in range(0, n-1):
    if i > right and not l[i] == 200000:
        count += 1
        right = l[i]
    elif i <= right and not l[i] == 200000:
        right = min([l[i], right])

print(count)

Solution (Python):
N,M = map(int,input().split())
problems =[]

for i in range(0,M):
    a = list(map(int,input().split()))
    problems.append(a)

problems.sort(key=lambda x:x[1])

x = 0
ans = 0

for j in range(0,len(problems)):
    if problems[j][0] <= x:
        continue
    else:
        x = problems[j][1] - 1
        ans += 1

print(ans)

Solution (Python):
import sys

def na(): return map(int,sys.stdin.readline().split())
def ns(): return sys.stdin.readline().strip()

n,m=na()

a=[]
for i in range(m):
    a.append(list(na()))

a.sort(key=lambda x:x[0])

inf = 10000000000
end = inf
ptr = 0
count = 0
for i in range(1,n+1):
    if end == i:
        count += 1
        end = inf
    while ptr < m and a[ptr][0] == i:
        end = min(end, a[ptr][1])
        ptr += 1

print(count)

Solution (Python):

import sys
n, m = map(int, input().split())
demands = [
    tuple(map(int,
              line.strip().split())) for line in sys.stdin.readlines()
]
demands.sort(key=lambda demand: demand[1])

x = -1
cnt = 0
for ai, bi in demands:

    if ai <= x:
        continue

    else:
        x = bi - 1
        cnt += 1

print(cnt)

Solution (Python):

import sys
n, m = map(int, input().split())
demands = [
    tuple(map(int,
              line.strip().split())) for line in sys.stdin.readlines()
]
demands.sort(key=lambda demand: demand[1])

bridge = -1
cnt = 0
for ai, bi in demands:

    if ai <= bridge:
        continue

    else:
        bridge = bi - 1
        cnt += 1

print(cnt)

Solution (Python):

import sys
n, m = map(int, input().split())
ary = [tuple(map(int, line.strip().split())) for line in sys.stdin.readlines()]

demands = sorted(ary, key=lambda x: x[1])[::-1]
compressed = []
while demands:
    a, b = demands.pop()

    for i, (ai, bi) in enumerate(compressed):
        if bi <= a:
            continue

        else:
            if ai < a:
                compressed[i] = (a, min(b, bi))
            break

    else:
        compressed.append((a, b))

print(len(compressed))

Solution (Python):
a,b = map(int,input().split())

c = []
for i in range(b):
  c.append(list(map(int,input().split())))

c.sort(key = lambda x:x[1])

s = c[0][0]
l = c[0][1]
re = 1

for i in range(1,b):
  if l <= c[i][0]:
    re += 1
    l = c[i][1]
    
print(re)

Solution (Python):


N,M = map(int,input().split())
List = sorted([list(map(int,input().split())) for i in range(M)], key=lambda x:x[1])

cnt = 1
maxa = 0
b = List[0][1]
for i in range(1,M):
    newB = List[i][1]
    maxa = max(maxa,List[i][0])
    if newB > b:
        if maxa >= b:
            cnt += 1
            b = newB
        maxa = 0
        
print(cnt)

Solution (Python):



if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    ab = list()
    count = 1

    
    
    for i in range(m):
        ab.append(tuple(map(int, input().split())))

    sorted_ab = sorted(ab, key=lambda x: x[1])
    b = sorted_ab[0][1]

    for i in range(1, m):
        if sorted_ab[i][0] < b <= sorted_ab[i][1]:
            pass
        else:
            b = sorted_ab[i][1]
            count += 1

    print(count)

Solution (Python):
n,m = map(int, input().split())

a = [0] * m

for i in range(m):
    a[i] = tuple(map(int, input().split()))

a.sort(reverse=True)

c = n + 1
ret = 0
for i in range(m):
    lft, rgt = a[i]
    if rgt <= c:
        c = lft
        ret += 1

print(ret)

Solution (Python):
from operator import itemgetter
n, m = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(m)]
g = 1
e.sort(key = itemgetter(1))
for i in range(m):
    if i == 0:
        a = e[0][1] - 1
    else:
        if a >= e[i][0]:
            pass
        else:
            g += 1
            a = e[i][1] - 1
print(g)

Solution (Python):
N, M = [int(t) for t in input().split()]
R = []
for i in range(M):
    a, b = [int(t) for t in input().split()]
    R.append((a, b))

R.sort(key=lambda r: r[1])

last  = -1
count = 0
for a, b in R:
    if last < a:
        last = b - 1
        count += 1
print(count)

Solution (Python):

"""
Created on Sat Jul 21 21:41:43 2018
ABC103D
@author: maezawa
"""

n, m = list(map(int, input().split()))

w = []
cnt = 1

for i in range(m):
    ai, bi = list(map(int, input().split()))
    w.append([ai, bi])

w.sort(key = lambda x: x[1])


cnt = 1
icut = w[0][1]-1 
for bs in w:
    if bs[0] > icut:
        cnt += 1
        icut = bs[1]-1

print(cnt)

Solution (Python):
from operator import itemgetter
from collections import deque

n, m = map(int, input().split())

bridge = [0 for i in range(n-1)]

query = [list(map(int, input().split())) for i in range(m)]
query = sorted(query, key = itemgetter(1))

cut_cnt = 1
cut_bridge = deque()
cut_bridge.append(query[0][1] - 2)

for i in range(1, m):
    a, b = query[i]
    if a - 1 > cut_bridge[-1]:
        cut_bridge.append(b - 2)
        cut_cnt += 1


print(cut_cnt)

Solution (Python):
from sys import stdin, stdout
input = lambda: stdin.readline()
write = stdout.write

def main():
    N, M = map(int, input().split())
    AB = [tuple(map(int, input().split())) for _ in [0] * M]

    AB.sort(key=lambda x: x[1])
    cuts, p = 1, AB[0][1]
    for a, b in AB:
        if a >= p:
            cuts += 1
            p = b
    print(cuts)

main()

Solution (Python):
w = [int(i) for i in input().split()]
N = w[0]
M = w[1]
A = {tuple([int(j) for j in input().split()]) for i in range(M)}

A_ = sorted(A, key=lambda x: x[1])
bridge = 0
c = 0
for i in A_:
    if i[0] > bridge:
        bridge = i[1]-1
        c += 1
print(c)

Solution (Python):
class cxn():
    def __init__(self,start,end):
        self._start = start
        self._end = end
        self._removed = False

    def getStart(self):
        return self._start

    def getEnd(self):
        return self._end

    def isRemoved(self):
        return self._removed

    def remove(self):
        self._removed = True

N,M = map(int,input().split())

starts = []
ends = []
for i in range(M):
    s,e = map(int,input().split())
    c = cxn(s,e)
    starts.append(c)
    ends.append(c)

starts = sorted(starts,key = cxn.getStart)
ends = sorted(ends,key = cxn.getEnd)

ans = 0
temp = 0

p = 0
for i in range(M):
    rs = False
    while p < M and (not ends[i].isRemoved()) and starts[p].getStart() < ends[i].getEnd():
        temp += 1
        if starts[p] == ends[i]:
            rs = True
        else:
            starts[p].remove()
        p += 1

    if temp != 0:
        ans += 1

    if rs:
        ends[i].remove()

    temp = 0

print(ans)

Solution (Python):
'''
https://beta.atcoder.jp/contests/abc103/submissions/2888942
'''

n, m = map(int, input().split())
M = list(tuple(map(int, input().split())) for _ in range(m))
M_b_asc = sorted(M, key=lambda x: x[1])

cnt = 0
c = -1
for a, b in M_b_asc:
    
    if a <= c < b:
        continue
    else:
        cnt += 1
        c = b - 1   

print(cnt)

Solution (Python):
n,m = list(map(int, input().split()))

arr = [[] for i in range(n)]

for i in range(m):
	a, b = list(map(int, input().split()))
	arr[a-1].append(b)	

cnt = 0	
sting = n+1
for i,isl in enumerate(arr):
	isl.sort()
	if sting == i+1:
		cnt += 1
		sting = n+1

	if len(isl) == 0:
		continue
		
	if isl[0] < sting:
		sting = isl[0]
print(cnt)

Solution (Python):
N, M = map(int, input().split())

islands = [list(map(int, input().split()))for _ in range(M)]
islands = sorted(islands, key=lambda x: x[1])

count = 0
current = float('inf')

for island in islands:
    if island[0] <= current < island[1]:
        continue

    count += 1
    current = island[1] - 1

print(count)

Solution (Python):
def d_islands_war(N, M, Demands):
    demands = sorted(Demands, key=lambda x: x[1])
    ans = 1
    _, previous_cut = demands.pop(0)  
    for a, b in demands:
        if a >= previous_cut:
            
            ans += 1
            previous_cut = b
    return ans

N, M = [int(i) for i in input().split()]
Demands = [[int(i) for i in input().split()] for j in range(M)]
print(d_islands_war(N, M, Demands))

Solution (Python):

N, M = map(int, input().split())

a = [None] * M
b = [None] * M

for i in range(M):
    a[i], b[i] = map(int, input().split())

p = [[] for i in range(N + 1)]

for i in range(M):
    p[b[i]].append(M + i)

for i in range(M):
    p[a[i]].append(i)

ok = [False] * M
nx = []
ans = 0

for i in range(1, N + 1):
    for j in p[i]:
        if j < M:
            nx.append(j)
        else:
            if ok[j - M]:
                continue

            while nx:
                ok[nx[-1]] = True
                nx.pop()

            ans += 1

print(ans)

Solution (Python):
n , m = map(int, input().split())
l = [list(map(int,input().split())) for _ in range(m)]
l.sort()
l = l[::-1]
cnt = 1
key = l[0][0]
for i in range(1,m):
    if key >= l[i][1]:
        cnt += 1
        key = l[i][0]
print(cnt)

Solution (Python):
N, M = map(int, input().split())

war = {}
for i in range(M):
    a, b = map(int, input().split())
    if(b in war):
        if(a > war[b]):
            war[b] = a
    else:
        war[b] = a

ans = 1
b_list = list(war.keys())
b_list = sorted(b_list)
r_bridge = b_list[0] - 1

for b in b_list:
    a = war[b]
    if(a <= r_bridge):
        continue
    else:
        r_bridge = b - 1
        ans += 1

print(ans)

Solution (Python):
nm=[int(x) for x in input().split()]
N=nm[0]
M=nm[1]

to=[N]*N
for _ in range(M):
    nm=[int(x) for x in input().split()]
    A=nm[0]-1
    B=nm[1]-1
    if to[B]==N or to[B]<A:
        to[B]=A

last=0
ans=0
for i in range(N):
    if to[i]==N:
        continue
    if to[i]>=last:
      last=i
      ans+=1
print(ans)

Solution (Python):
n, m = map(int, input().split())
AB = []
for _ in range(m):
    a, b = map(int, input().split())
    AB.append((a, b))
C = [10**6] * n
for a, b in AB:
    C[a-1] = min(C[a-1], b-1)

count = 0
for i in range(n-1):
    if C[i] == i+1:
        count += 1
    else:
        C[i+1] = min(C[i+1], C[i])
print(count)

Solution (Python):
n, m = map(int, input().split())
ab = []
for i in range(m):
    ab.append(list(map(int, input().split())))
ab.sort(reverse = True)

bridge = [float("inf")]
for i in range(m):
    if ab[i][0] <= min(bridge) < ab[i][1]:
        continue
    else:
        bridge.append(ab[i][0])
print(len(bridge) - 1)

Solution (Python):
import sys
from collections import defaultdict

n, m = map(int, input().split())
disputes = defaultdict(int)
for line in sys.stdin.readlines():
    a, b = map(int, line.split())
    if disputes[b] < a:
        disputes[b] = a
ans = 0
most_right_bridge = 0
for b in sorted(disputes.keys()):
    if most_right_bridge <= disputes[b]:
        ans += 1
        most_right_bridge = b
print(ans)

Solution (Python):
from collections import defaultdict

n, m = map(int, input().split())
disputes = defaultdict(int)
for i in range(m):
    a, b = map(int, input().split())
    disputes[b] = max(disputes[b], a)
ans = 0
most_right_bridge = 0
for b in sorted(disputes.keys()):
    if most_right_bridge <= disputes[b]:
        ans += 1
        most_right_bridge = b
print(ans)

Solution (Python):
n, m = map(int, input().split())
disputes = []
for i in range(m):
    a, b = map(int, input().split())
    disputes.append((b, a))

disputes.sort()

ans=0
flag=0
for b,a in disputes:
    if flag<=a:
        ans+=1
        flag=b

print(ans)

Solution (Python):
N, M = map(int, input().split())
T = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    if not a <= b:
        a, b = b, a
    T[a-1].append(b-1)

dp = [0]*(N+1)
for i in range(N):
    dp[i+1] = max(dp[i+1], dp[i])
    for j in T[i]:
        dp[j] = max(dp[i]+1, dp[j])
print(dp[N])

Solution (Python):
import bisect

N,M = [int(x) for x in input().split()]

l = [[int(x) for x in input().split()] for _ in range(M)]



def do(l):

    count = 1

    l.sort()
    right = l[0][1]


    for i in range(len(l)):
        if l[i][0] >= right:
            right = l[i][1]
            count+=1
            continue
        if l[i][1] < right:
            right = l[i][1]
    return count

testl = [[x,x+1] for x in range(1)]

result = do(l)
print(result)

Solution (Python):
import sys

n,m = map(int, input().split())
AB = []
for _ in range(m):
    a,b = map(int, input().split())
    AB.append((a,b))

AB= sorted(AB, key=lambda x: x[1])

count = 0
pb = AB[0][1]
for a, b in AB[1:]:
    if a >= pb:
        count += 1
        pb = b
count += 1
print(count)

Solution (Python):
n, m = map(int, input().split())
disputes = []
for i in range(m):
    a, b = map(int, input().split())
    disputes.append((b, a))
disputes.sort()
ans = 0
most_right_bridge = 0
for b, a in disputes:
    if most_right_bridge <= a:
        ans += 1
        most_right_bridge = b
print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
cnt=0
x=0
for a,b in ab:
   if x<a:x=b-1;cnt+=1
print(cnt)

Solution (Python):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(M)]

A = sorted(A, key=lambda x: x[1])
count = 0
x = 0
for a in A:
    if a[0] > x:
        x = a[1] - 1
        count += 1
        
        
print(count)

Solution (Python):
n,m=map(int,input().split())
ass=[tuple(map(int,input().split())) for i in range(m)]
ass.sort(key=lambda x:x[1])

acum = []

def ifsatisfied(ab):
    for x in acum:
        if ab[0]<x <=ab[1]:
            return(True)
    return(False)

for ab in ass:
    if (acum == [] or ab[0]>=acum[-1]):
        acum.append(ab[1])

print(len(acum))

Solution (Python):
N,M = map(int,input().split())
requests = []
for _ in range(M):
        a,b = map(int,input().split())
        requests.append([a,b])

ans = 1
requests = sorted(requests, key=lambda x:(x[1],x[0]))
b = requests[0][1]
for i in range(M):
        if requests[i][0] < b:
                continue
        else:
                ans += 1
                b = requests[i][1]

print(ans)

Solution (Python):
from operator import itemgetter
N, M = list(map(int, input().split()))
ab = [[0,0] for i in range(M)]
for i in range(M):
    ab[i] = list(map(int, input().split()))

ab = sorted(ab, key=itemgetter(0,1))

re = 1
x, y = ab[0]
for i in range(1, M):
    a, b = ab[i]
    if b <= x or y <= a: 
        re += 1
        x, y = a, b
    else:
        x = max(x, 1)
        y = min(y, b)
print(re)

Solution (Python):
n, m = map(int, input().split())
l = []

for _ in range(m):
    l.append(list(map(int, input().split())))

l.sort(key=lambda x:x[1])

last = -1
ans = 0
for a, b in l:
    if a < last:
        continue
    last = b
    ans += 1

print(ans)

Solution (Python):
N,M = map(int,input().split())
listAB = [list(map(lambda x:int(x),input().split())) for i in range(M)]

listAB.sort(key=lambda x: x[1])

last = -1
ans = 0
for a,b in listAB:
    
    if last <= a:
        ans += 1
        
        last = b

print(ans)

Solution (Python):
n,m=map(int,input().split())
ass=[tuple(map(int,input().split())) for i in range(m)]
ass.sort(key=lambda x:x[1])

acum = []

def ifsatisfied(ab):
    for x in acum:
        if ab[0]<x <=ab[1]:
            return(True)
    return(False)

for ab in ass:
    if not ifsatisfied(ab):
        acum.append(ab[1])

print(len(acum))

Solution (Python):


import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

_, M = map(int, raw_input().split())
lst = []

for i in range(M):
    a, b = map(int, raw_input().split())
    lst.append((a, b))

lst.sort()
pos = 0
ans = 0

for p in lst:
    if p[0] > pos:
        ans += 1
        pos = p[1] - 1
    elif p[1] <= pos:
        pos = p[1] - 1

print ans

Solution (Python):
n, m = map(int, input().split())
l = []

for _ in range(m):
    l.append(list(map(int, input().split())))

l.sort(key=lambda x:x[1])

last = -1
ans = 0
for x in l:
    a, b = x
    if a < last:
        continue
    last = b
    ans += 1

print(ans)

Solution (Python):
N, M = map(int, input().split(' '))

break_list = [list(map(int, input().split(' '))) for _ in range(M)]
break_list = sorted(break_list, key = lambda key:key[1])

result = 1
most_min_right = break_list[0][1]

for i in range(1, M):
    if most_min_right <= break_list[i][0]:
        most_min_right = break_list[i][1]
        result += 1

print(result)

Solution (Python):
import numpy as np
n,m = map(int,input().split())
a = np.zeros(m)
b = np.zeros(m)
for i in range(m):
    a[i] , b[i] = map(int ,input().split())

a -= 1
b -= 1 
cnt = 0

while(a.shape[0]):
    out_of_task = (a >= np.min(b)).astype(np.bool)
    cnt += 1
    a = a[out_of_task]
    b = b[out_of_task]
    
print(cnt)

Solution (Python):


import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

_, M = map(int, raw_input().split())
lst = []

for i in range(M):
    a, b = map(int, raw_input().split())
    lst.append((b, a))

lst.sort()
pos = 0
ans = 0

for p in lst:
    if p[1] > pos:
        ans += 1
        pos = p[0] - 1

print ans

Solution (Python):
import numpy as np
n,m = map(int,input().split())
a = np.zeros(m)
b = np.zeros(m)
for i in range(m):
    a[i] , b[i] = map(int ,input().split())

a -= 1
b -= 1 
cnt = 0

while(a.shape[0]):
    min_b = np.min(b)
    in_task = (a < min_b).astype(np.bool)
    cnt += 1
    tmp = np.logical_not(in_task)
    a = a[tmp]
    b = b[tmp]
    
print(cnt)

Solution (Python):
N, M = map(int, input().split())
I = [[N + 1] for i in range(N)]
for i in range(M):
  a, b = map(int, input().split())
  I[a - 1].append(b - 1)
cut = 0
for i in range(N - 1):
  if min(I[i]) == i + 1:
    cut += 1
  else:
    I[i + 1].append(min(I[i]))
print(cut)

Solution (Python):


import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

_, M = map(int, raw_input().split())
lst = []

for i in range(M):
    a, b = map(int, raw_input().split())
    lst.append((b, a))

lst.sort()
pos = 0
ans = 0

for p in lst:
    if p[1] > pos:
        ans += 1
        pos = p[0] - 1

print ans

Solution (Python):
n, m = map(int, input().split())
tasks = []
for i in range(m):
    a, b = map(int, input().split())
    tasks.append([a, b])
tasks = sorted(tasks, key=lambda x:x[1])
cnt = 1
r = tasks[0][1]
for task in tasks:
    if task[0] >= r:
        cnt +=1
        r = task[1]
print(cnt)

Solution (Python):
N,M = map(int,input().split())
requests = []
for _ in range(M):
        a,b = map(int,input().split())
        requests.append([a,b])

ans = 1
requests = sorted(requests, key=lambda x:x[1])
b = requests[0][1]
for i in range(M):
        if requests[i][0] < b:
                continue
        else:
                ans += 1
                b = requests[i][1]

print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
x=l=0
for(a,b)in ab:
   if a>x:x=b-1;l+=1
print(l)

Solution (Python):
from operator import itemgetter
N,M=map(int,input().split())
table=[]
for i in range(M):
    a,b=map(int,input().split())
    table.append((a,b))
table=sorted(table, key=itemgetter(1))
ans=0
t=0
for a,b in table:
    if t<a:
        ans+=1
        t=b-1
print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
x=0
l=[0]*n
for(a,b)in ab:
   if a>x:x=b-1;l[x]=1
print(sum(l))

Solution (Python):
n,m=map(int, input().split(" "))
arr=[]
for i in range(m):
    a,b=map(int, input().split(" "))

    arr.append([a,b])


arr.sort(key=lambda x:(x[0],x[1]))
left=0
num=0
count=0
for i in range(len(arr)):
    if arr[i][0]==left:
        pass
    if arr[i][1]<=num:
        num=arr[i][1]
    if arr[i][0]-num>=0:
        count+=1
        num=arr[i][1]
        left=arr[i][0]
    
print(count)

Solution (Python):
n,m=map(int,input().split())
s=[list(map(int,input().split()))for _ in range(m)]
s.sort(key=lambda x:x[0])
c=a=0
while c<m:
    i=1
    t=s[0][1]
    while i<len(s)and s[i][0]<t:
        t=min(t,s[i][1])
        i+=1
    c+=i
    s=s[i:]
    a+=1
print(a)

Solution (Python):
N, M = map( int, input().split())
Q = [[int(s) for s in input().split()] for _ in range(M)]
Q = sorted(Q)
ans = 1
stan = Q[0][1]
for i in range(M):
    if Q[i][0] < stan:
        stan = min(stan,Q[i][1])
    else:
        ans += 1
        if i == M-1:
            break
        else:
            stan = Q[i][1]
print(ans)

Solution (Python):
n, m = map(int, input().split())

l = []

for i in range(m):
    a, b = map(int, input().split())
    l.append([b, a])

l.sort()

ans = 0
x = 0

for i in range(m):
    if l[i][1] >= x:
        ans += 1
        x = l[i][0]

print(ans)

Solution (Python):
n, m  = [int(i) for i in input().split()]
ab = []
b = []
for i in range(m):
    ab.append([int(i) for i in input().split()])

ab.sort()
op_sum = 1
left = ab[0][0]
right = ab[0][1]

for a, b in ab:
    if right <= a:
        op_sum += 1
        left = a
        right = b
    else:
        left = a
        right = min(right, b)

print(op_sum)

Solution (Python):
N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(M)]

AB.sort(key=lambda x: x[1])
num = 0
p = 0
for i in AB:
    if i[0] >= p:
        num += 1
        p = i[1]

print(num)

Solution (Python):

import sys
n, m = map(int, input().split())

demands = [
    tuple(map(int,
              line.strip().split())) for line in sys.stdin.readlines()
]
demands.sort(key=lambda x: x[1])

compressed = []
while demands:
    a, b = demands.pop()

    for i, (ai, bi) in enumerate(compressed):
        if b <= ai or bi <= a:
            continue

        else:
            compressed[i] = [max(a, ai), min(b, bi)]
            break

    else:
        compressed.append([a, b])

print(len(compressed))

Solution (Python):
N, M = map(int, input().split())
I = [N + 1 for i in range(N)]
for i in range(M):
  a, b = map(int, input().split())
  I[a - 1] = min(I[a - 1], b - 1)
cut = 0
for i in range(N - 1):
  if I[i] == i + 1:
    cut += 1
  else:
    I[i + 1] = min(I[i + 1], I[i])
print(cut)

Solution (Python):
def run(n, m, ab):
    ab = sorted(ab)
    cnt = 1
    b = ab[0][1]
    c = b
    for i in range(1, m):
        a = ab[i][0]
        b = ab[i][1]
        if a < c and c <= b:
            pass
        elif c <= a:
            cnt += 1
            c = b
        else:
            c = b
    return cnt


def read_line():
    n, m = list(map(int, input().split()))
    ab = []
    for i in range(m):
        a, b = list(map(int, input().split()))
        ab.append([a, b])
    return (n, m, ab)


def main():
    n, m, ab = read_line()
    print(run(n, m, ab))


if __name__ == '__main__':
    main()

Solution (Python):
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(list(map(int, input().split())))
a.sort(key=lambda x: x[1])
t = 0
b = 1
for i in range(m):
    if a[i][0] >= b:
        t += 1
        b = a[i][1]
print(t)

Solution (Python):
import sys
import numpy as np
n,m = map(int, input().split())
ask = np.array([list(map(int, input().split())) for i in range(m)],dtype=int)





if n*(n-1)//2==m:
    print(n-1)
    sys.exit()

ask2 = ask[np.argsort(ask[:,1])]
delete = np.array([ask2[0,1]-1])
for a in ask2[1:]:
    if a[0] <= delete[-1]:
        pass
    else:
        delete = np.append(delete, a[1]-1)
print(len(delete))

Solution (Python):
N, M = map( int, input().split())
Q = [[int(s) for s in input().split()] for _ in range(M)]
Q = sorted(Q)
ans = 1
LQ = len(Q)
while LQ != 0:
    stan = Q[0][1]
    k = 1
    while LQ != 0:
        if Q[0][0] < stan:
            stan = min(stan,Q.pop(0)[1])
            LQ -= 1
        else:
            ans += 1
            break
print(ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, AB):
    AB.sort(key=lambda x: x[1])
    ans = 0
    p = -1
    for a, b in AB:
        if a <= p:
            continue
        p = b - 1
        ans += 1
    return ans


def main():
    N, M = read_int_n()
    AB = [read_int_n() for _ in range(M)]
    print(slv(N, M, AB))


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N, M = map(int, input().split())

a = np.zeros((M,2))

for i in range(M):
	a[i] = list(map(int, input().split()))

a = a[np.argsort(a[:,1])]

index = 0
ans = 0
for tmp in a:
	if tmp[0] <= index:
		continue
	else:
		ans += 1
		index = tmp[1] - 1

print(ans)

Solution (Python):
if __name__ == '__main__':
    n, m = map(int, input().split())
    ab = []
    for i in range(m):
        ab_i = tuple(map(int, input().split()))
        ab.append(ab_i)

    ab.sort(key=lambda pair: pair[1])

    ans = 0
    left = 0
    for i in range(m):
        if left <= ab[i][0]:
            left = ab[i][1]
            ans += 1

    print(ans)

Solution (Python):
N, M = map(int, input().split())
demand = []

for _ in range(M):
    a, b = map(int, input().split())
    demand.append((a,b))

demand = sorted(demand, key=lambda x: x[1])
count = 0
bridge = None

for d in demand:
    if bridge == None or bridge < d[0]:
        bridge = d[1] - 1
        count += 1

print(count)

Solution (Python):
n,m = map(int,input().split())
ab = [tuple(map(int,input().split())) for _ in range(m)]
ab.sort(key=lambda x:x[1])
last=-1
cnt=0
for i in range(len(ab)):
    if(ab[i][0]<=last):pass
    else:
        last=ab[i][1]-1
        cnt+=1
print(cnt)

Solution (Python):
N,M = map(int,input().split())
x = []
for i in range(M):
    a,b=[int(i) for i in input().split()]
    x.append([a,b])
x = sorted(x, key=lambda x:x[1])
no = 0
ans = 0
for i in range(M):
  a = x[i]
  if (a[0] <= no and a[1] >= no):
    pass
  else:
    no = a[1] -1
    ans += 1
print(ans)

Solution (Python):
from operator import itemgetter
N, M = map(int, input().split())
ab = []
for i in range(M):
    ab.append([int(xi) for xi in input().split()])
ab = sorted(ab, key=itemgetter(0, 1))
x, y = ab[0]
count = 1
for ai, bi in ab[1:]:
    if bi < x or y <= ai:
        count += 1
        x, y = ai, bi
    else:
        x = max(x, ai)
        y = min(y, bi)
print(count)

Solution (Python):
from sys import stdin
from fractions import gcd

N,M=[int(i) for i in input().split()]
datas = []
for i in range(M):
    x,y=[int(i) for i in input().split()]
    datas.append([y,x])
datas.sort()
count = 0
start = 0
for data in datas:
    if data[1] < start : continue
    end = data[0]
    start = end 
    count +=1        
        
print(count)

Solution (Python):
from operator import itemgetter

n, m = [int(i) for i in input().split()]
A, B = zip(*[[int(i) for i in input().split()] for j in range(m)])

A, B = zip(*sorted(zip(A, B), key=itemgetter(1, 0)))

last = -1
ans = 0

for a, b in zip(A, B):
    if last <= a:
        ans += 1
        last = b

print(ans)

Solution (Python):

import numpy as np
buf = input().split()
n = int(buf[0])
m = int(buf[1])

l = np.zeros((m, 2))
for i in range(m):
	buf = input().split()
	for j in range(2):
		l[i][j] = int(buf[j])
l = l[l[:,1].argsort(), :]

sum = 0
c1 = 0
c2 = 0
for i in range(m):
	b1 = l[i][0]
	b2 = l[i][1]
	if(b1 >= c2):
		sum += 1
		c1 = b1
		c2 = b2
	elif(b2 < c2):
		c2 = b2
	
print(sum)

Solution (Python):
import sys
n, m = map(int, sys.stdin.readline().split())
req = [ list(sorted(map(int, sys.stdin.readline().split())))
        for _ in range(m) ]
req.sort(reverse=True)
lastcut = None
c = 0
for a, b in req:
    if lastcut is None or a > lastcut or lastcut >= b:
        lastcut = a
        c += 1
print(c)

Solution (Python):
n, m = map(int, input().split())
demands = []
for _ in range(m):
    a, b = map(int, input().split())
    demands.append([b, a])
demands.sort()

p = demands[0][0]
ans = 1

for i in range(1, m):
    if p <= demands[i][1]:
        ans += 1
        p = demands[i][0]


print(ans)

Solution (Python):
n, m = list(map(int, input().split()))
ab = []
for i in range(m):
  a, b = list(map(int, input().split()))
  ab.append((a, b))
ab.sort(key=lambda x:x[1])

ans = 0
max_ = 0
for ele in ab:
  if ele[0]>=max_:
    max_ = ele[1]
    ans += 1
print(ans)

Solution (Python):
n,m=[int(i) for i in input().split()]
count=m
a=[]

for i in range(m):
  a.append([int(j) for j in input().split()])
 
a.sort(key=lambda x:x[1])

k=0
for i in range(1,m):
    if a[k][1] > a[i][0]:
      count -= 1
    else:
      k = i

print(count)

Solution (Python):
n, m = map(int, input().split())
store = []
for _ in range(m):
    a, b = map(int, input().split())
    store.append([a, b])
store.sort(key = lambda x:x[0])
count = 0
ans = 0
while count < m:
    index = 1
    t = store[0][1]
    while index < len(store) and store[index][0] < t:
        t = min(t, store[index][1])
        index += 1
    count += index
    store = store[index:]
    ans += 1
print(ans)