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())
ans = 0
L = [0]*M
R = [0]*M

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

for i in range(1,N+1): #ID
    for j in range(M):
        if L[j] <= i <= R[j]:
            flag = 1
        else:
            flag = 0
            break

    if flag == 1:
        ans += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M= (int(i) for i in input().split())

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

for i in range(M):
    L_tmp, R_tmp = map(int, input().split())
    ans = ans & set(range(L_tmp,R_tmp+1))

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

cardlist = []
for i in range(M):
  cardlist.append(list(map(int, input().split())))

count = 0
for i in range(1, N+1):
  flag = True
  for j in range(M):
    if (cardlist[j][0] > i or cardlist[j][1] < i):
      flag = False
      break
  
  if (flag):
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

ls = []

for i in range(M):
    L, R = map(int,input().split())
    l = [k+L for k in range(R-L+1)]
    ls.append(l)
    
if M == 1:
    print(len(l))
else:
    for i in range(M-1):
        l_set = set(ls[i]) & set(ls[i+1])
        if len(l_set) == 0:
            break
        ls[i+1] = copy.deepcopy(l_set)

    print(len(l_set))

Submission status: Time Limit Exceeded

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

for i in range(M):
  L, R = map(int, input().split())
  pass_card = [i for i in range(L, R+1)]
  if  i == 0:
    ans = pass_card
  elif i > 0:
    ans = set(ans) & set(pass_card)

print(len(ans))

Submission status: Time Limit Exceeded

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

for k in range(m):

  if k == 0:
    unq=set(g[0])
  else:
    unq=unq&set(g[k])

print(len(list(unq)))

Submission status: Time Limit Exceeded

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

l = [nu for nu in range(1, n + 1)]

for i in range(m):
    L, R = map(int, input().split())
    card = [nu for nu in range(L, R + 1)]
    l = list(set(l) & set(card))

print(len(l))

Submission status: Time Limit Exceeded

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

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

#print(r)
ac = 0
for c in range(n):
    num = c + 1 # num がカード番号
    for co in range(m): # co がゲート番号
        if r[co][0] <= num <= r[co][1]:
            #print("card{} is ok for gate{}".format(num, co))
            pass
        else:
            break
    else:
        ac += 1

print(ac)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n, m = map(int, stdin.readline().split())
ans = set(range(1, n+1))
for i in range(m):
    l, r = map(int, stdin.readline().split())
    ans = list(filter(lambda x:l<=x<=r, ans))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = map(int, input().split())
l_list = []
r_list = []
for i in range(m):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)
min_list = []
for i in range(m):
    min_list.append(min(l_list[i:]))

card = np.ones(n)
for l, r in zip(min_list, r_list):
    card[:(l - 1)] = 0
    card[r:] = 0
print(int(np.sum(card)))

Submission status: Time Limit Exceeded

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


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

lst = set.intersection(*lst)
print(len(lst))

Submission status: Time Limit Exceeded

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

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

passIDs = [0] * (10**6)

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

#print(N,M)

okList = [1] * N

#print(okList)
#print(type(okList))

for i in range(M):
    L, R = map(int, input().split())
    L -= 1
    if 0 < L:
        okList[0:L] = [0] * L
        #print(okList)

    if R < len(okList):
        okList[R:len(okList)] = [0] * (len(okList) - R)
        #print(okList)

print(sum(okList))

Submission status: Time Limit Exceeded

Solution code:
N, M= map(int, input().split())
L1, R1= map(int, input().split())
Ans = set(range(L1, R1+1))

for x in range(M-1):
    L, R = map(int, input().split())
    List = set(range(L, R+1))
    Ans = (Ans) & (List)

print(len(Ans))

Submission status: Time Limit Exceeded

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

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:
x = input()
li = x.split()
n = int(li[0])
m = int(li[1])
o = 0
f = []
for k in range (m):
  y = input()
  e = y.split()
  l = int(e[0])
  r = int(e[1])
  f.append(l)
  f.append(r)
