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 = [k for k in range(1,10**5+1)]
s = set(a)
for i in range(m):
  l,r = map(int,input().split())
  ml = [p for p in range(l,r+1)]
  newset = set(ml)
  s = s&newset
print(len(s))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = [k for k in range(1,10**5+1)]
s = set(a)
for i in range(m):
  l,r = map(int,input().split())
  ml = []
  for j in range(l,r+1):
    ml.append(j)
  newset = set(ml)
  s = s&newset
print(len(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n, m = map(int, input().split())
card = defaultdict(int)
for i in range(m):
    l, r = map(int, input().split())
    for j in range(l, r+1):
        card[j] += 1
cnt = 0
for i, v in card.items():
    if v == m:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def iread():
    return int(input())


def sread():
    return input()


def aread(t="int"):
    if t == "int":
        ret = [int(i) for i in input().split()]
    elif t == "str":
        ret = list(input())
    else:
        ret = tmp
    return ret


def scan():
    return list(map(int, input().split()))
    


def main():
    n, m = scan()
    gate = set(range(1, n + 1))
    cnt = 0
    for i in range(m):
        l, r = scan()
        gate = gate.intersection(range(l, r + 1))
    # print(gate)
    print(len(gate))
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = {i for i in range(1, N + 1)}
for line in sys.stdin:  # much faster than input
    L, R = map(int, line.split())
    allowed = {i for i in range(L, R + 1)}
    ans = ans & allowed

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(x) for x in input().split(" ")]
L, R = [], []
for i in range(M):
    l, r = [int(x) for x in input().split(" ")]
    L.append(l - 1)
    R.append(r - 1)

pass_key = set([i for i in range(N)])
for i in range(M):
    if len(pass_key) == 0:
        print(0)
        exit(0)
    else:
        pass_key = pass_key & set([ID for ID in range(L[i], R[i] + 1)])

print(len(pass_key))

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 = True
  for j in range(m):
    if not lr[j][0] <= i <= lr[j][1]:
      flag = False
      break
  if flag:
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=list(range(N))
ans=0
for i in range(M):
  L,R=map(int,input().split())
  for j in range(N):
    if A[j]+1<L or R<A[j]+1:
      A[j]=-1
for k in range(N):
  if A[k]!=-1:
    ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(m):
    l, r = list(map(int, input().split()))
    lr_list.append((l, r))

card2canopen_dict = {i: True for i in range(1, n+1)}

for n_card in range(1, n+1):
    for i, lr in enumerate(lr_list):
        target_l = lr[0]
        target_r = lr[1]
        if card2canopen_dict[n_card] and target_l <= n_card <= target_r:
            pass
        else:
            card2canopen_dict[n_card] = False
            break

card_list = [key for key, value in card2canopen_dict.items() if value]

ans = len(card_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_c
# C - Prison

n, m = list(map(int, input().split()))
lr_list = []

for _ in range(m):
    l, r = list(map(int, input().split()))
    lr_list.append((l, r))

card2canopen_dict = {i: True for i in range(1, n+1)}

for i, lr in enumerate(lr_list):
    target_l = lr[0]
    target_r = lr[1]
    for n_card in range(1, n+1):
        if card2canopen_dict[n_card] and target_l <= n_card <= target_r:
            pass
        else:
            card2canopen_dict[n_card] = False

card_list = [key for key, value in card2canopen_dict.items() if value]

ans = len(card_list)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M= map(int,input().split())
LRlis = [list(map(int, input().split())) for i in range(M)]
A = list(range(LRlis[0][0],LRlis[0][1]+1))
for i in range(M):
  B = list(range(LRlis[i][0],LRlis[i][1]+1))
  C = list(set(A) & set(B))

print(len(C))

Submission status: Time Limit Exceeded

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

R_ = list(m for m in R if m >= max(L))
print(sorted(R_)[0]-sorted(L)[len(L)-1]+1)

Submission status: Time Limit Exceeded

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

R_ = list(m for m in R if m >= max(L))
print(min(R_)-max(L)+1)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split())
L = []
R = []
for _ in range(M):
    l,r = map(int, input().split())
    L.append(l)
    R.append(r)
for m in R:
    if m < max(L):
        R.remove(m)
        
R_ = list(m for m in R if m >= max(L))
print(min(R_)-max(L)+1)

Submission status: Time Limit Exceeded

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

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

for i in range(1, m):
    for j in range(0, lr[i][1] - lr[i][0]+1):
        l2.append(lr[i][0]+j)
    l1 = list(set(l1) & set(l2))
    l2 = []
    
print(len(l1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

key = [0]*N
x = [list(map(int,input().split())) for i in range(M)]

for i in range(M):
	for j in range(x[i][0],x[i][1]+1):
		key[j-1] += 1

count = 0
for k in range(N):
	if key[k] == M:
		count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
L = []
R = []
ans = 0

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

for i in range(N):
    if max(L) <= i+1 <= min(R):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

gates = [list(map(int, input().split())) for _ in range(m)]
count=0
for i in range(1, n+1):
    tf = True
    for j in gates:
        if j[0] > i or j[1] < i:
            tf = False
            break
        
    if tf:
        count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
a = input().split()
a = [int(i) for i in a]

card_list = [int(i + 1) for i in range(a[0])]

for i in range(a[1]):
    b = input().split()
    b = [int(i) for i in b]
    gate_list = [int(i) for i in range(b[0], b[1] + 1)]
    delete_list = []
    for u in range(len(card_list)):
        if card_list[u] not in gate_list:
            delete_list.append(card_list[u])
    for i in delete_list:
        index = card_list.index(i)
        card_list.pop(index)

print(len(card_list))

Submission status: Time Limit Exceeded

Solution code:
a = input().split()
a = [int(i) for i in a]

card_list = [int(i + 1) for i in range(a[0])]

for i in range(a[1]):
    b = input().split()
    b = [int(i) for i in b]
    gate_list = [int(i) for i in range(b[0], b[1] + 1)]
    card_list = list(set(gate_list) & set(card_list))

print(len(card_list))

Submission status: Time Limit Exceeded

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

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

print(len(s))

Submission status: Time Limit Exceeded

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

x = LR[0]
for y in LR:
    x &= y
print(len(x))

Submission status: Time Limit Exceeded

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

nopass = 0
for i in range(1, N+1):
	for j in gate:
		if j[0] > i or j[1] < i:
			nopass += 1
			break
print(N - nopass)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
from itertools import chain
from collections import Counter

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
arr = [[j for j in range(li[k][0], li[k][1] + 1)] for k in range(M)]
set_arr = list(chain.from_iterable(arr))
arr_duplicate = [l for l in Counter(set_arr).values() if l == M]
print(len(arr_duplicate))

Submission status: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]
id_range = []
id_card = [0] * n

for x in range(m):
    id_range.append([int(y) for y in input().split()])

for x in range(m):
    for y in range(id_range[x][0], id_range[x][1] + 1):
        id_card[y - 1] += 1

id_card.sort()
cnt = 0
for x in id_card:
    if(x == m):
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
tamanho=list(map(int,input().rstrip().split()))
conjunto=set()

for i in range(tamanho[1]):
  linha=list(map(int,input().rstrip().split()))
  for j in range(1,linha[0]):
    conjunto.add(j)
  for k in range(linha[1]+1,tamanho[0]+1):
    conjunto.add(k)

print(tamanho[0]-len(conjunto))

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)


N, M = LI()
LR = []

for i in range(M):
    LR.append(LI())

#低い方と高い方を求める
lower = None
found = False
for i in range(1,N+1):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        lower = i
        found = True

if lower == None:
    print(0)
    exit()
#  print('lower', lower)
if lower == N:
    print(1)
    exit()

upper = None
found = False
for i in reversed(range(lower+1, N+1)):
    if found:
        break
    for lr in LR:
        if not (lr[0] <= i <= lr[1]):
            break
    else:
        upper = i
        found = True
if upper == None:
    upper = lower
#  print('upper', upper)
print(upper - lower + 1)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

for i in range(1, N + 1):
    if i >= max(L) and i <= min(R):
        ctr += 1
print(ctr)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
from itertools import chain
from collections import Counter

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
arr = [[j for j in range(li[k][0], li[k][1] + 1)] for k in range(M)]
set_arr = list(chain.from_iterable(arr))
arr_duplicate = [l for l in Counter(set_arr).values() if l == M]
print(len(arr_duplicate))

Submission status: Time Limit Exceeded

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

for i in range(0, len(l_lst)):
    for j in range(0, len(l_lst[i])):
        l_lst[i][j] = int(l_lst[i][j])

temp_cnt = 0
cnt = 0
for i in range(1, n+1):
    # print(i)
    for j in range(0, len(l_lst)):
        if l_lst[j][0] <= i and i <= l_lst[j][1]:
            temp_cnt += 1
        if temp_cnt == len(l_lst):
            cnt += 1
            # temp_cnt = 0
    temp_cnt = 0

print(cnt)

Submission status: Time Limit Exceeded

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

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

    key_set = {i for i in range(L[0], R[0]+1)}
    for i in range(1, M):
        key_set = key_set & {i for i in range(L[i], R[i]+1)}

    print(len(key_set))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,M = [int(i) for i in input().split()]
arr_list = [input().split() for i in range(M)]
num_list = [list(range(int(arr[0]),int(arr[1])+1)) for arr in arr_list]
num_list.insert(0, list(range(1,N+1)))

for i in range(M):
  num_list[i+1] = set(num_list[i])&set(num_list[i+1])

print(len(num_list[-1]))

Submission status: Time Limit Exceeded

Solution code:
data = list(map(lambda x: int(x),input().split(" ")))

cardNum = []

for n in range(data[1]):
	cardNum.append(list(map(lambda x: int(x),input().split(" "))))

#print(cardNum)

card = [0]*(data[0]+1)

for n in range(data[1]):
	for m in range(cardNum[n][0],cardNum[n][1]+1):
		card[m] += 1

print(card.count(data[1]))

Submission status: Time Limit Exceeded

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

for i in range(M):
    l, r = map(int, input().split())
    if i == 0:
        s = set(range(l, r+1))
    s = s & set(range(l, r+1))

print(len(s))

Submission status: Time Limit Exceeded

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

x = [[0 for _ in range(m)] for i in range(n)]
for i in range(m):
    l, r = [int(i) for i in input().split()]
    for j in range(l-1, r):
        x[j][i] = 1

counter = 0
for i in range(n):
    if sum(x[i]) == m:
        counter += 1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = range(1, n+1)

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

print(len(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:


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

    gate = []

    for _ in range(m):
        ngate = list(map(int, input().split()))
        gate.append(ngate)
    

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

    
    return result


if __name__ == "__main__":
    print(main())

Submission status: Time Limit Exceeded

Solution code:
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
for i in range(M):
  inf = input().split()
  L = int(inf[0])-1
  R = int(inf[1])-1
  x = max(L,R)
  if x > m:
    m = x
  ls += [(L,R)]

ans = [True]*(m+1)

for s in ls:
  ans[:s[0]] = [False]*len(ans[:s[0]])
  ans[s[1]+1:] = [False]*len(ans[s[1]+1:])

print(sum(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
ll=[];rl=[];count=0;ans=0
count1=0;count2=0
for i in range(m):
    l,r=map(int,input().split())
    ll.append(l);rl.append(r)
for o in range(n):
    count1+=1
    for i in range(m):

        if ll[i]<=o+1<=rl[i]:
            count2+=1
            
            if count2==m:
                ans+=1
    count2=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
data = []
for i in range(m):
    data.append(list(map(int, input().split())))
num = 1
count = 0
while num <= n:
    flag = True
    for i in range(m):
        if data[i][0] > num or data[i][1] < num:
            flag = False
    num += 1
    if flag:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, M = map(int, input().split())
LR = [[int(i) for i in input().split()] for i in range(M)]
ID = np.zeros(N)
for i in range(M):
    ID[LR[i][0] - 1:LR[i][1]] += 1
print(np.count_nonzero(ID == M))

Submission status: Time Limit Exceeded

Solution code:
def enter_card(card, L, R):
    for k in range(L-1,R):
        card[k] += 1
    return card

def open_list(a, M):
    b = []
    for (index, i) in enumerate(a):
        if i == M:
            b.append(index)
    return b

N, M = map(int, input().split(' '))
idcard = [0 for i in range(N)]
for i in range(M):
    L,  R = map(int, input().split(' '))
    idcard = enter_card(idcard, L, R)
a = open_list(idcard, M)
print(len(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

answer = set()

for idx, w in enumerate(li_w):
    if idx == 0:
        answer = set(range(w[0], w[1] + 1))
    else:
        answer = answer.intersection(range(w[0], w[1] + 1))

print(len(answer))

Submission status: Time Limit Exceeded

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

#print(r)
#print(l)
count = 0
k = 0
for i in range(1,n+1):
    k=0
    for j in range(m):
        if (l[j] <= i) and (i <= r[j]):
            k += 1

            
    #print(k)
    if k== m:
        count +=1


print(count)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

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

l_first, r_first = map(int, input().split())

answer_set = {value for value in range(l_first, r_first + 1)}

for _ in range(m - 1):
    l_current, r_current = map(int, input().split())
    current_set = {value for value in range(l_current, r_current + 1)}
    answer_set.intersection_update(current_set)

print(len(answer_set))

Submission status: Time Limit Exceeded

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

l_first, r_first = map(int, input().split())

answer_set = {value for value in range(l_first, r_first + 1)}

for _ in range(m - 1):
    l_current, r_current = map(int, input().split())
    current_set = {value for value in range(l_current, r_current + 1)}
    answer_set.intersection_update(current_set)

print(len(answer_set))

Submission status: Time Limit Exceeded

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

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

print(len(ans))

Submission status: Time Limit Exceeded

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

for i in range(1, N+1):
  for j in range(M):
    if not(L[j] <= i <= R[j]):
      break
    if j == M-1:
      result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
# C
# Prison

N_M = input().split(" ")

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

LR_list = []

for i in range(M):

    LR = [int(x) for x in input().split(" ")]
    LR_list.append(LR)

gates = [0] * N

for i in range(M):

    for j in range(N):

        if j >= LR_list[i][0] - 1 and j <= LR_list[i][1] - 1:

            gates[j] += 1

count = 0

for i in gates:

    if i == M:

        count += 1

print(count)

Submission status: Time Limit Exceeded

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

for i in range(M):
  L, R = map(int, input().split())
  lis.append(set(range(L,R+1)))
  if i >= 1:
    lis[i] = lis[i] & lis[i-1]

print(len(lis[-1]))

Submission status: Time Limit Exceeded

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

for i in range(M):
  L, R = map(int, input().split())
  lis.append(set())
  for j in range(L, R+1):
    lis[i].add(j)
  if i >= 1:
    lis[i] = lis[i] & lis[i-1]

print(len(lis[-1]))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int, input().split())
lr=[]
def calc_bit(l,r):
    return "0b"+"0"*(l-1)+"1"*(r-l+1)+"0"*(10**5-r)
ans="0b"+"1"*(10**5)
for i in range(m):
    l,r=map(int, input().split())
    ans=bin(int(ans,0)&int(calc_bit(l,r),0))
ans=str(ans)
print(ans.count("1"))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
ls = []
rs = []
for i in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)
n_all = 0
for card in range(1,n+1):
    auth = True
    for gate in range(m):
        if not ls[gate] <= card <= rs[gate]:
            auth = False
    if auth == True:
        n_all += 1
print(n_all)

Submission status: Time Limit Exceeded

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

n_all = 0
go = np.zeros(n)
for i in range(m):
    l, r = map(int, input().split())

    

    for card in range(1,n+1):
        if l <= card <= r and (go[card-1] == 0 or go[card-1] == 1):
            go[card-1] = 1
        else:
            go[card-1] = 2
#print("DEBUG: go =", go)
n_all = 0
for g in go:
    if g == 1:
        n_all += 1
print(n_all)

Submission status: Time Limit Exceeded

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

card_list = [1] * n

for i in range(m):
    l, r  = map(int, input().split())
    l -= 1
    r -= 1
    for j in range(n):
        if l <= j <= r:
            continue
        card_list[j] = 0
print(sum(card_list))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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


s = set(range(gates[0][0], gates[0][-1] + 1)) 
for g in gates[1:]:
    tmp_s = set(range(g[0], g[-1] + 1))
    s = s.intersection(tmp_s)

print(len(s))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
for i in range(M):
  id = list(range(data[i][0],data[i][1]+1))
  if i == 0:
      num = id
  else:
      num = set(num) & set(id)
print(len(num))

Submission status: Time Limit Exceeded

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

ones = [1]*N

for i in range(M):
    ones[List[i][1]:]=[0]*(N-List[i][1])
    ones[:List[i][0]-1]=[0]*(List[i][0]-1)
    
print(sum(ones))

Submission status: Time Limit Exceeded

Solution code:

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

result=0
LR=[]
for i in range(M):
        LR.append(input().split())


for i in range(N+1):#0からNまで
    counter=0
    flag=False
    if flag:
        break
    for j in LR:

        L=int(j[0])
        R=int(j[1])
        if i<L or R<i:
            flag=True
            break
        if L<=i and i<=R:
            counter+=1

    if counter==M:
        result+=1
print(result)

Submission status: Time Limit Exceeded

Solution code:

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

result=0
LR=[]
for i in range(M):
        LR.append(input().split())


for i in range(N+1):#0からNまで
    counter=0
    flag=False
    for j in LR:
        if flag:
            break
        L=int(j[0])
        R=int(j[1])
        if i<L or R<i:
            flag=True
            break
        if L<=i and i<=R:
            counter+=1

    if counter==M:
        result+=1
print(result)

Submission status: Time Limit Exceeded

Solution code:

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

result=0
LR=[]
for i in range(M):
        LR.append(input().split())

for i in range(N+1):#0からNまで
    counter=0
    for j in LR:#['1','1']
        #print(j)

        L=int(j[0])
        R=int(j[1])
        if i<L or R<i:
            break
        if L<=i and i<=R:
            counter+=1

    if counter==M:
        result+=1
print(result)

Submission status: Time Limit Exceeded

Solution code:

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

result=0
LR=[]
for i in range(M):
        LR.append(input().split())

for i in range(N+1):#0からNまで
    counter=0
    for j in LR:#['1','1']
        #print(j)

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

        if L<=i and i<=R:
            counter+=1
        if i<L or R<i:
            break
    if counter==M:
        result+=1
print(result)

Submission status: Time Limit Exceeded

Solution code:


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

result=0
LR=[]
for i in range(M):
        LR.append(input().split())



for i in range(N+1):#0からNまで
    counter=0
    for j in LR:#['1','1']
        #print(j)

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

        if L<=i and i<=R:
            counter+=1
    if counter==M:
        result+=1
print(result)

Submission status: Time Limit Exceeded

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

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

list_in = []

for i in xrange(M):
	list_in.append(map(int,raw_input().split()))

count = 0
for i in xrange(N):
	flag = 0
	for j in xrange(M):
		if ((i+1)<list_in[j][0]) | ((i+1)>list_in[j][1]):
			flag = 1
			break

	if flag==1:
		flag = 0
	else:
		count += 1

print count

Submission status: Time Limit Exceeded

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

arr = [0]*(N+1)
for i in range(M):
    for n in range(L[i], R[i]+1):
        if n > N:
            break;
        arr[n] = arr[n]+1

print(arr.count(M))

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
tempL,tempR = map(int,input().split())
gate_id = set(range(tempL,tempR+1))
for i in range(M-1):
  tempL,tempR = map(int,input().split())
  temp_id = set(range(tempL,tempR+1))
  gate_id = gate_id & temp_id
print(len(gate_id))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    results = [0] * n
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            results[o[0]:o[1]+1] = [1] * (o[1]+1-o[0])
            continue
        for j in range(n):
            if not results[j]:
                continue
            if not (o[0] <= j and j <= o[1]):
                results[j] = 0
                if o[1] < j:
                    break
            if results.count(1) == 0:
                print(0)
                return

    print(results.count(1))


if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    intersects = set()
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            intersects = set(range(o[0], o[1]+1))
        else:
            intersects = intersects & set(range(o[0], o[1]+1))
            if len(intersects) == 0:
                break

    print(len(intersects))


if '__main__' == __name__:
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    intersects = set()
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            intersects = set(range(o[0], o[1]+1))
        else:
            intersects = intersects & set(range(o[0], o[1]+1))

    print(len(intersects))


if '__main__' == __name__:
    resolve()