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())
memo=[]
l,r=map(int,input().split())
for i in range(r-l+1):
    memo.append(l+i)
for v in range(m-1):
    memo1=[]
    l,r=map(int,input().split())
    for k in range(len(memo)):
        if l<=memo[k]<=r:
            memo1.append(memo[k])
    memo=memo1
print(len(memo))

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 = set(idls[L-1:R])
    ans.append( iidls)


s = ans[0]
for i in range(M-1):
    s1 = s.intersection(ans[i+1])
    s = s1

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

L = []
R = []
for i in range(M):
  L.append(LR[i][0])
  R.append(LR[i][1])
  
count = 0
flag = 0
for i in range(1, N+1):
  flag = 0
  for j in range(M-1, -1, -1):
    if not (L[j] <= i <= R[j]):
      break
    else:
      flag += 1
    if flag == M:
      count += 1

print(count)

Submission status: Time Limit Exceeded

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

l = [""] * m

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

ans2 = set()
ans3 = set()

for i in range(1,n+1):
    ans3.add(i)

tmp = set()

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
c = 0
l2 = []
a, b = map(int,input().split())
l = list(range(a,b+1))  
for i in range(M-1):
  c, d = map(int,input().split())
  l1 = list(range(c,d+1))  
  for j in l1:
    if j in l:
      l2.append(j)
  l = l2
  l2 = []
print(len(l))

Submission status: Time Limit Exceeded

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

for i in range(n):
    flg=True
    for j in range(m):
        if not (i+1 >= l_[j] and i+1 <= r_[j]) :
            flg=False
            break        
    if flg : 
        ans+=1
         
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.zeros(n)

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

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

Submission status: Time Limit Exceeded

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

for i in range(m):
    a, b = map(int, input().split())
    ans2 = [i for i in range(a, b+1)]
    if ans1 == []:
        ans1 = ans2
    else:
        ans1 = list(set(ans1) & set(ans2))

print(len(ans1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

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

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

print(cnt)

Submission status: Time Limit Exceeded

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

L1,R1 = lst[0]
common = 0
for i in range(L1,R1+1):

    count = 0
    for j in range(1,M):
        Lj,Rj = lst[j]   
        if i < Lj or i > Rj:
            break
        else:
            count += 1

    if count == M-1:
        common += 1

print(common)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

a = np.ones(N)
for gate in range(M):
    mask = np.zeros(N)
    mask[L[gate]:R[gate]+1] += 1
    a *= mask

print(int(a.sum()))

Submission status: Time Limit Exceeded

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

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

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

ans = 0
t = [[0] * n for i in range(m)]

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

            t[i][j] = 1

t_sum = np.sum(t, axis=0)

for i in range(n):
    if t_sum[i] == m:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
S2 = [set(range(s[0], s[1] + 1)) for s in S]
S3 = set.intersection(*S2)
print(len(S3))

#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:
def main():
    N, M = map(int, input().split())
    box = []
    for i in range(M):
        L, R = map(int, input().split())
        box.append(L)
        box.append(R)

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

            num += plus

    print(num)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np

N, M = [int(x) for x in stdin.readline().rstrip().split()]

# カード。すべて通過できるなら１
cards = np.ones(N)

for i in range(M):
    L, R = [int(x) for x in stdin.readline().rstrip().split()]

    # [【Python】スライス操作についてまとめ \- Qiita](https://qiita.com/tanuk1647/items/276d2be36f5abb8ea52e)
    cards[:L-1] = 0
    cards[R:] = 0

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

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

    for j in range(l,r+1):
        if not j in a:
            a[j] = 1
        else:
            a[j] += 1

count = 0
for key in a.keys():
    if a[key] == 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):
    flg = 1
    if i+1 < max(L) or i+1 > min(R):
            flg = 0
    ans += flg
print(ans)

Submission status: Time Limit Exceeded

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

L = []
R = []
for i in range(M):
  L.append(LR[i][0])
  R.append(LR[i][1])
  
count = 0
flag = 0
for i in range(1, N+1):
  flag = 0
  for j in range(M):
    if not (L[j] <= i <= R[j]):
      break
    else:
      flag += 1
    if flag == M:
      count += 1

print(count)

Submission status: Time Limit Exceeded

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

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

print(len(ans))

Submission status: Time Limit Exceeded

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

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

print(len(master))

Submission status: Time Limit Exceeded

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

ans = 0
t = 0

for i in range(N): 
    t = 1
    for j in range(M):
        if not li[j][0] <= i+1 <= li[j][1]:
            t = 0
            break
    ans += t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
c = [tuple(map(int, input().split())) for _ in range(M)]
nums = []
for i in range(0, M):
    mi = list(range(c[i][0], c[i][1]+1))
    nums.append(mi)
ans = set(nums[0])
dim = nums[1:]
print(len(ans.intersection(*dim)))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    # N, M = 4, 2
    # lr = [[1, 3], [2, 4]]

    count = 0

    for i in range(N):
        f = check(lr, i + 1)
        if f:
            count += 1
    print(count)


def check(lr: list, card: int):
    for line in lr:
        f = line[0] <= card <= line[1]
        if not f:
            return False
    return True


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))

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