for j in range (n):
  s = int(j) + 1
  ans = 0
  for i in range (m):
    b = f[2 * i]
    c = f[2 * i + 1]
    if(b <= s and s <= c):
      ans += 1
  if(ans == m):
    o += 1

print(o)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
l = [list(map(int, input().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())
cards = [0 for x in range(N)]
sum = 0
for x in range(M):
    l, m = map(int, input().split())
    for y in range(l - 1, m):
        cards[y] = cards[y] + 1
        if x == M - 1 and cards[y] == M:
            sum = sum + 1
print(sum)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

a = np.zeros(N, dtype=int)
for m in range(M):
  L, R = map(int, input().split())
  a[L-1:R] += 1

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

Submission status: Time Limit Exceeded

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

s = set( range(l,r+1) )
for m in range(M - 1):
    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
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
ids = set(range(1, n+1))

for gate in args[1:]:
  l, r = map(int, gate.split(' '))  
  ids = ids & set(range(l, r+1))
  
print(len(ids))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ID = []
for i in range(M):
    K = list(map(int, input().split())) 
    ID.append(list(range(K[0], K[1]+1)))
if len(ID) == 1:
    print(len(list(range(K[0], K[1]+1))))
else:
    match = set(ID[0]) & set(ID[1])
    for i in range(M):
        match = set(ID[i]) & match
    print(len(match))

Submission status: Time Limit Exceeded

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

n, m = map(int, input().split())
gate = []
answer = 0
for i in range(m):
    a, b = map(int, input().split())
    gate.append(a)
    gate.append(b)

for k in range(n):
    id = k + 1
    if id < min(gate) or id > max(gate):
        continue
    flg = True
    for l in range(m):
        if id < gate[2 * l] or id > gate[2 * l +1]:
            flg = False
            break 
    if flg:
        answer += 1
print(answer)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
lst = []
lst2 = []
lst3 = []

for _ in range(M):
  lst.append(list(map(int,input().split())))

#print(lst)

for i in range(M):
  for j in range(min(lst[i]),max(lst[i])+1):
    lst2.append(j)

#print(lst2)

for k in range(1,N+1):
  lst3.append(lst2.count(k))
  
#print(lst3)

print(lst3.count(M))

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

sum = 0
for id in range(1, N+1):
    for li, ri in zip(L, R):
        if id < li or ri < id:
            break
    else:
        sum += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ls = []
for i in range(m):
    l, r = map(int, input().split())
    ln = l
    while r >= ln:
        ls.append(ln)
        ln += 1
ls_duplicate = [x for x in set(ls) if ls.count(x) > (m-1)]
print(len(ls_duplicate))

Submission status: Time Limit Exceeded

Solution code:
Input = input().split()
N = int(Input[0])
M = int(Input[1])

import numpy as np
x = np.ones(N+1, int)
x[0] = 0

for i in range(M):
    Input = input().split()
    L = int(Input[0])
    R = int(Input[1])
    y = np.zeros(N+1, int)
    y[L:R+1] = 1
    x = x * y

print(int(sum(x)))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split(" "))
l_init,r_init=map(int,input().split(" "))
ans=range(l_init,r_init+1)
for i in range(m-1):
	l,r=map(int,input().split(" "))
	ans=list(set(ans)&set(range(l,r+1)))
if len(ans)>n:
	print(n)
else:
	print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M= (int(i) for i in input().split())

L = [0] * M
R = [0] * M

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

for i in range(M):
    L_tmp, R_tmp = map(int, input().split())
    ans = ans & set(range(L_tmp,R_tmp+1))

print(len(ans))

Submission status: Time Limit Exceeded

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

Answer = [p+1 for p in range(n)]

for i in range(m):
  l,r = map(int, input().split())
  Answer = [q for q in Answer if q >=l and q <= r]
  
print(len(Answer))

Submission status: Time Limit Exceeded

Solution code:

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

li = []

ans = 0

for i in range(M):
    x,y = map(int,input().split())
    li.append([x,y])

for i in range(1,N+1):
    count = 0
    for j,k in li :
        if j <= i and i <= k :
            count += 1
            if count == len(li):
                ans += 1
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1,n+1):
  for j in range(m):
    if i < li[j][0] or li[j][1]< i:
      break
  else:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
