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: Accepted

Solution code:
#n: id card
#m: gate
n, m = map(int, input().split())
data = [0 for i in range(n+1)]

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

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

print(data.count(m))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l = 1
r = N
for m in range(M):
  m_l, m_r = map(int, input().split())
  l = max(l, m_l)
  r = min(r, m_r)
  if l>r:
    print(0)
    exit()
print(r+1-l)

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
_min_list=[]
_max_list=[]
for i in range(M):
	_min,_max=map(int, input().split())
	_min_list.append(_min)
	_max_list.append(_max)

min_value=max(_min_list)
max_value=min(_max_list)

if min_value > max_value:
	print(0)
else:
	print(max_value-min_value+1)

Submission status: Accepted

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

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

id_l,id_r = max(L),min(R)
width = id_r - id_l + 1 if max(L)<=min(R) else 0

print(width)

Submission status: Accepted

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

listA=[]
while True:
    try:
        listA.append(list(map(int,input().split())))
    except:
        break;
L=[]
R=[]
for i in listA:
  L.append(i[0])
  R.append(i[1])
maxL = max(L)
minR = min(R)

count = 0
for i in range(1,N+1):
  if maxL <= i <= minR:
    count+=1
print(count)

Submission status: Accepted

Solution code:

n, m = map(int, input().split())
card = [0 for _ in range(n+2)]

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

#print(card)

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

#print(card)
print(card.count(m))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
L=[]
R=[]
for i in range(m):
  L.append(l[i][0])
  R.append(l[i][1])
print(max(min(R)-max(L)+1,0))

Submission status: Accepted

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())
mL = max(L)
mR = min(R)
ans = 0
for i in range(1,N+1):
    if ((mL <=i) and (i<=mR)):
        ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

L, R = 1, N

for _ in range(M):
    a, b = list(map(int, input().split()))
    L = max(a, L)
    R = min(b, R)

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

Submission status: Accepted

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)
print(max(min(R)-max(L)+1, 0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mis,mas=[],[]
for _ in range(m):
  tmp=list(map(int,input().split()))
  mis.append(tmp[0])
  mas.append(tmp[1])
ans=min(mas)-max(mis)+1
print(ans if ans>0 else 0)

Submission status: Accepted

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

maxtable1 = sorted(table, key = lambda x: x[0])
maxtable2 = sorted(table, key = lambda x: x[1])

print(max(0, maxtable2[0][1] - maxtable1[m-1][0] + 1))

Submission status: Accepted

Solution code:
#ABC 127 C Prison
N,M= map(int,input().split())
MIN=0
MAX=N
for i in range(M):
    L,R= map(int,input().split())
    MAX=min(MAX,R)
    MIN=max(MIN,L)
ans =MAX-MIN+1
print(max(0,ans))

Submission status: Accepted

Solution code:
#ABC 127 C Prison
N,M= map(int,input().split())
MIN=0
MAX=N
for i in range(M):
    L,R= map(int,input().split())
    MAX=min(MAX,R)
    MIN=max(MIN,L)
ans =MAX-MIN+1
if ans <0:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(M)]
L = [LR[i][0] for i in range(M)]
R = [LR[i][1] for i in range(M)]
print(max(min(R)-max(L)+1,0))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
Left = []
Right = []
for i in range(M):
  L,R = map(int,input().split())
  Left.append(L)
  Right.append(R)
dif = min(Right) - max(Left)
print(dif+1 if dif >= 0 else 0)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MAX=0
MIN=N
for i in range(M):
  L,R=map(int,input().split())
  MAX=max(MAX,L)
  MIN=min(MIN,R)
A=MIN-MAX+1
if A<=0:
  print(0)
else:
  print(A)

Submission status: Accepted

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

L = 1
R = N
for LR in LR_list:
    if LR[0] > L:
        L = LR[0]
    if LR[1] < R:
        R = LR[1]
output = 0
if L <= R:
    output = R - L + 1
print (output)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = 0
r = 10 ** 5 + 1
for i in range(m):
    L, R = map(int, input().split())
    if L > l:
        l = L
    if R < r:
        r = R
print(max(0, r - l + 1))

Submission status: Accepted

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)
print(max(min(R)-max(L)+1, 0))