for i in range(M):
    Li, Ri = s_input()
    L.append(Li)
    R.append(Ri)

cards = set()
for i in range(len(L)):
    cards |= set(range(L[i], R[i]+1))

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

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

Submission status: Time Limit Exceeded

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

C = [1] * N

for i in range(M):
    a, b = map(int, input().split())
    a = a - 1
    if a != 0:
        C[:a] = [0] * a
    if b-1 != len(C):
        C[b:] = [0] * (len(C) - b)

print(sum(C))

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

    if i == 0:
        si = set(a_list[0])
    else:
        si = si & set(a_list[i])

print len(si)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as co
n,m=map(int,input().split())
a=[]
for i in range(m):
    l,r=map(int,input().split())
    
    a.extend(list(range(l,r+1)))
d=co(a)
x,y=zip(*d.items())
print(y.count(m))

Submission status: Time Limit Exceeded

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

m_array = []
for i in range(M):
    array = [0 for _ in range(N)]
    L, R = map(lambda x: int(x), input().split())
    for k in range(L - 1, R):
        array[k] = 1
    m_array.append(array)

ans_array = [1 for _ in range(N)]

for array in m_array:
    for i in range(N):
        ans_array[i] = ans_array[i] and array[i]

print(ans_array.count(1))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
each_valid_keys = [[x for x in range(l[0],l[1]+1)] for l in gate_cond]
all_valid_key = each_valid_keys[0]
for keys in each_valid_keys[1:]:
    all_valid_key = list(set(all_valid_key) & set(keys))
print(len(all_valid_key))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

_n, _m = input().split()
n = int(_n)
m = int(_m)
lr = []
f = [True] * n
for i in range(m):
    _l, _r = input().split()
    lr.append((int(_l), int(_r)))

for g in range(m):
    for h in range(0, lr[g][0] - 1):
        f[h] = False
    for h in range(lr[g][1], n):
        f[h] = False

print(f.count(True))

Submission status: Time Limit Exceeded

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

@author: Owner
"""

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

@author: Owner
"""
import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

