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 = [int(i) for i in input().split()]
cards = [input().split() for l in range(M)]

count = 0
for c in range(1, N+1):
    for g in range(M):
        if c >= int(cards[g][0]) and c <= int(cards[g][1]):
            if g == M-1:
                count += 1
        else:
            break

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
l = [] 
r = []
count = 0

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

#idカードの生成
for i in range(0,M):
    l_temp,r_temp = map(int, input().split())
    l.append(l_temp)
    r.append(r_temp)
    
for i in range(1,N+1):
    if max(l) <= i and i <= min(r):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L, R = map(int, input().split())
pool = set(range(L, R+1))
for i in range(M-1):
  L, R = map(int, input().split())
  pool = set(range(L, R+1)).intersection(pool)
print(len(pool))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input().split()
n=int(s[0])
m=int(s[1])

l=[]
r=[]
for i in range(m):
    s=input().split()
    l.append(int(s[0]))
    r.append(int(s[1]))

sum=0
for i in range(n):
    j=i+1
    count=0
    for k in range(m):
        gate=np.arange(l[k],r[k]+1)
        if j in gate:
            count+=1
        else:
            break
    if count==m:
        sum+=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
L = []
R = []
for i in range(m):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)
ans = 0
for i in range(1,n+1):
    if max(L) <= i and min(R) >= i:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

N, M = map(lambda x: int(x), sys.stdin.readline().split(" "))

prev = set(range(1, N+1))
for i in range(0, M):
    L, R = map(lambda x: int(x), sys.stdin.readline().split(" "))
    accepted = set(range(L, R+1))
    prev = prev & accepted
print(len(prev))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
inputData = input()
ID, Gate = map(int, inputData.split())
x = []
for i in range(Gate):
    inputId = input()
    idStart, idEnd = map(int, inputId.split())
    idList = list(range(idStart, idEnd + 1))
    x.append(idList)

# print(x[0])


def solve():

    for i in range(len(x)):
        if i == 0:
            matched = set(x[0])
            continue

        tempX = set(x[i])
        # print('++')
        # print(matched)
        # print(tempX)
        matched = set(list(matched & tempX))
        # print(matched)
        if len(matched) == 0:
            #
            return 0

    return len(matched)


ans = solve()
print(ans)

Submission status: Time Limit Exceeded

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

nums=[]
count=[]

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

all_set=set(nums[0])

for i in range(1,m):
    all_set = all_set & set(nums[i])

print(len(all_set))

Submission status: Time Limit Exceeded

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

li = np.array([i for i in range(1,n+1)])
for i in range(m):
  l,r = map(int,input().split())
  li = li[l <= li]
  li = li[li <= r]
  if len(li)==0:
    break
else:
  print(len(li))
  exit()
print(0)

Submission status: Time Limit Exceeded

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

for i in range(M):
  ilist = []
  for j in range(LR[i][1]-LR[i][0]+1):
    ilist.append(LR[i][0]+j)
  elements.append(ilist)
  if i != 0:
    sets = sets & set(elements[i])
  else:
    sets = set(elements[i])

print(len(list(sets)))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import numpy as np

N, M = map(int, sys.stdin.readline().split(" ", 2))

ans = []
for line in sys.stdin:
    l, r = map(int, line.split(" ", 2))
    ans.append(np.array([ 1  if l-1 <= i < r else 0  for i in range(N)] ))
print(sum (1 for a in sum(ans) if a == M))
    # L.append(l)
    # R.append(r)

Submission status: Time Limit Exceeded

Solution code:

def main():
    n, m = map(int, input().split())
    l = []
    r = []
    ans = 0
    for i in range(m):
        a, b = map(int, input().split())
        l.append(a)
        r.append(b)
    for i in range(1, n+1):
        if i >= max(l) and i <= min(r):
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L, R = [], []

for i in range(M):
    l, r = map(int, input().split())
    L.append(l), R.append(r)

ans = 0    
for i in range(N):
    if max(L) <= i+1 <= min(R):
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections


def main():
    # N,M は10^5
    N, M = [int(ele) for ele in input().split()]
    tmp = str(1)*N
    ans = int('0b' + tmp, 2)

    for i in range(M):
        l, r = [int(ele) for ele in sys.stdin.readline().split()]

        secure = int('1' * (r-l+1) + '0' * (N-r), 2)
        ans = ans & secure

    print(bin(ans).count("1"))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

