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:
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 = lr[0]
for e in lr[1:]:
    passed = passed & e

print(len(passed))

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)
count=0
for i in range(1,N+1):
  if max(L)<=i<=min(R):
    count+=1
print(count)

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)
count=0
for i in range(1,N+1):
  for j in range(M):
    if L[j]>i or i>R[j]:
      break
  else:
    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N,M=map(int,input().split())
    l, r = map(int, input().split())
    s = {i for i in range(l, r + 1)}
    for i in range(1,M):
        l,r=map(int,input().split())
        s=s&{i for i in range(l,r+1)}

    print(len(s))


resolve()

Submission status: Time Limit Exceeded

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

s=[0]*n

for i in range(m):
    l,r=map(int,input().split())
    x=l
    while x<=r:
        s[x-1]+=1
        x+=1
        
ans=[str(i) for i in s] 
print(ans.count(str(m)))

Submission status: Time Limit Exceeded

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

print(l.count(M))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
array = [list(map(int, input().split())) for i in range(m)]
result = set([i for i in range(1, n+1)])
for el in array:
    L, R = el[0], el[1]
    work = set([i for i in range(L, R+1)])
    result = work & result
print(len(result))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
A = set(n for n in range(1,N+1))

for m in range(M):
  L,R = map(int,input().split())
  B = set(n for n in range(L,R+1))
  A = A&B

print(len(A))

Submission status: Time Limit Exceeded

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


card_list = np.zeros(n)

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

print(np.count_nonzero(card_list == m))

Submission status: Time Limit Exceeded

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep = ' ', end = '\n'):
    print(*var, file=sys.stderr, end = end, sep = sep)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, m = I()
a = [0] * (n + 10)
for i in range(m):
    x, y = I()
    a[x] += 1
    a[y + 1] -= 1
ans = 0
for i in range(1, n + 10):
    a[i] = a[i] + a[i - 1]
    if a[i] == m:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[]
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    tes=set(range(x,y+1))
    te=te&tes
print(len(te))

Submission status: Time Limit Exceeded

Solution code:
import collections
N,M=map(int,input().split())
L=[]
R=[]
for i in range(M):
  l,r=[int(i) for i in input().split()]
  L.append(l)
  R.append(r)
LR=[]
for i in range(M):
  for j in range(L[i],R[i]+1):
    LR.append(j)
LR=collections.Counter(LR)
LR=LR.most_common()
LR=dict(LR)
cnt=0
for i in list(LR.values()):
  if i==M:
    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

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

res = [x for x in set(d) if d.count(x) == M]
print(len(res))

Submission status: Time Limit Exceeded

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

print(cnt.count(m))

Submission status: Time Limit Exceeded

Solution code:
#127_C
n, m = map(int, input().split())
s = set(range(1,n+1))

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

