Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
li1 = []
for i in range(m):
    a,b = map(int,input().split())
    li2 = []
    for j in range(a,b+1):
        li2.append(j)
    li1.append(set(li2))
for i in range(m):
    li1[0] = li1[0] & li1[i]
print(len(li1[0]))

Submission status: Time Limit Exceeded

Solution code:
# C - Prison

N, M = map(int, input().split())
ls = [i for i in range(1, N + 1)]
for _ in range(M):
    l, r = map(int, input().split())
    ls = set(ls) & set([i for i in range(l, r + 1)])

print(len(ls))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=[0 for i in range(N)]
for i in range(M):
    L,R=map(int,input().split())
    for t in range(L-1,R):
        A[t]+=1
cnt=A.count(M)
print(cnt)

Submission status: Time Limit Exceeded

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

ans = set(range(1, n+1))

for i in range(m):
  l, r = map(int, input().split())
  ans = ans & set(range(l, r+1))

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
cnt = 0
L,R = map(int,input().split())
set_all = [i for i in range(L,R+1,1)]
for i in range(2,M+1,1):
    L,R = map(int,input().split())
    li = [i for i in range(L,R+1,1)]
    set_all = set(set_all) & set(li)
     
print(len(set_all))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
cnt = 0
L,R = map(int,input().split())
set_all = {i for i in range(L,R+1,1)}
for i in range(2,M+1,1):
    L,R = map(int,input().split())
    li = {i for i in range(L,R+1,1)}
    set_all = set_all & li
     
print(len(set_all))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lr = [[0]*2]*m
for i in range(m):
  lr[i] = list(map(int,input().split()))
valid = set(range(1,n+1))
for i in range(m):
  subleft = set(range(1,lr[i][0]))
  subright = set(range(lr[i][1]+1,n+1))
  valid.difference_update(subleft)
  valid.difference_update(subright)
print(len(valid))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l=[]
for _ in range(m):
    l.append(list(map(int,input().split())))
d=[0]*(n+1)
for i in range(m):
    for j in range(l[i][0],l[i][1]+1):
        d[j]+=1
print(d.count(m))

Submission status: Time Limit Exceeded

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

for m_i in range(m):
  
  l,r = map(int, input().split())
  if m_i==0:
    ans = set(range(l, r+1))
  else:
    ans &= set(range(l, r+1))
    
print(len(ans))

Submission status: Time Limit Exceeded

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

id_set = set(range(N + 1))

for m in range(M):
  L, R = map(int, input().split())
  id_set = id_set & set(range(L, R + 1, 1))
  if len(id_set) == 0:
    break


print(len(id_set))

Submission status: Time Limit Exceeded

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

id_set = set(range(N + 1))

for m in range(M):
  L, R = map(int, input().split())
  id_set = id_set & set(range(L, R + 1, 1))
  if len(id_set) == 0:
    break
  
print(len(set(range(1, N + 1, 1)) & id_set))

Submission status: Time Limit Exceeded

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

id_range = range(N + 1)

for m in range(M):
  L, R = map(int, input().split())
  id_range = set(id_range) & set(range(L, R + 1, 1))
  
print(len(set(range(1, N + 1, 1)) & id_range))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    N, M = map(int, input().split())
    Card = [[] for i in range(M)]
    Diff = [i for i in range(1, N + 1)]

    for i in range(0, M):
        L, R = map(int, input().split())
        tmp = []
        for j in range(L, R + 1):
            if j in Diff:
                tmp.append(j)
        Diff = tmp
    print(len(Diff))    


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    N, M = map(int, input().split())
    Card = [[] for i in range(M)]
    Diff = [i for i in range(1, N + 1)]

    for i in range(0, M):
        L, R = map(int, input().split())
        for j in range(L, R + 1):
            Card[i].append(j)
        Diff = list(set(Diff) & set(Card[i]))
    print(len(Diff))


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    N, M = map(int, input().split())
    Diff = [i for i in range(1, N + 1)]

    for i in range(0, M):
        L, R = map(int, input().split())
        Card = []
        for j in range(L, R + 1):
            Card.append(j)
        Diff = list(set(Diff) & set(Card))
    print(len(Diff))