all_lists = [list(range(L[i], R[i] + 1)) for i in range(M)]

new_set = set(all_lists[0])
for i in range(len(all_lists) - 1):
    if len(new_set) == 0:
        break
    new_set = new_set & set(all_lists[i + 1])

print(len(new_set))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int, input().split())
l = [0] * M
r = [0] * M
for i in range(M):
  l[i], r[i] = map(int, input().split())
a=0
for i in range(1,N+1):
  flag=0
  for j in range(M):
    if i<l[j] or i>r[j]:
      flag=1
  if flag==0:
    a+=1
print(a)

Submission status: Time Limit Exceeded

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

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

ans = 0
for i in range(N+1):
    if i in a:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split(' '))
L = []
R = []
for i in range(M):
    l, r = map(int, input().split(' '))
    L.append(l)
    R.append(r)
range_list = [set(range(L[i], R[i]+1)) for i in range(M)]
intersect_list = range_list.pop()
while len(range_list):
    intersect_list = list(set(intersect_list) & range_list.pop())
print(len(intersect_list))

Submission status: Time Limit Exceeded

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

count = 0
ans=[]
for i in range(M):
    l, r = map(int, input().split())
    if count == 0:
        for j in range(l, r+1):
            ans.append(j)
    else:
        tmp = []
        for j in range(l, r+1):
            tmp.append(j)
        ans = list(set(tmp) & set(ans))
    count += 1

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

N, M = input().split()

N = int(N)
M = int(M)

cnt = 0

NM = np.zeros([N])
B = np.zeros([N])

for i in range(M):
    A = input().split()
    B[int(A[0])-1:int(A[1])] = 1
    NM += B
    B = np.zeros([N])


print(np.sum(NM==M))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split(' '))
L_list=[]
R_list=[]

for i in range(M):
    l,r = map(int, input().split(' '))
    L_list.append(l)
    R_list.append(r)

all_list=[]
num_list = range(1,len(L_list))

#Listを足しこむ
cnt = 0
all_uniq = range(L_list[0],R_list[0]+1)
# print(all_uniq)
for i in range(1,M):
    tmp_ran = range(L_list[i],R_list[i]+1)
    all_uniq = set(all_uniq) & set(tmp_ran)

all_uniq = list(all_uniq)
# print(all_uniq)
for i in all_uniq:
    if(i<=N):
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def main():
    N,M= map(int,input().split())
    List=[list(map(int,input().split())) for i in range(M)]
    ListN = list()
    List3 = list()
    for k in range(1,N+1):
        ListN.append(k)
    for i in range(M):
        List2 = list()
        x = List[i][0]
        y = List[i][1]
        for j in range(x,y+1):
            List2.append(j)
        for Li in List2:
            for Ln in ListN:
                if Li == Ln:
                    List2.append(Ln)
    List3 = list(set(List2))
    print(len(List3))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

inu = []
neko = []
for i in range(m):
    x = set(range(res[i][0],res[i][1]+1))
    inu.append(x)

if m == 1:
    print(len(inu[0]))
else:
    for j in range(1,m):
        if j == 1:
            p = (inu[0].intersection(inu[j]))
        else:
            p = p.intersection(inu[j])
    print(len(p))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int, input().split())
l = np.full_like(N,M)
N_i = np.zeros(N)
for i in range(M):
  l_i, r_i = map(int, input().split())
  N_i[l_i-1:r_i] -= 1
l = l + N_i
ans = np.sum(l == 0)
print(ans)

Submission status: Time Limit Exceeded

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


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	result = set()
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlus = { j for j in range(Li + 1, Rs[i - 1] + 1) }
		LiPlus.add(Li)

		if len(result) == 0:
			result = LiPlus
			continue
		result = result & LiPlus

	print(len(result))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

L = []
R = []
rang = []
for i in range(0, M):
    L.append(0)
    R.append(0)
    
for i in range(0, M):
    L[i],R[i] = map(int, input().split())
    
for i in range(0, M):
    rang.append([])
    for j in range(0, R[i] - L[i] + 1):
    	rang[i].append(L[i] + j)

ans = [[]]
ans[0] = rang[0]
for j in range(0, M-1):
    ans.append([])
    
    for i in range(0,len(ans[j])):
        for k in range(0, len(rang[j + 1])):
      
            if ans[j][i] == rang[j+1][k]:
                ans[j+1].append(ans[j][i])
    
