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())
Lm=0
Rm=N
ans=0

for i in range(M):
    Lt,Rt=map(int,input().split())
    if Lm < Lt:
        Lm = Lt
    if Rm > Rt:
        Rm = Rt
if Rm < Lm:
    print(0)
else:
    print(Rm-Lm+1)

Submission status: Accepted

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))


def merge(a,b):
    l=sorted([a[0],b[0]])
    r=sorted([a[1],b[1]])
    return l[1],r[0]


N,M=spaceinput()
gates=[]
for i in range(M):
    gates.append(spaceinput())

d=gates[0]
for i in range(1,M):
    d=merge(d,gates[i])

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = 0, N
for i in range(M):
	_L, _R = map(int, input().split())
	L = max(_L, L)
	R = min(_R, 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)
    lmax = max(L)
    rmin = min(R)
    if rmin - lmax + 1 <= 0:
        ans = 0
    else:
        ans = rmin - lmax + 1
    print(int(ans))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
lr =[[int(i)for i in input().split()]for _ in range(M)]
ans =[0,N+1]
for LR in lr:
    if ans[0] < LR[0]:
        ans[0]=LR[0]
    if LR[1] < ans[1]:
        ans[1] =LR[1]
print(max(0,ans[1]-ans[0]+1))

Submission status: Accepted

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

a = [0] * M
b = [0] * M

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

top = min(b) #ｒの最小、上限
under = max(a) #ｌの最大、下限

if top >= under:
    ans = top - under + 1
if top < under:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
# スペース区切りの整数の入力
N,M = map(int, input().split())
LR_list=[]
Card_list=[]

Min_L = 1
Max_R = N

#LR読み込み
for i in range(M):
  L,R = map(int, input().split())
  if L>Min_L:
    Min_L=L
  if Max_R>R:
    Max_R=R

if Max_R >= Min_L:
  print(Max_R - Min_L + 1)
else:
  print(0)

Submission status: Accepted

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

for x in range(M-1):
    L, R = map(int, input().split())
    if L > L1:
        L1 = int(L)
    if R < R1:
        R1 = int(R)

if R1 == L1:
    print("1")
if L1 > R1:
    print("0")
if L1 < R1: 
	print(R1-L1+1)

Submission status: Accepted

Solution code:
# coding:utf-8
if __name__=='__main__':
  N, M = map(int, input().split())
  L, R = 1, N
  for i in range(M):
    l, r = map(int, input().split())
    if L < l: L = l
    if R > r: R = r
  if L > R:
    print(0)
  else:
    print(R-L+1)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
 
l_p = 1
r_p = n
 
for j in range(m):
  l, r = map(int,input().split())
  if l > l_p:
    l_p = l
  if r < r_p:
    r_p = r

a = r_p - l_p + 1

if a > 0:
  print(a)
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)
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 = 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:
Ns,Ms=input().split()
N=int(Ns)
M=int(Ms)
lists=[]
ansmins,ansmaxs=input().split()
ansmin=int(ansmins)
ansmax=int(ansmaxs)
for i in range(M-1):
	tmpmin,tmpmax=input().split()
	if int(tmpmin)>ansmin:
		ansmin=int(tmpmin)
	if int(tmpmax)<ansmax:
		ansmax=int(tmpmax)

if (ansmax-ansmin+1)<0:
	ans=0
else:
	ans=ansmax-ansmin+1
print(ans)

Submission status: Accepted

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

for i in range(m):
    left.append(lr[i][0])
    right.append(lr[i][1])

ans = min(right) - max(left) + 1

if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
M,N = map(int,input().split())
ans=set(range(1,M+1))
START=0
END=10**5
for s in range(N):
  S,Z = map(int,input().split())
  if START <= S:
    START = S
  if END >= Z:
    END = Z
ans = range(START,END+1)
print(len(ans))

Submission status: Accepted

Solution code:
def calc():
    i_list = input().split()
    n = int(i_list[0])
    m = int(i_list[1])

    min_card = 1
    max_card = n

    for i in range(m):
        i_list = input().split()
        l = int(i_list[0])
        r = int(i_list[1])
        if l > min_card:
            min_card = l
        if r < max_card:
            max_card = r

    ok_num = max_card - min_card + 1
    if ok_num > 0:
        print(ok_num)
    else:
        print(0)

if __name__ == '__main__':
    calc()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

# print(q, list(accumulate(q)))

print(sum(M == m for m in list(accumulate(q))[:-1]))