G=[]
a=[]
S=[]
for i in range(M):
    l,r=map(int,input().split())
    a=[x for x in range(l,r+1)]
    G.append(a)

G_diff=set(G[0])
for i in range(1,M):
    G_diff=set(G_diff).intersection(set(G[i]))

if G_diff:
    print(len(G_diff))
else:
    print(0)

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())
    ids=[b for b in range(L,R+1)]
    all.append(ids)
for a in range(M):
    ans=set(all[a])&set(all[0])
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

a = np.ones(N, dtype=int)
for m in range(M):
  L, R = map(int, input().split())
  b = np.zeros(N, dtype=int)
  b[L-1:R] = 1
  a = a * b

print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
gates = []
for gate in args[1:]:
  l, r = map(int, gate.split(' '))
  gates.append((l, r))
  
ids = set(range(1, n+1))
for gate in gates:
  l, r = gate
  ids = ids & set(range(l, r+1))
  
print(len(ids))

Submission status: Time Limit Exceeded

Solution code:
N, M= map(int, input().split())
L1, R1= map(int, input().split())
Ans = list(range(L1, R1+1))

for x in range(M-1):
    L, R = map(int, input().split())
    List = list(range(L, R+1))
    Ans = list(set(Ans) & set(List))

print(len(Ans))

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
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
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())
for i in range(m):
    a,b = map(int,input().split())
    if i == 0:
        l = [a+j for j in range(b-a+1)]
    l = list(set([a+j for j in range(b-a+1)]) & set(l))
print(len(set(l)))

Submission status: Time Limit Exceeded

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

ans=set(int(i) for i in range(100000+10))

for i in range(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:
import numpy as np

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

data = []
for m in range(M):
    data.append(list(map(int, input().split(' '))))

npdata = np.array(data)

cnt = 0
for n in range(1, N+1):
  if M == len(npdata[(npdata[:,0]<=n)&(n<=npdata[:,1])]):
  	cnt += 1
print(cnt)

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)
for i in range(M):
    L, R = map(int, input().split())
    N_list= [i for i in N_list if i >= L and i<=R]
print(len(N_list))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

ls = []

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

for i in range(M-1):
    l_set = set(ls[i]) & set(ls[i+1])
    ls[i+1] = copy.deepcopy(l_set)

print(len(ls[-1]))

Submission status: Time Limit Exceeded

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

check_list = [0] * N

for i in range(M):
    l, r = [int(n) for n in input().split()]
    for j in range(l-1, r):
        check_list[j] += 1

print(check_list.count(M))

Submission status: Time Limit Exceeded

Solution code:
d = input().split(" ")
id_list = list(range(1,int(d[0])+1))

for i in range(int(d[1])):
  tmp = input().split(" ")
  id_list = set(id_list) & set(list(range(int(tmp[0]),int(tmp[1])+1)))