Submission status: Accepted

Solution code:
N ,M = map(int , input().split())
max_L = []
min_R = []
gate_number = [i for i in range(1, M+1)]
for j in range(M):
    L , M = map(int,input().split())
    max_L.append(L)
    min_R.append(M)
print(len([s for s in range(max(max_L) , min(min_R) + 1)]))

Submission status: Accepted

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

left  = 1
right = N
for i in range(M):
    L, R = map(int, input().split())
    left  = max(left,  L)
    right = min(right, R)

print(right - left + 1 if left <= right else 0)

Submission status: Accepted

Solution code:
N, M = [int(nm) for nm in input().split()]
L, R = [int(lr) for lr in input().split()]
LR = [L, R]
for m in range(M-1):
  L_2, R_2 = [int(lr) for lr in input().split()]
  if L_2 < LR[0]:
    if R_2 < LR[0]:
      print(0)
      break
    elif LR[0] <= R_2 <= LR[1]:
      LR = [LR[0], R_2]
    else: # LR[1] < R_2:
      continue
  elif LR[0] <= L_2 <= LR[1]:
    if LR[0] <= R_2 <= LR[1]:
      LR = [L_2, R_2]
    else: # LR[1] < R_2:
      LR = [L_2, LR[1]]
  elif LR[1] < L_2:
    print(0)
    break
else:
  card = LR[1] - LR[0] + 1
  print(card)

Submission status: Accepted

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

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

print(len([i for i in range(max(all_L), min(all_R)+1)]))

Submission status: Accepted

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

l = 1
r = N
flag = False
for i in range(M):
    l_1, r_1 = list(map(int, input().split()))
    if l_1 > l:
        l = l_1
    if r_1 < r:
        r = r_1
    if l > r:
        flag = True
        break
    
if flag:
    print(0)
else:
    print(r - l + 1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m=map(int,input().split())
L=[]
R=[]
for _ in range(m):
    a,b=map(int,input().split())
    L.append(a)
    R.append(b)
print(max(min(R)-max(L)+1,0))

Submission status: Accepted

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

L_max = max(L)
R_min = min(R)

count = 0
for i in range(1, N + 1):
    if i >= L_max and i <= R_min:
        count += 1
print(count)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l = 0
r = N + 1
for _ in range(M):
  ll, rr = map(int, input().split())
  l = max(l, ll)
  r = min(r, rr)
print(max(0, r - l + 1))

Submission status: Accepted

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

l_max=max(l)
h_min=min(h)
if (h_min-l_max)<0:
    print(0)
else:
    print(h_min-l_max+1)

Submission status: Accepted

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

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

max_L, min_R = 0, N+1
for i in range(M):
    L, R = map(int, input().split())
    max_L = max(max_L, L)
    min_R = min(min_R, R)

ans = max(min_R - (max_L - 1), 0)

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = 0
r = n
for _ in range(m):
    nl, nr = map(int, input().split())
    l = max(nl, l)
    r = min(nr, r)
 
print(max(r - l + 1, 0))

Submission status: Accepted

Solution code:
from itertools import accumulate
N,M=map(int,input().split())
L = [0]*(N+1)
for _ in range(M):
  l,r = map(lambda x:int(x)-1,input().split())
  L[l]+=1
  L[r+1]-=1

L = list(accumulate(L))
print(L.count(M))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = 0
r = n
for _ in range(m):
    nl, nr = map(int, input().split())
    l = max(nl, l)
    r = min(nr, r)

print(max(r - l + 1, 0))

Submission status: Accepted

Solution code:
def main():
    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)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
N,M = map(int,input().split())
L,R = sorted(map(int, input().split()))
range_min = L
range_max = R

def end_checker(L,R,range_min,range_max):
  if  L > range_max or R < range_min:
    return True
  return False

while M > 1:
  L,R = sorted(map(int, input().split()))
  if end_checker(L,R,range_min,range_max):
    print(0)
    sys.exit()
  
  range_min = L if L > range_min else range_min
  range_max = R if R < range_max else range_max
  M -= 1

ans = range_max - range_min + 1  
print(ans)

Submission status: Accepted

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)
    
j = min(R) -max(L) + 1

