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

a = []
for i in range(m):
    l,r = map(int,input().split())
    
    if i == 0:
        for j in range(l,r+1):
            a.append(j)
    else:        
        b = []
        for j in range(l,r+1):
            b.append(j)

        c = a.copy()
        for j in c:
            if not j in b:
                a.remove(j)
       
print(len(a))

Submission status: Time Limit Exceeded

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

seki = set(range(LR[0][0],LR[0][1]+1))
for i in range(M):
    seki = seki & set(range(LR[i][0],LR[i][1]+1))
print(len(seki))

Submission status: Time Limit Exceeded

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

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]
  Answer = [s for s in Answer if s <=r]
  
print(len(Answer))

Submission status: Time Limit Exceeded

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

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

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

ans = []
for i in range(N):
  ans.append(i+1)

for i in range(M):
  ans = [j for j in ans if L[i]<=j and R[i]>=j]

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
n_lis = [i+1 for i in range(n)]
 
for j in range(m):
  l, r = map(int,input().split())
  n_lis = [k for k in n_lis if k >= l and k <= r]
 
print(len(n_lis),flush=True)

Submission status: Time Limit Exceeded

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

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

print(len(ans))

Submission status: Time Limit Exceeded

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

for i in range(M):
    a_list.append(map(int, raw_input().split()))

for i in range(M):
    idx = 1
    while a_list[i][0]+idx < a_list[i][-1]:
        a_list[i].insert(idx, a_list[i][0]+idx)
        idx = idx + 1

si = set(a_list[0])
for i in range(1, M):
   si = si & set(a_list[i])

print len(si)

Submission status: Time Limit Exceeded

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

#1~10**5
# print(10**5)
allcount = np.zeros(10**5,dtype=np.int)
# print(allcount)
for i in range(m):
    st = lr[i][0]-1
    g = lr[i][1]
    allcount=np.hstack((allcount[:st],allcount[st:g]+1,allcount[g:]))

# print(allcount)

allcount = list(allcount)
print(allcount.count(m))

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

count = 0
for i in range(1, n+1):
    if all(map(lambda gate: gate[0] <= i <= gate[1], gates)):
        count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
list1 = [0]*N

for i in range(M):
    a, b = map(int, input().split())
    for j in range(a-1,b):
        list1[j] = list1[j]+1

count1 = 0
for i in range(len(list1)):
     if list1[i]==M:
        count1 +=1

print(count1)

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 judge(num):
    num += 1
    counter = 0
    for k in gate:
        if k[0] <= num <= k[1]:
            counter += 1
    if 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 = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
ans = set(range(S[0][0], S[0][1] + 1))
for s in S[1:] :
    a = set(range(s[0], s[1] + 1))
    ans = ans & a
    if len(ans) == 0 :
        break
print(len(ans))

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 = map(int,input().split())

l,r=map(int, input().split())

test = set([j for j in range(l,r+1)])

for i in range(1, m):
    l,r=map(int, input().split())
    test_1 = set([j for j in range(l,r+1)])
    test = test & test_1