print(len(ans[M-1]))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
MC = [1] * N
cnt = list(range(1, N+1))
for i in range(M):
    L, R = map(int, input().split())
    rm_ls = []
    for j in range(len(cnt)):
        if cnt[j] < L or R < cnt[j]:
            rm_ls.append(cnt[j])
    set_c = set(cnt) - set(rm_ls)
    cnt = list(set_c)
print(len(cnt))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
 
for i in range(m):
    s,t = map(int,input().split())
    if i == 0:
        ans2 = [s,t+1]
    try:
        ans2 = [j for j in range(max(min(ans2),s),min(max(ans2)+1,t+1)) if s <= j <= t]
    except:
        break

print(len(ans2))

Submission status: Time Limit Exceeded

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

for i in range(m):
    l,r = map(int,input().split())
    tmp = [i for i in range(l,r+1)]
    if i == 0:
        kyotu = tmp
    else:
        kyotu = list(set(tmp)&set(kyotu))
        if len(kyotu)==0:
            break

        # print(kyotu)

print (len(kyotu))

Submission status: Time Limit Exceeded

Solution code:
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i)-1 for i in input().split(' ')]
a=[0]*n
for i in range(m):
    for j in range(l[i],r[i]+1):
        a[j]+=1
print(len([i for i in a if i==m]))

Submission status: Time Limit Exceeded

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

l = [0]*100000
r = [0]*100000

for i in range(0,m):
    l[i],r[i] = map(int,input().split())

count = 0
num = 0

for key in range(1,n + 1):#鍵の番号
    for i in range(0,m):#
        if(key < l[i]):
            break
        if(key >= l[i] and key <= r[i]):
            count += 1
    if(count == m):
        num += 1
    count = 0
        
print(num)

Submission status: Time Limit Exceeded

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

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

    for j in range(b-a+1):
        y.append(c)
        c+=1
    
    x.append(y)

if m==1:
    print(b-a+1)
else:
    kyoutu=set(x[0])& set(x[1])
    for k in range(2,m):
        kyoutu=set(kyoutu) & set(x[k])

    print(len(kyoutu))

Submission status: Time Limit Exceeded

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

for i in range(m):
    s,t = map(int,input().split())
    if i == 0:
        ans2 = [s,t+1]
    try:
        ans2 = [j for j in range(max(min(ans2),s),min(max(ans2)+1,t+1)) if s <= j <= t]
    except:
        break


print(len(ans2))

Submission status: Time Limit Exceeded

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

all_lists = [list(range(L[i], R[i] + 1)) for i in range(M)]

new_set = set(all_lists[0])
for i in range(len(all_lists) - 1):
    new_set = new_set & set(all_lists[i + 1])

print(len(new_set))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(lambda x: int(x), input().split(' '))
l = []
r = []
for i in range(m):
    l_tmp, r_tmp = map(lambda x: int(x), input().split(' '))
    l.append(l_tmp)
    r.append(r_tmp)

result = 0
card_id = l[0]
for j in range(n):
    for locked_l, locked_r in zip(l, r):
        if not (locked_l <= card_id + j <= locked_r):
            break
    else:   
        result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
table = []
for i in range(m):
    nest_table = []
    l, r = map(int, input().split())
    nest_table.append(l)
    nest_table.append(r)
    table.append(nest_table)

ans = 0 
for j in range(n):
    for k in range(m):
        frag = 0
        if j+1 >= table[k][0] and j+1 <= table[k][1]:
            pass
        else:
            frag += 1
            break
    if frag == 0:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	result = set()
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlus = [j for j in range(Li + 1, Rs[i - 1] + 1)]
		if len(result) == 0:
			result = set([Li] + LiPlus)
			continue
		result = result & set([Li] + LiPlus)

	print(len(result))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys

N, M = map(int, sys.stdin.readline().split(" ", 2))

L = []
R = []

ans = { i:0 for i in range(N)}
anss = []
for line in sys.stdin:
    l, r = map(int, line.split(" ", 2))
    for i in range(l, r+1):
        ans[i-1] += 1
print(sum(1 for a in ans.values() if a == M))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

x = []  
for _ in range(M):
  L,R = map(int,input().split())
  x.extend(list(range(L,R+1)))
    
