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())
table = [[int(i) for i in input().split()] for _ in range(M)]

ans = [table[0][0], table[0][1]]
cnt = 0

for i in table[1:]:
    if ans[0] <= i[0] <= ans[1]:
        ans[0] = i[0]
    if ans[0] <= i[1] <= ans[1]:
        ans[1] = i[1]
    if ans[1] < i[0] or i[1] < ans[0]:
        cnt += 1
        break

if cnt != 0:
    print(0)
else:
    print(ans[1] - ans[0] + 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)
ml = max(L)
mr = min(R)
print(mr - ml + 1 if ml <= mr else 0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split(' '))
i = 0
lmax = 0
rmin = n
while i < m:
    l,r = map(int,input().split(' '))
    if l > lmax:
        lmax = l
    if r < rmin:
        rmin = r
    i += 1
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a,b = map(int,input().split())
for i in range(M-1):
    c,d = map(int,input().split())
    a = max(a,c)
    b = min(b,d)
if b>=a:
	print(b-a+1)
else:
  print(0)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
L, R = ZIP(M)
l, r = max(L), min(R)
print(0 if l > r else r-l+1)

Submission status: Accepted

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

Submission status: Accepted

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

lList = [arr[0] for arr in LR]
rList = [arr[1] for arr in LR]

l = max(lList)
r = min(rList)

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
LR = [list(map(int,input().split())) for _ in range(M)]
INF = 10**5+1

small,big = 0,INF
for i in range(M):
    small = max(small,LR[i][0])
    big = min(big,LR[i][1])

if small <= big:
    print(big-small+1)
else:
    print(0)

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())
    if L > l:
        l = L
    if R < r:
        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):
    x , y = map(int,input().split())
    l.append(x)
    r.append(y)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = list(list(map(int,input().split())) for i in range(M))
l = 1
r = N
for i in range(M):
    l = max(l,L[i][0])
    r = min(r,L[i][1])
