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: Wrong Answer

Solution code:
N, M = map(int, input().split())
L, R = map(int, input().split())
for _ in range(M-1):
    l, r = map(int, input().split())
    if L < l:
        L = l
    if r < R:
        R = r
print(R-L+1)

Submission status: Wrong Answer

Solution code:
n, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
l_max = max(x[0] for x in X)
r_min = min(x[1] for x in X)
print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
L=0
R=N
for i in range(M):
    l,r=map(int,input().split())
    L=max(L,l)
    R=min(R,r)
print(R-L+1)

Submission status: Wrong Answer

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

ansL = 0
ansR = float('inf')
for _ in range(M):
  l, r = map(int, input().split())
  ansL = ansL if ansL > l else l
  ansR = ansR if ansR < r else r

print(ansR - ansL + 1)

Submission status: Wrong Answer

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

l = 1
r = N
for _ in range(M):
    L, R = map(int, input().split())
    if L > l:
        l = L
    if R < r:
        r = R

print(r - l + 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lr_list = [list(map(int, input().split())) for _ in range(m)]
max_val, min_val = n, 1
for l, r in lr_list:
    if l > min_val:
        min_val = l
    if r < max_val:
        max_val = r
print(max_val - min_val + 1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)

N,M = map(int,input().split())
maxL = 0
minR = N+1
for _ in range(M):
    l,r = map(int,input().split())
    maxL = max(maxL,l)
    minR = min(minR,r)
ans = minR - maxL + 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l = []
r = []
for _ in range(m):
    a,b = map(int,input().split())
    l.append(a)
    r.append(b)
print(min(r)-max(l)+1)

Submission status: Wrong Answer

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

max_left = 1
min_right = n

for i in range(m):
    left, right = map(int, input().split())
    max_left = max(max_left, left)
    min_right = min(min_right, right)

print(min_right - max_left + 1)

Submission status: Wrong Answer

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

l = -1
r = float("inf")

for x,y in A :
    if l < x :
        l = x
    if y < r :
        r = y
print(r-l+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=[]
l=[]
r=[]
for i in range(m):
    a.append(list(map(int,input().split())))
    l.append(a[i][0])
    r.append(a[i][1])
ans=min(r)-max(l)+1
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

def main():
    N, M = map(int, input().split())

    max_l = 0
    min_r = 10 ** 6
    for _ in range(M):
        l, r = map(int, input().split())
        max_l = max(max_l, l)
        min_r = min(min_r, r)

    print(min_r - max_l + 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l_max = 0
r_min = 10**5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)
print(r_min - l_max + 1)

Submission status: Wrong Answer

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

min = 1
max = N
for i in range(M):
	l, r = map(int, input().split(" "))
	if l > min:
		min = l
	if r < max:
		max = r

print(max - min + 1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
left = 1
right = N
for _ in range(M):
    l, r = list(map(int, input().split()))
    left = max(left, l)
    right = min(right, r)

print(right - left + 1)

Submission status: Wrong Answer

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

    A.append(l)
    B.append(r)
print(min(B)-max(A)+1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import math
sys.setrecursionlimit(20000000)
#input = sys.stdin.readline
n,m = map(int,input().split())
x = []
LE = 0
RI = n
for i in range(m):
    a,b = map(int,input().split())
    LE = max(LE,a)
    RI = min(RI,b)
print(RI-LE+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
range_l = 1
range_r = N
for i in range(M):
  L,R = map(int,input().split())
  range_l = max(range_l, L)
  range_r = min(range_r, R)
if range_r - range_l < 0:
  print(0)
else:
  print(range_l - range_r + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
minR = N + 1
maxL = 0
for _ in range(M):
    l, r = map(int, input().split())
    minR = min(r, minR)
    maxL = max(l, maxL)

print(max(0, minR-maxL+1))
print(minR, maxL)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a,b=0,n-1
for i in range(m):
  l,r=map(int,input().split())
  a=max(a,l);b=min(b,r)
print(max(0,b-a+1))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
bottom = 0
up = 0
for i in range(m):
    l,r = map(int,input().split())
    if i == 0:
        bottom = l
        up = r
    else:
        if bottom < l:
            bottom = l
        if up > r:
            up = r

print(up-bottom+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ll = 1
rr = n
for i in range(m):
  l, r = map(int, input().split())
  ll = max([ll, l])
  rr = min([rr, r])

print(rr-ll+1)

Submission status: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split()]
L, R = 0, N + 10
for l, r in [[int(i) for i in input().split()] for _ in range(M)]:
    L = max(L, l)
    R = min(R, r)
print(R - L + 1)

Submission status: Wrong Answer

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

l_max, r_min = -1, 100000
for _ in range(M):
    L, R = list(map(int, input().split()))
    if l_max < L:
        l_max = L
    if r_min > R:
        r_min = R

print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)

# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())

def solve():
    l_list = []
    r_list = []
    n, m = map(int, input().split())
    for i in range(m):
        l, r = map(int, input().split())
        l_list.append(l)
        r_list.append(r)

    max_l = max(l_list)
    min_r = min(r_list)
    print(min_r - max_l + 1)

solve()

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
l = 1
r = n
for i in range(m):
  ll, rr = list(map(int, input().split()))
  l = max(l, ll)
  r = min(r, rr)
print(r-l+1)

Submission status: Wrong Answer

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


l_max = 1
r_min = n
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L, R = 0, N
for i in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)

print(R - L + 1)

Submission status: Wrong Answer

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

x = 0
y = N
for i in range(M):
    L, R = map(int, input().split())
    x = max(x, L)
    y = min(y, R)

print (y-x+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
L=[0 for i in range(M)]
R=[0 for i in range(M)]
for i in range(M):
    L[i],R[i]=map(int,input().split())
    L[i]-=1;R[i]-=1
inf=0
sup=N-1
for i in range(M):
    inf=max(inf,L[i])
    sup=min(sup,R[i])
if L[i]>R[i]:
    print(0)
else:
    print(sup-inf+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l =[]
r = []
for i in range(m):
    la,ra = map(int,input().split())
    l.append(la)
    r.append(ra)
print( min(r)-max(l)+1)

Submission status: Time Limit Exceeded

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

p = [0] * (10**5)

for y in gate:
  p[y[0]-1:y[1]] = list(map(lambda x:x+1, p[y[0]-1:y[1]]))
  
cnt = 0

  
for z in range(n):
  if p[z] == m:
    cnt += 1
    
print(cnt)

Submission status: Time Limit Exceeded

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


count = 0

for x in range(1,n+1):
  for y in gate:
    if x < y[0] or y[1] < x:
      break
  else:
    count += 1
    
print(count)

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 &= set(range(l,r+1))
print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
arr = []
for _ in range(m):
    x, y = map(int, input().split())
    arr.append([i for i in range(x, y + 1)])
ans = arr[0]
for ar in arr:
    tmp = []
    for ele in ar:
        if ele in ans:
            tmp.append(ele)
    ans = tmp
print(len(ans))

Submission status: Time Limit Exceeded

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

kyotu_s = {k for k in range(1,n+1)}

for i in range(m):
    l,r = map(int,input().split())
    s = {j for j in range(l,r+1)}
    kyotu_s = s & kyotu_s
    if len(kyotu_s)==0:
        break

print(len(kyotu_s))

Submission status: Time Limit Exceeded

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

kyotu_s = {k for k in range(1,n+1)}

for i in range(m):
    l,r = map(int,input().split())
    s = {j for j in range(l,r+1)}
    kyotu_s = s & kyotu_s

print(len(kyotu_s))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
L,R=map(int,input().split())
ans=list(range(L,R+1))
for i in range(M-1):
    H,I=map(int,input().split())
    ans=list(filter(lambda key:H<=key<=I,ans))
    if ans==[]:
        break
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
s = set(list(range(l, r+1)))
for i in range(1, M):
  l, r = map(int, input().split())
  s1 = set(list(range(l, r+1)))
  s = s & s1
print(len(s))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L, R = [0]*M, [0]*M
A = set(range(N))
for i in range(M):
    L[i], R[i] = map(int, input().split())
    A = A & set(range(L[i]-1, R[i]))
print(len(A))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=lambda :sys.stdin.readline().rstrip()
n, m = map(int, input().split())

key = [0]*n
for _ in range(m):
    l, r = map(int, input().split())
    for i in range(l-1, r):
        key[i] = key[i]+1
print(len([i for i in key if i == m]))

Submission status: Time Limit Exceeded

Solution code:
card_num, gate_num = map(int, input().split())
table = [0] * card_num
count = 0


for i in range(gate_num):
    start_num, end_num = map(int, input().split())
    for j in range(start_num-1, end_num):
        table[j] += 1

for l in range(card_num):
    if table[l] == gate_num:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
card_num, gate_num = map(int, input().split())
table = []
count = 0
for k in range(card_num):
    table.append(0)

for i in range(gate_num):
    start_num, end_num = map(int, input().split())
    for j in range(start_num-1, end_num):
        table[j] += 1

for l in range(card_num):
    if table[l] == gate_num:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
A = set(range(1,n+1))

for _ in range(m):
  l,r = map(int,input().split())
  S = set(range(l,r+1))
  A = A & S
print(len(A))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int, input().split())
ans=set()
for i in range(m):
    l,r=map(int, input().split())
    if i==0:
        ans=set(range(l,r+1))
    else:
        tmp=set(range(l,r+1))
        ans=ans & tmp

print(len(ans))

Submission status: Time Limit Exceeded

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


L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

counter = 0
for j in range(min(L_list),(max(R_list) + 1)):
    if j >= max(L_list) and j <= min(R_list):
        counter += 1

print(counter)

Submission status: Time Limit Exceeded

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

l = [0] * N

for i in range(M):
    A, B = map(int, input().split())
    for j in range(A-1,B):
        l[j] += 1

ans = [k for k in l if k >= M]
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
a,b = map(int, input().split())
former_set = { i for i in range(a,b+1)}
for i in range(M-1):
  a,b =  map(int, input().split())
  tmp_set = { i for i in range(a,b+1)}
  former_set = tmp_set & former_set
print(len(former_set))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
former_set = set()
for i in range(M):
  a,b =  map(int, input().split())
  if i != 0: 
    tmp_set = { i for i in range(a,b+1)}
    former_set = tmp_set & former_set
  else:
    former_set = { i for i in range(a,b+1)}
print(len(former_set))

Submission status: Time Limit Exceeded

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

ans = set()

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

else:
    print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    gates={}
    s={}
    n,m = map(int,input().split())
    gates=[list(map(int,input().split())) for _ in range(m)]

    for gate in gates:
        if len(s)==0:
            s = set([i for i in range(gate[0],gate[1]+1)])
        else:
            s = s & set([i for i in range(gate[0],gate[1]+1)])
    print(len(s))

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    gates=[]
    n,m = map(int,input().split())
    gates=[list(map(int,input().split())) for _ in range(m)]
    gate_dict={}

    for gate in gates:
        for i in range(min(gate),max(gate)+1):
            if i not in gate_dict:
                gate_dict[i]=1
            else:
                tmp=gate_dict[i]
                tmp+=1
                gate_dict[i]=tmp
    answers=[k for k, v in gate_dict.items() if v == m]
    print(len(answers))

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
gates = []
res = 0
for _ in range(M):
  L, R = map(int, input().split())
  gates.append((L, R))

for i in range(1, N+1):
  flag = True
  for g in gates:
    if g[0] <= i <= g[1]:
      continue
    else:
      flag = False
      break 
  if flag: res+=1
  
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))

ans=[]
ha1=[]
ha = [k for i in range(e2) for k in range(1,e1+1) if h[i][0]<=k<=h[i][1]]
sha=sorted(ha)

ma=0

count=1
#最大値求める
for i in range(len(sha)-1):
    if sha[i]==sha[i+1]:
        count+=1
    else:
        if count>=e2:
            ha1.append(count)
        count=1

if count>=e2:
    ha1.append(count)
print(len(ha1))



# for i in range(1,e1+1):
#     count=0
#     for k in range(e2):
#         if h[k][0]<=i<=h[k][1]:
#             count+=1
#     if count==e2:
#         ans.append(count)

# print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))

ans=[]

for i in range(1,e1+1):
    count=0
    for k in range(e2):
        if h[k][0]<=i<=h[k][1]:
            count+=1
    if count==e2:
        ans.append(count)

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = map(int, input().split())
ans = None
for i in range(m):
    l, r = map(int, input().split())
    card = set(range(l, r + 1))
    if ans is None:
        ans = card
    else:
        ans = ans.intersection(card)

print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

    ans = N
    for i in range(N):
        for j in range(M):
            c = 0
            if not L[j] <= i + 1 <= R[j]:
                ans -= 1
                c = 1
            if c == 1:
                break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

    c = [0] * (N + 1)

    for i in range(M):
        for j in range(1, L[i]):
            c[j] += 1
        for j in range(R[i] + 1, N + 1):
            c[j] += 1

    print(c.count(0) - 1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

    g = {}
    for i in range(1, N + 1):
        g[i] = 0

    for i in range(M):
        for j in range(L[i], R[i] + 1):
            g[j] += 1

    ans = 0
    for i in range(1, N + 1):
        if g[i] == M:
            ans += 1
    print(ans)

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())

keys = list(range(1, N+1))
common = [x for x in keys if x>=max(L) and x<=min(R)]
    
print(len(common))

Submission status: Time Limit Exceeded

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

L=[0]*M
R=[0]*M
common = set(range(1, N+1))
for i in range(M):
  L, R = map(int, input().split())
  common = common & set(range(L,R+1))
print(len(common))

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())

common = set(range(1, N+1))
for i in range(M):
  common = common & set(range(L[i],R[i]+1))

print(len(common))

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())

keys = []

for i in range(M):
  keys.append(set(range(L[i],R[i]+1)))
  
common = set(range(1, N+1))
for i in range(M):
  common = common & keys[i]
    
print(len(common))

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())