c = collections.Counter(x)

print(len([i[0] for i in c.items() if i[1] == M]))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
t_list = [[0] * 2 for _ in range(M)]
for i in range(M):
  t_list[i] = list(map(int, input().split()))
ans = 0
a = [j for j in range(t_list[0][0], t_list[0][1]+1)]
for i in range(M):
  b = [j for j in range(t_list[i][0], t_list[i][1]+1)]
  a = list(set(a) & set(b))
ans = len(set(a))
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(m):
    x, y = map(int, input().split())
    array_set = set(list(range(x, y + 1)))
    if i != 0:
        ans = array_set & ans
    else:
        ans = array_set
    tmp = array_set


print(len(ans))

Submission status: Time Limit Exceeded

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

if M == 1:
    l1, r1 = map(int, input().split())
    print(r1 - l1 + 1)
else:
    C = 0
    
    l1, r1 = map(int, input().split())
    for _ in range(M - 1):
        l2, r2 = map(int, input().split())

        A = list(range(l1, r1 + 1))
        B = list(range(l2, r2 + 1))

        C = set(A) & set(B)
    print(len(C))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, raw_input().split())
a_list = []

for i in range(M):
    a_list.append(map(int, raw_input().split()))
    tmp_set = set(j for j in range(a_list[i][0], a_list[i][-1]+1))
    if i == 0:
        si = tmp_set
    else:
        si = si & tmp_set

print len(si)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
a, b = map(int, input().split())
d = list(range(a, b+1))
for i in range(m-1):
    a, b = map(int, input().split())
    src_set = set(d)
    tag_set = set(list(range(a, b+1)))
    d = list(src_set & tag_set)
print(len(d))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

counts = np.zeros(N)

for i in range(M):
    for j in np.arange(LR[i][0], LR[i][1]+1):
        counts[j-1] += 1

print(len(np.where(counts==M)[0]))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L, R = [], []
for i in range(M):
    l, r = map(int, input().split())
    L.append(l), R.append(r)

s = []
for i in range(M):
    s.append([i for i in range(L[i], R[i]+1)])

a = s[0]
for i in s:
    a = set(a) & set(i)

ans = 0
for i in range(N+1):
    if i in a:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	result = set()
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlus = [j for j in range(Li + 1, Rs[i - 1] + 1)]
		if len(result) == 0:
			result = set([Li] + LiPlus)
		result = result & set([Li] + LiPlus)

	print(len(result))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))
b = [] * M
r = []
for i in range(M):
    x, y = map(int, input().split())
    g = list(range(x, y + 1))
    b.append(g)
# print(b)
for i in range(M - 1):
    r = list(set(b[i]) & set(b[i + 1]))
    # print(r)
    b[i + 1] = r
if M >= 2:
    print(len(r))