"""



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


A = set([i for i in range(1, N+1)])

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


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

    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
L_list=[]
R_list=[]
for x in range(M):
    a,b=(int(x) for x in input().split())
    L_list.append(a)
    R_list.append(b)
count=0
for n in range(N+1):
    for m in range(M):
        if n>=L_list[m]:
            if n<=R_list[m]:
                if m==M-1:
                    count=count+1
            else:
                break
        else:
            break
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l1, r1 = map(int, input().split())
num = list(range(1, n+1))
ans = set(num[l1-1:r1])
for i in range(2, m+1):
    l, r = map(int, input().split())
    can_pass = set(num[l-1:r1])
    ans = ans & can_pass
    if len(ans) == 0:
        break
print(len(ans))

Submission status: Time Limit Exceeded

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

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

IDlist=[0]*N

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

print(IDlist.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())))

passed = set(range(gates[0][0], gates[0][1]+1))
for gate in gates[1:]:
    passed &= set(range(gate[0], gate[1]+1))
print(len(passed))

Submission status: Time Limit Exceeded

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

l = [0] * (n + 1)
s = 0

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

for k in range(n + 1):
    if l[k] == m:
        s += 1

print(s)

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    if count[i] == M:
        ans += 1
print(ans)

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([i for i in range(a,b+1)])

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

for i in range(m):
    ans = list(set(ans) & set(para_val[i]))

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = list()
for i in range(m):
    temp = list(map(int, input().split()))
    tempall = set()
    for j in range(temp[0],temp[1]+1):
        tempall.add(j)
    x.append(tempall)

ans = set(x[0])
for i in range(m):
    ans = ans & x[i]

print(len(ans))

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())
    A=[A[i] for i in range(len(A)) if A[i]>=L and A[i]<=R]
print(len(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

ans = 0
c = Counter(ll)
for k, v in c.items():
    if v == m:
        ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

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

L = []
R = []
C = [1] * N

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

for i in range(M):
    a = L[i] - 1
    b = R[i]

    if a != 0:
        C[:a] = [0] * a
    if b-1 != len(C):
        C[b:] = [0] * (len(C) - b)

print(sum(C))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n,m=map(int,input().split())
d = defaultdict(int)
for i in range(m):
  s, e = map(int,input().split())
  for j in range(s,e+1):
    j = str(j)
    d[j] += 1
#print(d)
count = 0
for k,v in d.items():
  if v==m:
    count+=1
print(count)

Submission status: Time Limit Exceeded

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

print(len(gate))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n,m = map(int, raw_input().split())
# 文字列の入力
# 出力
gate = [[0,0]]*m
for i in range(m):
    gate[i]=raw_input().split()

cnt = 0
c = 0
for j in range(n):
    j = j+1
    for i in range(m):
        if j >= int(gate[i][0]) and int(gate[i][1]) >= j:
            c += 1
    if c == m:
        cnt = cnt+1
    c = 0

print cnt

Submission status: Time Limit Exceeded

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

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

        num += plus

    print(num)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

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

def prison(N,M,L,R):
  count=0
  for n in range(N):
    num_of_truth=0
    for l,r in zip(L,R):
      if l<=(n+1)<=r:
        num_of_truth+=1
    if num_of_truth==M:
      count+=1
  return count
print(prison(N,M,L,R))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
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 = s1 & s2
    
print(len(s1))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())
gate = []
flag = 0
cnt = 0

for i in range(M):
    temp = []
    l, r = map(int, input().split())
    for j in range(r - l + 1):
        temp.append(l + j)
    gate.append(temp)

for i in range(N):
    flag = 1
    for j in range(M):
        if i + 1 not in gate[j]:
            flag = 0
            break
    if(flag == 1):
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

for i in range(M):
    s = input().split()
    L = int(s[0])
    R = int(s[1])    
    ID = [i for i in ID if (i >= L) and (i <= R)]
    
print(len(ID))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

keys = [True] * N

l1 = 1
r1 = N

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
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:
# -- coding:utf-8 --
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for ii in range(m)]
L = [lr[jj][0] for jj in range(m) ]
R = [lr[jj][1] for jj in range(m) ]

count = 0

for nn in range(1,n+1):
    check = [0]*m
    for kk in range(m):
        if nn >= L[kk] and nn <= R[kk]:
            check[kk] = 1
        if sum(check) == m:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as  np

#入力取得
N, M = map(int, input().split())


gate = []
count = 0

for i in range(M):
    x = list(map(int,input().split()))
    gate.append(x)


l_b = list(np.arange(gate[0][0], gate[0][1]+1, 1))


for i in range(len(gate)):

    l_a = list(np.arange(gate[i][0], gate[i][1]+1, 1))


    l_b = list(set(l_a) & set(l_b))


print(len(l_b))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
id=[0]*100010
for i in range(m):
    ll,rr=input().split()
    for j in range(int(ll),int(rr)+1):
        id[j]+=1

cnt=0
for i in range(1,n+1):
    if id[i]==m:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

con = 0

for i in range(N+1):
    for j in range(M):
        if L[j] > i or i > R[j]:
            break
    else:
        con += 1


print(con)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
MC = []
for i in range(M):
    L, R = map(int, input().split())
    tmp = [0] * N
    for j in range(L-1, R):
        tmp[j] = 1
    MC.append(tmp)
cnt = 0
for i in range(N):
    mc = True
    for j in range(M):
        if not MC[j][i]:
            mc = False
            break
    if mc:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

ok = [i for i in range(n+1) if L<=i and i<=R]
ok2 = []

for i in range(m-1):
  l, r = map(int,input().split())
  for j in ok:
    if not(j < l or r < j):
      ok2.append(j)
  ok = ok2
  ok2 = []
      
print(len(ok))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
ans = list(range(lr[0][0], lr[0][1]+1))
for l, r in lr:
    # print(ans, set(range(l, r+1)))
    ans = list(set(ans) & set(range(l, r+1)))
print(len(ans))

Submission status: Time Limit Exceeded

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

answer = []
tmp = []

L, R = map(int, input().split())
for i in range(L,R+1):
    answer.append(i)

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

print(len(answer))

Submission status: Time Limit Exceeded

Solution code:
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 = [1] * N
for gate in range(M):
    for id in range(1, N+1):
        if id < L[gate] or R[gate] < id:
            a[id-1] = 0

print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M = i2nn()
    R, L = i2nn()
    ss = set(range(R, L + 1))
    for i in range(1, M):
        R, L = i2nn()
        ss = ss & set(range(R, L + 1))
    print(len(ss))
    return

main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    # lr = []
    # for i in range(M):
    #     lr.append(list(map(int, list(input()))))
    # N, M = 4, 2
    # lr = [[1, 3], [2, 4]]

    count = 0

    for i in range(N):
        f = True
        for line in lr:
            f = f and line[0] <= i+1 <= line[1]
        if f:
            count += 1
    print(count)


if __name__ == '__main__':
    main()

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

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n, m = map(int, input().split())
l, r = 10**6, 0
ll = []
ans_l = []
tmp_ans = []
for i in range(m):
    a, b = map(int, input().split())
    ans_l = []
    if i == 0:
        tmp_ans = list(range(a, b+1, 1))
    else:
        for j in range(len(tmp_ans)):
            if a <= tmp_ans[j] <= b:
                ans_l.append(tmp_ans[j])
        tmp_ans = ans_l[:]
print(len(set(tmp_ans)))

Submission status: Time Limit Exceeded

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

gate = []

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

check = [0] * num[0]

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

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