Submission status: Accepted

Solution code:
#C問題
import numpy as np
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)
if min(R_list) - max(L_list) < 0:
    print(0)
else:
    print(min(R_list) - max(L_list)+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

l = 1
r = N
for _ in range(M):
    L, R = map(int, input().split())
    if L > l:
        l = L
    if R < r:
        r = R

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

Submission status: Accepted

Solution code:
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()

LR = [IL() for i in range(M)]

l = 0
r = MAX_INT
for L,R in LR:
    if l < L:
        l = L
    if r > R:
        r = R
print(max(0,r-l+1))

Submission status: Accepted

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

start, end = e[0][0], e[0][1]
for i in e: 
  if start < i[0]:
    start = i[0]
  if end > i[1]:
    end = i[1]
  if start > end:
    break

if start > end:
  print(0)
else:
  print(end-start+1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

gate = np.zeros(N+1)
for i in range(0, M):
    L, R = [int(j) for j in input().split()]
    gate[L-1] += 1
    gate[R] -=1

count = 0
for i in range(1, len(gate)):
    gate[i] = gate[i] + gate[i-1]
    if gate[i] == M:
        count += 1

if gate[0] == M:
    count += 1
print(count)

Submission status: Accepted

Solution code:
n,m=(int(i) for i in input().split())
list_a=[]
list_b=[]
for i in range(m):
  a,b=(int(i) for i in input().split())
  list_a.append(a)
  list_b.append(b)
  
max_a=max(list_a)
min_b=min(list_b)

if min_b-max_a+1>=0:
  print(min_b-max_a+1)
else:
  print(0)

Submission status: Accepted

Solution code:
#   C



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


mini, maxi = map(int, input().split())

for i in range(1, M):
    L, R = map(int, input().split())
    if L > mini:
        mini = L
    if R < maxi:
        maxi = R
    if mini > maxi:
        break
#    print(mini, L, R, maxi)
if mini > maxi:
    print(0)
else:
    print(maxi - mini + 1)

Submission status: Accepted

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

n, m = map(int, input().split())
l = []
r = []
answer = 0
for i in range(m):
    a, b = map(int, input().split())
    l.append(a)
    r.append(b)
min_ok = max(l)
max_ok = min(r)
answer = max_ok - min_ok + 1
if answer < 0:
    answer = 0
print(answer)

Submission status: Accepted

Solution code:
n,m=input().split()
n=int(n)
m=int(m)
i=0
x=list()
y=list()
ans=0
g=0
ll=[]
rr=[]


while i!=m:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    ll.append(l)
    rr.append(r)
l=max(ll)
r=min(rr)

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

Submission status: Accepted

Solution code:
# coding: utf-8

n, m = [int(x) for x in input().rstrip().split(" ")]

l_max = 1
r_min = n

for i in range(m):
    l, r = [int(x) for x in input().rstrip().split(" ")]
    if l > l_max:
        l_max = l
    if r_min > r:
        r_min = r

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())

l_max = 1
r_min = n
for i in range(m):
    li, ri = map(int, input().split())
    if l_max < li:
        l_max = li
    if r_min > ri:
        r_min = ri

print(r_min-l_max+1 if r_min-l_max >= 0 else 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())

def prison(N,M,L,R):
  max_val=min(R)
  min_val=max(L)
  num=max_val-min_val+1
  if num<=0:
    num=0
  return(num)

print(prison(N,M,L,R))

Submission status: Accepted

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

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

l_min = 1
r_max = n+1

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

ans = r_max - l_min + 1
if ans > 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().rstrip().split()]

    _l = 0
    _r = n

    for i in range(m):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        _l = max(l, _l)
        _r = min(r, _r)

    if _r - _l < 0:
        print(0)
    else:
        print(_r - _l + 1)
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**6
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i > min_num else min_num
  max_num = n_j if n_j < max_num else max_num

if max_num - min_num < 0:
  print(0)
else:
  print(max_num - min_num +1)

Submission status: Accepted

Solution code:
# n=int(input())  #数値入力 「N」だけの入力のとき
# a,b=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
# c=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
# s=[list(map(int,list(input()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# a=100
# b=0.987654321
# print('{0:06d}-{1:6f}'.format(a,b))  # 0埋めのときの出力
# 000100-0.987654

import numpy as np
import math
import copy

N,M=map(int, input().split())
A=np.array([i for i in range(N+1)[1::]])


