Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

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

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

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

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

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

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

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

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Time Limit Exceeded

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

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

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

        for y in range(L, R+1):
            arr.append(y)

    arrcnt = collections.Counter(arr).values()
    print(sum(i == M for i in arrcnt))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

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

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

        for y in range(L, R+1):
            arr.append(y)

    arrcnt = collections.Counter(arr).values()
    print(sum(i == M for i in arrcnt))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
idcard = [[int(j) for j in input().split()] for i in range(M)]
ans = {}
if M == 1:
    print(idcard[0][1] - idcard[0][0] + 1)
    exit()
t = 0
for i in idcard:
    for j in range(i[0], i[1] + 1):
        if j in ans:
            ans[j] += 1
        else:
            ans[j] = 1
        t = max(ans.values())
w = 0
for i in ans.values():
    if i == t:
        w += 1

print(w)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split())
L,R = map(int, input().split())
ans = set(list(range(L,R+1)))

for _ in range(M-1):
    L,R = map(int, input().split())
    gate = set(range(L,R+1))
    ans &= gate

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split())
L,R = map(int, input().split())
ans = set(list(range(L,R+1)))

for _ in range(M-1):
    L,R = map(int, input().split())
    gate = set(list(range(L,R+1)))
    ans = ans & gate

print(len(ans))

Submission status: Time Limit Exceeded

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

l, r = map(int, input().split())
ID = list(range(l, r + 1))
for _ in range(m - 1):
    l, r = map(int, input().split())
    if r < min(ID):
        print(0)
        exit()
    elif max(ID) < l:
        print(0)
        exit()
    elif min(ID) >= l and max(ID) >= r:
        l = min(ID)
        ID.clear()
        ID.extend(range(l, r + 1))
    elif min(ID) < l and max(ID) >= r:
        ID.clear()
        ID.extend(range(l, r + 1))
    elif min(ID) < l and max(ID) < r:
        r = max(ID)
        ID.clear()
        ID.extend(range(l, r + 1))

print(len(ID))

Submission status: Time Limit Exceeded

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

l, r = map(int, input().split())
lis = [i for i in range(l, r+1)]

for i in range(M-1):
    l, r = map(int, input().split())
    tmp_lis = [i for i in range(l, r+1)]
    lis = set(lis) & set(tmp_lis)

lis = [i for i in lis if i <= N]
print(len(lis))

Submission status: Time Limit Exceeded

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

for i in range(M):
    if i==0:
        landl=set(range(L[i],R[i]+1))
    else:
        landl=landl & set(range(L[i],R[i]+1))


print(len(landl))

Submission status: Time Limit Exceeded

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

ans = 0
for ci in c:
    if ci==m:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for _ in range(M)]
print(len(set.intersection(*[set(range(l, r+1)) for l, r in LR])))

Submission status: Time Limit Exceeded

Solution code:
#ABC-127-C
N, M = map(int, input().split())
LR  = []
for _ in range(M):
    Li, Ri = map(int, input().split())
    LR.append((Li, Ri))

possible_card_list = [i for i in range(LR[0][0], LR[0][1]+1)]

for Li, Ri in LR:
    for possible_card in possible_card_list[:]:
        if Li <= possible_card and possible_card <= Ri:
            #OK
            pass
        else:
            possible_card_list.remove(possible_card)
            
print(len(possible_card_list))

Submission status: Time Limit Exceeded

Solution code:
def main():
	import itertools
	n,m = map(int,input().split())
	l = [list(map(int,input().split())) for i in range(m)]
	# print(n,m)
	# print(l)
	ans = 0
	ans_pre = 0

	for ID in range(1,n+1):
		# print("---ID = {}---".format(ID))
		ans_pre = 0
		for i in range(m):
			# print("-i = {}-".format(i))
			li = l[i][0]
			# print("li = {}".format(li))
			ri = l[i][1]
			# print("ri = {}".format(ri))
			if ID >= li and ID <= ri:
				ans_pre += 1
			# print("ans_pre = {}".format(ans_pre))
		if ans_pre == m:
			ans += 1
	print(ans)
	




if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

max = 0
for i in range(m):
    if max < l[i]:
        max = l[i]
min = 10**5
for i in range(m):
    if min > r[i]:
        min = r[i]

ans = 0
for i in range(max,min+1):
    for j in range(m):
        if not((i) >= l[j] and (i) <= r[j]):
            break
        if j == m - 1:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n):
    for j in range(m):
        if not((i + 1) >= l[j] and (i + 1) <= r[j]):
            break
        if j == m - 1:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

wildcard = set(range(lr[0][0],lr[0][1]+1))
for i in range(1,len(lr)):
    wildcard &= set(range(lr[i][0],lr[i][1]+1))