print(len(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = map(int,input().split(" "))
ID = np.array([0]*N)

for _ in range(M):
    l = np.array([0]*N)
    L,R = map(int,input().split(" "))
    l[L-1:R] = 1
    ID+=l

print(len(ID[ID==M]))

Submission status: Time Limit Exceeded

Solution code:
n = list(map(int, input().split()))
N = n[0]
M = n[1]
X = []
Y = []
for i in range(M):
    a = list(map(int, input().split()))
    X.append(a[0])
    Y.append(a[1])
v=0
w=0
z=[X[0],Y[0]]
while v==0:
    for i in range(1,M):
        if z[0]<=Y[i] and z[1]>=X[i]:
            z[0]=max(z[0],X[i])
            z[1]=min(z[1],Y[i])
        else:
            v=1
            w=1
        if i==M-1:
            v=1
if w==0:
    print(z[1]-z[0]+1)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
card = False
for _ in range(M):
    L, R = map(int, input().split())
    if card is not False:
        card = card & set([i for i in range(L, R + 1)])
    else:
        card = set([i for i in range(L, R + 1)])
print(len(card))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,m=map(int,input().split())
A=np.ones(n)
for i in range(m):
    L,R=map(int,input().split())
    A[:L-1]=0
    A[R:]=0
print(int(sum(A)))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
z = []
for i in range(m):
    x, y = map(int, input().split())
    z.append({a for a in range(x, y+1)})
ans = z[0]
for i in z:
    ans &= i
print(len(ans))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]
ans = set(range(1, n + 1))

for l, r in LR:
  ans &= set(range(l, r + 1))
  
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lists = []
for i in range(m):
    l,r = map(int,input().split())
    if m == 1:
        print(r-l+1)
        exit()
    tmp = list(range(l,r+1))
    lists.append(tmp)
ans = set(lists[0]) & set(lists[1])
for i in range(m):
    ans = ans & set(lists[i])
print(len(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n, m = MAP()
arr = [1 for _ in range(n)]
for _ in range(m):
    l, r = MAP()
    for i in range(l - 1):
        arr[i] = 0
    for i in range(r, n):
        arr[i] = 0
print(sum(arr))

Submission status: Time Limit Exceeded

Solution code:

n, m = map(int, input().split())
L = [0] * m
R = [0] * m
judge = [0] * n
for i in range(m):
    L[i], R[i] = map(int, input().split())
    for j in range(L[i], R[i] + 1):
        judge[j - 1] += 1

cnt = 0
for i in range(n):
    if judge[i] == m:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M=[int(x) for x in input().rstrip().split()]
l=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(b)
count=0
ans=0
for i in range(1,N+1):
    for x,y in l:
        if int(x)<=i and i<=int(y):
            count+=1
        else:
            break
        if count==M:
            ans+=1

    count=0
print(ans)

Submission status: Time Limit Exceeded

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

card = [0 for i in range(1, n+1)]

for l, r in gate:
    for i in range(l, r+1):
        card[i-1] += 1
print(card.count(m))

Submission status: Time Limit Exceeded

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

cnt = 0
LR = zip(L,R)
res = set(range(1,N+1))
for l,r in LR:
    res &= set(range(l,r+1))
print(len(res))

Submission status: Time Limit Exceeded

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

a = [0]*m
b = [0]*m
for i in range(m):
  l,r = map(int, input().split())
  a[i] = l
  b[i] = r
  
s = 0
for i in range(a[m-1], b[m-1]+1):
  t = 0
  for j in range(m):
    if a[j] <= i and i <= b[j]:
      t += 1
    else:
      break
  if t == m:
    s += 1
print(s)

Submission status: Time Limit Exceeded

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

p = []
for i in range(m):
  l,r = map(int, input().split())
  p.append([l,r])
  
s = 0
for i in range(1, n+1):
  t = 0
  for l,r in p:
    if l <= i and i <= r:
      t += 1
    else:
      break
  if t == m:
    s += 1
print(s)

Submission status: Time Limit Exceeded

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

p = []
for i in range(m):
  l,r = map(int, input().split())
  p.append([l,r])
  
  
s = 0
for i in range(1, n+1):
  t = 0
  for l,r in p:
    if l <= i and i <= r:
      t += 1
  if t == m:
    s += 1
print(s)

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 i in range(m-1):
    tmp_l,tmp_r = map(int,input().split())
    tmp = set(range(tmp_l,tmp_r+1))
    ans = ans&tmp

print(len(ans))

Submission status: Time Limit Exceeded

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

count=0
judge=0

for i in range(1,N+1):
    judge=0
    for lr in LR:
        if lr[0]<=i<=lr[1]:
            judge+=1
    if judge==M:
        count+=1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

card = [0] * (n+1)
for i, j in zip(l, r):
    for k in range(i, j+1):
        card[k] += 1

print(card.count(m))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

n,m = map(int,input().split())
card_flg = [True for i in range(n + 1)]
false_flg = [False for i in range(n + 1)]



for i in range(m):
    l,r = map(int,input().split())
    card_flg_tmp = copy.copy(false_flg)
    card_flg_tmp[l:r + 1] = [True for i in range(l,r + 1)]
    card_flg = np.logical_and(card_flg, card_flg_tmp)

print(list(card_flg).count(True))

Submission status: Time Limit Exceeded

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

checker = [0 for i in range(n)]
for i in gates:
    for j in range(i[0],i[1]+1):
        checker[j] += 1

ans = 0
for i in checker:
    if i == m:
        ans += 1

print(ans)

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())
s=0
for j in range(1,N+1):
  if max(L)<=j<=min(R):
    s+=1
print(s)

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())
s=0
for i in range(1,N+1):
  if max(L)<=i<=min(R):
    s+=1
print(s)

Submission status: Time Limit Exceeded

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

if m == 1:
  l, r = map(int, input().split())
  print(r-l+1)
else:
  l, r = map(int, input().split())
  ans = list(range(l, r+1))
  for _ in range(m-1):
    l, r = map(int, input().split())
    lr_list = list(range(l, r+1))
    ans = [a for a in ans if lr_list.count(a) == 1]
    if len(ans) == 0:
      break
  print(len(ans))

Submission status: Time Limit Exceeded

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

if m == 1:
  l, r = map(int, input().split())
  print(r-l+1)
else:
  l, r = map(int, input().split())
  ans = list(range(l, r+1))
  for _ in range(m-1):
    l, r = map(int, input().split())
    lr_list = list(range(l, r+1))
    removes = []
    for a in ans:
      if a not in lr_list:
        removes.append(a)
    for r in removes:
      ans.remove(r)
    if len(ans) == 0:
      break
  print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
LR = [[int(x) for x in readline().split()] for _ in range(M)]
se = set(range(LR[0][0], LR[0][1]+1))
for i in LR:
    sa = set(range(i[0], i[1]+1))
    se = se & sa

print(len(se))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
mat = [list(map(int,input().split())) for o in range(M)]
ans = set(j for j in range(mat[0][0], mat[0][-1] + 1))
for i in range(1, M):
    s = set(j for j in range(mat[i][0], mat[i][-1] + 1))
    ans = ans & s
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
mat = [list(map(int,input().split())) for o in range(M)]
ans = 0
for i in range(mat[0][0], mat[0][-1] + 1):
    for j in range(1, M):
        if i in range(mat[j][0], mat[j][-1] + 1):
            continue
        else:
            break
    else:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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)]