print(len(id_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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):
  count=0
  step=0
  for l,r in zip(L,R):
    num=list(range(l,(r+1)))
    if step!=0:
      num=list(set(num) & set(num_pre))
    num_pre=num
    step+=1
  return len(num_pre)
print(prison(N,M,L,R))

Submission status: Time Limit Exceeded

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

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

print(len(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
nm = input().split()
n = int(nm[0])
m = int(nm[1])
lr = [input().split() for i in range(m)]
l = [int(lr[i][0]) for i in range(m)]
r = [int(lr[i][1]) for i in range(m)]

card = [i for i in range(n + 1)]
if m == 1:
  print(len(card[l[0]:r[0]]) + 1)
  sys.exit()
card2 = []
card_duplicate = []
for i in range(m):
  #card2 = card_duplicate
  card2.extend(card[l[i]-1:r[i]])

card_duplicate = [x for x in set(card2) if card2.count(x) == m]

print(len(card_duplicate))

Submission status: Time Limit Exceeded

Solution code:
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 = ans & {i for i in range(l, r+1)}
print(len(ans))

Submission status: Time Limit Exceeded

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

x = set(e[0])
for i in e:
  x = x & set(i)
  
print(len(list(x)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

passed = set(range(1, n+1))
for _ in range(m):
    L, R = map(int, input().split())
    passed &= set(range(L, R+1))
print(len(passed))

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)

    print(len(l_set))

Submission status: Time Limit Exceeded

Solution code:
import copy
N,M=map(int,input().split())
D=[]
for _ in range(M):
    if _==0:
        LR=list(map(int,input().split()))
        for u in range(LR[0],LR[1]+1):
            D.append(u)
    else:
        LR=list(map(int,input().split()))
        DD=copy.deepcopy(D)
        for v in DD:
            if v not in range(LR[0],LR[1]+1):
                D.remove(v)
print(len(D))

Submission status: Time Limit Exceeded

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

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

tmp = []
for gate in range(1, M+1):
    for i in range(L[gate-1], R[gate-1]+1):
        tmp.append(i)
    tmpLR = list(set(tmp) & set(LR))
    LR.clear()
    LR = tmpLR
    tmp.clear()

print(len(LR))

Submission status: Time Limit Exceeded

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

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

print(len(list(sets)))

Submission status: Time Limit Exceeded

Solution code:
numCard, numGate = map(int, input().split())
idList = []
for i in range(numGate):
        L, R = map(int, input().split())
        idList.append(range(L, R+1))

rootCards = set(idList[0])
for l in range(len(idList)-1):
        rootCards = rootCards & set(idList[l+1])

print(len(rootCards))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]
l, r = [int(x) for x in input().split()]
s1 = {int(x) for x in range(l, r+1)}
for i in range(m-1):
    l, r = [int(x) for x in input().split()]
    s2 = {int(x) for x in range(l, r+1)}
    s1 = s1 & s2
    
print(len(s1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

lr_mat = np.zeros((m, 2))

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

cnt = 0
for i in range(n):
    flag = True
    for j in range(m):
        left = lr_mat[j, 0]
        right = lr_mat[j, 1]
        if not ((left <= i+1) and (i+1 <= right)):
            flag = False
            break
    if(flag):
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

lr = []

for i in range(m) :
  lr.append([int(i) for i in input().split(" ")])

x = set(range(lr[0][0], lr[0][1] + 1, 1))

for i in range(1, m, 1):
  x = x & set(range(lr[i][0], lr[i][1] + 1, 1))
  
print(len(list(x & set(range(1, n + 1)))))

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
"""
"""
import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
"""

#素因数を並べる
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 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([i for i in range(1, N+1)])

for i in range(M):
    L[i], R[i] = list(map(int, input().split()))
    A = A & set([i for i in 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:
import collections
# s = [[]]
n, m = map(int,input().split())
s = [list(map(int,input().split())) for i in range(m)]

# n, m = 4, 2
# s = [[1,3], [2,4]]

if m == 1:
    print(s[0][-1] - s[0][0] + 1)
else:
    new = [[] for i in range(m)]
    for i in range(m):
        for j in range(s[i][0],s[i][1]+1):
            new[i].append(j)
    tmp = [flatten for inner in new for flatten in inner]
    c = collections.Counter(tmp)
    count = 0
    for i, num in c.most_common():
        if num == m:
            count += 1
        else:
            break
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
cards = [True for x in range(N)]

for x in range(M):
    l, m = map(int, input().split())
    cards[0:l-1] = [False for y in range(0, l-1)]
    cards[m:] = [False for y in range(m, N)]
sum = 0
for x in cards:
    if x:
        sum = sum + 1

print(sum)