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: Runtime Error

Solution code:
r=input().split()
N=int(r[0])
M=int(r[1])
data=[[int(s) for s in input().split()] for i in range(M)]
left=[]
right=[]
for i in range(M):
    left.append(data[i][0])
    right.append(data[i][1])
if min(right)-max(left)+1=<0:
    print(0)
else:
    print(min(right)-max(left)+1)

Submission status: Runtime Error

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


L, R = map(int, input().split())
ar = [i for i in range(L, R+1)]
for i in range(M-1):
      L, R = map(int, input.split())
      if ar[-1] < L or ar[0] > R:
        break
      else:
        if ar[0] < L < ar[-1]:
          ar = [x for x in range(L, ar[-1]+1)]
        elif ar[0] < R <ar[-1]:
          ar = [x for x in range(ar[0], R+1)]
print(len(ar))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
l_max=0
r_min=n 
for i in range(n):
  l,r = map(int,input().split())
  l_max = max(l_max,l)
  r_min = min(r_min,r)
print(r_min - l_max)

Submission status: Runtime Error

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


x = min(R)-max(L)


print(0 if x <0 else x+1)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
L_list = []
R_list = []
for o in range(M):
    L,R = map(int,input().split())
    L_list.append(L)
    R_list.append(R)

count = list(range(L_list[0],R_list[0]+1))
for i in range(1,M):
    count = list(count)
    if L_list[i] > count[0]:
        sta = L_list[i]
    else:
        sta = count[0]
    if R_list[i] > count[-1]:
        fin = count[-1]
    else:
        fin = R_list[i]
    count = set(count) & set(range(sta,fin+1))

ans = len(count)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

card_num, gate_num = map(int,input().split())
d = np.zeros((gate_num,2))
card_valid = [0]*card_num

for i in range(gate_num):
    l, r = map(int, input().split())
    d[i,0] = l
    d[i,1] = r
for i in range(gate_num):
    for ii in range(card_num):
        if ii + 1 >= d[i][0] and ii + 1 <= d[i][1]:
            card_valid[ii] += 1
        else:
            card_valid[ii] += 0
counter = 0
for i in range(card_num):
    if card_valid[i] == gate_num:
        counter += 1
        
print(counter)

Submission status: Runtime Error

Solution code:
import numpy as np

card_num, gate_num = map(int,input().split())
d = np.zeros((gate_num,2))
card_valid = [0]*card_num

for i in range(gate_num):
    l, r = map(int, input().split())
    d[i,0] = l
    d[i,1] = r
for i in range(gate_num):
    for ii in range(card_num):
        if ii + 1 >= d[i][0] and ii + 1 <= d[i][1]:
            card_valid[ii] += 1
        else:
            card_valid[ii] += 0
counter = 0
for i in range(card_num):
    if card_valid[i] == gate_num:
        counter += 1
print(counter)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=1
r=n
for i in range(m):
  li,ri=map(int,input().split())
  l = max(l,li)
  r = min(r,ri)
