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 = [0]*m
r = [0]*m
lmax = 1
rmin = n

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

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

Submission status: Accepted

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

l = []
for i in range(M):
    l.append(lr[i][0])

r = []
for i in range(M):
    r.append(lr[i][1])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(): 


    card_cnt, gate_cnt = map(int, input().split())

    min_v, max_v = map(int, input().split())
    for _ in range(1, gate_cnt):
        L,R = map(int, input().split())
        min_v = max(min_v, L)
        max_v = min(max_v, R)
        if min_v > max_v:
            print(0)
            break
    else:
        print(len(range(min_v, max_v+1)))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,M = (int(i) for i in input().split())
min = 1
max = N

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

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

Submission status: Accepted

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

maxl = 0
minr = 10 ** 6
for i in range(m):
    l, r = map(int, input().split())
    maxl = max(maxl,l)
    minr = min(minr,r)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
lst=[list(map(int, input().split())) for _ in range(M)]
maxL, minR=-1, N
for L, R in lst:
  L, R=L-1, R-1
  maxL=max(maxL, L)
  minR=min(minR, R)
if minR>=maxL:
  print(minR-maxL+1)
else:
  print(0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ans_l = None
ans_r = None
for i in range(m):
    
    l,r = map(int,input().split())

    
    if i == 0:
        ans_l = l
        ans_r = r

    else:
        ans_r = min(r,ans_r)
        ans_l = max(l,ans_l)
        
    if ans_r - ans_l < 0:
        print(0)
        exit()


print(ans_r-ans_l+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

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

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

Submission status: Accepted

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

print(0 if min(R)<max(L) else min(R)-max(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)
L = sorted(L)
R = sorted(R)
Lmax = L[-1]
Rmin = R[0]
if Rmin<Lmax:
    print(0)
else:
    print(Rmin-Lmax+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)
print(max(0,b-a+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)

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

Submission status: Accepted

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

Submission status: Accepted

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

print(max(0,min(rs) - max(ls) + 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C - Prison(ABC127)
N,M=map(int, input().split())
l,r=map(int, input().split())
for i in range(M-1):
    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:
N,M = map(int,input().split())
Max_l = 0
Min_r = float('inf')

for i in range(M):
    l,r = map(int,input().split())
    Max_l = max(Max_l,l)
    Min_r = min(Min_r,r)
ans = 0
if Max_l<=Min_r:
    ans = Min_r-Max_l+1
    

   
print(ans)

Submission status: Accepted

Solution code:
# -*-coding: utf-8 -*-
N, M = map(int, input().split())
lr = []
for i in range(M):
    lr.append(list(map(int, input().split())))
l = lr[0][0]
r = lr[0][1]
for i in range(1, M):
    if lr[i][0] > l:
        l = lr[i][0]
    if lr[i][1] < r:
        r = lr[i][1]
    
if r >= l:
    print(r - l + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    x = [0] * (n + 2)
    for i in range(m):
        l, r = list(map(int, input().rstrip('\n').split()))
        x[l] += 1
        x[r+1] -= 1
    t = 0
    cnt = 0
    for i in range(n + 2):
        t += x[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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


for i in range(M):
    l,r=map(int,input().split())
    cards_l=max(cards_l,l)
    cards_r=min(cards_r,r)
    #print(r,cards_r)
print(max(0,cards_r-cards_l+1))

Submission status: Accepted

Solution code:
while True:
    try:
        n, m = map(int, input().split())
        start = 1
        end = n
        for i in range(m):
            a, b = map(int, input().split())
            if a > end or b < start:
                print(0)
                for j in range(m-i-1):
                    input()
                break
            start = max(start, a)
            end = min(end, b)   
        else:
            print(end-start+1)
    except:
        break

Submission status: Accepted

Solution code:
import itertools

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

imos = [0] * (N + 2)
for lr in LR:
    l, r = lr[0], lr[1]
    imos[l] += 1
    imos[r + 1] -= 1
acc = itertools.accumulate(imos)

res = 0
for a in acc:
    if a == M:
        res += 1

print(res)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
maxL = 1
minR = N
for i in range(M):
    L, R = map(int, input().split())
    if maxL < L:
        maxL = L
    if minR > R:
        minR = R

if minR - maxL >= 0:
    print(minR - maxL + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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

leftest = 1
rightest = N
for gate in gates:
    L = gate[0]
    R = gate[1]
    if L > leftest:
        if L > rightest:
            print(0)
            break
        leftest = L
    if R < rightest:
        if R < leftest:
            print(0)
            break
        rightest = R
else:
    print(rightest - leftest + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(e-s+1 if s <= e else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

min_temp = L[0]
max_temp = R[0]
ans = R[0] - L[0] + 1

for i in range(1, M):
    
    if min_temp < L[i]:
        ans -= (L[i] - min_temp)
        min_temp = L[i]
    
    if R[i] < max_temp:
        ans -= (max_temp - R[i])
        max_temp = R[i]
        
if ans < 0:
    ans = 0
    
print(ans)

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split()]
arr = [0]*(n+2)
for I in range(m):
    l,r=[int(i) for i in input().split()]
    arr[l]+=1
    arr[r+1]-=1
for i in range(1,n+2):
    arr[i]+=arr[i-1]
print(arr.count(m))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+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())

n,m = inpl()
cnt = [0]*(n+1)
for i in range(m):
    l,r = inpl()
    cnt[l-1] += 1
    cnt[r] -= 1
cntsum = list(itertools.accumulate(cnt))
print(cntsum.count(m))

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(M):
  a,b = map(int,input().split())
  if(i == 0): a_Max,b_Min = a,b
  else: 
    a_Max = max(a_Max,a)
    b_Min = min(b_Min,b)
    
ans = b_Min - a_Max + 1 
if(ans <= 0): ans = 0 
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
G = [0] * (N + 1)
for L, R in X:
    G[L - 1] += 1
    G[R] -= 1
for i in range(N):
    G[i + 1] += G[i]
print(sum([1 if g == M else 0 for g in G]))

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)

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
low = 0
high = n+1

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

print(high-low+1 if low <= high else 0)

Submission status: Accepted

Solution code:
a,b= map(int,input().split())
x, y= zip(*[list(map(int,input().split())) for i in range(b)])
f=  min(y) - max(x) 
if f< 0 :
  print(0)
else :
  print(min(y) - max(x) +1)

Submission status: Accepted

Solution code:
a,b= map(int,input().split())
x, y= zip(*[list(map(int,input().split())) for i in range(b)])
f=  min(y) - max(x) 
if f< 0 :
  print(0)
else :
  print(min(y) - max(x) +1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
max_value = 10 ** 5
min_value = 1
for i in range(m):
    l, r = map(int, input().split())
    min_value = max(min_value, l)
    max_value = min(max_value, r)

ans = max_value - min_value + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lr = [tuple(map(int, input().split())) for _ in range(m)]
maxl = max(_[0] for _ in lr)
minr = min(_[1] for _ in lr)
if minr - maxl >= 0:
    answer = minr - maxl + 1
else:
    answer = 0
print(answer)

Submission status: Accepted

Solution code:
while True:
    try:
        N, M = map(int, input().split())
        
        dp = [0]*(10**5+5)
        for i in range(M):
            L, R = map(int, input().split())
            dp[L-1] += 1
            dp[R] -= 1
        
        cnt = 0
        ans = 0
        for i in range(N):
            cnt += dp[i]
            if cnt == M:
                ans += 1
            
        print(ans)
    except:
        break

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
li = [0]*(10**5+5)
for _ in range(m):
	l,r=map(int,input().split())
	li[l-1]+=1
	li[r]-=1
cnt=0
ans=0
for i in range(n):
	cnt+=li[i]
	if cnt==m:
		ans+=1
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
data=[list(map(int,input().split())) for i in range(m)]
import sys
input = lambda: sys.stdin.readline()[:-1]
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:
# C - Prison
N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
new_L = []
new_R = []
for i in range(M):
    new_L.append(L[i][0])
    new_R.append(L[i][1])
if min(new_R) - max(new_L) >= 0:
    ans = min(new_R) - max(new_L) + 1
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    lr = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    a = [0] * (n + 2)
    for l, r in lr:
        a[l] += 1
        a[r + 1] -= 1
    t = 0
    cnt = 0
    for i in range(1, n + 1):
        t += a[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
max_L = 1
min_R = 100000
for _ in range(M):
	L, R = map(int, input().split())
	if max_L < L:
		max_L = L
	if R < min_R:
		min_R = R
if min_R - max_L + 1 < 0:
	print(0)
else:
	print(min_R - max_L + 1)

Submission status: Accepted

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

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

if y-x >= 0:
    print(y-x+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
L1 = []
R1 = []
for i in range(M):
    L1.append(LR[i][0])
    R1.append(LR[i][1])
if min(R1)>=max(L1):
    print(min(R1)-max(L1)+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)

ans_min,ans_max=L[0],R[0]

def compare(min_,max_,min_i,max_i):
  if max_<min_i:
    ret_min,ret_max = -1,-1
  elif min_>max_i:
    ret_min,ret_max = -1,-1
  elif (min_<=min_i) and (min_i<=max_) and (max_<=max_i):
    ret_min,ret_max = min_i,max_
  elif (min_>=min_i) and (max_<=max_i):
    ret_min,ret_max = min_,max_
  elif (min_>=min_i) and (min_<=max_i) and (max_>=max_i):
    ret_min,ret_max = min_,max_i
  elif (min_<=min_i) and (max_i<=max_):
    ret_min,ret_max = min_i,max_i
  else:
    pass
#    print("else!")
  return ret_min,ret_max

for i in range(1,M):
  ans_min,ans_max = compare(ans_min,ans_max,L[i],R[i])
  if ans_min==-1 and ans_max==-1:
    ans_min=N+1
    break
#print("End",ans_min,ans_max)
if ans_min<=N:
  if ans_max<=N:
    print(ans_max-ans_min+1)
  else:
    print(N-ans_min+1)
else:
  print(0)

Submission status: Accepted

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

for m in range(M-1):
  _first, _last = map(int,input().split(" "))
  min_set_cnt = max(min_set_cnt,_first)
  max_set_cnt = min(max_set_cnt,_last)


ans = max(max_set_cnt - min_set_cnt + 1, 0)

print(ans)

Submission status: Accepted

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

main()

Submission status: Accepted

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

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

Submission status: Accepted

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

def ans():
    ans = lr[0]
    for i in range(1, m):
        if ans[0] > lr[i][-1] or ans[-1] < lr[i][0]:
            print(0)
            return
        ans = [max(ans[0], lr[i][0]), min(ans[-1], lr[i][-1])]

    print(ans[-1]- ans[0] + 1)
    return

ans()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

overlap_l = 1
overlap_r = n
for i in range(m):
    l, r = list(map(int,input().split()))
    if overlap_r < l or r < overlap_l:
        print(0)
        sys.exit()
    else:
        overlap_l = max(l, overlap_l)
        overlap_r = min(r, overlap_r)

print(overlap_r - overlap_l + 1)

Submission status: Accepted

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

gl.sort()
s = 1
e = n
for g in gl:
	if s < g[0]:
		s = g[0]
	if e > g[1]:
		e = g[1]
	#print("{}->{}".format(s, e))

print(max(e - s + 1, 0))

Submission status: Accepted

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

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

L0,R0 = 0,10**6

for i in IDS:
    L0 = max(L0,i[0])
    R0 = min(R0,i[1])

print(max(0,R0-L0+1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
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())
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i] > RR or LL > R[i]:
        print(0)
        sys.exit()
    else:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
print(RR - LL + 1)

Submission status: Accepted

Solution code:
import sys
N,M = map(int,input().split())
L = []
R = []
for i in range(M):
    a =list(map(int,input().split()))
    L.append(a[0])
    R.append(a[1])
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i] > RR or LL > R[i]:
        print(0)
        sys.exit()
    else:
        LL = max(LL, L[i])
        RR = min(RR, R[i])
print(RR - LL + 1)

Submission status: Accepted

Solution code:
import sys
N,M = map(int,input().split())
L = []
R = []
for i in range(M):
    a =list(map(int,input().split()))
    L.append(a[0])
    R.append(a[1])
LL = L[0]
RR = R[0]
for i in range(1,M):
    if L[i]>RR or LL>R[i]:
        print(0)
        sys.exit()
    elif LL>=L[i] and RR>=R[i]:
        RR=R[i]
    elif LL>=L[i] and RR<R[i]:
        continue
    elif L[i]>=LL and R[i]>=RR:
        LL = L[i]
    elif L[i]>=LL and R[i]<RR:
        continue
print(RR-LL+1)

Submission status: Accepted

Solution code:
import heapq
n, m = map(int, input().split())
lr = []
heap1 = []
heap2 = []
for i in range(m):
    lr.append(list(map(int, input().split())))
for j in range(m):
    heapq.heappush(heap1, -lr[j][0])
    heapq.heappush(heap2, lr[j][1])
d = heapq.heappop(heap2) + heapq.heappop(heap1) +1 
if d < 0:
    print(0)
else:
    print(d)

Submission status: Accepted

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    print(max(0, min(R)-max(L)+1))
    return


# Generated by 1.1.5 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=map(int,input().split())
l_list,r_list=[],[]
for _ in range(m):
  l,r=map(int,input().split())
  l_list.append(l)
  r_list.append(r)
ans=max(0,min(r_list)-max(l_list)+1)
print(ans)

Submission status: Accepted

Solution code:
def intersect(a, b, c, d):
    if b < c or d < a:
        return -2, -1
    else:
        li = sorted([a, b, c, d])
        return li[1], li[2]
        
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(tuple(map(int, input().split())))
    
a, b = LR[0]
for lr in LR:
    a, b = intersect(a, b, *lr)
    
ans = b-a+1
print(ans) if a > 0 else print('0')

Submission status: Accepted

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

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)
l_max = max(ls)
r_min = min(rs)
print(max(r_min - l_max + 1, 0))

Submission status: Accepted

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

gate = [list(map(int, input().split())) for _ in range(M)]
pL = max(gate[0][0], 0)
pR = min(gate[0][1], N)
lst = range(pL, pR+1)

for i in range(1, M):
    L = max(gate[i][0], 0)
    R = min(gate[i][1], N)
    lst = range(max(L, pL), min(R, pR)+1)
    pL = max(L, pL)
    pR = min(R, pR)

print(len(list(lst)))

Submission status: Accepted

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

L, R = [], []
for _ in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

max_l = max(L)
min_r = min(R)

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())
src = [tuple(map(int,input().split())) for i in range(m)]

l_max,r_min = 0,10**5
for l,r in src:
    l_max = max(l_max,l)
    
for l,r in src:    
    r_min = min(r_min,r)
else:
    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())
src = [tuple(map(int,input().split())) for i in range(m)]

l_max,r_min = 0,10**5
for l,r in src:
    l_max = max(l_max,l)
    r_min = min(r_min,r)
else:
    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):
  a,b=map(int,input().split())
  l.append(a)
  r.append(b)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
D=[list(map(int,input().split())) for i in range(m)]
maxI=n
minI=1
for i in range(m):
  maxI=min(maxI,D[i][1])
  minI=max(minI,D[i][0])
print(max(maxI-minI+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()
R.sort()

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

Submission status: Accepted

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

min_i = 0
max_i = 100000

for m in range(M):
    min_i = max(min_i, LR[m][0])
    max_i = min(max_i, LR[m][1])

print(max_i - min_i + 1 if (max_i >= min_i) else 0)

Submission status: Accepted

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

Submission status: Accepted

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

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)

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

L=1
R=N

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC127 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m = LI()
L,R = 1,n
for _ in range(m):
    l,r = LI()
    if L < l:
        L = l
    if r < R:
        R = r
if R - L < 0:
    print(0)
else:
    print(R-L+1)

Submission status: Accepted

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

left = 0
right = N
for i in range(M):
    L, R = map(int, input().split())
    left = max(left, L)
    right = min(right, R)

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

Submission status: Accepted

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

Submission status: Accepted

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