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, M = map(int, input().split())
LN, RN = 0, N
for _ in range(M):
    L, R = map(int, input().split())
    LN = max(LN, L)
    RN = min(RN, R)
ans = RN - LN + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

def main(lines):
  N, M = np.fromstring(lines[0], dtype=int, sep=" ")
  a = np.fromstring(''.join(lines[1:]), dtype=int, sep=" ").reshape((2, M), order='F')
  
  lmax = a[0].max()
  rmin = a[1].min()
  if lmax > rmin:
    print(0)
  else:
    print(rmin - lmax + 1)

lines = sys.stdin.readlines()
main(lines)

Submission status: Accepted

Solution code:
s=list(map(int,input().split()))
t=[list(map(int,input().split())) for i in range(s[1])]
a=0
b=100001
for i in range(s[1]):
  if a<t[i][0]:
    a=t[i][0]
  if b>t[i][1]:
    b=t[i][1]
if b-a>=0:
  print(b-a+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

if m==1:
  print(L[0][1]-L[0][0]+1)
else:
  l=[]
  r=[]
  for item in L:
    l.append(item[0])
    r.append(item[1])
  x=max(l)
  y=min(r)
  if y>=x:
    print(y-x+1)
  else:
    print(0)

Submission status: Accepted

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

master_l, master_r = -1, N
for l, r in LR:
	master_l = max(master_l, l)
	master_r = min(master_r, r)

print(max(0, master_r - master_l + 1))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

l_list = []
r_list = []

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

l_maybe_answer = max(l_list)
r_maybe_answer = min(r_list)

if l_maybe_answer <= r_maybe_answer:
    print(min(r_list) - max(l_list) + 1)
else:
    print(0)

Submission status: Accepted

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

imos = [0] * (N+1) 
for l,r in LR:
    imos[l-1] += 1
    imos[r] -= 1

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

Submission status: Accepted

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

for i in range(M):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

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

Submission status: Accepted

Solution code:
import numpy as np

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

l = np.zeros(m, dtype=np.int)
r = np.zeros(m, dtype=np.int)

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

# 1枚だけで全てのゲートを通過できるのは max(l) から min(r) まで
x = max(r.min() - l.max() + 1, 0)

print(x)

Submission status: Accepted

Solution code:
N,M = (int(x) for x in input().split())
Lmax = 0
Rmin = N
for i in range(M):
  Li,Ri = (int(x) for x in input().split())
  if Lmax < Li:
    Lmax = Li
  if Rmin > Ri:
    Rmin = Ri
answer = Rmin - Lmax + 1
if answer < 0:
  answer = 0
print(answer)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
imos = [0]*(N+1)
for i in range(M):
    l,r = map(int,input().split())
    imos[l-1] += 1
    imos[r] -= 1
from itertools import accumulate
imos = list(accumulate(imos))
res = 0
for a in imos:
    if a == M:
        res += 1
print(res)

Submission status: Accepted

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

for gates 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())
L = []
R = []
for _ in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

answer = max([min(R) - max(L) + 1, 0])
print(answer)

Submission status: Accepted

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

    if s[1]-s[0]<0:
        s=[0,-1]
        break
print(s[1]-s[0]+1)

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
cards = [0] * (N + 1)

for _ in range(M):
  l, r = [int(i) for i in input().split()]
  cards[l - 1] += 1 
  cards[r] += -1

for i in range(N):
  cards[i + 1] += cards[i] 