cnt=0
for i in range(1,n+1):
  sub=0
  for m in range(M):
    if i>=lr[m-1][0] and i<=lr[m-1][1]:sub+=1
    if sub==M:cnt+=1
#    print('i:',i,'l:',lr[m-1][0],'r:',lr[m-1][1],'sub:',sub)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = map(int, input().split())
lrl = list(list(map(int, input().split())) for _ in range(m))
idl = np.array([0]*n)

for lr in lrl:
    idl[lr[0]-1:lr[1]] += 1

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
lrl = list(list(map(int, input().split())) for _ in range(m))
idl = [0]*n

for lr in lrl:
    for i in range(lr[0]-1, lr[1]):
        idl[i] += 1

print(idl.count(m))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
lrl = list(list(map(int, input().split())) for _ in range(m))
idl = [0]*n
count = 0

for lr in lrl:
    for i in range(lr[0]-1, lr[1]):
        idl[i] += 1

for i in idl:
    if i == m:
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

    card_list = [0] * n

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

    print(card_list.count(m))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cards = range(1, N+1)
for _ in range(M):
    l, r = map(int, input().split())
    cards = set(cards) & set(range(l, r+1))
print(len(cards))

Submission status: Time Limit Exceeded

Solution code:
k,m=map(int,input().split())
ans=set(range(1,k+1))
for i in range(m):
  f=list(map(int,input().split()))
  ans&=set(range(f[0],f[1]+1))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
gate = []
l, r = map(int, input().split())
ID = {i for i in range(l, r + 1)}
for i in range(1, m):
    l, r = map(int, input().split())
    gate = {j for j in range(l, r + 1)}
    ID = ID & gate
print(len(ID))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
gate = []
for i in range(m):
    l, r = map(int, input().split())
    gate.append({j for j in range(l, r + 1)})
ID = gate[0]
for i in range(1, m):
    ID = ID & gate[i]
print(len(ID))

Submission status: Time Limit Exceeded

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

l, r = list(map(int,input().split(" ")))
s = set(range(l, r+1))

for _ in range(1,m):
    l, r = list(map(int,input().split(" ")))
    s_temp = set(range(l, r+1))
    s = s & s_temp
    
print(len(s))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
N,M=map(int,input().split())
x=set()
L,R=map(int,input().split())
x=set(range(L,R+1))
for i in range(M-1):
    L,R=map(int,input().split())
    x=x & set(range(L,R+1))
print(len(x))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
N,M=map(int,input().split())
lst=[]
for i in range(M):
    L,R=map(int,input().split())
    lst.append(set(range(L,R+1)))
print(len(reduce(lambda a,b:a&b,lst)))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()
def main():
    N, M = map(int, input().split())
    dict = {}
    for _ in range(M):
        L, R = map(int, input().split())
        for x in range(L, R+1):
            if x in dict:
                dict[x] += 1
            else:
                dict[x] = 1
    print(sum([1 for k, v in dict.items() if v == M]))
main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
dict = {}
for _ in range(M):
    L, R = map(int, input().split())
    for x in range(L, R+1):
        if x in dict:
            dict[x] += 1
        else:
            dict[x] = 1
print(sum([1 for k, v in dict.items() if v == M]))

Submission status: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]
lst = []
for _ in range(m):
    l, r = [int(x) for x in input().split()]
    lst.append(set(range(l, r+1)))

uni = lst[0].intersection(*lst[1:])
print(len(uni))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
card = [i for i in range(1, n+1)]
for _ in range(m):
   l, r = map(int, input().split())
   x = [i for i in range(l, r+1)]
   card = list(set(card) & set(x))

print(len(card))

Submission status: Time Limit Exceeded

Solution code:
N , M = map(int,input().split())
L = []
R = []
cnt = 0
flag = True
for i in range(M):
	l,r = map(int,input().split())
	L.append(l)
	R.append(r)
for i in range(N+1):
	flag = True
	for j in range(M):
		if i < L[j] or i > R[j]:
			flag = False
			break
	if flag == True:
		cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(s.count(m))

