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:
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
ans = 0
maxL = 0
minR = INF 
[n,m] = inpl()
for i in range(m):
    [L,R] = inpl()
    maxL = max(maxL,L)
    minR = min(minR,R)

ans = minR - maxL +1





print(max(ans,0))

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())

Min = max(l)
Max = min(r)
for i in range(n):
    if Min<=i+1<=Max:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max, r_min = 0, 100000000

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

print(max(0, r_min-l_max+1))

Submission status: Accepted

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

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)

if min(LR[:,1])-max(LR[:,0])+1>0:
  print(min(LR[:,1])-max(LR[:,0])+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=(map(int,input().split()))
x = []
y = []
for i in range(m):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
ans=min(y)-max(x)+1
print(ans if min(y)-max(x)>=0 else 0)

Submission status: Accepted

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

Rmin = np.min(R)
Lmax = np.max(L)
if Rmin >= Lmax:
    num = np.min(R) - np.max(L) + 1
else:
    num = 0
print(num)

Submission status: Accepted

Solution code:

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

MIN=0
MAX=10**9
 
for a,b in G:
    MIN=max(MIN,a)
    MAX=min(MAX,b)
 
print(max(0,MAX-MIN+1))

Submission status: Accepted

Solution code:
from itertools import accumulate

n, m = map(int, input().split())
card = [0] * (n + 1)

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

ans = 0
for i in accumulate(card):
    if i == m:
        ans += 1
print(ans)

Submission status: Accepted

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

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    ans = 0
    min_r = N
    max_l = 0

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

    print(max(min_r - max_l + 1, 0))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

L_max = 0
R_min = N
for i in range(M):
    L, R = map(int, input().split(" "))

    if L_max < L:
        L_max = L
    if R_min > R:
        R_min = R

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())
lr = [[int(i) for i in input().split(" ")] for i in range(m)]
a = max(lr, key=lambda x: x[0])[0]
b = min(lr, key=lambda x: x[1])[1]
if a <= b:
    print(b-a+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = []
for i in range(m):
    l.append(list(map(int,input().split())))
ans = 0
lmax = 0
rmin = float('inf')
for i in l:
    if i[0]>lmax:
        lmax = i[0]
    if i[1]<rmin:
        rmin = i[1]
if lmax <= rmin:
    ans = rmin - lmax + 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

min = 0
max = n

for i in range(m):
    if (min < lr[i][0]):
        min = lr[i][0]

    if (lr[i][1] < max):
        max = lr[i][1]

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

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
L = [0] * (N + 1)
for m in range(M):
  l, r = [int(x) for x in input().split()]
  L[l-1] += 1
  L[r] -= 1
  
for i, l in enumerate(L[1:], start=1):
  L[i] = L[i-1] + l
 
print(L.count(M))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    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())
    print(max(0,min(R)-max(L)+1))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

sup = []
inf = []
  
for i in range(len(L)):
  sup.append(L[i][1])
  inf.append(L[i][0])
  
sup = min(sup)
inf = max(inf)

if sup - inf < 0:
  print(0)
else:
  print(sup - inf + 1)

Submission status: Accepted

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

c_min = -1
c_max = n+1

for i in range(m):
    tmp_min, tmp_max = map(int, input().split())
    if tmp_min > c_min:
        c_min = tmp_min
    if tmp_max < c_max:
        c_max = tmp_max

if c_max >= c_min:
    print(c_max - c_min +1)
else:
    print(0)

Submission status: Accepted

Solution code:
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

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

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    print(min(r) - max(l) + 1 if min(r) >= max(l) else 0)



if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

Submission status: Accepted

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

a=0
b= float('inf')
for i in range(m):
      c,d=map(int,input().split())
      a=max(a,c)
      b=min(b,d)