print(len([i for i in cards if i == 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())

Lmax = max(L[i] for i in range(M))
Rmin = min(R[i] for i in range(M))

if Lmax>Rmin:
  print('0')
else:
  print(str(Rmin-Lmax+1))

Submission status: Accepted

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

lmax = 0
rmin = n+1

for i in range(m):
    l,r = map(int,input().split())
    if l > lmax:
        lmax = l
    if r < rmin:
        rmin = r

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

Submission status: Accepted

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

left_max, right_min = map(int, input().split())
for m in range(M-1):
    L, R = map(int, input().split())
    left_max = max(L, left_max)
    right_min = min(R, right_min)

if left_max <= right_min:
    print(right_min-left_max+1)
else:
    print(0)

Submission status: Accepted

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

for  i  in  range(m):
    nums.append([int(s)  for  s  in  input().split()])
for  j  in  range(m):
    left.append(nums[j][0])
    right.append(nums[j][1])
if  min(right)-max(left)>=0:
    print(min(right)-max(left)+1)
else:
    print(0)

Submission status: Accepted

Solution code:
maxi =[]
mini =[]
n,m =map(int, input().split())
for i in range(m):
    k =[int(s) for s in input().split()]
    mini.append(min(k))
    maxi.append(max(k))
if min(maxi)- max(mini) >= 0:
    print(min(maxi)-max(mini)+1)
else:
    print(0)

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)
n_all = min(rs) - max(ls) + 1
if n_all < 0: n_all =0
print(n_all)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

if __name__ == "__main__":
    _, M = map(int, input().split())

    A, B = map(int, input().split())

    for i in range(1, M):
        a, b = map(int, input().split())
        A = max(A, a)
        B = min(B, b)

    print(max(B - A + 1, 0))

Submission status: Accepted

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

card_list = [1] * n

minr = 10**9
maxl = 0

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

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

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# いくつか入力
n, m = (int(i) for i in input().split())  


# n行複数列の数をリストに
lr = [[int(i) for i in input().split()] for i in range(m)] 

l=1
r=n
for i in range(m):
    l=max(l,lr[i][0])
    r=min(r,lr[i][1])

#print(l,r)

a=r-l+1
if a < 0:
    a=0
print(a)

Submission status: Accepted

Solution code:
a, b = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(b)] 
tmin = e[0][0]
tmax = e[0][1]
for i, l in enumerate(e):
  if (tmin < l[0]):
    tmin = l[0]
  if (tmax > l[1]):
    tmax = l[1]
  if (tmax - tmin) < 0:
    break
if (tmax-tmin < 0):
  print(0)
else:
  print(tmax-tmin+1)

Submission status: Accepted

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

min_list = []
max_list = []
for i in range(m):
  min_num, max_num = map(int,input().split())
  min_list.append(min_num)
  max_list.append(max_num)

min_num = max(min_list)
max_num = min(max_list)

ans = max_num - min_num + 1

if ans < 0:
  ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = list(map(int,input().split()))
l=[]
r=[]
for j in range(m):
	a,b = list(map(int,input().split()))
	l.append(a)
	r.append(b)
low = max(l)
high = min(r)
if(low>high):
	count = 0
else:
	count = min(high - low + 1, n)
print(count)

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

Submission status: Accepted

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

A, B = 1, N
for lr in LR:
    L, R = lr
    if R < A or B < L:
        print(0)
        exit()

    if A <= L <= R <= B:
        A, B = L, R
    elif L <= A <= R <= B:
        B = R
    elif A <= L <= B <= R:
        A = L
print(B - A + 1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
l = []
r = []
for m in range(M):
  lr1,lr2 = map(int,input().split())
  l.append(lr1)
  r.append(lr2)
print(max(0,min(r) - max(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)

l_max = max(L)
r_min = min(R)

if l_max <= r_min:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Accepted

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

Lmax = 0
Rmin = N

for i in range(M):
  L, R = map(int, input().split())
  Lmax = max(L, Lmax)
  Rmin = min(R, Rmin)
  
if (Lmax > Rmin):
  print(0)
else:
  print(Rmin - Lmax + 1)

Submission status: Accepted

Solution code:

n, g = map(int, input().split())
cards = [[int(x) for x in input().split()] for _ in range(g)]

cards.sort(key=lambda x: x[0])
lmx = cards[g - 1][0]
cards.sort(key=lambda x: x[1])
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

Submission status: Accepted

Solution code:
import sys

n, g = map(int, input().split())
cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(g)]

cards.sort(key=lambda x: x[0])
lmx = cards[g - 1][0]
cards.sort(key=lambda x: x[1])
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

Submission status: Accepted

Solution code:
import sys
import operator

n, g = map(int, input().split())
cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(g)]

cards.sort(key=operator.itemgetter(0))
lmx = cards[g - 1][0]
cards.sort(key=operator.itemgetter(1))
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())  # N枚のIDカード, M個のゲート

l_max = 0
r_min = N

for i in range(M):
    l, r = map(int, input().split())
    l_max = max(l, l_max)
    r_min = min(r_min, r)

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

Submission status: Accepted

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
data = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
min_id =0
max_id = 100000
for i in range(M):
  min_id = max(min_id,data[i][0])
  max_id = min(max_id,data[i][1])