minimum = 1
maximum = N

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

    if(R < minimum or maximum < L):
        print(0)
        exit()

    if(minimum < L):
        minimum = L
    if(R < maximum):
        maximum = R

print(maximum - minimum + 1)

Submission status: Accepted

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

gates = [[1, N]]

for m in range(M):
  L, R = map(int, input().split())
  updated = []
  for gate in gates:
    if L <= gate[0]:
      if gate[0] <= R:
        updated.append([gate[0], min(gate[1], R)])
    else:
      if L <= gate[1]:
        updated.append([L, min(gate[1], R)])
  gates = updated


print(sum([g[1] - g[0] + 1 for g in gates]))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
la, ra = map(int, input().split())
for i in range(1, m):
    lm, rm = map(int, input().split())
    overlap = False
    if lm <= la <= rm and lm <= ra <= rm:
        continue
    if la <= lm <= ra:
        la = lm
        overlap = True
    if la <= rm <= ra:
        ra = rm
        overlap = True
    if not overlap:
        print(0)
        exit(0)

print(ra - la + 1)

Submission status: Accepted

Solution code:
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
# i番目のゲートはl~rのIDカードを持っていれば通過できる
l_r = [input().split() for i in range(m)]
l_r.sort(key=lambda x:int(x[0]))
l_max = int(l_r[-1][0])
l_r.sort(key=lambda x:int(x[1]))
r_min = int(l_r[0][1])
answer = 0
# for i in range(n):
#     if l_max <= i + 1 and i + 1 <= r_min:
#         answer += 1
diff = r_min - l_max + 1
if diff > 0:
    print(diff)
else:
    print(0)

Submission status: Accepted

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

NM = [int(i) for i in input().split()]
LR = []
for i in range(NM[1]):
    LR.append([int(i) for i in input().split()])

mi = LR[0][0]
ma = LR[0][1]
for i in range(NM[1]):
    if LR[i][0] > mi:
        mi = LR[i][0]
    if LR[i][1] < ma:
        ma = LR[i][1]

'''
if ma > NM[0]:
    ma = NM[0]
if mi > NM[0]:
    mi = ma + 1
print(ma - mi + 1)
'''
if ma < mi:
    print(0)
elif NM[0] >= ma:
    print(ma-mi+1)
elif NM[0] < ma and NM[0] > mi:
    print(NM[0]-mi+1)
elif NM[0] <= mi:
    print(0)

Submission status: Accepted

Solution code:
from sys import stdin
n, m = map(int, stdin.readline().split())
mn = 1
mx = n

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

Submission status: Accepted

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

    l = List[j+1][0]
    r = List[j+1][1]

    if l > left:
        left = l
    if r < right:
        right = r
    if right < left:
        print(0)
        break
else:
    print(right-left+1)

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)

min = 1
max = n
for i in range(m):
    temp = l[i]
    min = min if min>temp else temp
    temp = r[i]
    max = max if max<temp else temp
    #print(min, max)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
N,M = map(int,input().split())
gates = []
for i in range(M):
    r = list(map(int, input().split()))
    gates.append(r)

left = gates[0][0]
right = gates[0][1]
#print(gates)
#print("left = " + str(left))
#print("right = " + str(right))
for i in range(1, M):
    if right >= gates[i][0]: #重なっていれば
        left = max([left, gates[i][0]])
        right = min([right, gates[i][1]])
        #print("left = " + str(left))
        #print("right = " + str(right))
    else:
        print(0)
        sys.exit()
print(right - left + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxL = 0
minR = N
for _ 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 sys
input = sys.stdin.readline
num = []
num = [int(i) for i in input().split()]

syou = []
dai = []

for temp in range(num[1]):
    suuzi = []
    suuzi = [int(i) for i in input().split()]
    syou.append(suuzi[0])
    dai.append(suuzi[1])

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = list()
r = list()
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 = map(int, input().split())
L = []
R = []
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

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

Submission status: Accepted

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

gates = []
start, end = 1, n
for _ in range(m):
    L, R = map(int, input().split())
    if L > start:
        start = L
    if R < end:
        end = R

print(len(range(start, end+1)))

Submission status: Accepted

Solution code:
inputs = input().split()
n = int(inputs[0])
m = int(inputs[1])
l = []
r = []
for i in range(0,m):
    inputs = input().split()
    l.append(int(inputs[0]))
    r.append(int(inputs[1]))
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())

Lmax = 0
Rmin = N

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

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

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