ans = len(wildcard)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x = [int(i) for i in input().split()]
a, b = x
j = []
flat_list = []
for i in range(b):
    p = [int(i) for i in input().split()]
    j.append([i for i in range(p[0], p[1] + 1)])

y = len(j)
curr = j[0]

for i in range(1, y):
    curr = list(set(curr).intersection(j[i]))


print(len(curr))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

LRs = [list(map(int, input().split())) for i in range(m)]
LRs = [set(range(LRs[i][0], LRs[i][1] + 1)) for i in range(m)]

for i in range(m):
    if i == 0:
        union = LRs[i]
    else:
        union = union & LRs[i]

print(len(union))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
N, M=input().split()
N=int(N)
M=int(M)
i=0
ID_list=[]
ID_sublist=[]
gate_possible_list=[]
#センテンスを暗記するのではなく、まずinput()を書いて、膨らます感じで記述する。すると#思考の流れ通りに書ける。素晴らしい！
for i in range(N):
 ID_list.append(i+1)
#print(ID_list)
#for i in range(N)
gate_list =  [list(map(int, input().split(" ")))for i in range(M)]
#センテンスを暗記するのではなく、まずinput()を書いて、膨らます感じで記述する。すると#思考の流れ通りに書ける。素晴らしい！

#x=[]
#y=[]
#t, x, y = [0]*(N+1), [0]*(N+1), [0]*(N+1)#N+1個の空のリストを作成(原点を入れて##るから一つ増える)
#delta_t=0
#delta_x=0
#delta_y=0
#l=0
#t[0],x[0],y[0]=[0, 0, 0]
#とりあえず入力までは完成した．
#for i in range(N):
# t[i+1], x[i+1], y[i+1] = map(int, input().split())
#child_i =  list(map(int, input().split()))
#w_len=len(w)
#w_list=list(w)
#print(len(w_list))
#print(w_list)
#child_il=sorted(child_i)
#print(child_il)#リストの右端のほうが大きい
i=0
j=0
k=0
l=0
m=0
for i in range(M):#ゲートの個数分回す
 for j in range(gate_list[i][1]-gate_list[i][0]+1):
     gate_possible_list.append(gate_list[i][0]+j)
 #print(gate_possible_list)
 for k in range(len(ID_list)):
     for j in range(len(gate_possible_list)):
      if ID_list[k]==gate_possible_list[j]:
         ID_sublist.append(gate_possible_list[j])
      else:
          pass

 gate_possible_list=[]
 ID_list=[]
 for m in range(len(ID_sublist)):
  ID_list.append(ID_sublist[m])
 #print(ID_list)
 ID_sublist=[]
print(len(ID_list))
#これ時間かかりまくり

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
nums=[x+1 for x in range(n)]
cnt=0
ans=0
for i in nums:
  cnt=0
  for t in l:
    if(t[0]<=i<=t[1]):
      cnt+=1
  if(cnt==len(l)):
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def fetch_one_line():
    return stdin.readline().rstrip()


def fetch_int_input():
    return [int(s) for s in fetch_one_line().split()]


def fetch_inputs(times):
    return [fetch_one_line() for _ in range(times)]


def fetch_int_inputs(times):
    return [fetch_int_input() for _ in range(times)]


N, M = fetch_int_input()
gate_len = fetch_int_inputs(M)
range_card = [[False] * N for _ in range(M)]

for gl, rc in zip(gate_len, range_card):
    l = gl[0] - 1
    r = gl[1] - 1
    rc[l: r + 1] = [True] * (r - l + 1)