main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

N,M = map(int,input().split())
L,R = map(int,input().split())
curSet = set(range(L,R+1))

for i in range(2,M+1):
    L,R = map(int,input().split())
    curVec = set(range(L,R+1))

    curSet = curSet.intersection(curVec)
    
print(len(curSet))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

for i in range(1,M+1):
    L,R = map(int,input().split())
    curVec = set(range(L,R+1))
    
    if (i==1):
        curSet = curVec
    
    curSet = curSet.intersection(curVec)
    
print(len(curSet))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
LR = [input().split(" ") for m in range(M)]
res = 0
for i in range(1, N + 1):
    r = 0
    for d in LR:
        if int(d[0]) <= i <= int(d[1]):
            r += 1
    if r == len(LR):
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
LR = []
for m in range(M):
    l, r = input().split()
    LR.append([int(l), int(r)])
res = 0
for i in range(1, N + 1):
    r = 0
    for d in LR:
        if d[0] <= i <= d[1]:
            r += 1
    if r == len(LR):
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

k = np.zeros(n,dtype='int')

for i in range(m):
    l,r = map(int,input().split())
    k[l-1:r] += 1
print(len(k[k==m]))

Submission status: Time Limit Exceeded

Solution code:
import collections
n,m = map(int, input().split())
cnt = [0 for _ in range(n)]
for i in range(m):
	l,r = map(int, input().split())
	for i in range(l-1, r):
		cnt[i] += 1
c = collections.Counter(cnt)
print(c[m])

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
u = set(range(1, n+1))
for i in range(m):
	l,r = map(int, input().split())
	u = u & set(range(l, r+1))
print(len(u))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
zahyo=[0]*m
for i in range(m):
    zahyo[i]=list(map(int,input().split()))
 
collec_lis=[]
for i in range(m):
    collec_lis.extend(list(range(int(zahyo[i][0]),int(zahyo[i][1])+1)))
    
import collections
c = collections.Counter(collec_lis)
 
kyotu=[int(i) for i in list(c.values()) if i==m]
print(len(kyotu))

Submission status: Time Limit Exceeded

Solution code:

n,m=map(int,input().split())
zahyo=[0]*m
for i in range(m):
    zahyo[i]=list(map(int,input().split()))

collec_lis=[]
for i in range(m):
    collec_lis.extend(list(range(int(zahyo[i][0]),int(zahyo[i][1])+1)))
    
import collections
c = collections.Counter(collec_lis)

kyotu=[int(i) for i in list(c.values()) if i==m]
print(len(kyotu))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
g = [tuple(map(int,input().split())) for i in range(m)]
ans=0
for i in range(1,n+1):
  if all([t[0]<=i and i<=t[1] for t in g]):
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
li=[list(map(int,input().split())) for i in range(m)]
cnt = []
ans=[]

for j in li:
    cnt.append(set(num for num in range(j[0],j[1]+1)))

ans = cnt[0]
for k in range(len(cnt)-1):
    ans = ans & cnt[k+1]
print(len(ans))

Submission status: Time Limit Exceeded

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

pass_num_set = {}

for i in range(M):
    L, R = list(map(int, input().split()))
    if i == 0:
        pass_num_set = set(range(L, R + 1))
    else:
        pass_num_set = pass_num_set & set(range(L, R + 1))

ans = len(pass_num_set)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
can_through = ["1" for _ in range(n)]
# cnt = 0

for i in range(m):
    l, r = map(int, input().split())
    # for j in range(1, n+1):
    #     if can_through[j-1]:
    #         if l <= j <= r:
    #             pass
    #         else:
    #             can_through[j-1] = False

    # print(can_through[:2])
    # print(can_through[2:])
    can_through[:l-1] = "0"*(l-1)
    # print(can_through)
    can_through[r:n] = "0"*(n-r)

    # print(can_through)