print(max(r-l+1,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 min(b)<max(a):
	print(0)
else:
	print(min(b)-max(a)+1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L = 0
R = 100000

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

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = list(list(map(int,input().split())) for _ in range (m))
a = l[0][0]
b = l[0][1]
for s,t in l:
  a = max(a,s)
  b = min(b,t)
print(max(b-a+1,0))

Submission status: Accepted

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

for n 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:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
LR = np.array([lr() for _ in range(M)])
L = LR[:, 0]; R = LR[:, 1]
answer = R.min() - L.max() + 1
if answer < 0:
    answer = 0
print(answer)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
L = 0; R = 10 ** 6
for _ in range(M):
    l, r = lr()
    if l > L:
        L = l
    if r < R:
        R = r

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lScope = []
rScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    lScope.append(l)
    rScope.append(r)
# k in keyScope[i] (i= 0~m) を確かめたい
if min(rScope) >= max(lScope):
    print(min(rScope) - max(lScope) + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,m = iim()
lmax = 0
rmin = n
for i in range(m):
    l,r = iim()
    lmax = max(l,lmax)
    rmin = min(r,rmin)

print(max(rmin-lmax+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)
left = max(l)
right = min(r)

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

Submission status: Accepted

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

mn = 0
mx = n

for i in range(m):
    tmp = [int(i) for i in input().split()]
    mn = max(tmp[0], mn)
    mx = min(tmp[1], mx)

print(mx - mn + 1 if mx >= mn else 0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = []
lmax = 0
rmin = 10**6
for i in range(m):
    a.append(list(map(int,input().split())))
    if a[i][0] > lmax:
        lmax = a[i][0]
    if a[i][1] < rmin:
        rmin = a[i][1]
if rmin-lmax < 0 :
    print(0)
else :   
    print(1+rmin-lmax)

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n,m=MI()
ls=[]
rs=[]
for i in range(m):
    line=input().rstrip().split()
    ls.append(int(line[0]))
    rs.append(int(line[1]))
mx=0
mn=10**6
for l in ls:
    if l>mx:
        mx=l
#print(mx)
for r in rs:
    if r<mn:
        mn=r
#print(mn)
ans=max(mn-mx+1,0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for k in range(m):
  i,r = map(int,input().split())
  
  i_now.append(i)
  r_now.append(r)

i_max = max(i_now)
r_min = min(r_now)

ans = r_min - i_max + 1

if ans <= 0:
  ans = 0
  
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n,m = load_list()
lr = []

lmax = 0
rmin = 10**6
for _ in range(m):
    l, r = load_list()
    lmax = max(lmax, l)
    rmin = min(rmin, r)

print(len(range(lmax, rmin+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())
  L=max(l,L)
  R=min(r,R)

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    N, M = map(int, input().split())
    L = [None] * M
    R = [None] * M
    for i in range(M):
        L[i], R[i] = map(int, input().split())

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# python 3.4.3
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# library
# -------------------------------------------------------------



# -------------------------------------------------------------
# main
# -------------------------------------------------------------

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

maxL,minR = 0,10**5+1
for l,r in LR:
    maxL = max(maxL,l)
    minR = min(minR,r)

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

Submission status: Accepted

Solution code:
def main():
    n,m = tuple([int(t)for t in input().split()])

    a = [tuple([int(t)for t in input().split()])for _ in [0]*m]

    t = max(a,key=lambda x:x[0])[0]
    s = min(a,key=lambda x:x[1])[1]
    ans = s-t+1
    if ans<0:
        print(0)
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

LR = []
for _ in range(M):
  l, r = map(int, input().split())
  LR.append((l, r))

L_max = 1
R_min = N

for lr in LR:
  if (L_max < lr[0]):
    L_max = lr[0]

  if (R_min > lr[1]):
    R_min = lr[1]

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

Submission status: Accepted

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

def main():
    N, M = input_nums()
    L = 1
    R = N
    for i in range(M):
        l, r = input_nums()
        L = max(L, l)
        R = min(R, r)
        if L > R:
            print(0)
            exit(0)
    print(R-L+1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N, M = input_nums()
    L = 1
    R = N
    for i in range(M):
        l, r = input_nums()
        L = max(L, l)
        R = min(R, r)
        if L > R:
            print(0)
            exit(0)
    print(R-L+1)







if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
#import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, m = map(int, readline().split())
  left = 0
  right = 10**106
  for _ in range(m):
    l, r = map(int, readline().split())
    left = max(l, left)
    right = min(r, right)
  print(max(right-left+1, 0))
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

mini = max(LR[:,0])
maxi = min(LR[:,1])

if maxi-mini < 0:
    print(0)
else:
    print(maxi-mini+1)

Submission status: Accepted

Solution code:
#C
n, m = map(int, input().split())
l = []
r = []
for i in range(m):
    lin = list(map(int, input().split()))
    l.append(lin[0])
    r.append(lin[1])
lma = max(l)
rmi = min(r)
if lma > rmi:
    print(0)
else:
    print(rmi - lma + 1)

Submission status: Accepted

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

l = 0
r = 10 ** 5 + 1
for i in range(M):
    l = max(l, L[i])
    r = min(r, R[i])

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

Submission status: Accepted

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

maxi = 10**6
mini = 0
for lr in LR:
    mini = max(lr[0],mini)
    maxi = min(lr[1],maxi)
if maxi-mini < 0:
    print(0)
else:
    print(maxi-mini+1)

Submission status: Accepted

Solution code:
# input = sys.stdin.readline

def resolve():
    N,M=map(int,input().split())
    l, r = map(int, input().split())
    for i in range(1,M):
        ll,rr=map(int,input().split())
        l=max(l,ll)
        r=min(r,rr)

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


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

l_max = 0
r_min = 10**9

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

ans = 0
for i in range(l_max, r_min+1):
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left, right = map(int, input().split())
for _ in range(M-1):
    c_left, c_right = map(int, input().split())
    if c_left > left:
        left = c_left
    if c_right < right:
        right = c_right

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

Submission status: Accepted

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

s=[0]*n
left=[]
right=[]
for i in range(m):
    l,r=map(int,input().split())
    left.append(l)
    right.append(r)
    
if min(right)<max(left):
    print(0)
else:
    print(min(right)-max(left)+1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
array = [list(map(int, input().split())) for i in range(m)]
result = [1, n]
for el in array:
    if el[0] > result[1] or result[0] > el[1]:
        result = [1, 0]
        break
    else:
        work = sorted(result + el)
        result = [work[1], work[2]]
print(result[1]-result[0]+1)

Submission status: Accepted

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

LM = 0
RM = N+1

for i in range(M):
  L, R = map(int, input().split())
  if L > LM:
    LM = L
  
  if R < RM:
    RM = R
if LM <= RM:
  print(N-(LM-1)-(N-RM))
else:
  print(0)

Submission status: Accepted

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

for m in range(M):
  L2,R2 = map(int,input().split())
  L1 = max(L1,L2)
  R1 = min(R1,R2)
  
print(max(0,R1-L1+1))

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
Lmin = 0
Rmax = n
for i in range(m):
    L,R = map(int, input().split())
    Lmin = max(L-1, Lmin)
    Rmax = min(R, Rmax)
if Lmin < Rmax:
    ans = Rmax - Lmin
else:
    ans = 0
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 = 1
R = N
for l,r in LR:
  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())
a=[]
b=[]
for i in range(m):
  f,g=map(int,input().split())
  a.append(f)
  b.append(g)

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

Submission status: Accepted

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

for i in range(m-1):
  a, b = map(int,input().split())
  if a > l:
    l = a
  if b < r:
    r = b

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

Submission status: Accepted

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

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

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

ans = min_r - max_l + 1

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

Submission status: Accepted

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


#card_list = np.zeros(n)

under = 1
top = n

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

#print(under, top)
if top-under+1 < 0:
    print(0)
else:
    print(top-under+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    l = -1
    r = 10**20
    for _ 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)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = [0, 10**6]

for i in range(M):
    ans[0] = max(ans[0], L[i][0])
    ans[1] = min(ans[1], L[i][1])

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for _ in range(m)]
for i in range(m-1):
  if lr[i][0] > lr[i+1][0]:
    lr[i+1][0] = lr[i][0]
  if lr[i][1] < lr[i+1][1]:
    lr[i+1][1] = lr[i][1]
ans = lr[-1][1] - lr[-1][0] + 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):
  l,r=[int(i) for i in input().split()]
  L.append(l)
  R.append(r)
Lmax=max(L)
Rmin=min(R)
if Rmin-Lmax>=0:
  print(Rmin-Lmax+1)
else:
  print(0)

Submission status: Accepted

Solution code:
def mi():
    return map(int, input().split())
'''
3 4
*G*S
G**S
*G*S
'''
n,m = mi()
l = -1
r = 1e10
for i in range(m):
    a,b=mi()
    if l>b or b<a:
        print(0)
        exit()
    l = max(l,a)
    r = min(r,b)
if r<l:
    print(0)
else:
    print(r-l+1)

Submission status: Accepted

Solution code:
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, m = I()
a = [0] * (n + 10)
for i in range(m):
    x, y = I()
    a[x] += 1
    a[y + 1] -= 1
ans = 0
for i in range(1, n + 10):
    a[i] = a[i] + a[i - 1]
    if a[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=1;ma=n
te=set(range(1,n+1))
for i in range(m):
    x,y=map(int,input().split())
    l=max(x,l)
    ma=min(ma,y)
print(abs(l-ma)+1 if l<=ma else 0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lmax=0
rmin=10**20
for i in range(m):
    l,r=map(int,input().split())
    lmax=max(lmax,l)
    rmin=min(rmin,r)
if lmax>rmin:
    print(0)
else:
    print(rmin-lmax+1)

Submission status: Accepted

Solution code:
x=list(map(int, input().split()))
a=1
b=x[0]
for i in range(x[1]):
	c,d=list(map(int, input().split()))
	if a<c:
		a=c
	if b>d:
		b=d

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

Submission status: Accepted

Solution code:
n, m=map(int, input().split())
lst=[None]*m
for i in range(m):
    lst[i]=tuple(map(int, input().split()))

left=max([el[0] for el in lst])
right=min([el[1] for el in lst])
if left<=right:
    print(right-left+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)

l = max(L_list)
r = min(R_list)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
li = [list(map(int,input().split())) for _ in range(m)]
maxl = max(li, key=lambda x:x[0])[0]
minr = min(li, key=lambda x:x[1])[1]
if maxl <= minr:
    print(minr-maxl+1)
else:
    print(0)

Submission status: Accepted

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

s = [0] * (N + 1)
for i in range(M):
    l, r = lr[i]
    s[l - 1] += 1
    if r != N + 2:
        s[r] -= 1

ruiseki = [0] * (N + 2)
for i in range(N + 1):
    ruiseki[i + 1] += ruiseki[i] + s[i]

#max_ = max(ruiseki)
print(ruiseki.count(M))

Submission status: Accepted

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

l,r=1,N

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

Submission status: Accepted

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

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())
s = [0 for _ in range(n+1)]
for i in range(m):
    s[l[i]-1] += 1
    s[r[i]] -= 1
h = [0]
for i in range(n):
    h.append(s[i]+h[i])
ans = 0
for i in range(n+1):
    if(h[i] == m):
        ans += 1
print(ans)

Submission status: Accepted

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

left = [i[0] for i in l]
right = [i[1] for i in l]

max_left = max(left)
min_right = min(right)

ans = min_right - max_left

if ans < 0:
    print(0)

else:
    print(ans+1)

Submission status: Accepted

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

lmax = -1
rmin = n * 100

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

print(max(rmin - lmax + 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)
L1 = max(l)
R1 = min(r)
if R1 >= L1:
    print(R1 - L1 + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    l = 0
    r = 10 ** 9
    for _ in range(M):
        a, b = list(map(int, input().split()))
        l = max(l, a)
        r = min(r, b)
    
    if l<=r:
        return r-l+1
    else:
        return 0
        
print(solve())

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    
    c = 0
    l = 0
    r = 10 ** 9
    for _ in range(M):
        a, b = list(map(int, input().split()))
        l = max(l, a)
        r = min(r, b)
        
    if r < l:
        return 0
    if r == l:
        if r <= N:
            return 1
        else:
            return 0
    
    if N < l:
        return 0
    elif N == l:
        return 1
    elif l < N < r:
        return N-l+1
    elif N == r:
        return r-l+1
    elif r < N:
        return r-l+1

print(solve())