print(len(list(test)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
l = []
r = []
for i in range(m):
  A, B = list(map(int,input().split()))
  l.append(A)
  r.append(B)
sum = 0
for i in range(1,n+1,1):
  flag = 1
  for j in range(m):
    if i < l[j] or i > r[j]:
      flag = 0
      break
  if flag == 1:
    sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

cards = [1 for i in range(N)]

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

    for i in range(R,N):
        cards[i] = 0


print(sum(cards))

Submission status: Time Limit Exceeded

Solution code:
cardNum,gate = map(int,input().split())
lr = []
for i in range(gate):
  lr.append(list(map(int,input().split())))
okCard = 0
for card in range(1,cardNum+1):
  ok = True
  for check in lr:
    ok = ok and check[0] <= card <= check[1]
    if not ok:
      break
  if ok:
    okCard += 1
print(okCard)

Submission status: Time Limit Exceeded

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

id_list = [0] * N

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

print(sorted(id_list, reverse=True).count(M))

Submission status: Time Limit Exceeded

Solution code:
n,m=input().split()
n=int(n)
m=int(m)
i=0
x=list()
ans=0



while m!=i:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    while l != r + 1:
        x.append(l)
        l = l + 1
i=0
while n+1!=i:
    i=i+1
    if x.count(i)==m:
        ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

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

count = [0] * n

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

num = 0

for i in range(n):
    if count[i] == m:
       num+=1
print(num)

Submission status: Time Limit Exceeded

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

id_list = list(range(1,N+1))

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

print(len(id_list))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, k = map(int, input().split())
val = [map(int, input().split()) for _ in range(k)]
cand = np.zeros(N)
for v in val:
    l,ll = v
    cand[l-1:ll]+=1
print(len(np.where(cand==k)[0]))

Submission status: Time Limit Exceeded

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

for i in range(m):
    l, r = list(map(int, input().split()))
    ll.append(set(range(l, r+1)))

print(len(set.intersection(*ll)))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=[]
for I in range(M):
	x,y=map(int,input().split())
	A.append(list(range(x,y+1)))
F=set(A[0]).intersection(*A[1:])
print(len(F))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split())
LR = [list(map(int,input().split())) for _ in range(M)]
gate_list = []
ans = set(range(LR[0][0], LR[0][1]+1))
for i in range(M):
    gate_list = set(range(LR[i][0], LR[i][1]+1))
    ans = gate_list & ans
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
cardNum,gate = map(int,input().split())
lr = []
for i in range(gate):
  lr.append(list(map(int,input().split())))
okCard = 0
for card in range(1,cardNum+1):
  ok = True
  for check in lr:
    ok = ok and check[0] <= card <= check[1]
  if ok:
    okCard += 1
print(okCard)

Submission status: Time Limit Exceeded

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

print(len(ans))

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(1,n+1):
    cnt_1 = 0
    for j in range(m):
        if i < x[j][0] or i > x[j][1]:
            cnt_1 = 1
            break

    if cnt_1 == 0:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
c=0
o = [1]*n
for i in range(m):
    l, r = map(int, input().split())
    for j in range(n):
        if not(l <= j+1 <= r) and o[j]==1:
            o[j]=0
        elif i==m-1:
            c+=o[j]
print(c)

Submission status: Time Limit Exceeded

Solution code:
card_num, gate_num = map(int, input().split())

s, t = map(int, input().split())
all_set = set(range(s, t+1))
for _ in range(gate_num-1):
    s, t = map(int, input().split())
    all_set = all_set.intersection(set(range(s, t+1)))

print(len(all_set))

Submission status: Time Limit Exceeded

Solution code:
import numpy

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

result = numpy.ones(n)

for card in gates:

    result[:(card[0])-1] = 0
    result[card[1]:] = 0

print(sum(result == 1))

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 = []
    for j in range(i[0], i[1]+1):
        tmp.append(j)
    start = set(start) & set(tmp)

print(len(start))

Submission status: Time Limit Exceeded

Solution code:
N, M = input().split(" ")
if M == 1:
    a, b = input().split(" ")
    print(int(b)-int(a)+1)
else:    
    a, b = input().split(" ")
    sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        sets = sets & set([i for i in range(int(a),int(b)+1)])
    print(len(sets))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
l = []
cnt = 0
for i in range(m):
  l.append([int(i) for i in input().split()])
  
for i in range(1,n+1):
  flag = 1
  for k in range(m):
    if i < l[k][0] or i > l[k][1]:
      flag = 0
  if flag == 1:
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

print(len(idList))

Submission status: Time Limit Exceeded

Solution code:
L, R = [[], []]
N, M = map(int, input().split())
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):
  count = min(R)-max(L)+1
  
print('%d' % count)

Submission status: Time Limit Exceeded

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

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

cnt = 0
for n in range(1, N+1):
    flg = True
    for m in range(M):
        li, ri = data[m]
        if not(li <= n <= ri):
            flg = False
    if flg:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

arr = [0 for i in range(M)]
seki = set(range(LR[0][0],LR[0][1]+1))
for i in range(M):
    arr[i] = set(range(LR[i][0],LR[i][1]+1))
    seki = seki & arr[i]
print(len(seki))