else:
    print(len(b[0]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = map(int,input().split())
L = [0] * M
X = [0] * M
for i in range(M):
    L[i], X[i] = map(int, input().split())
ans= np.zeros(N,dtype = np.int32)
for k in range(M):
  ans[L[k]-1:X[k]] += 1
ans_new = np.where(ans == M, 1, 0)
print(np.sum(ans_new))

Submission status: Time Limit Exceeded

Solution code:
n, m=map(int, input().split())
grid = []
for i in range(m):
    array = list(map(int, input().strip().split()))
    grid.append(array)

x=[]
for i in range(m):
    for j in range(grid[i][1]-grid[i][0]+1):
        x.append(grid[i][0]+j)

ans = [i for i in set(x) if x.count(i) == m]

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
#a, b = map(int, input().split())
#a, b, c, x = map(int, [input() for i in range(4)])

N, M = map(int, input().split())
L = []
R = []
LR_and = []
sum_list = []

for i in range(M):
    LR = input().split()
    L.append(int(LR[0]))
    R.append(int(LR[1]))

for i in range(M):
    for j in range(R[i] - L[i] + 1):
        LR_and.append(L[i] + j)

for i in range(len(LR_and)):
    if LR_and.count(LR_and[i]) == M:
        sum_list.append(str(LR_and[i]))

final_list = list(set(sum_list))

print(len(final_list))

Submission status: Time Limit Exceeded

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

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

    for j in range(b-a+1):
        y.append(c)
        c+=1
    
    x.append(y)

if m==1:
    print(b-a+1)
else:
    kyoutu=set(x[0])& set(x[1])
    for k in range(2,m):
        kyoutu=set(kyoutu) & set(x[k])

    print(len(kyoutu))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l_list=[]
r_list=[]
for i in range(1, m + 1):
    l_temp, r_temp = map(int, input().split())
    l_list.append(l_temp)
    r_list.append(r_temp)

cards = [i for i in range(1, n + 1)]
for i in range(1, m + 1):
    cards = [card for card in cards if l_list[i-1] <= card <= r_list[i-1]]
print(len(cards))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int, input().split(" "))
A = np.arange(1, N + 1)
for m in range(M):
    L, R = map(int, input().split(" "))
    A = A[L <= A]
    A = A[A <= R]
print(len(A))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    x, y = [int(i) for i in input().split()]
    L.append(x)
    R.append(y)

id = []
for i in range(M):
    id += [L[i:] + R[:i + 1]]
com = set(id[0])
for j in range(1, M):
    com = com & set(id[j])
com_max = max(com)
com_min = min(com)
print(com_max - com_min + 1)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
gates = [list(map(int, input().split())) for i in range(m)]
ids = [i for i in range(1, n+1)]

for i in range(0, m):
    ids = list(filter(lambda x: x >= gates[i][0], ids))
    ids = list(filter(lambda x: x <= gates[i][1], ids))

print(len(ids))

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
ans =[0]*N
for i in range (M):
    L,R = [int(x) for x in input().split()]
    ans[L-1:R] = map(lambda x: x+1, ans[L-1:R] )



       

print(ans.count(M))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
X = []
for i in range(M):
  L,R = map(int,input().split())
  for j in range (R-L+1):
    X.append(L + j)
X_duplicate = [x for x in set(X) if X.count(x) > M-1]
print(len(X_duplicate))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
list_append = list(range(1,N+1,1))
for i in range(M):
    L, R = list(map(int, input().split()))
    list_append += list(range(L,R+1,1))
    list_append = [x for x in set(list_append) if list_append.count(x) > 1]
print(len(list_append))

Submission status: Time Limit Exceeded

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

all_lists = [list(range(L[i], R[i] + 1)) for i in range(M)]

def get_intersect(*arg):
    
    new_set = set(arg[0][0])
    for i in range(len(arg[0]) - 1):
        new_set = new_set & set(arg[0][i + 1])
    return new_set

print(len(get_intersect(all_lists)))

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
N,M = [int(x) for x in input().split()]
ans =[0]*N
for i in range (M):
    L,R = [int(x) for x in input().split()]
    ans[L-1:R] = map(lambda x: x+1, ans[L-1:R] )

count = 0
for i in ans:
    if i == M :
        count+= 1

       

print(count)

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
ans=range(1,N+1)
l=[]
r=[]
for x in range(M):
    a,b=(int(_) for _ in input().split())
    l.append(a)
    r.append(b)
for x in range(M):
    aa=list(range(l[x],r[x]+1))
    ans=set(ans)&set(aa)
print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

counts = np.zeros(N)

for i in range(M):
    for j in range(LR[i][0], LR[i][1]+1):
        counts[j-1] += 1

print(len(np.where(counts==M)[0]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
d = []
for i in range(m):
    a, b = map(int, input().split())
    d.append(list(range(a, b+1)))

b = d[0]
for i in d:
    src_set = set(b)
    tag_set = set(i)
    matched_list = list(src_set & tag_set)

print(len(matched_list))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
lr = []
dif = []
all_dif = []
for i in range(m):
    lr.append(list(map(int, input().split())))

for i in range(m):
    for j in range(lr[i][0], lr[i][1]+1, 1):
        dif.append(j)
    all_dif.append(dif)
    dif = []

cnt = 0
ans = 0
for c in all_dif[0]:
    for i in range(1, len(all_dif), 1):
        if c in all_dif[i]:
                cnt += 1

    if cnt+1 == m:
        ans += 1

    cnt = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, m = map(int, input().split())
l = []
r = []
pp = np.zeros(n)
for i in range(m):
    tml, tmr = map(int, input().split())
    for i in range(tml-1, tmr):
        pp[i] += 1

print(np.sum(pp == m))

Submission status: Time Limit Exceeded

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

from collections import defaultdict


def main():
	Ls = []
	cardDic = defaultdict(int)
	Rs = []
	for i in range(M):
		inputs= list(map(int,input().split()))
		Ls.append(inputs[0])
		cardDic[inputs[0]] += 1
		Rs.append(inputs[1])
		cardDic[inputs[1]] += 1

	result = []
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlusOne = Li + 1
		LiPlus = [j for j in range(Li + 1, Rs[i - 1] + 1)]

		res = set([Li] + LiPlus)

		if len(result) == 0:
			result.extend(list(res))
		result = list(set(result) & res)

	print(len(result))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
 
l = [""] * m
ans2 = [1,n]

for i in range(m):
    s,t = map(int,input().split())
    try:
        ans2 = [j for j in range(min(ans2),max(ans2)+1) if s <= j <= t]
    except:
        break


print(len(ans2))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys

N, M = map(int, sys.stdin.readline().split(" ", 2))

L = []
R = []

ans = [0] * N
for line in sys.stdin:
    l, r = map(int, line.split(" ", 2))
    for i in range(l, r+1):
        ans[i-1] += 1
print(sum(1 for a in ans if a == M))

Submission status: Time Limit Exceeded

Solution code:
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i)-1 for i in input().split(' ')]
a=[set() for i in range(n)]
for i in range(m):
    for j in range(l[i],r[i]+1):
        a[j].add(i)
a=[(index,i) for index,i in enumerate(a)]
a.sort(key=lambda i:len(i[1]),reverse=True)
s={i for i in range(m)}
c=0
for k,v in a:
    t=s-v
    if len(t)==0:
        c+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
l,r = [],[]
for i in range(m):
    x,y = map(int,input().split())
    l.append(x)
    r.append(y)
cnt = 0
for i in range(1,n+1):
    for j in range(m):
        if l[j]>i or r[j]<i:
            break
    else:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(m):
    array = list(map(int, input().strip().split()))
    array_set = set(list(range(array[0], array[1] + 1)))
    if i != 0:
        
        ans = array_set & ans
    else:
        ans = array_set
    tmp = array_set


print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

N, M = (int(i) for i in input().split())
l = list(range(N+1))

while 1:
	try:
		L1, R1 = (int(i) for i in input().split())
		for i in l:
			if(i >= L1 and i <= R1):
				pass
			else:
				l[i]=0
	except EOFError:
		break
print(np.count_nonzero(l))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
o = []
for l in range(M):
    L,R = map(int,input().split())
    n1 = []
    for j in range(L,R+1):
        n1.append(j)
    if o:
        n2 = []
        for k1 in range(len(o)):
            for k2 in range(len(n1)):
                if o[k1] == n1[k2]:
                    n2.append(o[k1])
                elif o[-1] < n1[k2]:
                    continue
        n1 = n2
    o = n1

print(len(o))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
X = []
for i in range(M):
  L,R = map(int,input().split())
  for j in range (R-L+1):
    A = L + j
    X.append(A)
X_duplicate = [x for x in set(X) if X.count(x) > M-1]
print(len(X_duplicate))

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
L_R = list()

mini = 1000000
maxi = 0
A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
if A < mini:
    mini = A
if B > maxi:
    maxi = B
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,B])
    if A < mini:
        mini = A
    if B > maxi:
        maxi = B

#mini = 1000000
#for i in range(len(L_R)):
#    if L_R[i][0] < mini:
#        mini = L_R[i][0]
#maxi = 0
#for i in range(len(L_R)):
#    if L_R[i][1] > maxi:
#        maxi = L_R[i][1]

num = 0
for x in range(mini,maxi+1):
    n = 0
    for i in range(1,M+1):
        if L_R[i][0] <= x <= L_R[i][1]:
            n += 1
    if n == M:
        num += 1
    else:
        pass
print(num)

Submission status: Time Limit Exceeded

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

#Nのリスト
num=set()
for i in range(N):
    num.add(i+1)

for m in range(M):
    L,R=map(int,input().split())

    num_=num.copy()
    for n in num_:
        if n>=L and n<=R:
            #clear
            pass
        else :
            #out
            num.remove(n)
            
print(len(num))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L, R = map(int, input().split())
c = [j for j in range(L, R+1)]

for i in range(0, M-1):
  L, R = map(int, input().split())
  n = [j for j in range(L, R+1)]
  c = list(set(c) & set(n))
  if len(c) == 0:
    break

print(len(c))