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())
gate = []
l, r = map(int, input().split())
for i in range(1, m):
    m, s = map(int, input().split())
    if l < m:
        l = m
    if s < r:
        r = s
print(len([i for i in range(l, r + 1)]))

Submission status: Accepted

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

Submission status: Accepted

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

l_max, r_min = list(map(int,input().split(" ")))

for _ in range(1,m):
    l, r = list(map(int,input().split(" ")))
    
    if l >= l_max:
        l_max = l
        
    if r <= r_min:
        r_min = r


        
print(len(range(l_max, r_min+1)))

Submission status: Accepted

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

l_max=0
r_min=10**9
for tmp in l:
    l=tmp[0]
    r=tmp[1]
    if l_max < l:
        l_max=l
    if r_min > r:
        r_min=r
ans = r_min-l_max+1
if ans >= 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l, r = 0, 10**5 + 1
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:
n, m = map(int, input().split())
left = 1
right = n
for _ in range(m):
  l, r = map(int, input().split())
  if l > left:
    left = l
  if r < right:
    right = r
print(max(0, right - left + 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mi = 1
ma = 100000
for i in range(m):
    l,r = map(int,input().split())
    if l > mi:
        mi = l
    if r < ma:
        ma = r
if ma -mi < 0:
    print(0)
else:    
    print(ma-mi + 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())
   _min,_max=max(_min,L),min(_max,R)
print(max(_max+1-_min,0))

Submission status: Accepted

Solution code:
import sys
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 R<_min or L>_max:
       print(0)
       sys.exit()
   _min,_max=max(_min,L),min(_max,R)
print(_max+1-_min)

Submission status: Accepted

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

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())
a=0
b=0
f=0
for i in range(0,M):
    if i==0:
        a=L[i]
        b=R[i]
    else:
        if b<L[i] or a>R[i]:
            print(0)
            f=1
            break
        elif b>=R[i]>=a>=L[i]:
            b=R[i]
        elif b>=R[i]>=L[i]>=a:
            a=L[i]
            b=R[i]
        elif R[i]>=b>=L[i]>=a:
            a=L[i]

if f==0:
    print(b-a+1)

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
l_lst = []
r_lst = []
for _ in range(m):
    l, r = [int(x) for x in input().split()]
    l_lst.append(l)
    r_lst.append(r)

l_max = max(l_lst)
r_min = min(r_lst)

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

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

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())
   L = max(L, l)
   R = min(R, r)

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

Submission status: Accepted

Solution code:
import sys

n, m, *lr = map(int, sys.stdin.read().split())
l, r = lr[::2], lr[1::2]

def main():
    ans = max(min(r) - max(l) + 1, 0)
    return ans

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

Submission status: Accepted

Solution code:
import sys

n, m, *lr = map(int, sys.stdin.read().split())
lr = zip(*[iter(lr)] * 2)

def main():
    cnt = [0] * (n + 1)
    for l, r in lr:
        cnt[l-1] += 1
        cnt[r] -= 1
    
    for i in range(n):
        cnt[i+1] += cnt[i]
    
    res = 0
    for i in range(n):
        res += (cnt[i] == m) & 1
    
    return res

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

Submission status: Accepted

Solution code:
N , M = map(int,input().split())
L = []
R = []
cnt = 0
flag = True
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)
for i in range(N+1):
	flag = True
	if i >= L_max and i <= R_min:
		cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N_ID, M_gate = map(int, input().split())
mini = float('inf')
MAX = 0

for i in range(M_gate):
    L, R = map(int, input().split())
    MAX = max(MAX, L)
    mini = min(mini, R)

answer = max(0, mini - MAX + 1)
print(answer)

Submission status: Accepted

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

l_max, r_min = 1, 10**5
for di in d:
    l_max = max(l_max, di[0])
    r_min = min(r_min, di[1])

if r_min >= l_max:
    print(r_min - l_max + 1)
else:
    print(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)
  