print(can_through.count("1"))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/PycharmProjects/atcoder/C-Prison.py
#
n, m = map(int, input().split())
can_through = [True for _ in range(n)]
cnt = 0

for i in range(m):
    l, r = map(int, input().split())
    for j in range(1, n+1):
        if can_through[j-1]:
            if l <= j <= r:
                pass
            else:
                can_through[j-1] = False
    # print(can_through)

for j in range(n):
    if can_through[j]:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/local/bin python
# -*- coding: utf-8 -*-
#
# ~/PycharmProjects/atcoder/C-Prison.py
#
n, m = map(int, input().split())
can_through = [True for _ in range(n)]
cnt = 0

for i in range(m):
    l, r = map(int, input().split())
    for j in range(1, n+1):
        if l <= j <= r:
            pass
        else:
            can_through[j-1] = False
    # print(can_through)

for j in range(n):
    if can_through[j]:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
ans = 0

keylist = [[False]*M for _ in range(N)]

for i in range(M):
    L, R = map(int,input().split())
    for j in range(L-1,R):
        keylist[j][i] = True

for i in range(N):
    if all(keylist[i]):
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def two_input():
    return map(int, input().split())

N, M = two_input()
LR = [list(two_input()) for i in range(M)]

cnt = 0
for i in range(1, N+1):
    cnt += 1
    for j in range(M):
        if LR[j][0] > i or LR[j][1] < i:
            cnt -= 1
            break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
flg = [0] * N

for _ in range(M):
    L, R = map(int, input().split())
    for i in range(L - 1, R):
        flg[i] += 1

ans = 0
for j in range(N):
    if flg[j] == M:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
#b = list(map(int, input().split()))
dic = {}
for i in range(m):
    l, r = [int(j) for j in input().split()]
    for j in range(l, r+1):
        if str(j) in dic:
            dic[str(j)] += 1
        else:
            dic[str(j)] = 1

count = 0
for i in dic.values():
    if i == m:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def doqueri(S,N,L,R):
	s = S[L-1:R]
	l = [0] * (L-1)
	r = [0] * (N-R)

	return l+s+r

if __name__ == '__main__':
	N, M = map(int, input().split())
	S = [1] * N
	for _ in range(M):
		L,R = map(int, input().split())
		S = doqueri(S[:],N,L,R)
	print(S.count(1))

Submission status: Time Limit Exceeded

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def logical_and(lst, l, r):
    res = []
    for i in range(l, r+1):
        if i in lst:
            res.append(i)
    return res

def solve():
    n, m = MI()

    l, r = MI()
    ans = list(range(l, r+1))
    for _ in range(m-1):
        l, r = MI()
        ans = logical_and(ans, l, r)
    print(len(ans))


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
cards, gates = map(int, input().split())

no = []
for _ in range(gates):
    no.append(input())

accept_List = []
cNo_list = []
for x in range(1, cards+1):
    accept_List.append(x)
    cNo_list.append(x)

for x in no:
    a, b = map(int, x.split())
    for y in cNo_list:
        if (y < a or b < y) and (y in accept_List):
            accept_List.remove(y)

print(len(accept_List))

Submission status: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]

key_set = None
for _ in range(m):
    l, r = [int(x) for x in input().split()]
    if not key_set:
        key_set = set(range(l, r + 1))
    else:
        key_set &= set(range(l, r + 1))
print(len(key_set))

Submission status: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]
g_list = [[int(x) for x in input().split()] for _ in range(m)]

ans = 0
for i in range(1, n + 1):
    for l, r in g_list:
        if i < l or i > r:
            break
    else:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(m)] #n行に１つずつ入力される整数
num_li = [idx for idx in range(10**5+1)]
for l, r in li:
    temp_li = [i for i in range(l, r+1)]
    num_li = set(temp_li) & set(num_li)
print(len(num_li))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


N, M = lr()
l_list = []
r_list = []
for i in range(M):
    l, r = lr()
    l_list.append(l)
    r_list.append(r)