keys = []

for i in range(M):
  keys.append(range(L[i],R[i]+1))
  
gcount = 0
for j in range(1,N+1):
  count = 0
  for i in range(M):
    if j in keys[i]:
      count+=1
      
  if count == M:
    gcount +=1
    
print(gcount)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m =map(int,input().split())
a=[1]*n
b=[0]*n
for i in range(m):
  l,r=map(int,input().split())
  if l==1:
    a[r:]=b[r:]
  else:
    a[:l-1]=b[:l-1]
    a[r:]=b[r:]
print(sum(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
List = []
for i in range (M):
  List.append(list(map(int, input().split())))
resList = [1]*N
for i in range(M):
  if List[i][0] > 1:
    for j in range(0,List[i][0]-1):
      if resList[j] == 1:
        resList[j] = 0
  if List[i][1] < N:
    for k in range(List[i][1],N):
      if resList[k] == 1:
        resList[k] = 0
print(resList.count(1))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
List = []
for i in range (M):
  List.append(list(map(int, input().split())))
resList = [0]*N
for i in range(M):
  l = List[i][0]
  r = List[i][1]
  for j in range(l,r+1):
    resList[j-1] += 1
print(resList.count(M))

Submission status: Time Limit Exceeded

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

for m in range(M):
  for g in range(X[m][0],X[m][1]+1):
    L[g-1]+=1
    
print(L.count(M))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
set1 = set([x for x in range(1, N+1)])
for i in range(M):
  L, R = map(int, input().split())
  lr = set([x for x in range(L, R+1)])
  set1 =set1 & lr
print(len(set1))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ss = set(x for x in range(1,N+1))
for i in range(M):
  
  L, R = map(int, input().split())
  st = set(x for x in range(L, R+1))
  ss = ss & st
print(len(ss))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
list1 = []
ss = set(x for x in range(1,N+1))
for i in range(M):
  ar = []
  L, R = map(int, input().split())
  st = set(x for x in range(L, R+1))
  list1.append(st)
for s in list1:
  ss = ss & s
print(len(ss))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, m = map(int, input().split())
    lr_lst = [list(map(int, input().split())) for _ in range(m)]

    ans = 0

    for i in range(1, n + 1):
        is_able = True
        for j in range(m):
            if not lr_lst[j][0] <= i <= lr_lst[j][1]:
                is_able = False
                break
        if is_able:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

d = [0]*n
for i in range(m):
  for j in range(lr[i][0]-1, lr[i][1]):
    d[j] += 1
    
print(d.count(m))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
s = input().split()
n=int(s[0])
m=int(s[1])
d={}
for i in range(1,n+1):d[i]=0
for i in range(m):
    s = list(map(int,input().split()))
    for j in range(s[0],s[1]+1):
        d[j]+=1
print(list(d.values()).count(m))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

passable = [1]*(N+1)

for l,r in LR:
    for i in range(l):
        passable[i]=0
    for i in range(r+1, N+1):
        passable[i]=0

print(passable.count(1))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
lr = []
for i in range(m):
    l,r = map(int, input().split())
    lr.append((l,r))
num = 0
for h in range(1,n+1):
    t = 0
    for j in lr:
        if j[0] <= h <= j[1]:
            t += 1
        else:
            break
    if t == m:
        num += 1
print(num)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n,m = map(int, input().split())
lr = []
for i in range(m):
    l,r = map(int, input().split())
    lr += list(range(l,r+1))
lr = Counter(lr).most_common()
num = 0
for h in range(len(lr)):
    if lr[h][1] == m:
        num += 1
    else:
        break
print(num)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
L = [0]*m; R = [0]*m; cnt = 0
for i in range(m):
    L[i],R[i] = map(int,input().split())
for i in range(1, n+1):
    if max(L) <= i <= min(R):
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lr = []; cnt = 0
for i in range(m):
    l,r = map(int,input().split())
    lr.append(list(range(l,r+1)))
for i in range(1, n+1):
    check = 0
    for j in range(m):
        if i in lr[j]:
            check += 1
    if check == m:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = set(range(1,N+1))
for _ in range(M):
    a,b = map(int,input().split())
    filset = set(range(a,b+1))
    
    ans = ans & filset

print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

card = list(range(1, n+1))
delete = set()

for i in range(m):
  l,r = map(int, input().split())
  delete.update(card[:(l-1)])
  delete.update(card[r:])

card = set(card)
print(len(card^delete))

Submission status: Time Limit Exceeded

Solution code:
def solve():
    import sys
    n, m, *LR = map(int, sys.stdin.read().split())
    L = LR[::2]
    R = LR[1::2]
    res = set(range(1, n + 1))
    for l, r in zip(L, R):
        x = set(range(l, r + 1))
        res &= x
    print(len(res))


solve()

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
import numpy as np
l,r = map(int,input().split())
li = np.arange(l,r+1)
for i in range(m-1):
    l,r = map(int,input().split())
    li = set(li)& set(np.arange(l,r+1))
print(len(li))

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 127
# C - Prison
import numpy as np


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

cards=[i for i in range (1,N+1)]
C=np.array(cards)

for i in range (M):
    r,l = map(int,input().split())
    C=np.where(C<r,0,C)
    C=np.where(C>l,0,C)

print(np.count_nonzero(C))