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:
L,M=map(int,input().split())
MAX=0
MIN=10**5
for i in range(M):
  A,B=map(int,input().split())
  MAX=max(MAX,A)
  MIN=min(MIN,B)

print(max(0,MIN-MAX+1))

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)

print(max(0,MIN-MAX+1))

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
mi, ma = 1, N
for _ in range(M):
    L, R=map(int, input().split())
    mi = max(L, mi)
    ma = min(R, ma)
print(max(0, ma-mi+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
data=[[int(x) for x in input().split()] for m in range(M)]
_min=max([m[0] for m in data])
_max=min([m[1] for m in data])
a=_max-_min+1
print(a if a>=0 else 0)

Submission status: Accepted

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

for i in range(m):
    l, r = map(int, input().split())
    if min < l:
        min = l
    if r < max:
        max = r

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
card=[0 for _ in range(n)]
l, r = map(int, input().split())
l-=1
for i in range(m-1):
    li, ri = map(int, input().split())
    li-=1
    if li>r or ri<l:
        l=r
        break
    elif li>=l and ri<=r:
        l=li; r=ri
    elif li<=l and ri>=r:
        pass
    elif li<=r and ri>=r:
        l=li
    elif ri>=l and li<=l:
        r=ri
    else:
        pass
print(r-l)

Submission status: Accepted

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

Submission status: Accepted

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

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

L = []
R = []
for i in LR:
  L.append(i[0])
  R.append(i[1])
  
if min(R) - max(L) < 0:
  print(0)
else:
  print(min(R) - max(L) + 1)

Submission status: Accepted

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

L = 1
R = N
for l, r in LR:
    L = max(L, l)
    R = min(R, r)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L = []
P = []
for i in range(m):
    a, b = map(int, input().split())
    L.append(a)
    P.append(b)
ans = min(P) - max(L) + 1
if(ans > 0):
    print(ans)
else:
    print('0')

Submission status: Accepted

Solution code:
idCards, gates = input().split(" ")
idCards = int(idCards)
gates = int(gates)
minNum = []
maxNum = []

for count in range(0, gates, 1):
    num1, num2 = input().split(" ")
    minNum.append(int(num1))
    maxNum.append(int(num2))

minId = max(minNum)
maxId = min(maxNum)
if minId > maxId:
    print("0")
else:
    print((maxId - minId) + 1)

Submission status: Accepted

Solution code:
import sys
idCards, gates = input().split(" ")
idCards = int(idCards)
gates = int(gates)
minNum = []
maxNum = []

if idCards > 1000000 or gates > 1000000:
    sys.exit()

for count in range(0, gates, 1):
    num1, num2 = input().split(" ")
    minNum.append(int(num1))
    maxNum.append(int(num2))

minId = max(minNum)
maxId = min(maxNum)
if minId > maxId:
    print("0")
else:
    print((maxId - minId) + 1)

Submission status: Accepted

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

c = [0] * (n+1)

for L, R in LR:
    c[L - 1] += 1
    c[R] -= 1

ans = 0
temp = 0
for x in c:
    temp += x
    if temp == m:
        ans += 1

print(ans)

Submission status: Accepted

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

Lmin = 0
Rmax = N
for i in range(M) :
    L,R = [int(i) for i in input().split()]
    if (Lmin < L) :
        Lmin = L
    
    if (Rmax > R) :
        Rmax = R

if (Rmax-Lmin >= 0) :
    print(Rmax-Lmin+1)
else :
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

# 初期値を入力

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

A = np.zeros((M, 2))

for i in range(M):
    A[i, 0], A[i, 1] = map(int, input().split())

# メインプログラム

Flag = 0

Length = np.array([A[i, 0], A[i, 1]])

for i in range(M - 1):
    Length[0] = max(Length[0], A[i, 0])
    Length[1] = min(Length[1], A[i, 1])
    if Length[1] < Length[0]:
        Flag = 1


if Flag == 0:
    print(int(Length[1] - Length[0] + 1))
else:
    print(int(0))

Submission status: Accepted

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

Submission status: Accepted

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

ans=min(rigth)-max(left)+1
if ans<0:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
def main():
    n, m = [int(x) for x in input().rstrip().split(" ")]
    
    lowest_id = 1
    highest_id = n
    for i in range(m):
        l, r = [int(x) for x in input().rstrip().split(" ")]
        lowest_id = max(lowest_id, l)
        highest_id = min(highest_id, r)
    if lowest_id > highest_id:
        print(0)
    else:
        print(highest_id - lowest_id + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
l = [0]*m
r = [0]*m
for i in range(m):
    l[i], r[i] = [int(x) for x in input().split()]
lmax = max(l)
rmin = min(r)
if rmin >= lmax:
    print(rmin-lmax+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mi = 0
ma = 10**5+1
for i in range(m):
  s,e = map(int,input().split())
  if s > mi:
    mi = s
#    print('mi',mi)
  if e < ma:
    ma = e
#    print('ma',ma)
print(max(ma-mi+1,0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(M):
    L, R = map(int, input().split())
    L_ans = max(L, L_ans) # Lの中で一番大きいもの抽出
    R_ans = min(R, R_ans) # Rは最小

    if L_ans > R_ans:
        print(0)
        exit()

print(R_ans - L_ans + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left = 0
right = 10 ** 5
for _ in range(M):
  l, r = map(int, input().split())
  left = max(left, l)
  right = min(right, r)
ans = right - left + 1
print(ans if ans > 0 else 0)

Submission status: Accepted

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

for _ in range(M):
  tmp1,tmp2 = map(int,input().split())
  B.append(tmp1)
  C.append(tmp2)

p = max(B)
q = min(C)
res = q - p + 1
if res < 0:
  res = 0
print(res)

Submission status: Accepted

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

l = []
r = []
for i in range(0, m):
  tmp_l, tmp_r = [int(elem) for elem in input().split()]
  l.append(tmp_l)
  r.append(tmp_r)

p = max(l)
q = min(r)
res = q - p + 1
if res < 0:
  res = 0
print(res)

Submission status: Accepted

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

LList = []
RList = []

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

maxL = max(LList)
minR = min(min(RList), N)

print(max(minR - maxL + 1, 0))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
for _ in range(M-1):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

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

Submission status: Accepted

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

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

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

Ans = min(R) - max(L) + 1
if Ans < 0:
    Ans = 0
print(Ans)

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())
if min(r)<max(l):
  print(0)
else:
  print(min(r)-max(l)+1)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    mr = float('INF')
    ml = 0
    for _ in range(M):
        l, r = map(int, input().split())
        ml = max(ml, l)
        mr = min(mr, r)
    if mr - ml >= 0:
        print(mr - ml + 1)
    else:
        print(0)


main()

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    A = [0] * (N + 1)
    for _ in range(M):
        L, R = map(int, input().split())
        A[L - 1] += 1
        A[R] -= 1
    c = 0
    ans = 0
    for a in A:
        c += a
        if c == M:
            ans += 1
    print(ans)


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = [int(i) for i in input().split()]
rang = []
lmax = 0
rmin = 10**10
for i in range(m):
    l,r = [int(i) for i in input().split()]
    lmax = max(l,lmax)
    rmin = min(r,rmin)

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

Submission status: Accepted

Solution code:
n,m = [int(i) for i in input().split()]
rang = []
lmax = 0
rmin = 10**10
for i in range(m):
    l,r = [int(i) for i in 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())
l = [0] * m
r = [0] * m

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

u = max(l)
v = min(r)
    
ans = max(v-u+1,0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m = list(map(int,input().split()))
min_id = 1
max_id = n
for _ in range(m):
    l,r=list(map(int,input().split()))
    if l > min_id:
        min_id = l
    if r < max_id:
        max_id = r
print(max(max_id-min_id+1,0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
l=[list(map(int,input().split())) for i in range(M)]
maxL=1
minR=N
for j in range(M):
  maxL=max(maxL,l[j][0])
  minR=min(minR,l[j][1])

ans=minR-maxL+1
ans=max(ans,0)
print(ans)

Submission status: Accepted

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

minv = 100000
maxv = 1
for lr in LR:
  maxv = max(maxv, lr[0])
  minv = min(minv, lr[1])

print(max(0, minv - maxv + 1))

Submission status: Accepted

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

min_n = 0
max_n = N
for l,r in LRlist:
    min_n = l if l>min_n else min_n
    max_n = r if r<max_n else max_n

ans = max_n-min_n+1
print(ans if ans>0 else 0)

Submission status: Accepted

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

min_n = 0
max_n = N
for l,r in LRlist:
    min_n = l if l>min_n else min_n
    max_n = r if r<max_n else max_n

if max_n-min_n >= 0:
    print(max_n-min_n+1)
else:
    print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3

#import numpy as np
#import scipy, scipy.special

# Python 3 modules: https://docs.python.org/3/library/

import math, itertools, operator
from math import *
from math import factorial as fact
from itertools import permutations, combinations
from functools import reduce
# Use scipy for stats: https://docs.scipy.org/doc/scipy/reference/stats.html
# Before Python 3.5
from fractions import gcd

inf = float('inf')

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

def perm(a, b):
    import scipy.special
    return scipy.special.perm(a, b, exact = True)

def comb(a, b):
    import scipy.special
    return scipy.special.comb(a, b, exact = True)

def dfact(n):
    import scipy.special
    return scipy.special.factorial2(n, exact = True)

def prod(l):
    return reduce(operator.mul, l)

def input_ints(inp = input, sep = ' ', conv = int):
    return map(conv, inp().split(sep = sep))

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

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

Submission status: Accepted

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

import numpy as np
nl = np.array(l)
mrange = l[(nl[:, 1] - nl[:, 0]).argmin()]

def judge(l):
  a = l[1]<mrange[0]
  b = l[0]<mrange[0] and l[1]>=mrange[0] and l[1]<=mrange[1]
  c = l[0]>=mrange[0] and l[1]<=mrange[1]
  d = l[0]>=mrange[0] and l[0]<=mrange[1] and l[1]>mrange[1]
  e = l[0]>mrange[1]
  return a, b, c, d, e

for ll in l:
  a, b, c, d, e = judge(ll)
  if a | e:
    mrange = [1, 0]
    break
  elif b:
    mrange = [mrange[0], ll[1]]
  elif c:
    mrange = ll # これは出現することがないはず
  elif d:
    mrange = [ll[0], mrange[1]]
    
print(mrange[1] - mrange[0] + 1)

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())
    
    
l_max = 0
r_min = 1000000000000     
for i in range(m):
    a = max(l[i],r[i])
    b = min(l[i],r[i])
    if a<r_min:
        r_min = a
    if b>l_max:
        l_max = b

print(max(r_min-l_max+1,0))

Submission status: Accepted

Solution code:
c = []
d = []
n,m = map(int, input().split())
for i in range(m):
    a,b = map(int,input().split())
    c.append(a)
    d.append(b)
minl = max(c)   
maxr = min(d)
if minl>maxr:
    print(0)
else:
    print(maxr-minl+1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L, R = [], []
for _ in range(m):
    li, ri = map(int, input().split())
    L.append(li)
    R.append(ri)

# ans = 0

# for i in range(1, n+1):
#     flag = True
#     for li, ri in zip(L, R):
#         if not (i >= li and i <= ri):
#             flag = False
#             break
#     if flag:
#         ans += 1

# print(ans)

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

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)

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

res = 0
for i in range(1,N+1):
	if l<=i and i<=r:
		res+=1
print(res)

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
gate_list = []
while True:
  try:
    gate_list.append(list(map(int,input().split())))
  except:
    break

left_ = []
right_ = []

for i in gate_list:
  left_.append(i[0])
  right_.append(i[1])

min_ = max(left_)
max_ = min(right_)

counter = 0
for i in range(1,N+1):
  if i >= min_ and i<= max_:
    counter += 1
print(counter)

Submission status: Accepted

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

res = (min(Rs)-max(Ls)+1)
if res < 0:
    print(0)
else:
    print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections


def tle_solver():
    # N,M <=10^5
    N, M = [int(ele) for ele in input().split()]
    ans = int('0b' + str(1)*N, 2)

    for i in range(M):
        l, r = [int(ele) for ele in sys.stdin.readline().split()]
        secure = int('1' * (r-l+1) + '0' * (N-r), 2)
        ans = ans & secure

    print(bin(ans).count("1"))


def ac_solver():
    # N,M <=10^5
    N, M = [int(ele) for ele in input().split()]
    ll = 1
    rr = N
    ans = "Error"

    for i in range(M):
        l, r = [int(ele) for ele in sys.stdin.readline().split()]
        ll = max(ll, l)
        rr = min(rr, r)

    ans = max(0, rr - ll+1)
    print(ans)


if __name__ == "__main__":
    # tle_solver()
    ac_solver()

Submission status: Accepted

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

maxi = float("inf")
mini = 0

for i in range(m):
    l, r = [int(i) for i in input().split()]
    maxi = min(r, maxi)
    mini = max(l, mini)

print(max(0, maxi-mini+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
imos = [0]*(N+2)
for i in range(M):
    L, R = map(int, input().split())
    imos[L] += 1
    imos[R+1] -= 1

for i in range(2, N+1):
    imos[i] += imos[i-1]

ans = 0
for i in range(1, N+1):
    if imos[i] == M:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_list =list()
r_list = list()
for i in range(M):
    l, r = map(int, input().split())
    l_list.append(l)
    r_list.append(r)

ans = 0
for i in range(max(l_list), min(r_list)+1):
    ans += 1
print(ans)

Submission status: Accepted

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

l, r = 1, N
for m in range(M):
  L, R = map(int, input().split())
  l = max(l,L)
  r = min(r,R)

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

Submission status: Accepted

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(max(min(R) - max(L) + 1, 0))
    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())


def main():
	Ls = []
	Rs = []
	for i in range(M):
		inputs = list(map(int,input().split()))
		Ls.append(inputs[0])
		Rs.append(inputs[1])

	maxL = max(Ls)
	minR = min(Rs)

	if maxL > minR:
		print(0)
	else:
		print(minR - maxL + 1)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

print(max(min(rs)-max(ls)+1, 0))

Submission status: Accepted

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

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())
    if a > l:
        l = a
    if b < r:
        r = b
if r - l < 0:
    print(0)
else:
    print(r - l + 1)

Submission status: Accepted

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

Submission status: Accepted

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

low = 1
high = n

for i in range(m):
        lr_temp = lr[i]
        if low < lr_temp[0]:
                low = lr_temp[0]
        if high > lr_temp[1]:
                high = lr_temp[1]

ans = high - low + 1

if ans < 0:
        ans = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import accumulate
 
N,M = [int(i) for i in input().split()]
ans = [0]*(N+1)
for _ in range(M):
    L,R = [int(i)-1 for i in input().split()]
    ans[L] += 1
    ans[R+1] += -1
 
ans = list(accumulate(ans))
 
count = ans.count(M)
 
print(count)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
L = []
R = []
for i in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
max_L = max(L)
min_R = min(R)
if max_L <= min_R:
    print(min_R - max_L + 1)
else:
    print(0)

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)
if max(L) <= min(R):
    print(min(R) - max(L) + 1)
else:
    print(0)

Submission status: Accepted

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

l = 1
r = N
for _ in range(M):
  Li, Ri = map(int, input().split(" "))
  l = max(l, Li)
  r = min(r, Ri)
  
if l > r:
  print(0)
else:
  print(r - l + 1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lrs = [tuple(map(int, input().split())) for _ in range(m)]
gl = 1
gr = n
for l, r in lrs:
    gl = max(gl, l)
    gr = min(gr, r)
print(max(0, gr-gl+1))

Submission status: Accepted

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

Submission status: Accepted

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

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

max_L = max(L)
min_R = min(R)

if max_L <= min_R:
    print(min_R - max_L + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np

nm = input().split()
n = int(nm[0])
m = int(nm[1])

min = 0
max = np.inf

for i in range(m):
    a = input().split()
    if int(a[0])>=min:
        min = int(a[0])

    if int(a[1])<=max:
        max = int(a[1])

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

Submission status: Accepted

Solution code:
import numpy as np
N, M = list(map(int, input().split()))
LR_array = np.array([[int(i) for i in input().split()] for i in range(M)])
num_range_accepted_by_gate = np.vstack([LR_array[:,0], LR_array[:,0]+LR_array[:,1]])
#num_range_accepted_by_gate = [[LR_array[i][0], LR_array[i][0]+LR_array[i][1]] for i in range(M)]
min_number_accepted_by_all_gates = max(LR_array[:,0])
max_number_accepted_by_all_gates = min(LR_array[:,1])
if(max_number_accepted_by_all_gates - min_number_accepted_by_all_gates < 0):
    print(0)
else:
    print(max_number_accepted_by_all_gates - min_number_accepted_by_all_gates + 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)

l = L[0]
r = R[0]
cnt = 0
for i in range(max(L),min(R)+1) :
	if 0<=i<=r :
		cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
ID_num, gate_num = tuple([int(i) for i in input().split(" ")])
min_list = []
max_list = []
for i in range(gate_num):
    min_ID, max_ID = tuple([int(i) for i in input().split(" ")])
    min_list.append(min_ID)
    max_list.append(max_ID)
M = min(max_list)
m = max(min_list)
if M >= m:
    print(min(max_list) - max(min_list) + 1)
else:
    print(0)

Submission status: Accepted

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

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

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

ans = 0
cnt = 0
for i in range(1, n + 1):
    cnt = cnt + s[i] - e[i - 1]
    if cnt == m:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
#abc127c

n,m=map(int,raw_input().split())
l=[]
r=[]
for i in xrange(m):
 li,ri=map(int,raw_input().split())
 l.append(li)
 r.append(ri)
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())
l_max = 0
r_min = 10**5+1
for i in range(M):
	l, r = map(int, input().split())
	l_max = max(l, l_max)
	r_min = min(r, r_min)
if(l_max <= r_min):
	print(str(r_min - l_max + 1))
else:
	print("0")

Submission status: Accepted

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

Submission status: Accepted

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

#memorize current start and end
s_c,e_c = 1,n
for i in range(m):
    l,r = map(int,input().split())
    if l<s_c:
        if s_c<=r<e_c:
            e_c = r
        elif r<s_c:
            print(0)
            exit()
    elif s_c<=l<=e_c:
        s_c = l
        if r<e_c:
            e_c = r
    elif l>e_c:
        print(0)
        exit()
print(e_c-s_c+1)

Submission status: Accepted

Solution code:
N, M = [int(t) for t in input().split()]
l = [[0]*2 for i in range(M)]

l_max = 0
r_min = N
for i in range(M):
    L, R = [int(t) for t in input().split()]
    l_max = L if l_max < L else l_max
    r_min = R if r_min > R else r_min

ans = r_min-l_max+1 if r_min >= l_max else 0
print(ans)

Submission status: Accepted

Solution code:
def input_from_console():
    n, m = map(int, input().split())
    lr_list = []
    for i in range(m):
        l, r = map(int, input().split())
        lr_list.append((l,r))
    return n, m, lr_list

def solve(n, m, lr_list):
    lr_list.append((1,n))
    l_max = 0
    r_min = n
    for item in lr_list:
        l, r = item
        if l > l_max:
            l_max = l
        if r < r_min:
            r_min = r
    result = r_min - l_max + 1
    if result < 0:
        result = 0
    return result

def main():
    n, m, lr_list = input_from_console()
    print(solve(n, m, lr_list))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

r=n
l=1
ans=0
for i in range(1,m+1):
	lt, rt = map(int, input().split())
	if lt > l:
		l=lt
	if rt < r:
		r=rt
	#print(l,r)

ans = r-l+1
if ans < 0:
	ans = 0
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)

left = max(L)
right = min(R)

ans = max(0, right-left+1)

print(ans)

Submission status: Accepted

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

for i in range(M):
    L,R=map(int,input().split())
    start[L-1] += 1
    end[R] += 1

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

print(ans)

Submission status: Accepted

Solution code:
def prison():
    N, M = (int(i) for i in input().split())
    L_max = 0
    R_min = float("inf")
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)
        result = max(R_min - L_max + 1, 0)
    return result


print(prison())