ans = set()
for i in range(M):
    cards = sorted(l_list[i:] + r_list[:i + 1])
    cards = set(range(cards[0], cards[-1] + 1, 1))
    if (len(ans) == 0):
        ans = cards
        continue

    ans = cards & ans

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
count = [0]*N
for i in range(M):
    L, R = map(int, input().split())
    for i in range(L, R+1):
        count[i-1] += 1
ans = 0
for i in count:
    if i == M:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

l = [0] * N
count = 0
for i in range(M):
  for j in range(LR[i][1] - LR[i][0] + 1):
    l[LR[i][0]+j-1] += 1
else:
  for i in l:
    if i == M:
      count += 1
  else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
N, M = map(int, sys.stdin.readline().split())
LR = [list(map(int,sys.stdin.readline().split())) for i in range(M)]
B = list(range(0, 10 ** 5 + 1))
for i in LR:
	A = list(range(i[0], i[1] + 1))
	B = set(A) & set(B)
print(len(B))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
LR=[]
for i in range(M):
  LR.append(list(map(int,input().split())))
ans=[i+1 for i in range(N)]
for i in range(M):
  for j in range(LR[i][0]):
    if j in ans:
      ans.remove(j)
  for k in range(LR[i][1]+1,N+1):
    if k in ans:
      ans.remove(k)
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
# C - Prison
import numpy as np

n, m = map(int, input().split())
a = np.ones(n, dtype=np.int64)

for i in range(m):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    b = np.zeros(n, dtype=np.int64)
    b[l:r+1] = 1
    a = a&b

print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split(" "))
List=[input().split(" ") for _ in range(M)]

Ans=0
for i in range(1,N+1):
    count=0
    for j in range(M):
        if int(List[j][0])<=i<=int(List[j][1]):
            count+=1
    if count==M:
        Ans+=1