print(max(r-l,0)

Submission status: Runtime Error

Solution code:
# abc127_c.py
# https://atcoder.jp/contests/abc127/tasks/abc127_c

# C - Prison /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# N枚の ID カードと M個のゲートがあります。
# i番目のゲートは Li,Li+1,...,Ri 番目の ID カードのうちどれか 1枚を持っていれば通過できます。
# 1枚だけで全てのゲートを通過できる ID カードは何枚あるでしょうか。

# 制約
#     入力は全て整数である。
#     1≤N≤105
#     1≤M≤105
#     1≤Li≤Ri≤N

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N M
# L1 R1
# L2 R2
# ⋮
# LM RM

# 出力
# 1枚だけで全てのゲートを通過できる ID カードの枚数を出力せよ。

# 入力例 1
# 4 2
# 1 3
# 2 4

# 出力例 1
# 2

# 以下のように、1
# 枚だけで全てのゲートを通過できる ID カードは 2枚です。
#     1番目の ID カードでは 2番目のゲートを通過できません。
#     2番目の ID カードでは全てのゲートを通過できます。
#     3番目の ID カードでは全てのゲートを通過できます。
#     4番目の ID カードでは 1番目のゲートを通過できません。

# 入力例 2
# 10 3
# 3 6
# 5 7
# 6 9

# 出力例 2
# 1

# 入力例 3
# 100000 1
# 1 100000

# 出力例 3
# 100000


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    # N = int(lines[0])
    N, M = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    valueses = list()
    for i in range(M):
        valueses.append(list(map(int, lines[i+1].split())))

    ll = None
    rr = None

    log(f'valueses=[{valueses}]')

    for i in range(M):
        l = valueses[i][0]
        r = valueses[i][1]
        if ll is None:
            ll = l
        elif ll < l:
            ll = l
        else:
            log(f'll=[{l}], l=[{l}]')
        if rr is None:
            rr = r
        elif rr > r:
            rr = r
        else:
            log(f'rr=[{rr}], r=[{r}]')

    log(f'l=[{l}]')
    log(f'r=[{r}]')

    return [max(rr-ll+1, 0)]


# 引数を取得
def get_input_lines():
    lins = input()
    N, M = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(M):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4 2', '1 3', '2 4']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['10 3','3 6', '5 7', '6 9']
        lines_export = [1]
    if pattern == 3:
        lines_input = ['100000 1', '1 100000']
        lines_export = [100000]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

for i in range(m):
    if min(res) < l[i]:
        for j in range(min(res), l[i]):
            if j in res:
                res.remove(j)
    if max(res) > r[i]:
        for j in range(r[i]+1, max(res)+1):
            if j in res:
                res.remove(j)

print(len(res))

Submission status: Runtime Error

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

N,M=input2()
L,R=[],[]
for i in range(M):
	l,r=input2()
	L.append(l)
	R.append(r)

MIN=0
MAX=N

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

if MAX<MIN:
	print(0)
else:
	print((MAX-MIN)+1)

Submission status: Runtime Error

Solution code:
#36
N,M = map(int,input().split())
li = []
ri = []
x = []
y = []
L,R = map(int,input().split())
for i in range(L,R+1):
    x.append(i)
for i in range(M-1):
    L,R = map(int,input().split())
    if L < x[0]:
        L = x[0]
    if R > x[-1]:
        R = x[-1]
    for j in range(L,R+1):
        y.append(j)
    x = y
    y = []
          
cou = len(x)
print(cou)

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

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

L_max = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = map(int, input().split(" "))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_min <= i <= R_min]
print(len(ids))

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

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

L_min = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = map(int, input().split(" "))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_min <= i <= R_min]
print(len(ids))

Submission status: Runtime Error

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

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

ans = min(r) - max(s) + 1
print(ans)

Submission status: Runtime Error

Solution code:
#n=int(input())
#s,t=input().split()
n,m = map(int, input().split())
#l=list(map(int, input().split()))
l=[0]*100005
r=[1000005]*100005
z=1000010
for i in range(m):
   l[i],r[i]=map(int,input().split()) 
mi=max(l)
ma=min(r)



if mi>ma:
    print(0)
else
    print(ma-mi+1)

Submission status: Runtime Error

Solution code:
N,M=list(map(int,input_[0].split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input_[1+m].split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
L=[list(map(int,input().split()) for _ in range(m)]
  
a=max(L)
b=min(M)
if a<=b:
  print(b-a+1)
else:
  print(0)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[]
m=[]
for i in range(m):
  ll,mm=map(int,input().split())
  l.append(ll)
  m.append(mm)
a=max(l)
b=min(m)
if a<=b:
  print(b-a+1)
else:
  print(0)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
ans = 0
st = {*list(range(1, N+1))}
for _ in range(M):
    a, b = map(int, input().split())
    st.intersection_update({*list(range(a, b+1))})
print(len(st))

Submission status: Runtime Error

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)


res = min(R) - max(L) + 1
if res < 0:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

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

l = []
r = []

for i in range(M):
  ln, rn = map(int, input().split())
  l.append(ln)
  r.append(rn)
  
lmax = max(l)
rmin = min(r)

ans = rmin - lmax + 1

print(ans)

Submission status: Runtime Error

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

l, r = []

for i in range(M):
  ln, rn = map(int, input().split())
  l.append(ln)
  r.append(rn)
  
lmax = max(l)
rmin = min(r)

ans = rmin - lmax + 1

print(ans)

Submission status: Runtime Error

Solution code:

n,m = map(int, raw_input().split(' '))
keys = set(range(1,n+1))
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
    keys = keys.intersection(set( range(a,b+1)  ))
    if len(keys) == 0:
      break
print len(keys)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10*7)

def judge(c,l,r,counter):
  if counter == 0:
    return c
  elif len(c) == 0:
    return c
  else:
    return judge(list(filter(lambda x: l[0]<=x<=r[0],c)),l[1:],r[1:],counter-1)

def solve(c,l,r,m):
  return len(judge(c,l,r,m))
  
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())
cards = range(1,N+1)
print(solve(cards,L,R,M))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10*7)