Submission status: Time Limit Exceeded

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

cards = np.zeros(N) 
for j in range(M):
    L, R = map(int, input().split())
    for i in range(1, N+1):
        if cards[i-1] != 1:
            if L > i or R < i:
                cards[i-1] = 1

print(int(len(cards)-np.sum(cards)) )

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# IMPORT ###############################################################################################################
import sys                                                      # システム
# sys.setrecursionlimit(10**6)                                    # 再帰処理の上限変更
# from collections import deque, Counter                          # キュー・個数カウント
# from itertools   import accumulate, permutations, combinations  # 累積和・順列・組み合わせ
# from operator    import itemgetter                              # ソート補助
# from bisect      import bisect_left, bisect_right, bisect       # 二分探索
# from heapq       import heappop, heappush                       # プライオリティキュー
# from fractions   import gcd                                     # 最大公約数
# from math        import ceil, floor, sqrt, cos, sin, tan, pi    # 数学処理
# from copy        import deepcopy                                # オブジェクトコピー
# import numpy         as np                                      # 数値計算

# MAIN #################################################################################################################
def main():
    # Init #############################################################################################################
    im = InputManager()
    # Input ############################################################################################################
    n, m = im.inputMultiple()
    gates = [0 for _ in range(n)]
    for _ in range(m):
        l, r = im.inputMultiple()
        for i in range(l-1, r):
            gates[i] += 1
    # Logic ############################################################################################################

    # Output ###########################################################################################################
    print(gates.count(m))
    # Return ###########################################################################################################
    return

# INPUT MANAGER ########################################################################################################
class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]

# CALL MAIN ############################################################################################################
main()

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N, M = map(int, input().split())

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

    LR1 = LR[0]
    L1 = LR1[0]
    R1 = LR1[1]

    my_list = range(L1, R1+1)

    for tmp_LR in LR[1::]:
        tmp_L = tmp_LR[0]
        tmp_R = tmp_LR[1]

        my_list = [e for e in my_list if e >= tmp_L and e <= tmp_R]

    answer = len(my_list)
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
s = list(map(int, input().split()))
N = s[0]
M = s[1]

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

IDs = np.arange(1,N+1) # カードの番号

for i in range(M):
    IDs = IDs[s[i][0] <= IDs]
    IDs = IDs[IDs <= s[i][1]]

print(len(IDs))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
set1=set()
set2=set()
L,R=map(int,input().split())
for i in range(L,R+1):
    set1.add(i)
min=min(set1)
max=max(set1)+1
for i in range(M-1):
    L,R=map(int,input().split())
    if min <= L:
        min=L
    if max >= R+1:
        max=R+1
    for j in range(min,max):
        set2.add(j)
    set1=set1&set2
    set2.clear()
print(len(set1))

Submission status: Time Limit Exceeded

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

for _ in range(M):
    l, r = list(map(int, input().split(' ')))
    
    if _ == 0:
        if r > N:
            r = N
        ans = set(range(l, r+1))
        
    else:
        if r > N:
            r = N
        _ans = ans
        ans = set(range(l, r+1))
        ans = ans & _ans

print(len(ans))

Submission status: Time Limit Exceeded

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

for j in range(m):
  l, r = map(int,input().split())
  n_lis = [k for k in n_lis if k >= l and k <= r]

print(len(n_lis))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split(" "))
acc_gates = set([i for i in range(1, N+1)])
for m in range(M):
    i, j = map(int, input().split(" "))
    tmp = set([n for n in range(i, j+1)])
    acc_gates = acc_gates & tmp

print(len(set(acc_gates)))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

input_list_1 = input().split()

N = int(input_list_1[0])
M = int(input_list_1[1])
clear_list = []
for i in range(1,N+1):
    clear_list.append(i)
    
for i in range(M):
    input_list_2 = input().split()
    start = int(input_list_2[0])
    end = int(input_list_2[1])

    for i in range(1,N+1):
        if i < start or end < i:
            try:
                clear_list.remove(i)
            except:
                pass

print(len(clear_list))

Submission status: Time Limit Exceeded

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