print(Ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = list(map(int, input().split()))

ranges = np.array([0] * (n))
for i in range(m):
  r, l = list(map(int, input().split()))
  ranges[r-1:l] += 1
  
print((ranges == m).sum())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = list(map(int, input().split()))
rl = [list(map(int, input().split())) for i in range(m)]

ranges = np.array([0] * (n))
for r, l in rl:
  ranges[r-1:l] += 1
  
print((ranges == m).sum())

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
tmp_l = [i for i in range(l, r + 1)]
tmp_set = set(tmp_l)
for _ in range(m - 1):
    l, r = map(int, input().split())
    tmp_l = [i for i in range(l, r + 1)]
    tmp_set = tmp_set.intersection(set(tmp_l))
 
print(len(tmp_set))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
rc = [list(map(int,input().split())) for i in range(k)]

tmp = set(range(1,n+1))

for i in rc:
    tmp = tmp & set(range(i[0],i[1]+1))

print(len(tmp))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
tmp_l = [i for i in range(l, r + 1)]
tmp_set = set(tmp_l)
for _ in range(m - 1):
    l, r = map(int, input().split())
    tmp_l = [i for i in range(l, r + 1)]
    tmp_set = tmp_set.intersection(set(tmp_l))

print(len(tmp_set))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
dp=[0]*(N)
for _ in range(M):
    L,R=map(int,input().split())
    for j in range(L-1,R):
        dp[j]+=1
print(dp.count(M))

Submission status: Time Limit Exceeded

Solution code:
N,M= map(int,input().split())
gate=[list(map(int,input().split())) for i in range(M)]
count2=0

for i in range(1,N+1):
  count=0
  for j in gate:
    if j[0]<=i<=j[1]:
      count+=1
    else:
      break
  if count==M:
    count2+=1
print(count2)

Submission status: Time Limit Exceeded

Solution code:
N,M= map(int,input().split())
gate=[list(map(int,input().split())) for i in range(M)]
count2=0
for i in range(1,N+1):
  count=0
  for j in gate:
    if j[0]<=i<=j[1]:
      count+=1
  if count==M:
    count2+=1
print(count2)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, M = map(int, input().split())
    cards = list(range(1, N + 1))
    for i in range(M):
        L, R = map(int, input().split())
        cards = [x for x in cards if L <= x <= R]
    print(len(cards))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

while True:
  try:
    tl,tr = map(int,input().split())
    l.append(tl)
    r.append(tr)
  except:
    break

l = sorted(l)
r = sorted(r)
cnt = 0
for i in range(1,n+1):
  if i>=max(l) and i<=min(r):
    cnt +=1
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
#n = int(input())
n, m = list(map(int, input().split()))
#b = list(map(int, input().split()))
count =[0]*n

for i in range(m):
    x,y = [int(j) for j in input().split()]
    for j in range(x-1, y):
        count[j]+=1

print(count.count(m))

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np

N,M = [int(i) for i in input().split(" ")]
ans = np.array([True for _ in range(N)])
ans_ng = ans[::] & False
ans_num = 0

for _ in range(M):
    ans_cp = copy.deepcopy(ans_ng)
    L,R = [int(i) for i in input().split(" ")]
    ans_cp[L-1:R] = True
    ans = ans & ans_cp

for i in ans:
    if i:
        ans_num += 1

print(ans_num)

Submission status: Time Limit Exceeded

Solution code:
s = input().split()
n = int(s[0])
m = int(s[1])
card_id = []
open_id = []

for i in range(m):
    s = list(map(int, input().split()))
    l_i = s[0]
    r_i = s[1]
    #card_id += s
    
    #open_id += (range(l_i, r_i + 1))
    if i == 0:
        open_id = set((range(l_i, r_i + 1)))
    else:
        open_id = set(open_id) & set((range(l_i, r_i + 1)))
    #print(open_id)

card_id = set((range(1, n+ 1)))
ans_set = open_id & card_id
print(len(ans_set))

Submission status: Time Limit Exceeded

Solution code:
m, n = map(int, input().split())
l = []
for i in range(n):
  a, b = map(int, input().split())
  l.append(set(range(a,b+1)))
if n >= 2:
  for i in range(1,n):
    d = l[0].intersection(l[i])
else:
  d = l[0]
print(len(d))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
v = []
for i in range(n):
  v.append(i+1)
for i in range(m):
  l,r = map(int,input().split())
  for j in range(len(v)):
    if v[j]<l or r <v[j]:
      
      v[j] = 0
count = 0
for i in range(len(v)):
  if v[i] != 0:
    count += 1
print(count)

Submission status: Time Limit Exceeded

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

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

ans = [j+1 for j in range(N)]
for i in range(M):
    ans = [ans[j] for j in range(len(ans)) if L[i] <= ans[j] and ans[j] <= R[i]]
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

def main():
    li = []
    ans = 0
    n,m = map(int,input().split())
    for _ in range(m):
        l,r = map(int, input().split())
        for i in range(l,r+1):
            li.append(i)
    c = collections.Counter(li)
    for i,v in c.items():
        if v >= m:
            ans += 1
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
d = [0]*(10**5)
for _ in range(m):
    l, r = map(int, input().split())
    for i in range(l-1, r):
        d[i] += 1
ans = 0
for i in d:
    if i == m:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l,r=map(int,input().split())
a=set(range(l,r+1))
for i in range(m-1):
    l,r=map(int,input().split())
    a.intersection_update(range(l,r+1))
print(len(a))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l,r=map(int,input().split())
a=set(range(l,r+1))
for i in range(m-1):
    l,r=map(int,input().split())
    a.intersection_update(range(l,r+1))
print(len(a))

Submission status: Time Limit Exceeded

Solution code:
# indeednow_A_b
import sys
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
L = [0 for i in range(M)]
R = [0 for i in range(M)]
for i in range(M):
    L[i], R[i] = [int(x) for x in stdin.readline().rstrip().split()]
# print(L, R)
ans = set()

for k in range(M):
    temp = set()
    for l in range(L[k], R[k]+1):
        temp.add(l)
    if len(ans) != 0:
        ans = temp & ans
    else:
        ans = temp
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M= list(map(int,input().split()))
list_A = [0] * N
for i in range(M):
    A,B = list(map(int,input().split()))
    list_A[A-1:B] = map(lambda x: x+1,list_A[A-1:B])
print(list_A.count(M))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
key = [i+1 for i in range(n)]
for i in range(m):
  l,r = map(int,input().split())
  new_key = [i for i in range(l,r+1)]
  key = list(set(new_key) & set(key))
  
print(len(key))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import collections


t=[]

n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]