#[競プロ等におけるpython3の標準入力 \- Qiita](https://qiita.com/zenrshon/items/c4f3849552348b3dbe67)
L = []
R = []
for i in range(M):
    l,r=[int(i) for i in input().split()]
    L.append(l)
    R.append(r)

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
INT = list(map(int,input().split()))
N,M = INT[0],INT[1]

#print(N,M)

L_LIST = [0 for i in range(M)]
R_LIST = [0 for i in range(M)]
for i in range(M):
    LR_LIST = list(map(int,input().split()))
    L_LIST[i] = LR_LIST[0]
    R_LIST[i] =LR_LIST[1]

L_MAX = max(L_LIST)
R_MIN = min(R_LIST)

if R_MIN - L_MAX >= 0:
    print(R_MIN - L_MAX + 1)
    
else:
    print(int(0))

Submission status: Accepted

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

Lli = []
Rli =[]
for i in range(M):
    tmp = list(map(int,input().split()))
    Lli.append(tmp[0])
    Rli.append(tmp[1])

print(max(min(Rli)-max(Lli)+1,0))

Submission status: Accepted

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

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

L=0
R=N
for l,r in LR:
    if L<l:
        L=l
    if R>r:
        R=r
    #print(str(L)+str(R))

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

Submission status: Accepted

Solution code:
import numpy

N, M= map(int, input().split())
l = []
for _ in range(M):
  L, R = map(int, input().split())
  l.append([L, R])
a = numpy.array(l)

if (numpy.amin(a, axis=0)[1] - numpy.amax(a, axis=0)[0] + 1) < 0:
    print(0)
