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:
import collections
import math 
from operator import itemgetter
N,a = map(int, input().split())
t =[]
dp = []
qp = []
for i in range(a):
    n,m = map(int, input().split())
    dp.append(n)
    qp.append(m)
m = max(dp)
M = min(qp)
if m > M:s =0
else: s = M-m+1

print(s)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
lmax,mmin = 0,1000000
for i in range(M):
    l,m = map(int,input().split())
    if lmax < l:
        lmax = l
    if mmin > m:
        mmin = m

if lmax > mmin:
    ans = 0
elif lmax == mmin:
    ans = 1
else:
    ans = mmin-lmax
    ans += 1
print(ans)

Submission status: Accepted

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

max = 0
min = 0

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

    if min <= L:
        min = L
    elif min == 0:
        min = L

    if max >= R:
        max = R
    elif max == 0:
        max = R

ans = max - min + 1

if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:

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

L = []
R = []

ans = 0

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

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

for i in range(1,N+1):
    if Lmax <= i and i <= Rmin:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
nm = input().split()
minArr = []
maxArr = []

for m in range(int(nm[1])):
    tmp = input().split()
    minArr.append(int(tmp[0]))
    maxArr.append(int(tmp[1]))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

a1 = max(L)
a2 = min(R)

ans = a2-a1+1

if ans<0 : ans = int(0)

