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())
l, r = 1, 10**5
for _ in range(m):
    l_cur, r_cur = map(int, input().split())
    l = max(l_cur, l)
    r = min(r_cur, r)
    if r < l:
        break

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

Submission status: Accepted

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

import numpy as np

rets = np.array([0] * n)
lmax = 1
rmin = n
for _ in range(m):
  l, r = map(int, input().split())
  lmax = max(l, lmax)
  rmin = min(r, rmin)
print(rmin - lmax + 1 if rmin - lmax + 1 > 0 else 0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split(' '))
left,right = map(int,input().split(' '))
for i in range(M-1):
  L,R = map(int,input().split(' '))
  left = max(L,left)
  right = min(R,right)

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

Submission status: Accepted

Solution code:
import sys
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
n, m = MI()
lr = [LI() for _ in range(m)]
l = lr[0][0]
r = lr[0][1]
for i in range(1, m):
    if lr[i][0] < l and lr[i][1] < l:
        print(0)
        exit()
    if r < lr[i][0] and r < lr[i][1]: 
        print(0)
        exit()
    if l <= lr[i][0] <= r:
        l = lr[i][0] 
    if l <= lr[i][1] <= r:
        r = lr[i][1]
print(r - l + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
l = 0
r = 10000000
for i in range(M):
    x, y = map(int, input().split())
    l = max(l, x)
    r = min(r, y)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = (int(x) for x in input().split())
L, R = [None]*M, [None]*M
for i in range(M):
    L[i], R[i] = (int(x) for x in input().split())

tmp = [L[0], R[0]]
for j in range(1, M):
    if tmp[0] < L[j]:
        tmp[0] = L[j]
    if R[j] < tmp[1]:
        tmp[1] = R[j]
    if tmp[0] > tmp[1]:
        print(0)
        exit()

print(tmp[1]-tmp[0]+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import itemgetter
import sys

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


cng = []
L_max=0
R_min=float('inf')
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())
 
L = []
R = []
for _ in range(M):
  x,y = map(int,input().split())
  L.append(x)
  R.append(y)
 
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())
lmax,rmin=0,n+1
for i in range(m):
  l,r=map(int, input().split())
  lmax=max(l,lmax)
  rmin=min(r,rmin)
print(max(0,rmin-lmax+1))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*t=map(int,open(0).read().split());print(max(0,min(t[2::2])-max(t[1::2])+1))

Submission status: Accepted

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

ans = mi - ma + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
n,m,*t=map(int,open(0).read().split());print(max(0,min(t[1::2])-max(t[::2])+1))

Submission status: Accepted

Solution code:
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
l = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

sMax = 0
eMin = 0
for s, e in l:
  if sMax < s or sMax == 0:
    sMax = s
  if eMin > e or eMin == 0:
    eMin = e

if eMin - sMax >= 0:
  print(eMin - sMax + 1)
else:
  print(0)

Submission status: Accepted

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

lans = 1
rans = n
for i in range(m):
  l,r = map(int,input().split())
  lans = max(lans,l)
  rans = min(rans,r)
if lans <= rans:
  print(len(list(range(lans,rans+1))))
else:
  print(0)

Submission status: Accepted

