Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

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

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

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

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

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

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

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

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Time Limit Exceeded

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

ids = []
results = []
for i in range(1, n+1):
    ids.append(i)
    results.append(True)

for i in range(0, m):
    for num in ids:
        if num < gates[i][0] or num > gates[i][1]:
            results[num-1] = False

answer = 0
for i in results:
    if i:
        answer += 1

print(answer)

Submission status: Time Limit Exceeded

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

card = []

z=0

for i in range(M):
    L,R=map(int, input().split())
    for j in range(N):
        if L<=j+1<=R:
            card.append(j+1)

for x in card:
    if card.count(x)==M:
        z+=1
print(z//M)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

n, m = map(int, input().split())
ok =  [int(i) for i in range(1, n+1)]
answer = 0
for i in range(m):
    a, b = map(int, input().split())
    l = [int(n) for n in range(a, b+1)]
    ok = list(set(ok) & set(l))

print(len(ok))

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N):
  tmp = 0
  for L, R in LR:
    if L-1 <= i <= R-1:
      tmp += 1
    else:
      break
    if tmp == M:
      cnt += 1
      break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
gates = [list(map(int,input().split())) for _ in range(m)]
answers = [i for i in range(1,n+1)]
for gate in gates:
    for ans in answers[:]:
        if ans < gate[0] or ans > gate[1]:
            answers.remove(ans)
print(len(answers))

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
c=1
ans=[]
while c<=N:
    ans.append(c)
    c=c+1
for x in range(M):
    l,r=(int(x) for x in input().split())
    aa=[]
    t=l
    while t<=r:
        aa.append(t)
        t=t+1
    ans=set(ans)&set(aa)
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
ls=[]
for i in range(M):
    Li,Ri=map(int,input().split())
    set1=set([x for x in range(Li,Ri+1)])
    ls.append(set1)
set2=ls[0]    
for j in range(M):
    set2=set2&ls[j]
print(len(set2))

Submission status: Time Limit Exceeded

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

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = map(int, input().split())
    gate.append([gate_1, gate_2])


def numJudge(arr, num):
    if arr[0] <= num <= arr[1]:
        return 1
    else:
        return 0


def judge(num):
    num += 1
    ctr = 0
    for k in range(M):
        if gate[k][0] <= num <= gate[k][1]:
            ctr += 1
    if ctr == M:
        return 1
    else:
        return 0


ans = map(judge, range(N))
print(sum(list(ans)))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = map(int,input().split())
L = []
X = []
for i in range(M):
  l, m = map(int,input().split())
  L.append(l)
  X.append(m)
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(sum(ans_new))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
lists = []
cnt = 0
cnt_AC = 0
for i in range(M):
    L,R = map(int,input().split())
    L_R = list(range(L,R+1))
    lists.append(L_R)
for i in range(1,N+1):
    cnt = 0
    for j in range(M):
        if i in lists[j]:
            cnt += 1
    if cnt == M:
        cnt_AC += 1
print(cnt_AC)

Submission status: Time Limit Exceeded

Solution code:
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
LR=input().split()
ans=set(range(int(LR[0]),int(LR[1])+1))
for i in range(M-1):
	LR=[]
	LR=input().split()
#	print("ssssssssss")
#	print(ans)
	ansy=set(range(int(LR[0]),(int(LR[1])+1)))
	ans=ans&ansy
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m=input().split()
n=int(n)
m=int(m)
i=1
x=list()
y=list()
ans=0
g=0
i=i+1
l,r=input().split()
l=int(l)
r=int(r)
while l != r + 1:
     x.append(l)
     l = l + 1

i=1

while m!=i:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    while l != r + 1:
        y.append(l)
        l = l + 1

    while g!=len(x):
        v=x[g] in y
        if v==False:
            x.remove(x[g])
        else:
            g=g+1
    g=0
    y=[]



print(len(x))

Submission status: Time Limit Exceeded

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

for i in range(m):
    a,b=(int(i) for i in input().split())
    for j in range(a,b+1):
      mylist[j]+=1

print(mylist.count(m))

Submission status: Time Limit Exceeded