Submission status: Time Limit Exceeded

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

cards = range(N + 1)
u = set(cards[L[0]:R[0] + 1])
for i in range(1, M):
    u = u & set(cards[L[i]:R[i] + 1])
print(len(u))

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

canPass = [m] * n

for j in range(len(lr)):
    l = lr[j][0] - 1
    r = lr[j][1] - 1
    for k in range(l, r + 1):
        canPass[k] -= 1

ans = canPass.count(0)
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 i in range(M - 1):
    L, R = map(int, input().split())
    ans &= set(range(L, R + 1))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import os, sys, re, math

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

cards = set([(i + 1) for i in range(N)])
for i in range(M):
    (L, R) = [int(n) for n in input().split()]
    if min(cards) > R or max(cards) < L:
        cards = {}
        break
    cards -= set([(i + 1) for i in range(L - 1)])
    cards -= set([(i + 1) for i in range(R, N)])

print(len(cards))

Submission status: Time Limit Exceeded

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

for i in range(m-1):
    l , r =map(int,input().split())
    B = set(range(l,r+1))
    A = A.intersection(B)
print(len(A))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
card = set(range(l, r+1))
for i in range(1, m):
    l, r = map(int, input().split())
    card = card.intersection(set(range(l, r+1)))

print(len(card))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = map(int, input().split())
card = np.zeros(n+1, dtype=int)
for i in range(m):
    l, r = map(int, input().split())
    card[l:r+1] += 1

print(list(card).count(m))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = map(int, input().split())
card = np.ones(n+1, dtype=int)
for i in range(m):
    l, r = map(int, input().split())
    card[:l] = 0
    card[r+1:] = 0

print(int(sum(card)))

Submission status: Time Limit Exceeded

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

for i in range(M):
  l1 = list(range(l[i][0],l[i][1]+1))
  if i == 0:
    valid = l1
  valid = set(valid) & set(l1)
print(len(valid))

Submission status: Time Limit Exceeded

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

a = range(table[0][0],table[0][1]+1)
ans = a
for i,t in enumerate(table[1:]):
    if i == 0:
    	b = range(t[0], t[1]+1)
    	ans = set(a) & set(b)
    else:
        b = range(t[0], t[1]+1)
        ans = ans & set(b)
print(len(ans))

Submission status: Time Limit Exceeded

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

L,R = [],[]
ans = [0]*(N+1)
for i in range(M):
    a,b = map(int,input().split())
    
    for j in range(a,b+1):
        ans[j] += 1

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

print(count)

Submission status: Time Limit Exceeded

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

ans=set()
l, r = map(int, input().split())
for j in range(l,r+1):
    ans.add(j)

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

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
ans=0
for i in range(l[0][0],l[0][1]+1):
    if all([i in range(l[k][0],l[k][1]+1) for k in range(1,m)]):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    _, m = map(int, input().split())

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    _, m = map(int, input().split())

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
ans = 0
n, m = map(int, input().split())
listL, listR = [], []

for i in range(m):
    l, r = map(int, input().split())
    listL.append(l)
    listR.append(r)

for j in range(n):
    for i in range(m):
        if not (listL[i] <= j + 1 <= listR[i]):
            break
    else:
        ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

pass_ids = []
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())

	for j in range(L, R + 1):
		pass_ids.append(j)

print(int(len([x for x in pass_ids if pass_ids.count(x) == M]) / M))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

pass_ids = [True] * N
l, r = 0, N
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())

	for j in range(l, L):
		pass_ids[j] = False
	for j in range(R + 1, r):
		pass_ids[j] = False

	if l > L:
		l = L
	if r < R:
		r = R

print(pass_ids.count(True))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

#pass_ids = [x for x in range(N)]
#for i in range(M):
#	L, R = (int(x) - 1 for x in input().split())
#	pass_ids = [x for x in pass_ids if L <= x <= R]
#
#print(len(pass_ids))

non_pass_ids = set()
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())
	for x in range(0, L):
		non_pass_ids.add(x)
	for x in range(R + 1, N):
		non_pass_ids.add(x)

print(N - len(non_pass_ids))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

pass_ids = [x for x in range(N)]
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())
	pass_ids = [x for x in pass_ids if L <= x <= R]

print(len(pass_ids))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

pass_ids = [True] * N
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())
	for j in range(L):
		pass_ids[j] = False
	for j in range(R + 1, N):
		pass_ids[j] = False

print(pass_ids.count(True))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import collections

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

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

        if i == 0:
            ori_arr = set(range(L, R+1))
            continue

        next_arr = set(range(L, R+1))

        ori_arr = set(ori_arr & next_arr)


    print(len(ori_arr))

if __name__ == '__main__':
    main()