n, m = map(int, input().split())
gate = []
answer = 0
for i in range(m):
    gate.append([int(j) for j in input().split()])
for k in range(n):
    id = k + 1
    flg = True
    for l in range(m):
        if id < gate[l][0] or id > gate[l][1]:
            flg = False
            break 
    if flg:
        answer += 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
li = [i for i in range(1, N+1)]
ans = li
for lr in range(M):
  l, r = map(int, input().split())
  ans = [i for i in ans if l <= i <= r]
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

keys = [True] * N

for i in range(M):
    l, r = map(int, input().split())
    newKeys = [False] * N
    for j in range(l - 1, r):
        newKeys[j] = True
    keys = np.logical_and(keys, newKeys)

print(np.sum(keys == True))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[]
for i in range(m):
    a.append(list(map(int,input().split())))
ans=n
for i in range(1,n+1):
    for j in range(m):
        if i<a[j][0] or a[j][1]<i:
            ans=ans-1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N, G = map(int, stdin.readline().split())
dic = {key: 0 for key in range(1, N + 1)}
left, right = N+1, -1
this, next = set(), set()
for i in range(G):
    if i == 0:
        l, r = map(int, stdin.readline().split())
        for j in range(l, r + 1):
            this.add(j)
    else:
        l, r = map(int, stdin.readline().split())
        for j in range(l, r + 1):
            if j in this:
                next.add(j)
        this = next
        next = set()

print(len(this))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
l = []
r = []
for i in range(m):
  A, B = list(map(int,input().split()))
  l.append(A)
  r.append(B)
sum = 0
for i in range(1,n+1,1):
  tmp = 0
  for j in range(m):
    if i >= l[j] and i <= r[j]:
      tmp += 1
  if tmp == m:
    sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = map(int, input().split())
X = np.ones((N))
for i in range(M):
    Y = np.zeros((N))
    L, R = map(int, input().split())
    Y[L-1:R]=1
    X=X*Y
print(np.sum(X, dtype=np.int64))

Submission status: Time Limit Exceeded

Solution code:
M,N = map(int,input().split())
ans=set(range(1,M+1))
for s in range(N):
  S,Z = map(int,input().split())
  ans =  ans & set(range(S, Z+1))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
def prison():
    N, M = map(int, input().split())
    id_l = set(range(1, N+1))
    for _ in range(M):
            L, R = map(int, input().split())
            id_l = id_l & set(range(L, R+1))
    return len(id_l)

print(prison())

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

N, M = input().split()

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

A = np.zeros([2,M])

cnt = 0

for i in range(M):
    A[:,i] = input().split()

for i in range(1,N+1):
    for j in range(M):
        if A[0,j] <= i and i <= A[1,j]:
            continue
        else:
            cnt += 1
            break

print(N - cnt)

Submission status: Time Limit Exceeded

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

ans = [1] * (N+1)
for L,R in LR:
    ans[:L] = [0] * L
    ans[R+1:] = [0] * (N+1-R)