Solution code:
import copy
N, M = map(int, input().split())
LR = []
LR2 = []
count = 0
for i in range(M):
    l, r = map(int, input().split())
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()
for i in range(1, N+1):
    flag = 1
    for j in LR:
        if not (i >= j[0] and i <= j[1]):
            flag = 0
            break
    if flag == 1:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
n,m=map(int,input().split())
l=[[]*i for i in range(m)]
a=set()
for i in range(m):
  s,e=map(int,input().split())
  for j in range(s,e+1):
    l[i].append(j)
for i in range(m):
  if i==0:
    a=set(l[i])
  else:
    a=a&set(l[i])
print(len(a))

Submission status: Time Limit Exceeded

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

A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,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:
def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

cards = []
for i in range(M):
    Li, Ri = s_input()
    L.append(Li)
    R.append(Ri)
    cards.append(list(range(Li, Ri+1)))

cards = set(sum(cards, []))

ng_cards = set()
for card in cards:
    for i in range(M):
        if card < L[i] or card > R[i]:
            ng_cards.add(card)
            break

print(len(list(cards - ng_cards)))

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

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

for i in range(M):
    if M != 1:
        nice_key = list(set(skylist[0])&set(skylist[1]))
        if i != M-1:
            nice_key = list(set(nice_key)&set(skylist[i+1]))
        else:
            last_key = list(set(nice_key)&set(skylist[-1]))
    elif M ==1:
        last_key = skylist[0][::]

print(len(last_key))

Submission status: Time Limit Exceeded

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

l_inside = list(range(1, n+1))
for i in range(m):
    l_inside = [j for j in l_inside if gate[i][0] <= j and j <= gate[i][1]]

print(len(l_inside))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split(" "))
r = [""] * m
for c in range(m):
    r[c] = list(map(int, input().split(" ")))
ac = 0
for c in range(n):
    num = c + 1
    for co in range(m):
        if r[co][0] <= num <= r[co][1]:
            pass
        else:
            break
    else:
        ac += 1
print(ac)

Submission status: Time Limit Exceeded

Solution code:
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
LR=input().split()
ans=set(list(range(int(LR[0]),int(LR[1])+1)))
for i in range(M-1):
	LR=[]
	LR=input().split()
#	print("ssssssssss")
#	print(ans)
	ansy=set(list(range(int(LR[0]),(int(LR[1])+1))))
	ans=ans&ansy
ansx=list(ans)
print(len(ansx))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

card, gate = map(int, input().split())
ans = [i for i in range(1, card+1)]
for i in range(gate):
  l, r = map(int, input().split())
  ans = np.intersect1d(ans, [i for i in range(l, r+1)])
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# import numpy as np

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

t = [0]*n

for i in range(m):
    l, r = map(int, input().split())
    for j in range(n):
        if l<=j+1 and r>=j+1:
            t[j] += 1

print(t.count(m))

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N):
  tmp = 0
  for L, R in LR:
    if L-1 <= i <= R-1:
      tmp += 1
    if tmp == M:
      cnt += 1
      break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""


#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    

"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

#Distance Transformation
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