max_L   = max(L)
min_R = min(R)
print(len(range(max_L, min_R+1)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve(n, m, LR):
    maxl = 0
    minr = n
    for l, r in LR:
        if maxl < l:
            maxl = l
        if minr > r:
            minr = r
    return minr - maxl + 1 if maxl <= minr else 0

_n, _m = map(int, input().split())
_LR = [list(map(int, input().split())) for _ in range(_m)]
print(solve(_n, _m, _LR))

Submission status: Accepted

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

l = max(L)
r = min(R)
print(r - l + 1 if l <= r else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

# print(imos)
print(imos[:-1].count(M))

Submission status: Accepted

Solution code:
import os, sys, re, math

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

minC = 1
maxC = N
for i in range(M):
    (L, R) = [int(n) for n in input().split()]
    minC = max(minC, L)
    maxC = min(maxC, R)

print(max((maxC - minC + 1), 0))

Submission status: Accepted

Solution code:
import os, sys, re, math

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

L = 1
R = N

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = [int(s) for s in input().split(' ')]
l = 1
r = N
for m in range(M):
    L, R = [int(s) for s in input().split(' ')]
    l = max(l, L)
    r = min(r, R)
print(max(r - l + 1, 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split(' ')))
l = 0
r = 100000
for i in range(m):
    p, q = list(map(int, input().split(' ')))
    l = max(l, p)
    r = min(r, q)
print(max(0, r-l + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L_list = []
for i in range(M):
    A = list(map(int, input().split()))
    L_list.append(A)
min_range = L_list[0][0]
max_range = L_list[0][1]
for i in range(1, M):
    if L_list[i][0] >= min_range:
        min_range = L_list[i][0]
    if L_list[i][1] <= max_range:
        max_range = L_list[i][1]
print(max_range-min_range+1) if max_range>=min_range else print(0)

Submission status: Accepted

Solution code:
l = input().split(" ")
n = int(l[0])
m = int(l[1])

r = []
l = []

for i in range(m):
    gatei = input().split(" ")
    li = int(gatei[0])
    ri = int(gatei[1])
    l.append(li)
    r.append(ri)

l.sort()
r.sort()

count = r[0] - l[m-1] + 1

if count >= 0:
    print(count)
else:
    print(0)

Submission status: Accepted

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

limax=0
rimin=n
for i in range(m):
    li,ri=map(int,input().split())
    if li>limax:
        limax=li
    if ri<rimin:
        rimin=ri
if rimin<limax:
    print(0)
else:
    print(rimin-limax+1)

Submission status: Accepted

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

s = [0] * (n+2)

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

ans = [0] * (n+2)
for i in range(1, n+2):
  ans[i] = ans[i-1] + s[i]

print(ans.count(m))

Submission status: Accepted

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

n, m = map(int, input().split())
l = ["" for _ in range(m)]
r = ["" for _ in range(m)]

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

L = 1
R = n

for i in range(m):
    L = max(L, l[i])
    R = min(R, r[i])
    #print(L, R)

print(len(list(range(L, R+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)
Lmax = max(L)
Rmin = min(R)
s = range(Lmax,Rmin+1)
print(len(s))

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("0")
else:
  print(min(R_list) - max(L_list) + 1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
f = lambda:map(int,input().split())

n, m = f()

lm = 0
rm = n

for i in range(m):
  l, r = f()
  lm = max(lm, l)
  rm = min(rm, r)

print(max(rm-lm+1, 0))

Submission status: Accepted

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

mi = 0
ma = 10**9

for index, n in enumerate(li):
  if mi < n[0]:
    mi = n[0]

  if ma > n[1]:
    ma = n[1]

ans = N - (mi - 1) - (N - ma)

if mi > ma:
  print(0)
else:
  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())

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

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

Submission status: Accepted

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

Submission status: Accepted

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

lstL = list()
lstR = list()

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

minVal = max(lstL)
maxVal = min(lstR)

res = (maxVal - minVal + 1) if minVal <= maxVal else 0

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

def main():
    N,M = cin.mapInt()
    G = cin.intListList(M)

    L = [0] * (N + 1)
    for i in G:
        L[i[0] - 1] += 1
        L[i[1]] -= 1
    for i in range(1,N + 1):
        L[i] += L[i - 1]
    print(L.count(M))
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = 0 # 制約における最小を超える値
right = 10 ** 6 # 制約における最大を超える値
for _ in range(m):
  l, r = map(int, input().split())
  left = max(left, l)
  right = min(right, r)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

min_id = 1
max_id = n
for i in range(m):
    l, r = map(int, input().split())

    if min_id < l <= max_id:
        min_id = l
    elif max_id < l:
        print(0)
        sys.exit()

    if min_id <= r < max_id:
        max_id = r
    elif r < min_id:
        print(0)
        sys.exit()

print(max_id - min_id + 1)

Submission status: Accepted

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

Submission status: Accepted

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

for e in l:
  L,R = e[0],e[1]
  if e == l[0]:
    _L,_R = L,R
  else:
    if _L < L:
      _L = L
    if _R > R:
      _R = R
if _R - _L >= 0:
  print(_R - _L + 1)
else:
  print(0)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

L,R = 0,100100
ans = [0]*(N+1)
for i in range(M):
    a,b = map(int,input().split())
    L = max(L,a)
    R = min(R,b)
if R - L >= 0:
    print(R-L+1)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

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

a=0
b=n
for l,r in ll:
 if a<l:
  a=l
 if r<b:
  b=r
print(max(b-a+1,0))

Submission status: Accepted

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

a=0
b=n
for l,r in ll:
 a=max(a,l)
 b=min(b,r)
print(max(b-a+1,0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
Lmax=1
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:
def main():
    N, M = map(int, input().split(" "))
    left = 0
    right = N
    for _ in range(M):
        l, r = map(int, input().split(" "))
        left = max(left, l)
        right = min(right, r)
    if right - left < 0:
        print(0)
    else:
        print(right - left + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

maxL = 0
minR = float('inf')
for _ in range(M):
    l, r = map(int, input().split())
    maxL = max(maxL, l)
    minR = min(minR, r)


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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import collections
 
def main():
    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()))
 
    ans = (int(min(R)) - int(max(L)) + 1)
    if ans < 0:
        print('0')
    else:
        print(ans)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
a=0
z=10**5
for i in range(m):
    a=max(a,l[i][0])
    z=min(z,l[i][1])
if a<=z:
    print(z-a+1)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    _, m = map(int, input().split())

    max = -1
    min = 10 ** 5 + 1
    for _ in range(m):
        l, r = map(int, input().split())
        if l > max:
            max = l
        if r < min:
            min = r
    if min - max < 0:
        print(0)
    else:
        print(min - max + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for _ in range(m):
    tmpL, tmpR = map(int, input().split())
    l = max(l, tmpL)
    r = min(r, tmpR)

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

Submission status: Accepted

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

l_max = 0
r_min = 10 ** 6
for l, r in left_rights:
    l_max = max(l_max, l)
    r_min = min(r_min, r)

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
#booltable=[0]+booltable


def resolve():
    input = sys.stdin.readline
    N,M=map(int, input().rstrip().split())
    G=[list(map(int,input().rstrip().split())) for _ in range(M)]
    l=[0 for _ in range(N)]
    for i in range(M):
        l[G[i][0]-1]+=1
        if G[i][1]!=N:
            l[G[i][1]]-=1
    l=[0]+l
    cumsum=list(accumulate(l))#0~N

    ans=cumsum.count(int(M))
    print(ans)

    

from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4 2
1 3
2 4"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """10 3
3 6
5 7
6 9"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """100000 1
1 100000"""
        output = """100000"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

L2=max(L)
R2=min(R)

if L2<=R2:
    print(R2-L2+1)
else:
    print(0)

Submission status: Accepted

Solution code:
# coding: utf-8

N, M = (int(x) for x in input().split())

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

L_max, R_min = 0, N
for i in range(M):
	L, R = (int(x) - 1 for x in input().split())
	
	if L_max < L:
		L_max = L
	if R_min > R:
		R_min = R


if L_max < R_min + 1:
	print(len(list(range(L_max, R_min + 1))))
else:
	print("0")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [1, N]
for i in range(M):
  L, R = map(int, input().split())
  A = [max(A[0], L), min(A[1], R)] 
print(max(0, (A[1] - A[0] + 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)
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())
ll=0
rr=n+1
for i in range(m):
    l,r=map(int,input().split())
    ll=max(ll,l)
    rr=min(rr,r)

if rr<ll:
    print(0)
else:
    print(rr-ll+1)

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mxa = 0
mnb = n+2

for i in range(m):
    a, b = map(int, input().split())
    mxa = max(mxa, a)
    mnb = min(mnb, b+1)

if mxa > mnb:
    ans = 0
else:
    ans = mnb - mxa

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

left = 1
right = N

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

L = 0
R = 10**10
for i in idcard:
    L = max(L, i[0])
    R = min(R, i[1])
if R >= L:
    print(R - L + 1)
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())
if min(R)-max(L)<0:
    print(0)
else:
    print(min(R)-max(L)+1)

Submission status: Accepted

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

for i in range(m):
    lr.append(list(map(int, input().split())))

lmax = 0
mmin = 10**5

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

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

Submission status: Accepted

Solution code:


S = input()
N = int(S.split(" ")[0])
M = int(S.split(" ")[1])


lArr = []
rArr = []
for i in range(M):
    S = input()
    l1 = int(S.split(" ")[0])
    r1 = int(S.split(" ")[1])
    lArr.append(l1)
    rArr.append(r1)

lArr.sort()
rArr.sort()

l1 = max(lArr)
r1 = min(rArr)

if r1 < l1:
    print(0)
else:
    print(r1 - l1 + 1)

Submission status: Accepted

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

min_num = 1
max_num = N
for i in range(M):
    min_input, max_input = list(map(int, input().split()))
    if min_input > min_num:
        min_num = min_input
    
    if max_input < max_num:
        max_num = max_input

output = max_num- min_num + 1

if output <= 0:
	print(0)
else :
  	print(output)

Submission status: Accepted

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

L=[]
R=[]

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

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

Submission status: Accepted

Solution code:
N, M, *I = map(int, open(0).read().split())
l = 0
r = N

for i in range(M):
    x, y = I[i*2], I[i*2+1]
    l = max(l, x)
    r = min(r, y)

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

Submission status: Accepted

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

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

    if a > l:
        l = a
    if b < r:
        r = b

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
lr = [[int(i) for i in input().split()] for _ in range(m)]
lr_t = list(zip(*lr))
l = max(lr_t[0])
r = min(lr_t[1])
ans = len(range(l, r+1))
print(ans)

Submission status: Accepted

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

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 R>r:
        R = r
    if L<l:
        L = l

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