for i in range(m):
    for x in range(l[i][0],l[i][1]+1):
        t.append(x)

b=list(collections.Counter(t).values())

c=[y for y in b if y==m]
print(len(c))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import collections


t=[]

n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]


for i in range(m):
    for x in range(l[i][0],l[i][1]+1):
        t.append(x)

a=collections.Counter(t)
b=list(a.values())

c=[y for y in b if y==m]
print(len(c))

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(n) for n in input().split()]
S = set(range(1, N+1))
for i in range(M):
    l, r = [int(n) for n in input().split()]
    S &= set(range(l, r+1))
print(len(S))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
CARD=[0]*n
for i in range(m):
    L,R=map(int,input().split())
    for j in range(L-1,R):
        CARD[j]+=1
ans=0
for i in range(n):
    if CARD[i]==m:ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

L, R = map(int, input().split())
ans = set(range(L, R+1))
for _ in range(M-1):
    L, R = map(int, input().split())
    ans = ans & set(range(L, R+1))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
P = [input().split() for i in range(M)]

A=set(list(range(int(P[0][0]), int(P[0][1])+1)))

for i in range (1, M):
	B = set(list(range(int(P[i][0]), int(P[i][1])+1)))
	A = A.intersection(B)

print(len(A))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
ans = [0 for _ in range(n)]
for i in range(m):
    l,r = map(int,input().split())
    l-=1;r-=1
    for j in range(l,r+1):
        ans[j] += 1
print(ans.count(m))

Submission status: Time Limit Exceeded

Solution code:
n , m = map(int,input().split())
p = {i+1 for i in range(n)}
for i in range(m):
    l , r = map(int,input().split())
    s = {l+i for i in range(r-l+1)}
    p = p & s

print(len(p))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
A = [0]*N
for i in range(M):
    for j in range(L[i][0],L[i][1]+1):
        A[j-1] += 1       
count = 0
for i in range(N):
    if A[i] == M:
        count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
A = list(range(L[0][0],L[0][1]+1))
for i in range(1,M):
    A.extend(list(range(L[i][0],L[i][1]+1)))
count = 0
for i in range(1,N+1):
    if A.count(i) == M:
        count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
l = list(list(map(int,input().split())) for _ in range (m))
p = set(i for i in range(1,n+1))
for i in l:
  s,t = i
  a = set(q for q in range(s,t+1))
  p = p&a
print(len(p))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lScope = []
rScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    lScope.append(l)
    rScope.append(r)
# k in keyScope[i] (i= 0~m) を確かめたい
for key in range(1,n+1):
    if max(lScope) <= key <= min(rScope):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
keyScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    keyScope.append([j for j in range(l,r+1)])
# k in keyScope[i] (i= 0~m) を確かめたい
for k in range(1,n+1):
    gate = 0
    while k in keyScope[gate]:
        if gate == m-1:
            ans += 1
            break
        else:
            gate += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
ns = set(i+1 for i in range(n))
for i in range(m):
    l, r = map(int, input().split())
    ns &= set(j for j in range(l, r+1))
print(len(ns))

Submission status: Time Limit Exceeded

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

gate=[i for i in range(N)]
l,r=map(int,input().split())
ans=gate[l-1:r]

for i in range(M-1):
  l,r=map(int,input().split())
  new_ans=gate[l-1:r]
  ans=list(set(ans)&set(new_ans))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
cnt = [0]*(n+1)
for i in range(m):
  l, r = map(int, input().split())
  for j in range(l, r+1):
    cnt[j] += 1
print(cnt.count(m))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n,m = load_list()
lr = []
for _ in range(m):
    l, r = load_list()
    lr.append(set(range(l, r+1)))

passed = set.intersection(*lr)

print(len(passed))