def judge(c,l,r,counter):
  if counter == 0:
    return c
  else:
    return judge(list(filter(lambda x: l[0]<=x<=r[0],c)),l[1:],r[1:],counter-1)

def solve(c,l,r,m):
  return len(judge(c,l,r,m))
  
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())
cards = range(1,N+1)
print(solve(cards,L,R,M))

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range():
  a, b = rm()
  a = max(a, l)
  b = min(b, inf)
print(b-a+1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,M= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
left=1
right=N
for m in range(M):
    l,r = map(int,input().split())
    if left<=l and l<=right:
        left=l
    if left<=r and r<=right:
        right=r

    if right<l or r<left:
        p=0

if not p:
    print(p)
    exit()
print(right-left+1)

Submission status: Runtime Error

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

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

print(max(0,min(R)-max(L)+1))

Submission status: Runtime Error

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

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

print(max(0,min(R)-max(L))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

n, m = [int(x) for x in input().split()]
a = [0 for i in range(m + 2)]
for i in range(n):
	l, r = [int(x) for x in input().split()]
    a[l] += 1
    a[r + 1] -= 1
a = list(accumulate(a))
print(a.count(m))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
li,lia = [],[]
for i in range(m):
    a,b = map(int,input().split())
    li.append(a)
    lia.append(b)
print(min(min(lia)-max(li)+1),0)

Submission status: Runtime Error

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

L = 1
R = 10**5

for i in range(m):
    l, r = map(int, input().split())
    if L <= l <= R:
        L = l
    if L <= r <= R:
        R = r
    if R < l or r < L:
        ans = 0
        break

if ans != 0:
    ans = R - L + 1

print(ans)

Submission status: Runtime Error

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

hashset = None

for _ in range(M):
	for L, R in map(int, input().split()):
      if hashset is None:
        hashset = set(range(L, R+1))
      else:
        hashset = hashset & set(range(L, R+1))

print(len(hashset))

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,M = IL()

L = 0
R = INF

for i in range(N):
  l,r = IL()
  L = max(L,l)
  R = min(R,r)
  
print(R-L+1)

Submission status: Runtime Error

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

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

cnt=0
for k in range(m):
    if s[k]==m:
        cnt+=1

print(cnt)

Submission status: Runtime Error

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

# check
mastar_keys = set(list(range(1, N + 1)))
for k in sorted(check, key=lambda x: len(x)):
    mastar_keys = mastar_keys & check[k]
    if len(mastar_keys) == 0:
        break

print(len(mastar_keys))

Submission status: Runtime Error

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

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

for i in range(M):
    x,y= map(int,input().split())
    L = max(L,x)
    R = min(R,y)

print(R-L+1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
ans=list(range(1,n+1))
for i in range(m):
    a,b=map(int,input().split())
    if a-1 in ans:
        ans=ans[ans.index(a-1):]
    if b+1 in ans:
        ans=ans[:ans.index(b)]
print(len(ans))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)
###################################################
def main():
    N,M=list(map(int,list(input().split())))
    A=[0]*(N+1)
    for a in range(M):
        l,r=map(int,input().split())
        A[l]+=1
        A[r+1]-=1
    for a in range(1,len(A)):
        A[a]+=A[a-1]
    print(A.count(N))
main()

Submission status: Runtime Error

Solution code:
import numpy as np
def main():
    N, M = map(int, input().split())
    f = np.zeros((M,N))
    for i in range(M):
        l, r = map(int, input().split())
        f[i, l-1:r] += 1
    ans = sum(np.all(f, axis=0))
    print(ans)
main()

Submission status: Runtime Error

Solution code:
10 3
3 6
5 7
6 9

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
d=[]
for _ in range(m):
    d.append(list(map(int,input().split())))
def ID(i):
    if i==1:
        return (d[0][0],d[0][1])
    l,r=ID(i-1)
    L=max(l,d[i-1][0])
    R=min(r,d[i-1][1])
    return (L,R)
s,g=ID(m)
print(max(g-s+1,0))

Submission status: Runtime Error

Solution code:
mport numpy as np
n, m = map(int, input().split())
table = np.ones(n, dtype=int)
for i in range(m):
    l, r = map(int, input().split())
    table[:l-1] = 0
    table[r+1:] = 0
print(sum(table))

Submission status: Runtime Error

Solution code:
import java.io.*;
import java.util.*;

public class Main {
    static class Solver {
        public void solve(MyReader in, PrintWriter out) {
            int n = in.nextInt();
            int m= in.nextInt();

            int[][] gates = new int[m][2];
            for (int i = 0; i < m; i++) {
                gates[i] = new int[]{in.nextInt(),in.nextInt()};
            }

            Arrays.sort(gates, Comparator.comparingInt(a -> a[0]));

            int l = 1, r = n;
            for (int i = 0;i<m;i++){
                int cl = gates[i][0], cr= gates[i][1];

                if (cl <= r){
                    l = cl;
                    r = Math.min(r,cr);
                }
                else{
                    l = 0;
                    r = -1;
                    break;
                }
            }

            out.println(r-l +1);
        }


    }

    public static void main(String[] args) {
        MyReader mr = new MyReader();
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        new Solver().solve(mr, out);
        out.close();
    }

    //////////////////////////////////////////////////////////////////////////
    //    static int[][] dir = {{0,1}, {1,0},{-1,0}, {0,-1}};
//    static int[][] dir = {{0,1}, {1,0},{-1,0}, {0,-1}, {-1,-1},{-1,1}, {1,1}, {1,-1}};
    static class MyReader {
        BufferedReader br;
        StringTokenizer st;

        MyReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String res = "";
            try {
                res = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return res;
        }

        int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }

            return arr;
        }

        Integer[] nextIntegerArray(int n) {
            Integer[] arr = new Integer[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }

            return arr;
        }

        Long[] nextLongArray(int n) {
            Long[] arr = new Long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }

            return arr;
        }

        long[] nextlongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }

            return arr;
        }

        String[] nextStringArray(int n) {
            String[] arr = new String[n];
            for (int i = 0; i < n; i++) {
                arr[i] = next();
            }
            return arr;
        }
    }

    /////////////////////////////////////////////////////////////    

    static class Pair<K, V> {
        K x;
        V y;

        public Pair(K x, V y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Pair<?, ?> pair = (Pair<?, ?>) o;
            return Objects.equals(x, pair.x) &&
                    Objects.equals(y, pair.y);
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }


    /////////////////////////////////////////////////////////////

    static <T> void swap(T[] arr, int i, int j) {
        T tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

}

Submission status: Runtime Error

Solution code:

n,m = map(int,input().split())
s = -1
t = n+1
for i in range(m):
    l_i,r_i = map(int,input().split())
    l = max(l,l_i)
    r = min(r,r_i)
print(max(0,r-l+1))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
L = [0] * M
R = [0] * M
min = 1
max = N
X = 0
for i in range(M):
    L[i], R[i] = map(int, input().split())
    if L[i] > min:
        min = L[i]
    if R[i] < max:
        max = R[i]
    if R[i] < min or L[i] > max:
        X +=1


if X = 0:
    print(max-min+1)
else:
    print(0)

Submission status: Runtime Error

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

for i in li:
    max_l = max(max_l,li[0])
    min_r = min(min_r,li[1])
print(min_r-max_l+1)

Submission status: Runtime Error

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

for j in li:
    cnt.add(set(num for num in range(j[0],j[1]+1)))

ans = cnt[0]
for k in range(len(cnt)-1):
    ans = ans & cnt[k+1]
print(len(ans))

Submission status: Runtime Error

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

for i in range(m):
  l,r = list(map(int,input().split()))
  ll.append(l)
  rl.append(r)
  
if min(rr)<max(ll):
  print("0")

else:
  print(abs(max(rr)-min(ll)+1))