"""



N, M = list(map(int, input().split()))
L=[0]*M
R=[0]*M


A = set(range(1, N+1))


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


def main():
    #print([i for i in range(L[i], R[i]+1)])
    print(len(A))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

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.append(a)
    r.append(b)

ans = 0

for j in range(n):
    flag = 1
    for k in range(m):
        if j < l[k]-1 or j > r[k]-1:
            flag = 0
            break
    if flag == 1:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np;N, k = map(int, input().split());cand = np.zeros(N)
for _ in range(k):
    l,ll = map(int, input().split())
    cand[l-1:ll]+=1
print(np.sum(cand==k))

Submission status: Time Limit Exceeded

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

gates = []
for _ in range(m):
    gates.append(tuple(map(int, input().split())))

gates.sort(key=lambda x: (x[0], x[1]), reverse=True)

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

Submission status: Time Limit Exceeded

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

ans = []
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    temp = set(i for i in range(l,r+1))
    ans.append(temp)

anss = ans[0]
for j in range(1,m):
    anss &= ans[j]   
print(len(anss))

Submission status: Time Limit Exceeded

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

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))

print(len(nums))

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
c=1
ans=[]
while c<=N:
    ans.append(c)
    c=c+1

for x in range(M):
    l,r=(int(x) for x in input().split())
    aa=[]
    aa.append(l)
    sa=r-l
    for n in range(sa):
        aa.append(l+n+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())

l_=[int(n) for n in range(a,b+1)]

for i in range(M-1):
  c,d=map(int,input().split())
  l__=[n for n in range(c,d+1)]
  l_=list(set(l_)&set(l__))
  
print(len(l_))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
num = []
num = [int(i) for i in input().split()]

gate = []

for temp in range(num[1]):
    temp = []
    temp = [int(i) for i in input().split()]
    gate.append(temp)

check = [0] * num[0]

for temp in gate:
    while temp[0] <= temp[1]:
        check[temp[0]-1] += 1
        temp[0] += 1

print(check.count(num[1]))

Submission status: Time Limit Exceeded

Solution code:
n ,m = map(int, input().split())
result = set()
tmp = set()
first = True
for i in range(m):
    l, r = map(int, input().split())
    tmp = {i for i in range(l, r + 1)}
    if first:
        result = tmp
        first = False
    else:
        result = result.intersection(tmp)

print(len(result))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
all=[]
for i in range(M):
    L,R=map(int,input().split())
    if i==0:
        ans=[b for b in range(L,R+1)]
    else:
        ids=[b for b in range(L,R+1)]
        ans=set(ans)&set(ids)
print(len(ans))

Submission status: Time Limit Exceeded

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

gates = list(range(1, N + 1))
for m in range(M):
  L, R = map(int, input().split())
  gates = [g for g in gates if L <= g and g <= R]
  if len(gates) < 1:
    break

print(len(gates))

Submission status: Time Limit Exceeded

Solution code:
parents = list(map(int,input().split(' ')))
N = parents[1]
childs = [list(map(int, input().split())) for _ in range(N)]
start = list(range(childs[0][0], childs[0][1]+1))
for i in childs:
    start = set(start) & set(list(range(i[0], i[1]+1)))
print(len(start))

Submission status: Time Limit Exceeded

Solution code:
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
LR=input().split()
ans=set(list(range(int(LR[0]),int(LR[1])+1)))
for i in range(M-1):
	LR=[]
	LR=input().split()
#	print("ssssssssss")
#	print(ans)
	ansy=set(list(range(int(LR[0]),(int(LR[1])+1))))
	ans=ans&ansy
ansx=list(ans)
print(len(ans))

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 = list(range(res[i][0],res[i][1]+1))
	inu.append(x)
    
for j in range(m):
	neko += inu[j]

dup = [x for x in set(neko) if neko.count(x) == m]
print(len(dup))

Submission status: Time Limit Exceeded

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

answer = []
tmp = []

L, R = map(int, input().split())
answer = list(range(L, R+1))

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

print(len(answer))

Submission status: Time Limit Exceeded

Solution code:
def set_range(x, y):
    return {t+x for t in range(y-x)}

N, M =  map(int, input().split()) 
L = [0]*M
R = [0]*M

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

S = set_range(1, N+1)
for m in range(M):
    S = S.intersection(set_range(L[m], R[m]+1))
    
print(len(S))

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import islice

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


lst = []
for i in LR:
    lst_ = set(list(range(i[0],i[1]+1)))
    lst.append(lst_)

lst = set.intersection(set(lst[0]), *islice(lst, 1, None))
print(len(lst))

Submission status: Time Limit Exceeded

Solution code:
list_l = []
list_R = []
ID,gate = map(int,input().split())
s = 0
t = 0
for i in range(gate):
  l,r = map(int,input().split())
  list_l.append(l)
  list_R.append(r)

for i in range(ID+1):
  for j in range(gate):
    if list_l[j] <= i <= list_R[j]:
      s += 1
    else:
      None
  if s == gate:
    t += 1
  s = 0
print(t)

Submission status: Time Limit Exceeded

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

L,R=map(int, input().split())
s=[i for i in range(L,R+1)]
t=[]
for i in range(M-1):
    L,R=map(int, input().split())
    t=[j for j in range(L,R+1)]
    s = set(s) & set(t)
    #print(s)

print(len(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, M = map(int, input().split())
    box = []
    for i in range(M):
        LR = [j for j in map(int, input().split())]
        box += LR

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

    # print(box)
    num = 0
    for j in range(1, N+1):
        plus = 1
        # print(j)
        if plus == 1:
            for i in range(0, len(box), 2):
                # print(box[i], box[i+1])
                if j < box[i] or box[i+1] < j:
                    plus = 0
                    break

            num += plus

    print(num)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
cnt = 0
for i in range(1, N+1):
  isOk = True
  for start, end in l:
    if i < start or i > end:
      isOk = False
      break
  if isOk == True:
    cnt = cnt + 1

print(cnt)

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,r))
res = 0
L,R = gates[0]
if len(gates) < 2 :
	for i in range(1,n+1) :
		res += 1 if L<=i<=R else 0
	print(res)
else :
	for i in range(L,R+1) :
		flag =False
		for l,r in gates[1:] :
			if l<=i<=r :
				flag =True
			else :
				flag = False
				break
		res += 1 if flag else 0
	print(res)

Submission status: Time Limit Exceeded

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

X = {i+1 for i in range(N)}

for i in range(M):
    X = X & {j for j in range(ls[i][0],ls[i][1]+1)}

print(len(X))

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

def prison(N,M,L,R):
  num=list(range(L[0],(R[0]+1)))
  for j in range(M):
    num=[i for i in num if L[j]<=i<=R[j]]
  return len(num)
print(prison(N,M,L,R))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.zeros(n)

for i in range(m):
    l_, r_ = map(int, input().split())
    count[l_-1:r_]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import copy

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

if M == 1:
    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]
    print(len(l))
else:
    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]
    l_tmp = copy.deepcopy(l)

    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]

    l_set = list(set(l) & set(l_tmp))
    l_tmp = copy.deepcopy(l)
    
    for i in range(M-2):
        L, R = map(int,input().split())
        l = [k+L for k in range(R-L+1)]

        l_set = list(set(l) & set(l_set))
        l_tmp = copy.deepcopy(l)
        
        if len(l_set) == 0:
            break

    print(len(l_set))

Submission status: Time Limit Exceeded

Solution code:
parents = list(map(int,input().split(' ')))

N = parents[1]
childs = [list(map(int, input().split())) for _ in range(N)]
start = []

for i in range(childs[0][0], childs[0][1]+1):
    start.append(i)


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

print(len(start))

Submission status: Time Limit Exceeded

Solution code:
inputs = input().split()

N = int(inputs[0])
M = int(inputs[1])

passIDs = [0] * (10**5 + 10)

for i in range(M):
    inputs = input().split()

    L = int(inputs[0])
    R = int(inputs[1])

    for j in range(L, R+1):
        passIDs[j] += 1

IDs = 0

for passId in passIDs:
    if passId == M:
        IDs += 1

print(IDs)

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.append(a)
    r.append(b)

ans = 0

for j in range(n):
    flag = 1
    for k in range(m):
        if j < l[k]-1 or j > r[k]-1:
            flag = 0
    if flag == 1:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

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

ans = 0

for i in range(1, n+1):
    for j in range(m):
        if l[j] > i or r[j] < i:
            break
        elif j == m-1:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = map(int, input().split())
    gate.append([gate_1, gate_2])


def numJudge(arr, num):
    if arr[0] <= num <= arr[1]:
        return 1
    else:
        return 0


def judge(num):
    num += 1
    num = [num]*M
    counter = map(numJudge, gate, num)
    if sum(list(counter)) == M:
        return 1
    else:
        return 0


ans = map(judge, range(N))
print(sum(list(ans)))
"""
for i in range(N):
    i += 1
    counter = 0
    for k in gate:
        #print(k, i)
        if k[0] <= i <= k[1]:
            counter += 1
    #print(counter, M)
    if counter == M:
        stepper += 1