num = max_id - min_id + 1 if max_id - min_id >= 0 else 0
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
pool = []
for i in range(M):
    i_1,i_2 = map(int,input().split())
    pool.append([i_1,i_2])
min_val = max([int(pool[i][0]) for i in range(M)])
max_val = min([int(pool[i][1]) for i in range(M)])
if min_val > max_val:
    print(0)
else:
    print(max_val-min_val+1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max = 0
r_min = 100001

for i in range(M):
  l, r = map(int, input().split())
  if l > l_max:
    l_max = l
  if r < r_min:
    r_min = r
    
mylist = list(range(l_max,r_min+1))

print(len(mylist))

Submission status: Accepted

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

left = 1
right = N
for i in range(M):
    if left<List[i][0]:
        left = List[i][0]
    if List[i][1]<right:
        right = List[i][1]

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

Submission status: Accepted

Solution code:

N,M=map(int,input().split())#ID,ゲート


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


numbers=[]
for i in range(N+1):
    numbers.append(i)




Lmax=1
Rmax=100000
for j in LR:

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

    if Lmax<L:
        Lmax=L
    if Rmax>R:
        Rmax=R
result=0

for i in numbers:

    if i>=Lmax:

        if i<=Rmax:
            result+=1



print(result)

Submission status: Accepted

Solution code:
### 18. Prison

input_line = input()
N,M = [int(s) for s in input_line.split(' ') if s]
Ls = []
Rs = []
for _ in range(M):
    input_line = input()
    L,R = [int(s) for s in input_line.split(' ')if s]
    Ls.append(L)
    Rs.append(R)

output = min(Rs)-max(Ls)+1
output = output if output > 0 else 0
print(output)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Jun  6 21:15:08 2019

@author: hisaki
"""

import numpy as np

N,M = map(int,input().split())
L_array = np.zeros(M)
R_array = np.zeros(M)
for i in range(M):
    L,R = map(int,input().split())
    L_array[i] = L
    R_array[i] = R
    
L_max = np.max(L_array)
R_min = np.min(R_array)

if L_max <= R_min:
    print (int (R_min - L_max + 1))    
else:
    print (0)

Submission status: Accepted

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

S = [0] * (int(1e5) + 11)
for i in range(len(L)):
    S[i + 1] = S[i] + L[i]

ans = 0
for s in S:
    if s == m : ans += 1

print(ans)

Submission status: Accepted

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

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

L = 0
R = N

for i in xrange(M):
	li,ri = map(int,raw_input().split())
	L = max(L,li)
	R = min(R,ri)

print max(0,R-L+1)

Submission status: Accepted

Solution code:
import sys

N, M = map(int, input().split())
card = [1, N]
for i in range(M):
#    gate.append([int(x) for x in input().split()])
    gate = [int(x) for x in input().split()]
    card[0] = max(card[0], gate[0])
    card[1] = min(card[1], gate[1])
    if(card[0] > card[1]):
        print(0)
        sys.exit()
        
print(card[1] - card[0] + 1)

Submission status: Accepted

Solution code:
# 入力
N, M = map(int, input().split())
L, R = (
    zip(*(map(int, input().split()) for _ in range(M))) if M else
    ((), ())
)

ans = max(min(R) - max(L) + 1, 0)

# 出力
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Doors = [list(map(int, input().split())) for _ in range(M)]
IDs = [0 for _ in range(N+1)]

for Door in Doors:
    IDs[Door[0] - 1] += 1
    IDs[Door[1]]     -= 1
   

for i in range(N):
    IDs[i+1] = IDs[i] + IDs[i+1]
    

res = 0
for i in range(N):
    if IDs[i] == M:
        res += 1
        
    
print(res)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
gateid = [list(map(int,input().split())) for i in range(M)]
idleft = max(gateid,key=lambda x:x[0])
idright = min(gateid,key=lambda x:x[1])
ans = idright[1]-idleft[0]+1
if ans<0:
  ans = 0
print(ans)

Submission status: Accepted

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

lmax = max(gates, key=lambda x: x[0])[0]
rmin = min(gates, key=lambda x: x[1])[1]

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

Submission status: Accepted

Solution code:
from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]
G = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
L = [G[k][0] for k in range(M)]
R = [G[k][1] for k in range(M)]
L.sort(reverse=True)
R.sort()
print(max(R[0] - L[0] + 1, 0))

Submission status: Accepted

Solution code:
def main():
    NM = list(map(int, input().split()))
    N, M = NM[0], NM[1]
    LR = []
    L, R = [], []
    ans = 0
    maxL = 1
    minR = N

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

    for j in range(M):
        L.append(LR[j][0])
        R.append(LR[j][1])

    for k in range(M):
        maxL = max(maxL, L[k])
        minR = min(minR, R[k])

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
LL=[]
LR=[]
for i in range(M):
    LLR=list(map(int,input().split()))
    LL.append(LLR[0])
    LR.append(LLR[1])
ans=max(min(LR)-max(LL)+1,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)
a = min(R)-max(L)+1
if a>=1:
  print(a)
else:
  print(0)

Submission status: Accepted

Solution code:
def resolve():
    (n, m) = list(map(int, input().split(" ")))
    
    for i in range(m):
        o = list(map(int, input().split(" ")))
        if i == 0:
            lmax = o[0]
            rmin = o[1]
        else:
            lmax = max(lmax, o[0])
            rmin = min(rmin, o[1])

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


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def is_MM(s):
    num = int(s)
    return 0 < num <= 12


def solve():
    N, M = il()
    L = []
    R = []
    for m in range(M):
        lr = il()
        L.append(lr[0])
        R.append(lr[1])
    return max(0, min(R) - max(L) + 1)


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

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def is_MM(s):
    num = int(s)
    return 0 < num <= 12


def solve():
    N, M = il()
    L_max = 0
    R_min = float("inf")
    for m in range(M):
        lr = il()
        L_max = max(L_max, lr[0])
        R_min = min(R_min, lr[1])
    return R_min - L_max + 1 if L_max <= R_min else 0


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

Submission status: Accepted

Solution code:
import sys


scan = sys.stdin.readline


def main():
  n, m = map(int, scan().rstrip().split())
  l = 0
  r = n
  
  for _ in range(m):
    l_i, r_i = map(int, scan().rstrip().split())
    l = max(l, l_i)
    r = min(r, r_i)
    
  cards = r - l + 1 if r >= l else 0
  
  print(cards)
  

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
gate_idx = [[int(i) for i in input().split()] for M in range(M)]
import numpy as np
l_max = np.max(gate_idx, axis=0)
r_min = np.min(gate_idx, axis=0)
ans = r_min[1] - l_max[0] + 1
if ans < 0: ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n, m = map(int, input().split())
l = 1
r = n
for _ in range(m):
    a, b = 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())
m=0
M=9**9
for i in range(m_):
    l,r= map(int, input().split())
    m=max(m,l)
    M=min(M,r)
print(0if M<m else M-m+1)

Submission status: Accepted

Solution code:
def main():
    n, m = [int(i) for i in input().split()]
    l_max, r_min = 0, 10**6
    for i in range(m):
        l, r = [int(j) for j in input().split()]
        l_max = max(l, l_max)
        r_min = min(r+1, r_min)
    if r_min-l_max <= 0:
        print(0)
    else:
        print(r_min-l_max)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    l = []
    r = []
    for _ in range(m):
        a, b = list(map(int, input().split()))
        l.append(a)
        r.append(b)

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[]
R=[]
for m in range(M):
    Lm,Rm=map(int,input().split())
    L.append(Lm)
    R.append(Rm)
maxL=max(L)
minR=min(R)
print(max(minR-maxL+1,0))

Submission status: Accepted

Solution code:
import sys
     
def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]
   
    under_limit = [x[0] for x in input]
    over_limit = [x[1] for x in input]
     
    print(max(min(over_limit) - max(under_limit) + 1, 0))
     
     
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

def main():
    sinputl = sys.stdin.readline

    num_card, num_gate = [int(x) for x in sinputl().split()]
    input = [[int(x) for x in sinputl().split()] for _ in range(num_gate)]

    card = [1, num_card]

    for gate in input:
        card[0] = max(card[0], gate[0])
        card[1] = min(card[1], gate[1])

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()]
  if l > l_max:
    l_max = l
  if r < r_min:
    r_min = r

ans = r_min - l_max + 1

if ans > 0:
  print(ans)
else:
  print(0)

Submission status: Accepted

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

l = []
r = []
for _ in range(m):
  l_tmp, r_tmp = [int(i) for i in input().split()]
  l.append(l_tmp)
  r.append(r_tmp)
  
l_max = max(l)
r_min = min(r)

ans = r_min - l_max + 1

if ans > 0:
  print(ans)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
LR = [[int(_) for _ in input().split()] for _ in range(M)]
lmax = 1
rmin = N
for l, r in LR:
    lmax = max(lmax, l)
    rmin = min(rmin, r)
print(max([0, rmin - lmax + 1]))

Submission status: Accepted

Solution code:
n, m = [int(v) for v in input().split()]
#print(n, m)
gates = [[int(v) for v in input().split()] for i in range(m)]
#print(gates)
left = max(gates, key=lambda x: x[0])[0]
right = min(gates, key=lambda x: x[1])[1]
if left <= right:
  print(right - left + 1)
else:
  print(0)

Submission status: Accepted

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

l = []
r = []

for _ in range(m):
    lr = list(map(int, input().split()))
    l.append(lr[0])
    r.append(lr[1])

a = min(r) - max(l) + 1

if a < 0:
    a = 0

print(a)

Submission status: Accepted

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

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

if High >= Low:
    answer = High - Low + 1
else:
    answer = 0
    
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
list1=[list(map(int,input().split())) for i in range(M)]
start=[]
end=[]
for i in range(M):
    start.append(list1[i][0])
    end.append(list1[i][1])
stmax=max(start)
#print(stmax)
enmax=min(end)
#print(enmax)
if enmax>=stmax:
    print(enmax-stmax+1)
else:
    print(0)

Submission status: Accepted

Solution code:
#n idカード　m ゲート
n,m = map(int, input().split())
maxlist = []
minlist = []

for i in range(m):
	#l　ゲートの下限　r ゲートの上限
	l , r = list(map(int, input().split()))
	minlist.append(l)
	if r > 0:
		maxlist.append(r)

minid = max(minlist)
maxid = min(maxlist)
okgate = maxid - minid + 1

if okgate >= 0:
	print(okgate)
else:
	print(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())
L.sort()
R.sort()
ans=R[0]-L[M-1]+1
if ans>=0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = [int(x) for x in input().split(' ')]
id = [1,N]
ans = -1
for i in range(M):
    L,R = [int(x) for x in input().split(' ')]
    if id[1] < L:
        ans = 0
        break
    elif id[0] > R:
        ans = 0
        break
    if id[0] < L:
        id[0] = L
    if id[1] > R:
        id[1] = R

if ans == -1:
    print(id[1]-id[0]+1)
else:
    print(ans)

Submission status: Accepted

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

def prison(n,m,id1,id2):
    if min(id2) >= max(id1):
        print(min(id2)+1-max(id1))
    else:
        print(0)


def main():
    n,m = [int(i) for i in input().split()] 
    l = []
    r = []
    for _ in range(m):
        l1, r1 = [int(i) for i in input().split()] 
        l.append(l1)
        r.append(r1)
    prison(n,m,l,r)
    
# main関数呼び出し
if __name__ == "__main__":
    main()

Submission status: Accepted

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

cards = [x for x in range(N)]

tmp = cards
Ls = []
Rs = []
for i in range(M):
    L,R = map(int,input().split())
    Ls.append(L - 1)
    Rs.append(R - 1)

print(max(min(Rs) - max(Ls) + 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())

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

Submission status: Accepted

Solution code:
def solve():
    maxCards, gates = map(int, input().split())
    gateRanges = [tuple(map(int, input().split())) for i in range(gates)]

    for i, permitRange in enumerate(gateRanges):
        if i == 0:
            activeBegin, activeEnd = permitRange
            continue
        begin, end = permitRange
        if not (begin <= activeEnd and end >= activeBegin):
            return print(0)
        else:
            activeBegin, activeEnd = max(activeBegin, begin), min(activeEnd, end)
    else:
        if (maxCards < activeBegin):
            return print(0)
        ans = min(activeEnd, maxCards) - activeBegin + 1
    print(ans)
solve()

Submission status: Accepted

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

max_l = 0
min_r = float('inf')

for l,r in LR:
    max_l = max(max_l,l)
    min_r = min(min_r,r)

if max_l <= min_r:
    print(min_r - max_l + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split()]
L = []
R = []
for i in range(M):
    L1,R1 = [int(i) for i in input().split()]
    L.append(L1)
    R.append(R1)
result = min(R) - max(L)  +1
if result < 0:
    result = 0
print(result)