else:
    print(numpy.amin(a, axis=0)[1] - numpy.amax(a, axis=0)[0] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxL = 0
minM = N
for x in range(M):
    l, m = map(int, input().split())
    maxL = max(maxL, l)
    minM = min(minM, m)

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    # N, M = 4, 2
    # lr = [[1, 1], [3, 4]]
    ll = []
    rl = []
    for line in lr:
        ll.append(line[0])
        rl.append(line[1])
    diff = min(rl) - max(ll)
    if diff < 0:
        print(0)
        return
    print(diff + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
piyo = [list(map(int,input().split())) for i in range(m)]
k = 1
for i in range(m):
    if k < piyo[i][0]:
        k = piyo[i][0]
l = n
for i in range(m):
    if l > piyo[i][1]:
        l = piyo[i][1]
s = l - k + 1
print(max(0, s))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import numpy as np
N, M = map(int, input().split())
l_max = 1
r_min = 100000
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
    if r_min < l_max:
        print(0)
        exit()
print(r_min - l_max + 1)

Submission status: Accepted

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

for m in range(M):
    L[m], R[m] = map(int, input().split())
    
print(max(0,min(R)-max(L)+1))

Submission status: Accepted

Solution code:
import collections
N, M = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
smax = max([s[0] for s in S])
smin = min([s[1] for s in S])
if smin - smax + 1 >= 0 :
    print(smin - smax + 1)
else :
    print(0)


#S2 = [set(range(s[0], s[1] + 1)) for s in S]
#S3 = set.intersection(*S2)
#print(len(S3))

#ans = set(range(S[0][0], S[0][1] + 1))
#for s in S[1:] :
#    a = set(range(s[0], s[1] + 1))
#    ans = ans & a
#    if len(ans) == 0 :
#        break
#print(len(ans))

Submission status: Accepted

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

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)

ans = set(range(L_max,R_min+1))

print(len(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(lambda x: int(x)-1, input().split())

sum = 0
maxl = max(L)
minr = min(R)
for id in range(N):
    if maxl <= id and id <= minr:
        sum += 1

print(sum)

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
LR=list(sorted([list(map(int, input().split())) for _ in range(M)]))
a=LR[-1][0]
LR=list(sorted(LR,  key=lambda x: x[1]))
b=LR[0][1]
print(b-a+1 if b>=a else 0)

Submission status: Accepted

Solution code:
card_num, gate_num = map(int, input().split())

ans_start, ans_end = map(int, input().split())
for _ in range(gate_num-1):
    s, e = map(int, input().split())
    if e < ans_start or ans_end < s:
        print(0)
        exit()
    elif s < ans_start and ans_start < e < ans_end:
        ans_end = e
    elif ans_start < s < ans_end and ans_end < e:
        ans_start = s
    elif ans_start == e:
        ans_end = e
    elif ans_end == s:
        ans_start = s

print(ans_end - ans_start + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(M):
    L, R = map(int, input().split())
    if maxL < L:
        maxL = L
    if minR != None and R < minR:
        minR = R
    #print("\t\t\t", maxL, minR)

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

Submission status: Accepted

Solution code:
import numpy as np
a=[]#入力画面の二文字を別々の文字に格納
a=input().split(" ")
N=int(a[0])
M=int(a[1])
#cmdから2次元配列Gを作成
G = []
for i in range(M):
    array = list(map(int, input().strip().split()))
    G.append(array)
#列ごとに成分を抽出
b=[x[0] for x in G]#二次元配列Gの1列目
c=[x[1] for x in G]#二次元配列Gの2列目

B=max(b)
C=min(c)
y=C-B+1
if y>=0:
    print(y)
elif y<0:
    print('0')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ming = 100001
maxg = 0
for i in range(M):
    l, r = map(int, input().split())
    ming = min(r, ming)
    maxg = max(l, maxg)

print(max(0, ming-maxg+1))

Submission status: Accepted

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

Submission status: Accepted

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

small, large = 0, 0

for _ in range(0, m):
  i = input()
  s = int(i.split()[0])
  l = int(i.split()[1])
  if small == 0:
    small, large = s, l
  else:
    if small < s:
      small = s
    if large > l:
      large = l

if small > large:
  print(0)
elif small > n:
  print(0)
elif small <= n:
  if large <= n:
    print(large - small + 1)
  elif large > n:
    print(n - small + 1)

Submission status: Accepted

Solution code:
from sys import stdin

n, m = [int(x) for x in stdin.readline().rstrip().split()]

maxl = 1
minr = n
for i in range(m):
  l, r = [int(x) for x in stdin.readline().rstrip().split()]
  maxl = max(maxl, l)
  minr = min(minr, r)
  
ans = minr - maxl + 1

if ans < 0:
  ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
a = [0] * (n + 2)
r = defaultdict(int)
for i in range(m):
    x,y = map(int,input().split())
    a[x - 1] += 1
    a[y] += -1

for i in range(n + 1):
    a[i + 1] += a[i]
count = 0
flg = True
for i in range(n):
    if a[i] >= m:
        count += 1
print(count)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
min = 1
max = n
for var in range(0, m):
  a, b= map(int, input().split())
  if min < a:
    min = a

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

Submission status: Accepted

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

x = [0] * M
y = [0] * M
for i in range(M):
    x[i], y[i] = map(int, input().split())

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

Submission status: Accepted

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

gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
max_under_th = max([l[0] for l in gate_cond])
min_upper_th = min([l[1] for l in gate_cond])
if min_upper_th - max_under_th >= 0:
    print(min_upper_th - max_under_th + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split(" "))
min_l = 0
max_l = 10**5
for _ in range(m):
    l, r = map(int, input().split(" "))
    if min_l < l:
        min_l = l
    if r < max_l:
        max_l = r
if max_l < min_l:
    print(0)
else:
    print(max_l - min_l + 1)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
input = sys.stdin.readline

def io_generator():
	return input()

def iil(a_io):
	return map(int, a_io().split())


#+++++++++++++++++++


def main(io):
	rr,n =iil(io)
	lmax=-1
	rmin=rr+1
	for i in range(n):
		l,r=iil(io)
		lmax=max(lmax,l)
		rmin=min(rmin,r)
	
	return max(0, rmin-lmax+1)

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	ret = main(io)
	if ret is not None:
		print(ret)

Submission status: Accepted

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

N = inputs[0]
M = inputs[1]

start = 1
end = N

for i in range(M):
    inputs = list(map(int, input().split()))
    if start < inputs[0]:
        start = inputs[0]
    if inputs[1] < end:
        end = inputs[1]

if start <= end:
    print(end - start + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(M-1):
  l, r=map(int, input().split())
  if L < l:
    L = l
  if R > r:
    R = r
ans = R - L + 1
if ans > 0:
  print(ans)
else:
  print(0)

Submission status: Accepted

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

lown=[list[0][0]]
hin=[list[0][1]]
#print(lown,hin)

for i in range(1,g):
    if list[i][0]>lown[-1]:
        lown.append(list[i][0])
        
for i in range(1,g):
    if list[i][1]<hin[-1]:
        hin.append(list[i][1])
        
ans=[k for k in range(lown[-1],hin[-1]+1)]
print(len(ans))