if j < 0 : 
    print(0)
else : 
    print(j)

Submission status: Accepted

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

cards = np.zeros(n + 1)

for i in range(m):
    left, right = map(int, input().split())
    cards[left - 1] += 1
    cards[right] += -1
print(np.sum(cards.cumsum() >= m))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=0
r=10000000000000
for i in range(m):
  a,b=map(int,input().split())
  l=max(l,a)
  r=min(r,b)
if r-l+1 >0:
  print(r-l+1)
else:
  print(0)

Submission status: Accepted

Solution code:
def solve(n, m, l, r):
    return max(0, min(r) - max(l) + 1)

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())
print(solve(n, m, l, r))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
count = 0
for i in range(M):
    a,b = map(int,input().split())
    if count == 0:
        L = a
        R = b
        count = 1
    else:
        if L < a:
            L =a
            if L>R:
                break
        if R > b:
            R = b
            if R < L:
                break
if M == i+1:
    print(R-L+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = []
r = []
for i in range(m):
    l0,r0 = map(int,input().split())
    l.append(l0)
    r.append(r0)
ans = min(r)-max(l)+1
if ans >= 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

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

lmax = -1
rmin = n * 100
for i in range(m):
    l, r = map(int, input().split())
    lmax = max(l, lmax)
    rmin = min(r, rmin)

print(max(rmin - lmax + 1, 0))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
LMax,RMin = 0,n
for i in range(m):
	l,r =map(int,input().split())
	LMax = max(LMax,l)
	RMin = min(RMin,r)
if RMin < LMax:
	print(0)
else:print(RMin - LMax + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
table=[0]*(n+2)
for i in range(m):
    L[i],R[i]=map(int,input().split())
    table[L[i]]+=1
    table[R[i]+1]-=1
s=[0]*(n+2)#累積和をするリスト
for i in range(1,n+2):
    s[i]=s[i-1]+table[i]
print(s.count(m))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
table=[0]*(n+3)
for i in range(m):
    L[i],R[i]=map(int,input().split())
    table[L[i]]+=1
    table[R[i]+1]-=1
s=[0]*(n+3)#累積和をするリスト
for i in range(1,n+3):
    s[i]=s[i-1]+table[i]
print(s.count(m))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
for i in range(m):
    L[i],R[i]=map(int,input().split())
ans=min(R)-max(L)+1
print(max(ans,0))

Submission status: Accepted

Solution code:
def Technique_often_used():    
    # enumerate: for i, a in enumerate(a_list):
    # q, mod = divmod(a, b)
    # Functional Programming: filter, map, reduce
    pass
def naming_conventions():
    # if [s_1, s_2, s_3, ..., s_k] then I name this list sk
    # if [[s_11, s_12, ..., s_1k], ..,[s_j1, s_j2, ..., s_jk] then I name this list sjk
    # if there is something strange in a list then I add _ to the list's name (ex:sjk_)
    pass
def visual_studio_code_shortcut():    
    # visual studio code shortcut https://qiita.com/TakahiRoyte/items/cdab6fca64da386a690b
    # delete line: Ctrl+Shift+k
    # choose same words: Ctrl+Shift+l
    pass
from itertools import combinations, permutations # https://docs.python.org/ja/3/library/itertools.html
from collections import deque, Counter # https://docs.python.org/ja/3/library/collections.html#collections.deque
from heapq import heapify, heappop, heappush, heappushpop, heapreplace # https://docs.python.org/ja/3/library/heapq.html
from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
from functools import reduce

from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers): #
    return reduce(lcm, numbers, 1)

# toggle test environment
is_test = False
def debug_print(value,comments="",is_test=is_test):
    # print() for test environment
    # debug_print([]) => []
    # debug_print("") => 
    # debug_print(a, "a is") => "a is:" print(a)
    if not is_test:
        pass
    elif not comments and value is None:
        print("")
    elif not comments:
        print(value)
    else:
        print(comments,":",value)

# set the inputs
if is_test:
    n,m = [10, 3]
    lr = [[3,6],[5,7],[6,9]]
else:        
    R = lambda: map(int, input().split())
    # ex1) n, k = R()
    # ex2) v = list(R())
    n,m = R()
    l,r = 0, 10**6
    for i in range(m):
        l_, r_ = R()
        l = max(l, l_)
        r = min(r, r_)

print(max(r-l+1,0))
    
# solve the problem

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(m):
  l,r = list(map(int,input().split()))
  a.append(l) 
  b.append(r)

a_max = max(a)
b_min = min(b)

if b_min < a_max:
 print(0)
else:
 print(b_min - a_max + 1)

Submission status: Accepted

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    diff = min(R) - max(L)
    if diff < 0:
        print(0)
    else:
        print(diff + 1)
    return


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


if min(r) >= max(l):
    print(min(r) - max(l) + 1)
else:
    print(0)

Submission status: Accepted

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

for l,r in LR:
    left = max(left,l -1)
    right = min(right,r -1)

if left > right:
    print(0)
else:
    print( right - left +1)

Submission status: Accepted

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

L_list = []
R_list = []

for i in range(M):
  L, R  = map(int, input().split())
  L_list.append(L)
  R_list.append(R)
 
Lmax = max(L_list)
Rmin = min(R_list)

if Rmin - Lmax < 0:
  print('0')
else:
  print(int(Rmin - Lmax +1))

Submission status: Accepted

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

Lmax = 0
Rmin = 10**5 + 1

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

    Lmax = max(Lmax,L)
    Rmin = min(Rmin,R)


if Lmax <= Rmin :
    print(int(Rmin - Lmax + 1))
else :
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
cnt = 0
id = []
for _ in range(m):
        l= list(map(int, input().split()))
        if len(id) == 0:
                id = l
        else:
                if id[0] < l[0]:
                        id[0] = l[0]
                elif id[1] > l[1]:
                        id[1] = l[1]
if id[1] < id[0]:
        cnt = 0
else:
        cnt = id[1]-id[0]+1
print(cnt)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
rm=10**5+1
lm=0
for i in range(m):
    l,r=map(int,input().split())
    if r<rm:
        rm=r
    if l>lm:
        lm=l
if rm-lm+1<0:
    print(0)
else:
    print(rm-lm+1)

Submission status: Accepted

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

Lmax, Rmin = 1, N
for i in range(M):
     L, R = [int(_) for _ in input().split()]
     Lmax = max(Lmax, L)
     Rmin = min(Rmin, R)

print(max(0, Rmin - Lmax + 1))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
l = 1
r = N
for i in range(M):
    L,R = map(int,input().split())
    l = max(l,L)
    r = min(r,R)
if r-l >= 0:
    print(r-l+1)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys

n,m = map(int, sys.stdin.readline().split())
less,more = 0,n

for i in range(m):
    l,r = map(int, sys.stdin.readline().split())
    less = max(less, l)
    more = min(more, r)

print max(0,1+more-less)

Submission status: Accepted

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

allL = 1
allR = N
for L, R in (map(int, input().split()) for _ in range(M)):
  allL = max(allL, L)
  allR = min(allR, R)

print(max(allR - allL + 1, 0))

Submission status: Accepted

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

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
p = [0] * (N + 1)

for l, r in LR:
    p[l - 1] += 1
    p[r] -= 1
    
for i in range(N):
    p[i + 1] += p[i]
    
print(len([v for v in p if v == M]))

Submission status: Accepted

Solution code:
def main():
	N, M = map(int, input().split())
	l, r = 0, 10**9+7
	for _ in range(M):
		L, R = map(int, input().split())
		l = max(L, l)
		r = min(R, r)
	print(max(r-l+1, 0))


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
gates = []
for i in range(m):
    gates.append(list(map(int, input().split())))
candidate = [1, n]
for gate in gates:
    if candidate[0] < gate[0]:
        candidate[0] = gate[0]
    if gate[1] < candidate[1]:
        candidate[1] = gate[1]
if (candidate[1] >= candidate[0]):
    print(candidate[1] - candidate[0] + 1)
else:
    print(0)

Submission status: Accepted

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

min_right = min(right)
max_left = max(left)
if min_right < max_left:
    print(0)
else:
    print(min_right - max_left + 1)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=1
R=100000
for i in range(M):
  l,r=map(int,input().split())
  L=max(L,l)
  R=min(r,R)
if L>R :
  print(0)
else :
  print(R-L+1)

Submission status: Accepted

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

for m in range(M):
    L_in, R_in = list(map(int, input().split()))
    L.append(L_in)
    R.append(R_in)

if min(R) >= max(L):
    print(min(R) - max(L) + 1)
else:
    print(0)

Submission status: Accepted

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

# In[15]:


import sys
input = sys.stdin.readline
N, M = map(int, input().split())


# In[ ]:


"""
4 2
1 3
2 4
"""


# In[19]:


l_ = 0
r_ = N
for i in range(M):
    l, r = map(int, input().split())
    l_ = max(l_, l)
    r_ = min(r_, r)


# In[21]:


#print(l_, r_)
ids = r_-l_+1
if ids < 0:
    print(0)
else:
    print(ids)


# In[ ]:

Submission status: Accepted

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

l = -1
r = n+1
for i in range(m):
  l = max(l,gate[i][0])
  r = min(r,gate[i][1])
print((r-l+1)*(l <= r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
card_gate = input().split()
card = int(card_gate[0])
gate = int(card_gate[1])
L_list = []
R_list = []

for i in range(0, gate):
    id_list = input().split()
    L_list.append(int(id_list[0]))
    R_list.append(int(id_list[1]))

print(len(range(max(L_list), min(R_list)+1)))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mir = n
mal = 0
for _ in range(m):
  l,r = map(int,input().split())
  if l > mal:
    mal = l
  if r < mir:
    mir = r
print(0 if mir-mal < 0 else (mir-mal)+1)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for i in range(m)]
lr.sort()
r_min = 100000
l_max = 0
lr_min = 100000

for i in range(m):
    l_max = max(lr[i][0], l_max)
    r_min = min(lr[i][1], r_min)

lr_min = [r_min - l_max + 1 if l_max <= r_min else 0]
print(lr_min[0])

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
arr = [1, n]
check = True
for i in range(m):
    l, r = map(int, input().split())
    if l <= arr[0] and r>= arr[1]:
        continue
    elif l > arr[1] or r < arr[0]:
        check = False
    if arr[0] <= l and l <= arr[1]:
        arr[0] = l
    if arr[1] >= r and r >= arr[0]:
        arr[1] = r
if check:
    print (arr[1] - arr[0] + 1)
else:
    print (0)

Submission status: Accepted

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

# -*- coding: utf-8 -*-


from sys import stdin
import itertools



############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
M = int(temp[1])

data = []
for i in range(M):
    write = stdin.readline().split()
    data.append(write)

mina = 0
maxa = 10e7
for num in data:
    if int(num[0]) > mina:
        mina = int(num[0])
    if int(num[1]) < maxa:
        maxa = int(num[1]) 

count = maxa-mina+1

if count < 0:
    print(0)
else:
    print(count)

Submission status: Accepted

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

a = np.max(LR_mat[:,0],axis=0)
b = np.min(LR_mat[:,1],axis=0)
num_ID = int(b-a+1)
if num_ID >= 0:
  print(num_ID)
else:
  print(0)

Submission status: Accepted

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

s=[]
l= 0
r = n
for i in range(m):
    ln,rn=map(int,input().split())
    if ln>l:
        l=ln
    if rn<r:
        r=rn
if r-l+1>0:
    print(r-l+1)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
n=0
m=0

n, m  = map(int, input().split())
_min, _max  = map(int, input().split())
for i in range(1, m):
  gate_min, gate_max = map(int, input().split())
  if (_max > gate_max):
    _max = gate_max
   
  if _min < gate_min:
    _min = gate_min
 
result_list = list(filter(lambda x : (_min <= x) and (x <= _max), range(1, n+1)))
result = len(result_list)

print(result)
sys.exit(0)

Submission status: Accepted

Solution code:
def larger(x, y):
	if x > y: return x
	return y
def smaller(x, y):
	if x < y: return x
	return y

N, M = map(int, raw_input().split())
L = 1
R = N
flag = True
for i in range(0, M):
	lft, rgt = map(int, raw_input().split())
	if R < lft or rgt < L:
		flag = False
		break
	L = larger(L, lft)
	R = smaller(R, rgt)
if flag: print R-L+1
else: print 0

Submission status: Accepted

Solution code:
nm = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for i in range(nm[1])]

l = [i[0] for i in lr]
r = [i[1] for i in lr]
print(len(range(max(l), min(r)+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m,= map(int,input().split())  #n:カード，m:ゲートの数

l = [0]*m
r = [0]*m

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


lmax = max(l)
rmin = min(r)

if (lmax<=rmin):
    print(rmin-lmax+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
acc = [0 for i in range(N + 1)]
for i in range(M):
    L, R = map(int, input().split())
    L -= 1
    acc[L] += 1
    acc[R] -= 1
for i in range(N):
    acc[i + 1] += acc[i]
result = 0
for i in range(N):
    if acc[i] == M:
        result += 1
print(result)

Submission status: Accepted

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

l, r = 0, 10 ** 6
for i in range(m):
    tl, tr = map(int, input().split())
    l = max(tl, l)
    r = min(tr, r)
print(len(range(l, r+1)))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = []
R = []
for i in range(1,M+1):
  L_val,R_val = map(int,input().split())
  L.append(L_val)
  R.append(R_val)
ans = max(min(R) - max(L) + 1,0)
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = []
r = []
for i in range(1, m + 1):
    s, t = map(int, input().split())
    l.append(s)
    r.append(t)
ans = max(min(r) - max(l) + 1, 0)
print(ans)

Submission status: Accepted

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

lmin = 1
rmax = n
finish = False
for i in range(m):
    l, r = map(int, input().split())

    if l > lmin:
        lmin = l

    if r < rmax:
        rmax = r
    
    if rmax < lmin:
        finish = True
        

if finish:
    print(0)
else:
    print(rmax - lmin + 1)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]

def check(n, LR):
    return 1 if all(l <= n <=r for l, r in LR) else 0

L = 0
R = N
for _ in range(M):
    l, r = [int(x) for x in input().split()]
    if L < l:
        L = l
    if r < R:
        R = r

if L <= R:
    print(R - L + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
L = 1
R = 100000
for _ in range(M):
    l, r = [int(i) for i in input().split()]
    L = max(L, l)
    R = min(R, r)
if L > R:
    print(0)
else:
    print(R-L+1)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
lv=1
rv=N
for _ in range(M):
 L,R=map(int,input().split())
 lv=max(lv,L)
 rv=min(rv,R)
print(0 if lv>rv else rv-lv+1)

Submission status: Accepted

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)

ans = min(R) -max(L) + 1
print(ans if ans > 0 else 0)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    L = 0
    R = 10**5 + 1
    for _ in range(M):
        left, right = map(int, input().split())
        L = max(L, left)
        R = min(R, right)

    return max(R - L + 1, 0)


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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a,b=1,n
for i in range(m):
    l,r=map(int,input().split())
    a=max(a,l)
    b=min(b,r)
print(0 if a>b else b-a+1)

Submission status: Accepted

Solution code:
a=input().split()
N,M=int(a[0]),int(a[1])
L,R=[],[]
ans=0
o,p,q,O=[],[],[],[]
for i in range(0,M):
    a=input().split()
    L.append(int(a[0]))
    R.append(int(a[1]))
    a.clear()
num=len(L)

l=max(L)
r=min(R)+1

for i in range(l,r):
    ans=ans+1
print(ans)

Submission status: Accepted

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

print(max(res[1]-res[0]+1,0))

Submission status: Accepted

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

for i in LR:
    if i[0] > L:
        L = i[0]
    
    if i[1] < R:
        R = i[1]

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left = 0
right = N
for _ in range(M):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)

print(max(right - left + 1, 0))

Submission status: Accepted

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

small = []
large = []

for i in range(M):
    small.append(gate[i][0])
    large.append(gate[i][1])

small.sort(reverse=True)
large.sort()

if small[0] <= large[0]:
    ans = large[0] - small[0] + 1
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
L,M=map(int,input().split())
MAX=0
MIN=L
for i in range(M):
  A,B=map(int,input().split())
  MAX=max(MAX,A)
  MIN=min(MIN,B)
if MIN-MAX+1<=0:
  print(0)
else:
  print(MIN-MAX+1)