Solution code:
card, gate = map(int, input().split())
max_l = 0
min_r = 100000
for i in range(gate):
  l, r = map(int, input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print(len(range(max_l, min_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)
max_l=max(L)
min_r=min(R)
if min_r-max_l>=0:
    print(min_r-max_l+1)
else:
    print(0)

Submission status: Accepted

Solution code:
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
m,n= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
left = 1
right = m
for p in range(n):
    l,r = (int(i) for i in input().split())
    left = max(l,left)
    right = min(r,right)
if left<=right:
    print(right-left+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)
L.sort()
R.sort()
A = R[0] - L[-1] +1
print(max(A,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = input().split()
N = int(N)
M = int(M)
L_list =[]
R_list = []
for h in range(M):
    L, R = input().split()
    L_list.append(L)
    R_list.append(R)

L_list = map(int,L_list)
R_list = map(int,R_list)

L = max(L_list)
R = min(R_list)

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 i in range(m-1):
    l_c, r_c = map(int, input().split()) 
    l = max(l, l_c)
    r = min(r, r_c)
print(max(r-l+1, 0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[0 for i in range(M)]
R=[0 for i in range(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:

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)

maxl = max(L)
minr = min(R)

ans = minr - maxl + 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):
    a,b = map(int,input().split())
    l.append(a)
    r.append(b)
count = 0
ma = max(l)
mi = min(r)
for i in range(1,n+1):
        if ma <= i <= mi: 
            count += 1
print(count)

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)

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=0
R=0
mx=0
mn=100000
for i in range(M):
  L, R = map(int,input().split())
  if L > mx:
      mx=L
  if R < mn:
      mn=R

result =mn-mx+1
if result < 0:
    result = 0
print(result)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import numpy as np

N, M = map(int, sys.stdin.readline().split(" ", 2))

L = 0
R = M + N
for line in sys.stdin:
    l, r = map(int, line.split(" ", 2))
    if l > L:
        L = l
    if r < R:
        R = r
print(max(0, R - L + 1))
    # L.append(l)
    # R.append(r)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

l = []
r = []

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

ans = N-max(l)-max(r)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    id_and_gate = []
    for m in range(0, M):
        tmp = [int(x) for x in input().split()]
        id_and_gate.append(tmp)

    l = 1
    r = 100000
    for j in range(0, M):
        if id_and_gate[j][0] >= l:
            l = id_and_gate[j][0]
        if id_and_gate[j][1] <= r:
            r = id_and_gate[j][1]

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

if __name__ == "__main__":
    main()

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)

L_list.sort(reverse=True)
R_list.sort()
MAX = L_list[0]
MIN = R_list[0]
cnt=0
for i in range(1,N+1):
    if(i<=MIN and i>=MAX):
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    l = []
    r = []
    ans = 0
    for i in range(m):
        a, b = map(int, input().split())
        l.append(a)
        r.append(b)
    ml = max(l)
    mr = min(r)
    for i in range(1, n+1):
        if i >= ml and i <= mr:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m,*t=map(int,open(0).read().split());a,c=[0]*-~n,0
for l,r in zip(t[::2],t[1::2]):a[l-1]+=1;a[r]-=1
for i in range(n):a[i+1]+=a[i];c+=a[i]==m
print(c)

Submission status: Accepted

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

li = [[0, 99999999999]]
L_max   = 0
R_min  = 999999999999
for i in range(M):
    li.append(list(map(int, input().split())))
    if (L_max  < li[i+1][0] ):
        L_max = li[i+1][0]
        if (R_min  > li[i+1][1]):
            R_min = li[i+1][1]
    elif (R_min > li[i+1][1]):
            R_min = li[i+1][1]

if (L_max  < li[M][0] ):
       L_max = li[M][0]
       if (R_min  > li[M][1]):
          R_min = li[M][1]
elif (R_min  > li[M][1]):
            R_min = li[M][1]

if (L_max > N) or (L_max > R_min):
    print("0")
else:
    print(R_min - L_max + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    mi = gates[0][1]
    ma = gates[0][0]

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

s = (input().split(' '))

n,m = [int(i) for i in s]

l_list = []
r_list = []

for i in range(m):
    s = (input().split(' '))
    l,r = [int(i) for i in s]
    l_list.append(l)
    r_list.append(r)

range = (max(l_list), min(r_list))

if range[0] > range[1]:
    print(0)
else:
    print(range[1] - range[0] + 1)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

L_all,R_all = 1,N
for L,R in LR:
    L_all = max(L_all,L)
    R_all = min(R_all,R)

print(max(0,R_all+1-L_all))

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N,M = [int(x) for x in input().split()]
Llist =[]
Rlist =[]
for i in range (M):
    L,R = [int(x) for x in input().split()]
    Llist.append(L)
    Rlist.append(R)
 
lmax = max(Llist)
Rmax = min(Rlist)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

L = []
R = []

for i in range(0, 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
lr=[]
for i in range(m):
  lr.append(input().split())
for j in lr:
  if l<int(j[0]):
    l=int(j[0])
  if r>int(j[1]):
    r=int(j[1])
if r>=l:
  print(int(r-l+1))
else:
  print(0)

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
L = 0
R = 0
Lmax = 0
Rmin = 100000
for i in range(0,M):
  L, R = map(int,input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

result = Rmin - Lmax + 1
if result < 0:
    result = 0
print(result)

Submission status: Accepted

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

L = 1
R = N

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

L = R - L + 1
if L < 0:
    L = 0

print(L)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def main():
    N, M = i2nn()
    L, R = (0, N)
    for i in range(M):
        Li, Ri = i2nn()
        if L < Li:
            L = Li
        if Ri < R:
            R = Ri
    if L > R:
        print(0)
    else:
        print(R - L + 1)
    return

main()

Submission status: Accepted

Solution code:
import math

def main():
    n, m = [int(x) for x in input().split(" ")]
    gates = []
    mi = n
    ma = 0

    for x in range(m):
        l, r = [int(x) for x in input().split(" ")]
        gates.append((l, r))

    for gate in gates:
        # print(gate)
        if ma <= gate[0]:
            ma = gate[0]

        if mi >= gate[1]:
            mi = gate[1]

    res = 0
    if mi < ma:
        res = 0
    else:
        res = mi - ma + 1

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a = 1

n, m = map(int, input().split())
ans_r = 10 ** 5

l_list = []
r_list = []
for i in range(m):
    l, r = map(int, input().split())
    l_list.append(int(l))
    r_list.append(int(r))
for r in r_list:
    if ans_r > r:
        ans_r = r
for l in l_list:
    if a < l:
        a = l

if ans_r >= a:
    print(ans_r - a + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
l = raw_input().split()
N = int(l[0])
M = int(l[1])

L = []
R = []

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

l = max(L)
r = min(R)
# print(l, r)
if l > r:
    print(0)
else:
    print(r - l + 1)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
L = 1
R = N
for i in range(M):
    L_,R_ = [int(x) for x in input().split()]
    L = max(L, L_)
    R = min(R, R_)

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

Submission status: Accepted

Solution code:
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

max = N
min = 0
for i in range(M):
  inputcard = list(map(int, input().split()))
  if min < inputcard[0]:
    min = inputcard[0]
  
  if max > inputcard[1]:
    max = inputcard[1]
  
if (max - min + 1 < 0):
  print(0)
else:
  print(max - min + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

lst_1 = input().split()

N = int(lst_1[0])
M = int(lst_1[1])

lst_A = []

ans_min = 1
ans_max = N

for i in range(M):
    lst_A.append(input().split())

for m in range(0, M, 1):

    com_min = int(lst_A[m][0])

    if com_min > ans_min:
        ans_min = com_min

    com_max = int(lst_A[m][1])

    if com_max < ans_max:
        ans_max = com_max

ans = ans_max - ans_min + 1

if ans < 1:
    ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,m):
    if min<ab[i][0]:
        min=ab[i][0]
    if ab[i][1]<max:
        max=ab[i][1]
if max<min:
    print("0")
else:
    print(max-min+1)

Submission status: Accepted

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

Submission status: Accepted

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

from sys import stdin

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

clear_list = [1] * N
start = 0
end = 0
ans_start = 0
ans_end = 0
    
for i in range(M):
    start, end  = [int(x) for x in stdin.readline().rstrip().split()]
    if ans_start == 0 or ans_start < start : 
        ans_start = start
    if ans_end == 0 or ans_end > end:
        ans_end = end

if ans_end - ans_start < 0:
    print(0)
else:
    print(ans_end - ans_start + 1 )

Submission status: Accepted

Solution code:
hq_left = []
hq_right = []

#スペースなどで区切られた入力が複数行に続くとき
listA=[] #appendのために宣言が必要
N,M= map(int,input().split())

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


candidate_list = [i for i in range(1,N+1)]

for i in range(M):
    hq_left.append(listA[i][0])
    hq_right.append(listA[i][1])

if min(hq_right)-max(hq_left) <0:
    print(0)
else:
    print(len(candidate_list[max(hq_left):min(hq_right)])+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())
if n < min(b) and n >= max(a):
    print(n - max(a) + 1)
elif min(b) >= max(a):
    print(min(b)-max(a) + 1)
else:
  print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
 
data = []
l = 1
r = 10**5
for m in range(M):
    li, ri = map(int, input().split(' '))
    if li > l:
      l = li
    if ri < r:
      r = ri
 
if l<= N <= r:
  cnt = (N - l) + 1
elif l > r:
  cnt = 0
elif r < N:
  cnt = (r - l) + 1
else:
  cnt = 0
 
print(cnt)

Submission status: Accepted

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

# ll = Ls[0]
# rr  =Rs[0]
# for i in range(M):
#     ll = max(ll, Ls[i])
#     rr = min(rr, Rs[i])
#
# print(rr - ll + 1)
print(max(0, min(Rs) - max(Ls) + 1))

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())
ok = [1] * n
l, r = (1,n)
for i in range(1,m+1):
  tmp_l, tmp_r = (int(j) for j in input().split())
  if l < tmp_l:
    l = tmp_l
  if r > tmp_r:
    r = tmp_r

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

Submission status: Accepted

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

ans = []
minn = 10000000
maxx = 0
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    maxx = max(maxx,l)
    minn = min(minn,r)
if minn - maxx < 0:
    print(0)
else:
    print(minn - maxx+1)

Submission status: Accepted

Solution code:
# -- coding:utf-8 --
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for ii in range(m)]
L = [lr[jj][0] for jj in range(m) ]
R = [lr[jj][1] for jj in range(m) ]

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

Submission status: Accepted

Solution code:
def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])

L_max = max(L)
R_min = min(R)

num = R_min - L_max

if num >= 0:
    print(num + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split(" "))
l_init,r_init=map(int,input().split(" "))
ans_l=l_init
ans_r=r_init
isPossible=True
for i in range(m-1):
	l,r=map(int,input().split(" "))
	isupdated=False
	next_ans_l=ans_l
	next_ans_r=ans_r
	if ans_l <= l and l <= ans_r:
		next_ans_l=l
		isupdated=True
	if ans_l <= r and r <= ans_r:
		next_ans_r=r
		isupdated=True
	if l <= ans_l and ans_r <= r:
		isupdated=True
	if not isupdated:
		isPossible=False
		break
	ans_l=next_ans_l
	ans_r=next_ans_r
ans=ans_r-ans_l
if isPossible:
	print(ans+1)
else:
	print(0)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
l, r = [], []
x = 0
c = 0

a = []
for _ in range(n):
    a.append(0)

for _ in range(m):
    l,r = map(int, input().split())
    a[l-1] += 1
    if r != n:
        a[r] -= 1

for i in range(n):
    x += a[i]
    if(x == m):
        c += 1

print(c)

Submission status: Accepted

Solution code:
import copy
N, M = map(int, input().split())
LR = []
LR2 = []
count = 0
min = 1
max = N
for i in range(M):
    l, r = map(int, input().split())
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()
#print(M, N, LR)

for i in LR:
    if min < i[0]:
        min = i[0]
    if max > i[1]:
        max = i[1]
    #print(min, max)
if max - min >= 0:
    ans = max - min + 1
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
list_l = []
list_R = []
ID,gate = map(int,input().split())
s = 0
for i in range(gate):
  l,r = map(int,input().split())
  list_l.append(l)
  list_R.append(r)
a = max(list_l)
b = min(list_R)

for i in range(ID+1):
  if a <= i <= b:
    s += 1
  else:
    None
print(s)

Submission status: Accepted

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

stepper = 0
gate = []

for j in range(M):
    gate_1, gate_2 = map(int, input().split())
    gate.append([gate_1, gate_2])

gate_min = 0
gate_max = N

for m in range(M):
    if gate[m][0] > gate_min:
        gate_min = gate[m][0]

    if gate[m][1] < gate_max:
        gate_max = gate[m][1]

#print(gate_min, gate_max)
for k in range(N):
    k = k+1
    if gate_min <= k <= gate_max:
        stepper += 1

print(stepper)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

l_max=-1
r_min=float("inf")
for i in range(M):
	l,r=map(int, raw_input().split())
	l_max=max(l_max, l)
	r_min=min(r_min, r)

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = []
b = []
c = []
for i in range(m):
  a.append(list(map(int, input().split())))
for i in range(m):
  b.append(a[i][0])
  c.append(a[i][1])
  
b_max = max(b)
c_min = min(c)

ans = max(0, min(c) - max(b) + 1)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
table = [[int(i) for i in input().split()] for m in range(M)]
L=[]
R=[]
c=0
d=0
x=0
for i in range(M):
  L.append(table[i][0])
  
for i in range(M):
  R.append(table[i][1])
  
c=max(L)
d=min(R)
    


if c>d:
    print("0")
else:
    x=d-c+1
    print("%d"%x)

Submission status: Accepted

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

s = 1
e = N

for l,r in zip(Ls, Rs):
    s = max(s, l)
    e = min(e, r)
    if s > e:
        break
print(max(0, (e+1)-s))

Submission status: Accepted

Solution code:
a,b=map(int, input().split())
c,d=map(int, input().split())
start=c
end=d


  
  

for i in range(b-1):
  e,f=map(int, input().split())
  if e>=start:
    start=e
  if f<=end:
    end=f

if end>= start:
  out=end-start+1
else:
  out=0
  
  

print(out)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Lmax = 0
Rmin = 1000000
for i in range(m):
	L,R = map(int,input().split())
	if Lmax < L:
		Lmax = L
	if Rmin > R:
		Rmin = R
res = Rmin - Lmax + 1
if res > 0:
	print("%d" % (Rmin - Lmax +1))
else:
	print(0)

Submission status: Accepted

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


if __name__ == '__main__':
    main()