print(max(0,b-a+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
key = []
for i in range(1,n+1):
    key.append(i)
    
ls = [0,100000]
for i in range(m):
    L,R = map(int,input().split())
    if L > ls[0]:
        ls[0] = L
    if R < ls[1]:
        ls[1] = R
    
ans = []    
for i in range(ls[0],ls[1]+1):
    ans.append(i)
    
print(len(set(ans)&set(key)))

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
f = 0
for i in range(M):
    if i==0:
        L1, R1 = list(map(int, input().split()))
    else:
        L2, R2 = list(map(int, input().split()))
        if R1 < L2 or L1 > R2:
            print(0)
            f+=1
            break
        elif L2<L1 and R1<R2:
            continue
        elif L1<L2 and R2<R1:
            L1 = L2
            R1 = R2
        elif L2<L1 and R2<R1:
            R1 = R2
        elif L1<L2 and R1<R2:
            L1 = L2
        else:
            continue
s = set(range(L1,R1+1))
if f!=1:
    print(len(s))

Submission status: Accepted

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

min=1
max=n


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

Submission status: Accepted

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

Submission status: Accepted

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

lmin = 1
rmax = n
for _ in range(m):
    l,r = map(int,input().split())
    if l > lmin:
        lmin = l
    if rmax > r:
        rmax = r
    
print(max(rmax - lmin + 1, 0))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LR = sorted([list(map(int, input().split())) for i in range(M)], key=lambda x: (x[0], -x[1]))

minv = 0
maxv = N
for l, r in LR:
    if maxv < l:
        print(0)
        exit()
    minv = max(minv, l)
    maxv = min(maxv, r)

print(maxv-minv+1)

Submission status: Accepted

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

p_c()

Submission status: Accepted

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

min_id = 1
max_id = float('INF')
na_flag = False
for m in range(M):
    left = LR[m][0]
    right = LR[m][1]
    if right < min_id or max_id < left:
        na_flag = True
        break

    if min_id < left and left <= max_id:
        min_id = left
    if right < max_id and min_id <= right:
        max_id = right

if na_flag:
    print(0)
else:
    print(max_id - min_id + 1)

Submission status: Accepted

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

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

max_L = max(L)
min_R = min(R)
import numpy as np
L = np.array(L)
R = np.array(R)

print(min_R-max_L+1) if min_R-max_L+1>=1 else print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
mat = [list(map(int,input().split())) for i in range(M)]
L = 0
R = 10**5+1
for i in range(M):
    if L < mat[i][0]:
        L = mat[i][0]
    if R > mat[i][1]:
        R = mat[i][1]
print(max([R-L+1,0]))

Submission status: Accepted

Solution code:
tmp = list(map(int, input().split()))
n = tmp[0]
m = tmp[1]

maxl = 1
minr = n
for i in range(m):
  tmp = list(map(int, input().split()))
  l = tmp[0]
  r = tmp[1]
  if l > maxl:
    maxl = l
  if r < minr:
    minr = r

if maxl <= minr:
  print(minr - maxl + 1)
else:
  print('0')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    L=-INF; R=INF
    for _ in range(m):
        l,r=map(int,input().split())
        L=max(L,l)
        R=min(R,r)
    print(max(R-L+1,0))
resolve()

Submission status: Accepted

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

for i in range(1, N + 1):
    K[i] += K[i - 1]
print([i == M for i in K].count(True))

Submission status: Accepted

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

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

Submission status: Accepted

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

N, M = map(int, input().split())
L = [0] * M
R = [0] * M
for i in range(M):
    L[i], R[i] = map(int, input().split())
L_min = L[0]
R_max = R[0]
for i in range(1, M):
    if L_min < L[i]:
        L_min = L[i]
    if R[i] < R_max:
        R_max = R[i]
result = R_max-L_min+1
if result <= 0:
    print(0)
else:
    print(R_max-L_min+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.buffer.readline()[:-1].decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.buffer.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7


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





l, r = 0, 10**6
for i, j in data:
	l = max(l, i)
	r = min(r, j)
print(max(0, r-l+1))

Submission status: Accepted

Solution code:
N,R = map(int,input().split())
min = 0
max = 10000000
ans = 0
for i in range(R):
  A,B = map(int,input().split())
  if A > min:
    min = A
  if B < max:
    max = B
if min < max:
  ans = max - min + 1
elif min == max:
  ans = 1
print(ans)

Submission status: Accepted

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

for j in range(m):
  num1 = max(num[j][0], num1)
  num2 = min(num[j][1], num2)

ans = num2 - num1 + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


def intersect(lhs, rhs):
    if lhs[1] < rhs[0] or rhs[1] < lhs[0]:
        return [0, 0]
    min_value = lhs[0] if lhs[0] > rhs[0] else rhs[0]
    max_value = lhs[1] if lhs[1] < rhs[1] else rhs[1]
    return [min_value, max_value]


ans = [-1, 2 * 10 ** 5]
for _ in range(m):
    ans = intersect(ans, list(map(int, input().split())))
    if 0 == ans[0] and 0 == ans[1]:
        print(0)
        exit(0)
print(ans[1] - ans[0] + 1)

Submission status: Accepted

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

Submission status: Accepted

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

max_L, min_R = (0, N)

for r in l:
    if r[0]>max_L:
        max_L = r[0]
    if r[1]<min_R:
        min_R = r[1]
if min_R >= max_L:
    print(min_R - max_L + 1)
else:
    print(0)

Submission status: Accepted

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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

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


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

Submission status: Accepted

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

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

Submission status: Accepted

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

_min = 1
_max = n

for d in data:
    if _min < d[0]:
        _min = d[0]
    if _max > d[1]:
        _max = d[1]

if _max >= _min:
    print(_max-_min+1)
else:
    print(0)

Submission status: Accepted

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

min_val_list = []
max_val_list = []
for i in range(M):
    min_id_range, max_id_range = map(int, input().split())
    min_val_list.append(min_id_range)
    max_val_list.append(max_id_range)

min_val_id = max(min_val_list)
max_val_id = min(max_val_list)


if min_val_id > max_val_id:
    print(0)
else:
    print(max_val_id - min_val_id + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)
INF = 1<<32


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    
    lm = max(L)
    rm = min(R)

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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l = [list(map(int, input().split())) for _ in range(M)]
l = np.array(l)
ans = list(range(l[:, 0].max(), l[:, 1].min() + 1))
print(len(ans))

Submission status: Accepted

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

Nlist=[0 for i in range(N+2)]

for i in range(M):
    p=lr[i][0]
    q=lr[i][1]+1
    Nlist[p]+=1
    Nlist[q]-=1

for i in range(1,N+1):
    Nlist[i]+=Nlist[i-1]

num=Nlist.count(M)
if Nlist[N+1]==M:
    num-=1
print(num)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    L = max(L)
    R = min(R)

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

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M, *X = map(int, open(0).read().split())
ans = max(0, min(X[1::2]) - max(X[::2]) + 1)
print(ans)

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)

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

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 = max(L_list)
R_min = min(R_list)

#print(range(L_max,R_min+1))
print(len(range(L_max,R_min+1)))

Submission status: Accepted

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

n, m = map(int, input().split())
left, right = [None]*m, [None]*m

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

ans = min(right)-max(left)+1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

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

print(max(0, r_min - l_max + 1))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
Key=[0]*(N+1)
for i in range(M):
    L,R = map(int,input().split())
    Key[L-1] += 1
    Key[R] -= 1
for i in range(N):
    Key[i+1] += Key[i] # 累積和
print(Key.count(M))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
max_l = float('-inf')
min_r = float('inf')
for _ in range(m):
  l, r = (int(x) for x in input().split())
  max_l = max(max_l, l)
  min_r = min(min_r, r)
print(max(0, min_r - max_l+1))

Submission status: Accepted

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

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

Submission status: Accepted

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

    if l>r:
        print(0)
        exit()
    
    #print("left:{}\tright:{}".format(l,r))

print(r-l+1)

Submission status: Accepted

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

keylist = [0,1000000]

for i in range(m):
	if keylist[0] < list1[i][0]:
		keylist[0] = list1[i][0]
	if keylist[1] > list1[i][1]:
		keylist[1] = list1[i][1]
if keylist[0] > keylist[1]:
	print(0)
else:
	print(keylist[1]-keylist[0]+1)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[0]*M
R=[0]*M
count=0
for i in range (0,M):
    L[i],R[i]=map(int,input().split())
    
L_max=max(L)
R_min=min(R)

for i in range(1,N+1):
    if(L_max<=i and i<=R_min):
        count+=1

print(count)

Submission status: Accepted

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

l=max([i[0] for i in lr])
r=min([i[1] for i in lr])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
low = -float('INF')
high = float('INF')
for i in range(M):
    l, r = map(int, input().split())
    if l > low:
        low = l
    if r < high:
        high = r
print(max(0, high - low + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = [], []
low = -float('INF')
high = float('INF')
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
    if l > low:
        low = l
    if r < high:
        high = r
print(max(0, high - low + 1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
min_c = 0
max_c = N
for i in range(M):
    L, R = map(int, input().split())
    min_c = max(min_c, L)
    max_c = min(max_c, R)

print(max(0, max_c - min_c + 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)

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

Submission status: Accepted

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

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

result = R_min - L_max + 1

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

Submission status: Accepted

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

inf = []
sup = []

for i in range(0,M):
	L,R = map(int,input().split())
	inf.append(L)
	sup.append(R)
	
inf = max(inf)
sup = min(sup)

ran = sup - inf

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

Submission status: Accepted

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

mi = 0
ma = 100001

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

if ma-mi+1 >= 0:
	print(ma-mi+1)
else:
	print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
mi,mx=0,n
for item in l:
  mi1=item[0]
  mx1=item[1]
  if mi1>mi:
    mi=mi1
  if mx1<mx:
    mx=mx1
if mx<mi:
  print(0)
else:
  print(mx-mi+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n , m = map(int,input().split())
a , b = 1 , 100000

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

Submission status: Accepted

Solution code:
from sys import stdin

n, m = [int(i) for i in stdin.readline().rstrip().split()]
max_l = -1
min_r = 100000

for _ in range(m):
  l, r = [int(i) for i in stdin.readline().rstrip().split()]
  if l > max_l:
    max_l = l
  if r < min_r:
    min_r = r
    
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())
li_l = []
li_r = []
for i in range(M):
    L, R = map(int, input().split())
    li_l.append(L)
    li_r.append(R)
largest_L = max(li_l)
smallest_R = min(li_r)
cards = smallest_R - largest_L + 1
if cards > 0 :
    print(cards)
else:
    print(0)

Submission status: Accepted

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

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

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

print(card.count(m))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,M = map(int,readline().split())
LR = np.array(read().split(),np.int32)

L = LR[::2]; R = LR[1::2]

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

Submission status: Accepted

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

L_R_list = []
N,M = map(int,input().split())
for i in range(M):
    L_R_list.append(list(map(int,input().split())))

min = 1
max = N

for i in range(M):
    if L_R_list[i][0] >= min:
        min = L_R_list[i][0]
    if L_R_list[i][1] <= max:
        max = L_R_list[i][1]

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

Submission status: Accepted

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

l_max = max(l)
r_min = min(r)

for k in range(n+1):
    if l_max <= k <= r_min:
        cnt += 1
print(cnt)

Submission status: Accepted

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

l = 0
r = N+1
for i in range(M):
    l = max(l, L[i][0])
    r = min(r, L[i][1])
    
if r-l >= 0:
    print(r-l+1)
else:
    print(0)