print(int(ans))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
LR=list(list(map(int,input().split())) for _ 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(R-L+1 if L<=R else 0)

Submission status: Accepted

Solution code:
l = list(map(int, input().split()))
n = l[1]
ans = []
anss = []
for i in range (0,n):
  m = list(map(int, input().split()))
  ans.append(m[0])
  anss.append(m[1])

if min(anss)-max(ans)+1 > 0:
  print(min(anss)-max(ans)+1)
if min(anss)-max(ans)+1 <= 0:
  print(0)

Submission status: Accepted

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

a=[]
b=[]
for i in range(M):

    X, Y= map(int, input().split())
    a.append(X)
    b.append(Y)
    
    if a[0]<a[i]:
      a[0]=a[i]
      
    if b[0]>b[i]:
      b[0]=b[i]

res=b[0]-a[0]+1

if res<0:
  res=0

print(res)

Submission status: Accepted

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

for i in range(m):
    l += [input().split()]
    if int(l[i][0]) > max:  
        max = int(l[i][0])
    if int(l[i][1]) < min:
        min = int(l[i][1])

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

Submission status: Accepted

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=10**6
for i in range(m):
  l,r=(int(j) for j in input().split())
  if l>a and r<b:
    a=l
    b=r
  elif l>a and r>=b:
    a=l
  elif r<b and l<=a:
    b=r
if b>=a:
	if n>=a and n<b:
  		print(n-a+1)
	elif n>=a and n>=b:
  		print(b-a+1)
	elif n<a:
  		print(0)
else:
  	print(0)

Submission status: Accepted

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

import re

def main():
    ID_gate = re.split(" +", input())
    ID = ID_gate[0]
    gate = ID_gate[1]
    min = 0
    max = 1000000
    for n in range(int(gate)):
        l_r = re.split(" +", input())
        if min < int(l_r[0]):
            min = int(l_r[0])
        if int(l_r[1]) < max:
            max = int(l_r[1])
    if max - min + 1 < 0:
        print(0)
    else:
        print(max - min + 1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin, exit

N, G = map(int, stdin.readline().split())
left, right = 1, N
for i in range(G):
    l, r = map(int, stdin.readline().split())
    if l > left:
        left = l
    if r < right:
        right = r
    if right < left:
        print(0)
        exit()

print(right - left + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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()
if r[0] - l[m-1] + 1 > 0:
  print(r[0] - l[m-1] + 1)
else:
  print("0")

Submission status: Accepted

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

MAX=np.max(list,axis=0)
MIN=np.min(list,axis=0)

ans = MIN[1]-MAX[0]+1
if ans>0:
  print(MIN[1]-MAX[0]+1)
else:
  print(0)

Submission status: Accepted

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

    ans = f(N, M, lr)
    #ans = WA2(N, M, lr)
    #ans = TLE(N, M, lr)
    print(ans)


def WA(N, M, lr):
    import numpy as np
    a = np.zeros(N, dtype=int)
    for L, R in lr:
        a[L-1:R] += 1
    ans = (a == M).sum()
    return ans


def f(N, M, lr):
    l_max = max(L for L, _ in lr)
    r_min = min(R for _, R in lr)

    ans = r_min - l_max + 1
    ans = max(ans, 0)
    return ans


def TLE(N, M, lr):
    ans = 0
    for i in range(1, N+1):
        g = 0
        for L, R in lr:
            if L <= i <= R:
                g += 1
        if g == M:
            ans += 1

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x = []
y = []
for i in range(m):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

mn=min(y)
mx=max(x)

ans=max(mn-mx+1,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())

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N, M = makeMultiInteger(input_string[0])
    S = makeIntMatrix(input_string[1:])
    c = 0
    ra = range(1, N+1)
        
    min_range = ra
    min_l = ra[0]
    min_r = ra[-1]
    for s in S:
        l = s[0]
        r = s[1]
        if l > min_range[0]:
            min_l = l
        if r < min_range[-1]:
            min_r = r
        min_range = range(min_l, min_r+1)
        if len(min_range) == 0:
            return 0
    #if ra[-1] >= min_range[-1]:
    return len(min_range)
    #else:
    #    return len(ra)



def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(max(0, min(arr_r) - max(arr_l) + 1))

Submission status: Accepted

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


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

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

Submission status: Accepted

Solution code:
import numpy as np

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

lr_mat = np.zeros((m, 2))

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

left_max = max(lr_mat[:, 0])
right_min = min(lr_mat[:, 1])

cnt = 0
for i in range(1, n+1):
    if(left_max <= i) and (i <= right_min):
        cnt += 1

print(cnt)

Submission status: Accepted

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

max_Li = -1
min_Ri = 10000000
for i in range(M):
    L_i, R_i = map(int, input().split())
    max_Li = max(max_Li, L_i)
    min_Ri = min(min_Ri, R_i)


print(max(0, min_Ri - max_Li + 1))

Submission status: Accepted

Solution code:
# coding: utf-8
n, m = map(int, input().split())
l, r = map(int, input().split())
for i in range(m - 1):
    tmp_l, tmp_r = map(int, input().split())
    if l < tmp_l:
        l = tmp_l
    if tmp_r < r:
        r = tmp_r

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
l_1 = 1
r_1 = n

for gate in args[1:]:
  l, r = map(int, gate.split(' '))  
  if l > l_1:
    l_1 = l
  if r < r_1:
    r_1 = r

ids = r_1 - l_1 + 1
if ids < 0:
  print(0)
else:
  print(ids)

Submission status: Accepted

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

l, r = 1, 10**5
for i in range(M):
    l_, r_ = [int(n) for n in input().split()]
    l = max(l, l_)
    r = min(r, r_)

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

Submission status: Accepted

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

Submission status: Accepted

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

l = 1
r = n



for list in lr:
    if list[0] > l:
        l = list[0]
    if list[1] < r:
        r = list[1]


result = r-l+1
if l > r:
    result = 0
elif l == r:
    result = 1

print(result)

Submission status: Accepted

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

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

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

ans = 0
for i in range(n):
    if imos[i] == m:
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

maxL=0
minR=N

for i in range(M):
    L, R = map(int, input().split(" "))
    
    if minR < L or R < maxL:
        print("0")
        exit()
    
    maxL=max(maxL, L)
    minR=min(minR, R)


print(minR-maxL+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n, m = list(map(int, input().split()))
ll = []
x = 0
y = 10 ** 5

for i in range(m):
    l, r = list(map(int, input().split()))
    x, y = max(x, l), min(y, r)

s = (y - x) + 1
if s < 0:
    s = 0
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

max_l = max(l)
min_r = min(r)

for i in range(1,n+1):
	
	if max_l <= i <= min_r:
		count += 1
			
print(count)

Submission status: Accepted

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

maxNum = 0
minNum = 0

for i in range(m):
    a, b = map(int, input().split())
    if maxNum == 0:
        maxNum = b
    else:
        if maxNum > b:
            maxNum = b

    if minNum == 0:
        minNum = a
    else:
        if minNum < a:
            minNum = a

if maxNum - minNum >= 0:
    print(maxNum - minNum + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
L = 1
R = N+1
ans = N*2

for i in range(M):
    tL,tR = list(map(int,input().split()))
    if tR < L or R < tL:
        ans = 0
        break
    if tL > L:
        L = tL
    if tR < R:
        R = tR

print(min(ans,R-L+1))

Submission status: Accepted

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

    if l > max:
        print(0)
        exit(0)

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

Submission status: Accepted

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

min = 0
max = n
for i in range(m):
  if min < l[i]:
    min = l[i]
  if max > r[i]:
    max = r[i]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[]
R=[]
for I in range(M):
	x,y=map(int,input().split())
	L.append(x)
	R.append(y+1)
F=list(range(max(L),min(R)))
print(len(F))

Submission status: Accepted

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

ans = min(gol) -max(sta) 
print(ans+1 if ans >= 0 else 0)

Submission status: Accepted

Solution code:
N,M=input().split()
list01=[]
for i in range(int(M)):
	list01.append(input().split())
min_list=[]
max_list=[]
for i in range(int(M)):
	min_list.append(int(list01[i][0]))
	max_list.append(int(list01[i][1]))
min_num=max(min_list)
max_num=min(max_list)
list_N=list(range(1,int(N)+1))
score=0
for i in range(len(list_N)):
	if int(min_num)<=list_N[i]<=int(max_num):
		score+=1
print(score)

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
LL, RR = map(int, input().split())
for i in range(M-1):
    L, R = map(int, input().split())
    LL = max(L, LL)
    RR = min(R, RR)
print(max(RR-LL+1,0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
card=[]
L=1
R=n
for i in range(m):
    card.append([int(i) for i in input().split()])
for j in card:
    if j[0]<=R:
        if L<=j[0]:
            L=j[0]
            
    else:
        print(0)
        quit()
    if L<=j[1]:
        if j[1]<=R:
            R=j[1]
            
    else:
        print(0)
        quit()
print(R-L+1)

Submission status: Accepted

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

gate_s = sorted(gate,key=lambda x:x[0])
min_r = 1000000
max_l = 0

for i in range(M):
  if gate_s[i][1] < min_r:
    min_r = gate_s[i][1]
  if gate_s[i][0] > max_l:
    max_l = gate_s[i][0]
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())

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

Submission status: Accepted

Solution code:
#入力取得
N, M = map(int, input().split())

#最大と最小を選べば良い

left_max = 0
right_min = N


for i in range(M):

    tmp_left,tmp_right = map(int, input().split())


    if tmp_left > left_max:
        left_max = tmp_left

    if tmp_right < right_min:
        right_min = tmp_right



result = right_min - left_max

if result < 0:
    print(0)
else:
    print(result + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


v = [int(d) for d in input().split()]

LM = []
for d in range(v[1]):
   LM.append([int(d) for d in input().split()])

L = [d[0] for d in LM]
M = [d[1] for d in LM]
L.sort()
M.sort()
if L[-1] <= M[0]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')

Submission status: Accepted

Solution code:
N, M = input().split(" ")
#S = []
if M == 1:
    a, b = input().split(" ")
    print(int(b)-int(a)+1)
else:    
    a, b = input().split(" ")
    aM, bm = int(a),int(b)
    #sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        aM = max(aM,int(a))
        bm = min(bm,int(b))
        #sets = sets & set([i for i in range(int(a),int(b)+1)])
    if(bm<aM):
        print(0)
    else:
        print(int(bm)-int(aM)+1)

Submission status: Accepted

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

migi = []
hidari = []
for i,j in para_val:
    migi.append(i)
    hidari.append(j)

a = max(migi)
b = min(hidari)

if b - a >= 0 : print("{}".format(b-a+ 1))
else : print("0")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

L = []
R = []
l = 0
r = 10 ** 6
for i in range(M):
    a,b = map(int, input().split())
    L.append(a)
    R.append(b)

for i in range(M):
    if l < L[i]:
        l = L[i]
    if r > R[i]:
        r = R[i]

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

Submission status: Accepted

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

mi, mx = map(int, input().split())
flag = False
for i in range(m-1):
    l, r = map(int, input().split())
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r
    if mx < l or r < mi:
        flag = True
        break

if flag:
    print(0)
else:
    print(mx - mi + 1)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N = int(s[0])
    M = int(s[1])
    L = []
    R = []
    for i in range(M):
        s = input().rstrip().split(' ')
        L.append(int(s[0]))
        R.append(int(s[1]))

    res = list(range(1,N+1))
    res_min = 1
    res_max = N

    for i in range(M):
        res_min = max(res_min,L[i])
        res_max = min(R[i],res_max)

    print(max(res_max-res_min+1,0))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = [1,n]
for i in range(m):
    temp = list(map(int, input().split()))
    l[0] = max(l[0],temp[0])
    l[1] = min(l[1],temp[1])

print(int(max(l[1]-l[0]+1, 0)))

Submission status: Accepted

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

MIN = [LR[i][0] for i in range(M)]
MAX = [LR[i][1] for i in range(M)]

if min(MAX) >= max(MIN):
    print(min(MAX) - max(MIN) + 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)
ans = max(r - l + 1, 0)
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
set1=set()
L,R=map(int,input().split())
for i in range(L,R+1):
    set1.add(i)
min=min(set1)
max=max(set1)
Flag=True
for i in range(M-1):
    L,R=map(int,input().split())
    if R < min:
        Flag=False
        break
    elif L > max:
        Flag=False
        break
    if L >= min and max >= L:
        min = L
    if R >= min and max >= R:
        max = R
if Flag:
    print(max-min+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
maxL = 1
minR = 100000
for i in range(M):
    Li,Ri = map(int,input().split())
    maxL = max(maxL,Li)
    minR = min(minR,Ri)
        
if minR - maxL >= 0:
    print(minR-maxL+1)
else:
    print(0)

Submission status: Accepted

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

min_num = 1
max_num = n
for i in range(m):
  l,r = map(int, input().split())
  if min_num < l:
    min_num = l
  if max_num > r:
    max_num = r

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
list = []
for i in range(M):
    L, R = map(int, input().split())
    list.append([L,R])
anslist = [list[0][0],list[0][1]]
arukadouka = True
cnt = 0
for i in range(1,M):
    if arukadouka == True:
        if anslist[0] > list[i][1]:
            arukadouka = False
        elif anslist[1] < list[i][0]:
            arukadouka = False
        elif list[i][0] == list[i][1]:
            anslist[0] = list[i][0]
            anslist[1] = list[i][1]
        else:
            if (anslist[0]<=list[i][0]) and (anslist[1]<=list[i][1]):
                anslist[0] = list[i][0]
            elif (anslist[0]<=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[0] = list[i][0]
                anslist[1] = list[i][1]
            elif (anslist[0]>=list[i][0]) and (anslist[1]<=list[i][1]):
                pass
            elif (anslist[0]>=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[1] = list[i][1]
        cnt += 1
if arukadouka == True:
    ans = anslist[1]-anslist[0]+1
else:
    ans = 0
print(ans)

Submission status: Accepted

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

for i in range(M):
    tmp = list(map(int, input().split()))
    if L<tmp[0]:
        L = tmp[0]
    if R>tmp[1]:
        R = tmp[1]

ans = [x for x in range(L, R+1)]

print(len(ans))

Submission status: Accepted

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

start, end = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if r < start or l > end:
    start, end = 1, 0
    break
  elif l <= start and start <= r <= end:
    end = r
  elif start <= l <= end and end <= r:
    start = l
  elif l <= start and r >= end:
    pass
  else:
    start, end = l, r

print(end - start + 1)

Submission status: Accepted

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

count = 0
minR = min(R)
maxL = max(L)
if minR < maxL:
  count = 0
else:
  for i in range(maxL, minR+1):
    if i <= N:
      count = count + 1
      
print('%d' % count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
l = 1
r = N

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

Lmax = L[0]
Rmin = R[0]
for i in range(m - 1):
    if Lmax < L[i + 1]:
        Lmax = L[i + 1]
    if Rmin > R[i + 1]:
        Rmin = R[i + 1]

if Lmax > n or Lmax > Rmin:
    print(0)
elif Rmin > n:
    print(n - Lmax + 1)
else:
    print(Rmin - Lmax + 1)

Submission status: Accepted

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

ans = min(R_list) - max(L_list) + 1

if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
c = [tuple(map(int, input().split())) for _ in range(M)]
right = N
left = 1
for i in range(0, M):
    left = max(left, c[i][0])
    right = min(right, c[i][1])
print(len(range(left, right+1)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a1 = 0
b1 = N
for i in range(M):
    a, b = map(int, input().split())
    if a1 <= a:
        a1 =a
    if b1 >= b:
        b1 = b

ans = b1-a1+1
if ans <0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L,H=map(int,input().split())
c=0
w=1
while c<M-1:
    l,h=map(int,input().split())
    if L<=l<=H:
        L=l
    elif l>H:
        w=0
        break
    if L<h<H:
        H=h
    c+=1
if w!=0:
    print(H-L+1)
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter as co
n,m=map(int,input().split())
a=[]
x=0
y=10**6
for i in range(m):
    l,r=map(int,input().split())
    if(l>x):
        x=l
    if(r<y):
        y=r
if(y>=x):
    print(y-x+1)
else:
    print(0)

Submission status: Accepted

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

s_list = list()
e_list = list()

for m in range(M):
    s, e = map(int, input().strip().split())
    s_list.append(s)
    e_list.append(e)

s_max = max(s_list)
e_min = min(e_list)

if s_max > e_min:
    print(0)
else:
    print(e_min-s_max+1)

Submission status: Accepted

Solution code:
id, gate=map(int,input().split())
s,f=0,id

for i in range(gate):
    l,r=map(int,input().split())
    s=max(s,l)
    f=min(f,r)

if s>f:
    print(0)
else:
    print(f-s+1)

Submission status: Accepted

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


left = lr[0][0]
right = lr[0][1]
for i in range(1,m):
    if left < lr[i][0]:
        left = lr[i][0]
    if right > lr[i][1]:
        right = lr[i][1]
    
    if left > right:
        break




if left > right:
    print(0)

if left <= right:
    print(right-left+1)

Submission status: Accepted

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

N, M = int(N), int(M)

mini = 1
maxi = N
for i in range(M):
    L, R = input().split()
    mini, maxi = max(mini, int(L)), min(maxi, int(R))
print(max((maxi-mini+1),0))

Submission status: Accepted

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

n_left = 1
n_right = n
result = 0

for i in range(m):
    l_i,r_i = [int(i) for i in input().split()]
    if(l_i > n_left):
        n_left = l_i
    if(r_i < n_right):
        n_right = r_i

if(n_left > n_right):
    print(0)
else:
    print(n_right - n_left + 1)

Submission status: Accepted

Solution code:
cardNum,gate = map(int,input().split())
l = []
r = []
for i in range(gate):
  ll,rr = map(int,input().split())
  l.append(ll)
  r.append(rr)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
minNum = 1
maxNum = N
for lr in range(M):
  l, r = map(int, input().split())
  if minNum <= l and r <= maxNum:
    minNum = l
    maxNum = r
  elif minNum <= l <= maxNum and maxNum <= r:
    minNum = l
  elif l <= minNum and minNum <= r <= maxNum:
    maxNum = r
  elif r < minNum or maxNum < l:
    print("0")
    exit()
#  print(l,r,minNum,maxNum)
print(maxNum - minNum +1)

Submission status: Accepted

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

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

if int(min(R) - max(L) + 1) < 0:
    print(0)
else:
    print(int(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,R = [],[]
for i in lr:
  l,r = i[0],i[1]
  L.append(l)
  R.append(r)
ans = max(min(R) - max(L) + 1,0)
print(ans)

Submission status: Accepted

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

for _ in range(M):
    l, r = map(int, input().split())
    min_gate = max(min_gate, l)
    max_gate = min(max_gate, r)

print(max(max_gate - min_gate + 1, 0))

Submission status: Accepted

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

for gate in LR:
    L.append(gate[0])
    R.append(gate[1])

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

Submission status: Accepted

Solution code:
N, M = (int(x) for x in input().split())
L = []
R = []
for i in range(M):
	l, r = (int(x) for x in input().split())
	L.append(l)
	R.append(r)
	
lmax = max(L)
rmin = min(R)
ans = rmin - lmax + 1
if ans < 0:
	ans = 0

print(ans)