"""

Submission status: Time Limit Exceeded

Solution code:
n,m=(int(i) for i in input().split())
a,b=(int(i) for i in input().split())
myset=set()
for i in range(a,b+1):
    myset.add(i)
if m==1:
  ans=myset
else:
  ans=myset
  for i in range(1,m):
    a,b=(int(i) for i in input().split())
    myset=set()
    for i in range(a,b+1):
      myset.add(i)
    ans=ans&myset

print(len(ans))

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):
        if l[m-1] <= i <= r[0]: 
            count += 1
for i in range(n):
    for j in range(m):
        l.append(1)
print(count)

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,r))
res = 0
L,R = gates[0]
if len(gates) < 2 :
	for i in range(1,n+1) :
		res += 1 if L<=i<=R else 0
	print(res)
else :
	for i in range(L,R+1) :
		flag =False
		for l,r in gates[1:] :
			if l<=i<=r :
				flag =True
			else :
				flag = False
		res += 1 if flag else 0
	print(res)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N, M = map(int, input().split())
seki = set(list(range(1,N+1)))
for i in range(M):
    l, r = map(int, stdin.readline().rstrip().split())
    seki &= set(list(range(l, r+1)))

print(len(seki))

Submission status: Time Limit Exceeded

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

ans = []
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    temp = [i for i in range(l,r+1)]
    ans.append(set(temp))

anss = ans[0]
for j in range(1,len(ans)):
    anss &= ans[j]   
print(len(anss))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    # N, M = 4, 2
    # lr = [[1, 3], [2, 4]]
    all_set = set(range(1, N + 1))
    for line in lr:
        all_set = all_set & set(range(line[0], line[1] + 1))
    print(len(all_set))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n ,m = map(int, input().split())
result = set()
first = True
for i in range(m):
    l, r = map(int, input().split())
    tmp = set()
    for i in range(l, r + 1):
        tmp.add(i)
    if first:
        result = tmp
        # print("tmp {}".format(tmp))
        first = False
    else:
        result = result.intersection(tmp)
        # print("result {} tmp {}".format(result, tmp))

print(len(result))

Submission status: Time Limit Exceeded

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

l = [""] * m

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

ans3 = [i for i in range(1,n+1)]
ans3 = set(ans3)


for i in range(m):
    ans2 = [j for j in range(1,n+1) if l[i][0] <= j <= l[i][1]]
    ans3 = ans3 & set(ans2)
    ans2.clear()
    
    
print(len(ans3))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
L = []
X = []
for i in range(M):
  l, m = map(int,input().split())
  L.append(l)
  X.append(m)
ans= []
for j in range(N):
  ans.append(0)
for k in range(M):
  for o in range(L[k]-1,X[k]):
    ans[o] += 1
cnt = 0
for n in ans:
  if n == M:
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

l = []
r = []

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

ok_card = []
for i in range(m):
  for j in range(int(r[i]) - int(l[i])+1):
    ok_card.append(int(l[i])+j)

big_a = list(set(ok_card))
super_card = []
for i in range(len(big_a)):
  if ok_card.count(big_a[i]) >= m:
    super_card.append(big_a[i])

print(len(super_card))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
lr=[]
result=0
gates=0
for i in range(m):
  lr.append(input().split())
#print(lr[0])
for k in range(1,n+1):
  gates=0
  for h in range(m):
    if int(k)>=int(lr[h][0]) and int(k)<=int(lr[h][1]):
      gates+=1
  #print(gates,k)
  if gates==m:
    result+=1
    gates=0
    #print('ID')
print(result)

Submission status: Time Limit Exceeded

Solution code:
#C問題
N, M = map(int, input().split())
N_list = []
for i in range (N):
    N_list.append(i+1)

L_list = []
R_list = []
for i in range(M):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)

N_list= [i for i in N_list if i >= max(L_list) and i<=min(R_list)]
print(len(N_list))

Submission status: Time Limit Exceeded

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

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

IDlist=[i for i in range(N)]

for l,r in LR:
    LRlist=[i for i in range(l-1,r)]
    IDlist=set(IDlist)&set(LRlist)
print(len(IDlist))

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

def prison(N,M,L,R):
  num=list(range(L[0],(R[0]+1)))
  for l,r in zip(L[1:],R[1:]):
    num=[i for i in num if l<=i<=r]
  return len(num)
print(prison(N,M,L,R))

Submission status: Time Limit Exceeded

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