print(ans.count(1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, M = map(int, input().split())
ls = np.empty(M)
rs = np.empty(M)


for i in range(M):
    l, r = map(int, input().split())
    ls[i] = l
    rs[i] = r

min_diff_arg = np.argmin(rs - ls)
candidate = np.arange(ls[min_diff_arg], rs[min_diff_arg]+1, 1)

valid_counter = 0
for c in candidate:
    if ((ls <= c) * (c <= rs)).all():
        valid_counter += 1

print(valid_counter)

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 judge(num):
    num += 1
    counter = 0
    for k in gate:
        if k[0] <= num <= k[1]:
            counter += 1
    if counter == M:
        return 1
    else:
        return 0


for ans in map(judge, range(N)):
    stepper += ans
print(stepper)
"""
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 = map(int, input().split())
cards = [0 for x in range(N)]

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

sum = 0
for x in cards:
    if x == M:
        sum = sum + 1

print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

a = set()
for i in range(m):
    l, r = map(int,input().split())
    b = set()
    for j in range(l, r + 1):
        b.add(j)
    a = a & b
    if i == 0:
        a = b

print(len(a))

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

N, M = map(int, raw_input().split())
_count = [0]*N

for i in range(M):
    L, R = map(int, raw_input().split())
    for j in range(L-1,R):
        _count[j] += 1

print _count.count(M)

Submission status: Time Limit Exceeded

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


ans = [[] for i in range(m+1)]
ans[0] = [(i+1) for i in range(n)]
for i in range(m):
    for j in range(len(ans[i])):
        if para_val[i][0] <= ans[i][j] <= para_val[i][1]:
            ans[i+1].append(ans[i][j])
print(len(ans[-1]))

Submission status: Time Limit Exceeded

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

cards = np.zeros(N) 
for j in range(M):
    L, R = map(int, input().split())
    for i in range(1, N+1):
        if L > i or R < i:
            cards[i-1] = 1

print(int(len(cards)-np.sum(cards)) )

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

cnt = 0
for n in range(1, N+1):
    flg = 0
    for m in range(M):
        li, ri = data[m]
        if li <= n <= ri:
            flg += 1
    if flg == M:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(1,n+1):
    cnt_1 = 0
    for j in range(m):
        if i < x[j][0] or i > x[j][1]:
            cnt_1 = 1
            break

    if cnt_1 == 0:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# n=int(input())  #数値入力 「N」だけの入力のとき
# a,b=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
# c=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
# s=[list(map(int,list(input()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# a=100
# b=0.987654321
# print('{0:06d}-{1:6f}'.format(a,b))  # 0埋めのときの出力
# 000100-0.987654

import numpy as np
import math
import copy

N,M=map(int, input().split())
A=np.array([i for i in range(N+1)[1::]])

for _ in range(M):
    L,R=map(int, input().split())
    tmp=np.array([i for i in range(R+1)[L::]])
    A = np.intersect1d(A,tmp)

print(len(A))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
def inc(a,b):
    h = []*(a-b)
    for p in range(a,b+1):
        if p <= b:
            h.append(p)
    return set(h)
for p in range(M):
    L,M  = map(int,input().split())
    if p == 0:
        ls = inc(L,M)
    else:
        ls = ls & inc(L,M)

print(len(ls))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split(" "))
ans = 0
GATE_ID = []
for i in range(M):
    GATE_ID1,GATE_ID2 = map(int,input().split(" "))
    GATE_ID.append([GATE_ID1,GATE_ID2])
#print(GATE_ID)

for i in range(N):
    CARD_ID = i+1
    #print("CARD_ID:",CARD_ID)
    ALL_GATE_PASS_COUNT = 0
    for j in range(M):
        GATE_ID1,GATE_ID2 = GATE_ID[j][0],GATE_ID[j][1]
        #print(GATE_ID1,GATE_ID2)
        if CARD_ID >= GATE_ID1 and CARD_ID <= GATE_ID2:
            ALL_GATE_PASS_COUNT += 1
            #print(CARD_ID,":PASS")
        else:
            break
    #print("ALL_GATE_PASS_COUNT:",ALL_GATE_PASS_COUNT)
    if ALL_GATE_PASS_COUNT == M:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
card_num, gate_num = map(int, input().split())

s, t = map(int, input().split())
all_set = set(range(s, t+1))
for _ in range(gate_num-1):
    s, t = map(int, input().split())
    new_set = set(range(s, t+1))
    all_set = all_set.intersection(new_set)

print(len(all_set))

Submission status: Time Limit Exceeded

Solution code:
cards, gates = map(int, input().rstrip().split(' '))
ok_cards = [list(map(int, input().rstrip().split(' '))) for _ in range(gates)]
ok_set = set(range(1, cards+ 1))

for L, R in ok_cards:
    tmp_set = set(range(L, R+1))
    ok_set = ok_set & tmp_set

print(len(ok_set))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

inputs = input().split(' ')
N = int(inputs[0])
M = int(inputs[1])

inputs = input().split(' ')
L = int(inputs[0])
R = int(inputs[1])
res = {x for x in range(L, R+1)}
for _ in range(M-1):
    inputs = input().split(' ')
    L = int(inputs[0])
    R = int(inputs[1])
    tmp = {x for x in range(L, R+1)}
    res = res & tmp

print(len(res))