range_card = list(zip(*range_card))
print(sum([all(r) for r in range_card]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())

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

for i in range(m):
  l, r = map(int, input().split())
  lst = [l + j for j in range(r - l + 1)]
  lst1 = lst if len(lst1) == 0 else list(set(lst1) & set(lst))

print(len(lst1))

Submission status: Time Limit Exceeded

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

a = {i for i in range(1, N+1)}
for i in range(M):
    L, R = map(int, input().split())
    # print(a, set([i for i in range(L, R+1)]))
    a = a & {i for i in range(L, R+1)}
    # print(a, set([i for i in range(L, R+1)]))

print(len(a))

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

a = [0]*M

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

origin = set(range(1,N+1))
for k in range(M):
    origin &= a[k]

print(len(origin))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m = map(int, input().split())
cards = np.zeros(n)
for _ in range(m):
    left, right = map(int, input().split())
    cards[left-1:right] += 1
print(len(cards[cards == m]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(n):

    for j in range(m):
        
        if  not l[j]<=i+1<=r[j]:
            break
        if j==m-1:
            ans += 1
            
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
cards = list(range(l, r+1))

for i in range(1, M):
    l, r = map(int, input().split())
    cards_new = list(range(l, r+1))
    cards = set(cards) & set(cards_new)

print(len(cards))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
li = []
counter = 0
for i in range(m):
    l,r = map(int,input().split())
    for j in range(l,r+1):
        li.append(j)
import collections
c = collections.Counter(li)
for k in c.values():
    if k == m:
        counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split(' '))
sum_=[0]*N
for i in range(M):
  LR=list(map(int,input().split(' ')))
  for j in range(LR[0]-1,LR[1]):
    sum_[j]+=1

from collections import Counter
c=Counter(sum_)
print(c[M])

Submission status: Time Limit Exceeded

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

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

for i in range(m):
    for j in range(l[i], r[i]+1):
        card[j-1] += 1


ans = 0
for c in card:
    if c >= m:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    all_mighty_cards = set(range(1,n+1))
    for i in range(m):
        all_mighty_cards &= set(l[i:] + r[:i+1])
    


    print(max(all_mighty_cards) - min(all_mighty_cards) + 1 if all_mighty_cards else 0)

    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
key = []
for i in range(1,n+1):
    key.append(i)
ans = []
for i in range(m):
    ls = []
    L,R = map(int,input().split())
    for j in range(L,R+1):
        ls.append(j)
    key = (set(ls)&set(key))
    
print(len(key))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding=utf-8

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

tmp = [_+1 for _ in range(N)]
#print(tmp)
for i in range(len(list)):
  list[i]=[_ for _ in range(list[i][0],list[i][1]+1)]
  #print(list[i])
  tmp = set(tmp)&set(list[i])
#print(tmp)
print(len(tmp))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,R = map(int,input().split())
id = [0]*N
ans = 0
for i in range(R):
  A,B = map(int,input().split())
  for j in range(B-A+1):
    id[j+A-1]+=1
for i in id:
  if i == R:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(1, n+1):
  ans = 0
  for j in range(m):
    if i >= num[j][0] and i <= num[j][1]:
      ans += 1
    else:
      break
  if ans == m:
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
def ip(l1, l2):
  ans = []
  if len(l1) != len(l2):
    print('length mismatch')
  else:
    for i in range(len(l1)):
      ans.append(l1[i]*l2[i])
    return ans
g = []
for i in range(m):
  g.append(list(map(int, input().split())))
l = [1 for i in range(n)]
for i in range(m):
  temp_l = [0 for i in range(n)]
  for j in range(g[i][0]-1, g[i][1]):
    temp_l[j-1] = 1
  l = ip(l, temp_l)
ans = sum(l)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
(n, m) = map(int, input().split())
counts = [0 for _ in range(n)]
for _ in range(m):
    gate_i = list(map(int, input().split()))
    for i in range(gate_i[0] - 1, gate_i[1]):
        counts[i] += 1
counts.sort()
ans = 0
for count in counts:
    ans += 1 if m == count else 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    
    ans = set(list(range(L[0], R[0]+1)))
    for i in range(1, M):
        ans = ans & set(list(range(L[i],R[i]+1)))

    print(len(ans))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


ans = set(range(l[0][0], l[0][1] + 1))
for i in range(1, M):
    ans = ans.intersection(set(range(l[i][0], l[i][1] + 1)))
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    result = set([i for i in range(int(1e5 + 1))])
    # print(result)
    for i in range(M):
        l_to_r = {j for j in range(L[i], R[i] + 1)}
        result = result & l_to_r
        # print(result)
    print(len(result))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

#list_aaa = []
#
#for i in range(M):
#    L,R = map(int,input().split())
#    aaa = set(range(L,R+1))
#    list_aaa.append(aaa)
#print(list_aaa)

#and_set = list_aaa[0]

for i in range(M):
    L,R = map(int,input().split())
    aaa = set(range(L,R+1))
    if i == 0:
        and_set = aaa
    else:
        and_set = and_set.intersection(aaa)


print(len(and_set))

Submission status: Time Limit Exceeded

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

list_aaa = []

for i in range(M):
    L,R = map(int,input().split())
    aaa = set(range(L,R+1))
    list_aaa.append(aaa)
#print(list_aaa)

and_set = list_aaa[0]

for i in range(M-1):
    and_set = and_set.intersection(list_aaa[i+1])


print(len(and_set))

Submission status: Time Limit Exceeded

Solution code:
n, m = (int(x) for x in input().split())
ss = set(range(n))
for _ in range(m):
  l, r = (int(x) for x in input().split())
  ss = ss.intersection(range(l-1, r))
print(len(ss))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,raw_input().split())
L =[]
R = []
ans = []
tmp=[]
for i in range(0,M):
  tmp = list(map(int,raw_input().split()))
  for j in range(tmp[0],tmp[1]+1):
    R.append(j)
  L.append(R)
  R =[]
if M > 1:
  ans = set(L[0]) & set(L[1])
else:
  ans = set(L[0])
if M > 2:
  for i in range(2,M):
    ans = set(L[i]) & ans

print len(ans)

Submission status: Time Limit Exceeded

Solution code:
n , m = map(int,input().split())
a = [1] * n
ans = 0

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

Submission status: Time Limit Exceeded

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

for i in range(M):
	LR[i] = LR[i] & LR[i-1]
    
 
print(len(LR[M-1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

LR = [[] for i in range(M)]

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

for i in range(M):
	LR[i] = LR[i] & LR[i-1]
    

print(len(LR[M-1]))

Submission status: Time Limit Exceeded

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

LR = [[] for i in range(M)]

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


for i in range(M):
	LR[i] = set(LR[i])

for i in range(M):
	LR[i] = LR[i] & LR[i-1]

print(len(LR[M-1]))

Submission status: Time Limit Exceeded

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

tmp = set()

for val in range(m):
    l,r = map(int,input().split())
    tmp = set(list(range(l,r+1)))
    if val ==0:
        seki = tmp
    else:
        seki = seki & tmp

print(len(list(seki)))

Submission status: Time Limit Exceeded

Solution code:
[n,m]=[int(x) for x in input().split()]
LR=[[int(x)-1 for x in input().split()] for _ in range(m)]

ok=list(range(n))
for c in LR:
  ok=[item for item in ok if c[0]<=item and item<=c[1]]
  #print(ok)
print(len(ok))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
[n,m]=[int(x) for x in input().split()]
LR=[[int(x)-1 for x in input().split()] for _ in range(m)]

ans=np.zeros(n)
for c in LR:
  ans[c[0]:c[1]+1]=ans[c[0]:c[1]+1]+1
print(np.sum(ans==m))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = []
R = []
ans = 0
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
for k in range(1,N + 1):
    for i,j in zip(L,R):
        if i <= k and k <= j:
            continue
        else:
            break
    else:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = []
R = []
ans = 0
tmp = 1
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
for k in range(1,N + 1):
    for i,j in zip(L,R):
        if tmp == 1:
            if i <= k and k <= j:
                tmp *= 1
            else:
                tmp *= 0
        else:
            break
    if tmp == 1:
        ans += 1
    else:
        tmp = 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = []
R = []
ans = 0
tmp = 1
for _ in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
for j in range(1,N + 1):
    for i in range(M):
        if tmp == 1:
            if L[i]-1 < j < R[i]+1:
                tmp *= 1
            else:
                tmp *= 0
        else:
            break
    if tmp == 1:
        ans += 1
    else:
        tmp = 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]
s = {i:0 for i in range(1,n+1)}
cnt = 0
for i in range(m):
    for j in range(lr[i][0],lr[i][1]+1):
        s[j] += 1
for v in s.values():
    if v == m:cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

if len(a) == 1:
    print(a[0][1] - a[0][0] + 1)
else:
    ans = n
    for i in range(1, n + 1):
        for j in range(m):
            if a[j][0] <= i <= a[j][1]:
                continue
            else:
                ans -= 1
                break
    print(ans)

Submission status: Time Limit Exceeded

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

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

ans = 0
for v in cnt.values():
    if v == M:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

print(A.count(M))

Submission status: Time Limit Exceeded

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

A = [0] * (N+1)

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

print(A.count(M))

Submission status: Time Limit Exceeded

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

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

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

print(A.count(M))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
cards = [1 for _ in range(n)]
for _ in range(m):
    l,r= map(lambda x: int(x) - 1,input().split())
    if 0 < l:    
        cards[:l] = [0 for _ in range(l)]
    if r < n-1:
        cards[r+1:] = [0 for _ in range(n-1 -r)]
print(sum(cards))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
cards = [1 for _ in range(n)]
for _ in range(m):
    l,r= map(lambda x: int(x) - 1,input().split())
    if 0 < l:    
        for i in range(l):
            cards[i] = 0
    if r < n-1:
        for i in range(r+1,n):
            cards[i] = 0
print(sum(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())

ALL = [0] * N

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

print(ALL.count(M))

Submission status: Time Limit Exceeded

Solution code:
N,M= list(map(int, input().split()))
li=[list(map(int,input().split())) for _ in range(M)]
id = [0]*N
for i in li:
    for j in range(i[0]-1,i[1]):
        id[j]+=1
print(len([i for i,x in enumerate(id) if x==M]))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
x={i for i in range(10**5+1)}
for _ in range(M):
    L,R=map(int,input().split())
    tmp={i for i in range(L,R+1)}
    x=x&tmp
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())
ans = 0
A = [0] * N
for i in range(M):
    for j in range(L[i]-1,R[i]):
        A[j] += 1
        if A[j] == M:
            ans += 1
print(ans)