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())
LR = [list(map(int, input().split())) for _ in range(m)]
f_inf = float("inf")

max_l = 0
min_r = f_inf
for l, r in LR:
    max_l = max(max_l, l)
    min_r = min(min_r, r)

if min_r >= max_l:
    print(min_r - max_l + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
# coding: utf-8
n, m = map(int,input().split())
r = float('inf')
l = 0
L = []
for i in range(m):
    l_, r_ = map(int,input().split())
    L.append([l_,r_])
    l = max(l_, l)
    r = min(r_, r)
ans = 0
print(max(0, r-l+1))

Submission status: Accepted

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

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

for i in range(n):
    s[i + 1] += s[i]
print(s.count(m))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n, m = map(int, input().split())
ans = [0]*n

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

c = list(np.cumsum(ans))
print(c.count(m))

Submission status: Accepted

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

INF = float('inf')
ans_l, ans_r = -INF, INF
for l, r in lr:
    ans_l = max(ans_l, l)
    ans_r = min(ans_r, r)
print(max(0, ans_r-ans_l+1))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = [0 for _ in range(N)]
for i in range(M):
  l,r = map(int,input().split())
  l-=1;r-=1 #0index
  L[l] += 1
  if r+1 < N:
    L[r+1] -= 1
S = [0]
for i in range(N):
  temp = L[i] + S[-1]
  S.append(temp)
#print(S)
ans = 0
for i in range(1,N+1):
  if S[i] == M:
    ans += 1
print(ans)

Submission status: Accepted

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

if y-x<0:
    print('0')
else:
    print(y-x+1)

Submission status: Accepted

Solution code:
# 先頭の最大と最後尾の最小を求める
card_num, gate_num = map(int, input().split())
lmax, rmin = map(int, input().split()) # 最初の入力がlの最大とrの最小
for i in range(1, gate_num):
    l, r = map(int, input().split())
    lmax = max(lmax, l) # lmaxにlmaxとlの大きい方
    rmin = min(rmin, r) # rminにrminとrの小さい方
print(max(0, rmin-lmax+1)) # 0かrmin-lmax+1の大きい方を出力

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
max_l = 0
min_r = 10 ** 10
for _ in range(m):
  l, r = map(int, 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 = [1]*n
L,R = 0,float('inf')
for _ in range(m):
    a,b = map(int,input().split())
    L = max(L,a)
    R = min(R,b)
ans = R-L+1
if ans<=0:ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc127/tasks/abc127_c"

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import collections
import itertools

def YesNo(Bool):
    if(Bool):
        print("Yes")
    else:
        print("No")
    return

def resolve():

    N,M=map(int,input().split())
    L=[0]*M
    R=[0]*M
    Lmax=0
    Rmin=10**10
    for i in range(M):
        L[i],R[i]=map(int,input().split())
        Rmin=min(R[i],Rmin)
        Lmax=max(L[i],Lmax)

    print(max(0,Rmin-Lmax+1))

resolve()

Submission status: Accepted

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

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

L_SORT = sorted(L)
R_SORT = sorted(R)

#print(L_SORT)
#print(R_SORT)

L_MAX = L_SORT[M-1]
R_MIN = R_SORT[0]

#print(L_MAX)
#print(R_MIN)


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())
min_id = 0
max_id = 10**9
for _ in range(m):
    l, r = map(int, input().split())
    min_id = max(l, min_id)
    max_id = min(r, max_id)

ans = max_id - min_id + 1
if ans <= 0:
    print(0)
else:
    print(ans)

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())
lmax, rmin = 0, n

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if r_ans>=l_ans:
    print(r_ans-l_ans+1)
else:
    print(0)

Submission status: Accepted

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

L, R = 0, 10 ** 5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r+1)

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

Submission status: Accepted

Solution code:
# coding: utf-8

def main():
    N, M = map(int, input().split())
    imos = [0] * (N + 2)
    ans = 0

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

    for i in range(1, N + 2):
        imos[i] += imos[i - 1]
        if imos[i] == M and i != N + 1:
            ans += 1
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

L_list = []
R_list = []

for i in range(M):
    A, B = map(int, input().split())
    L_list.append(A)
    R_list.append(B)

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

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 min<l: min=l
    if max>r: max=r 

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

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
L = list()
R = list()
for i in range(M):
  l, r = [float(x) for x in input().split()]
  L.append(l)
  R.append(r)
Lmax = max(L)
Rmin = min(R)
if(Rmin-Lmax)<0:
  print(0)
else:
  print(int(Rmin-Lmax+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lr = []
for i in range(m):
    LR = list(map(int,input().split()))
    lr.append(LR)
min,max = 1,n
for i in range(m):
    if lr[i][0] > min:
        min = lr[i][0]
    if lr[i][1] < max:
        max = lr[i][1]
if min > max:
    print("0")
else:
    print(max - min + 1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
min_i = 0
max_i = N+1
for _ in range(M):
  a,b = map(int,input().split())
  min_i = max(min_i,a)
  max_i = min(max_i,b+1)
  
c = max(max_i - min_i,0)
print(c)

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

Submission status: Accepted

Solution code:
# 36
n, m = map(int, input().split())
l_max, r_min = -1, float('inf')
for i in range(m):
    li, ri = map(int, input().split())
    l_max = max(l_max, li)
    r_min = min(r_min, ri)

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N, M = map(int, input().split())

# left = 1
# right = N
#
# for i in range(M):
#     a,b = map(int, input().split())
#     left = max(left,a)
#     right = min(right, b)
#
# print(max(0,right-left+1))

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

imos = list(accumulate(imos))

ans = 0
for im in imos:
    ans += (im == M)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, M = map(int, input().split())

left = 1
right = N

for i in range(M):
    a,b = map(int, input().split())
    left = max(left,a)
    right = min(right, b)

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

# imos = [0] * (N + 2)
# for i in range(M):
#     l, r = map(int, input().split())
#     imos[l] += 1
#     imos[r + 1] -= 1
#
# imos = list(accumulate(imos))
#
# ans = 0
# for im in imos:
#     ans += (im == M)
#
# print(ans)

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)
  
Rm=min(R)
LM=max(L)

if Rm-LM >= 0:
  print(Rm-LM+1)
  
else:
  print(0)

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())
    
    if L > l:
        l = L
            
    if R < r:
        r = R
                
    ans = r - l + 1
    if ans < 0:
        print(0)
        break

else:
    print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
diff=[0 for _ in range(N+1)]
for _ in range(M):
    L,R=map(int,input().split())
    diff[L-1]+=1
    diff[R]-=1
ans=0
x=0
for i in range(N):
    x+=diff[i]
    if x==M:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    gates=[]
    n,m = map(int,input().split())
    gates=[list(map(int,input().split())) for _ in range(m)]
    min_data=0
    max_data=10**5+1

    for gate in gates:
        if min_data < gate[0]:
            min_data=min(gate)
        if max_data > gate[1]:
            max_data=max(gate)
    print(len(list(range(min_data,max_data+1))))

if __name__=="__main__":
    main()

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)

a = 1
b = N

for i in range(M):
    if(L[i] >= a and R[i] >= b):
        a = L[i]
    if(L[i] <= a and R[i] <= b):
        b = R[i]
    if(L[i] >= a and R[i] <= b):
        a = L[i]
        b = R[i]
    if(R[i] < a or L[i] > b):
        print(0)
        exit()


print(b-a+1)

Submission status: Accepted

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

L = []

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

res = [L[0][0], L[0][1]]

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

if res[1] < res[0]:
    print(0)
else: 
    print(res[1] - res[0] + 1)

Submission status: Accepted

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

ll, rr = 1, l
for l, r in lr:
    if ll <= l <= rr or l <= ll <= r:
        ll, rr = max(ll, l), min(rr, r)
    else:
        print(0)
        exit()
print(rr-ll+1)

Submission status: Accepted

Solution code:
import sys
import copy
import math
import itertools
import numpy as np
l = [int(c) for c in input().split()]
N=l[0]
M=l[1]
LR=[list(map(int,input().split())) for c in range(M)]
small = 0
large = N
for i in range(M):
    if small<LR[i][0]:
        small=LR[i][0]
    
    if large>LR[i][1]:
        large=LR[i][1]

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

Submission status: Accepted

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

Lm = -1
Rm = float('inf')

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

print(max(Rm - Lm + 1, 0))

Submission status: Accepted

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

l_ans = 0
r_ans = float('inf')
for l, r in l_r:
    l_ans = max(l_ans, l)
    r_ans = min(r_ans, r)
if r_ans < l_ans:
    ans = 0
else:
    ans = r_ans-l_ans+1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
res = 0
L_max = 0
R_min = 100000
for _ in range(M):
  L, R = map(int, input().split())
  if L_max < L: L_max = L
  if R < R_min: R_min = R
  
  
print(max(0, (R_min - L_max + 1)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 01:27:46 2020

@author: liang
"""

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

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

Submission status: Accepted

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

for i in range(m):
	l, r = map(int, input().split())
	l_list.append(l)
	r_list.append(r)

limit_min = max(l_list)
limit_max = min(r_list)
cnt = 0

for i in range(1, n + 1):
	if limit_min <= i <= limit_max:
		cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))
L=[]
R=[]
for i in range(len(h)):
    L.append(h[i][0])
for i in range(len(h)):
    R.append(h[i][1])

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

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())
s=0
t=10**10
for i in range(m):
  l,r=map(int,input().split())
  s=max(s,l)
  t=min(t,r)

if s>t:
  print(0)
else:
  print(t-s+1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = map(int, input().split())
l_key = []
r_key = []
for i in range(m):
    l, r = map(int, input().split())
    l_key.append(l)
    r_key.append(r)

print(max(0, min(r_key) - max(l_key) + 1))

Submission status: Accepted

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N,M = MI()
A = [0]*(N+2)

for i in range(M):
    l,r = MI()
    A[l] += 1
    A[r+1] -= 1

from itertools import accumulate

A = list(accumulate(A))
print(sum(A[i] == M for i in range(1,N+1)))

Submission status: Accepted

Solution code:
'''
Created on 2020/09/08

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N,M=map(int,pin().split())
  L,R=map(int,pin().split())
  for i in range(M-1):
    l,r=map(int,pin().split())
    if L<l and l<=R:
      L=l
    elif R<l:
      print(0)
      return
    if L<=r and r<R:
      R=r
    elif r<L:
      print(0)
      return
  print(R-L+1)
  return 
main()

Submission status: Accepted

Solution code:
#import math
#import itertools
import numpy as np
#from collections import deque

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,m=INTM()
    ls=[]
    rs=[]
    for i in range(m):
        l,r=INTM()
        ls.append(l)
        rs.append(r)
    
    ans=min(rs)-max(ls)+1
    if ans<=0:
        ans=0
    print(ans)



    
if __name__=='__main__':
    do()

Submission status: Accepted

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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
if m>=s:
    print(m-s+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max = 0
min = n+1
for i in range(m):
    if lr[i][0] > max :
        max = lr[i][0]
    if lr[i][1] < min :
        min = lr[i][1]

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
x = []
y = []
for _ in range(m):
  l,r = map(int, input().split())
  x.append(l)
  y.append(r)
x = max(x)
y = min(y)

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
temp=0
TEMP=N-1
for i in range(M):
    a,b=map(int,input().split())
    if b-1<temp or TEMP<a-1:
        print(0)
        exit()
    if a-1>temp:
        temp=a-1
    if TEMP>b-1:
        TEMP=b-1
    
print(TEMP-temp+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_max = 0
R_min = 100000

for i in range(M):
	L_max = max(L_max, L[i])
	R_min = min(R_min, R[i])

cnt = R_min - L_max + 1

if cnt < 0:
	print("0")
else:
	print(cnt)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(list(map(int, input().split())))
haba = a[0]
for i in a:
    if haba[0] <= i[0] <= haba[1]:
        haba[0] = i[0]
    elif haba[1] < i[0]:
        print(0)
        exit()
    if haba[0] <= i[1] <= haba[1]:
        haba[1] = i[1]
    elif i[1] < haba[0]:
        print(0)
        exit()
if n < haba[0]:
    ans = 0
elif haba[0] <= n <= haba[1]:
    ans = n - haba[0] + 1
else:
    ans = haba[1] - haba[0] + 1
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(input().split())
haba = a[0]
for i in a:
    if int(haba[0]) <= int(i[0]) <= int(haba[1]):
        haba[0] = i[0]
    elif int(haba[1]) < int(i[0]):
        print(0)
        exit()
    if int(haba[0]) <= int(i[1]) <= int(haba[1]):
        haba[1] = i[1]
    elif int(i[1]) < int(haba[0]):
        print(0)
        exit()
if n < int(haba[0]):
    ans = 0
elif int(haba[0]) <= n <= int(haba[1]):
    ans = n - int(haba[0]) + 1
else:
    ans = int(haba[1]) - int(haba[0]) + 1
print(ans)

Submission status: Accepted

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

max_l, min_r = 0, N

for l, r in LR:
    max_l = max(l, max_l)
    min_r = min(r, min_r)
    if min_r < max_l:
        print(0)
        exit()

print(min_r - max_l + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
[n,m] = [int(i) for i in input().split()]
to_acc = [0] * (n+1)
for i in range(m):
  [l,r] = [int(j)-1 for j in input().split()]
  to_acc[l] += 1
  to_acc[r+1] -= 1
acc = [0]
ans = 0
for i in range(n+1):
  acc.append(acc[-1] + to_acc[i])
for i in range(1,n+1):
  if m == acc[i]:
    ans += 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):
  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())

mn = 0
mx = 10**6

for i in range(M):
    l, r = map(int, input().split())
    mn = max(l, mn)
    mx = min(r, mx)

result = max(0, mx - mn + 1)
print(result)

Submission status: Accepted

Solution code:
r=input().split()
N=int(r[0])
M=int(r[1])
data=[[int(s) for s in input().split()] for i in range(M)]
left=[]
right=[]
for i in range(M):
    left.append(data[i][0])
    right.append(data[i][1])
if min(right)-max(left)+1<=0:
    print(0)
else:
    print(min(right)-max(left)+1)

Submission status: Accepted

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

    common_section = [1, n]

    for i in range(m):
        a = list(map(int, input().split()))
        if a[0] > common_section[0]:
            common_section[0] = a[0]
        if a[1] < common_section[1]:
            common_section[1] = a[1]

    answer = common_section[1] - common_section[0] + 1
    if answer < 0:
        answer = 0
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

n = 0
m = 999999

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

print(max(m-n+1, 0))

Submission status: Accepted

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

n = 0
m = 999999

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

print(m-n+1 if (m >= n) else 0)

Submission status: Accepted

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    max_l = 0
    min_r = 1e6
    for i in range(m):
        l, r = Input()
        max_l = max(max_l, l)
        min_r = min(min_r, r)

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

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

now = 0
ans = 0
for i in range(1, n+1):
    now += g[i]
    if now >= m:
        ans += 1

print(ans)

Submission status: Accepted

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

max_left=0
min_right=N

for i in range(M):
    if s[i][0]>max_left:
        max_left=s[i][0]
    if s[i][1]<min_right:
        min_right=s[i][1]
        
cnt=0
for j in range(max_left,min_right+1):
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
def INT():
    return int(input())

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

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

N, M = MI()
gate = [0] * (N + 2)
ans = 0

for _ in range(M):
    L, R = MI()
    gate[L] += 1
    gate[R + 1] -= 1

for i in range(1, N + 1):
    gate[i] += gate[i - 1]
    
for i in range(1, N + 1):
    if gate[i] == M:
        ans += 1
        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l,r = [],[]
for i in range(m):
    a,b = map(int,input().split())
    l.append(a)
    r.append(b)
num = list(range(1,n+1))
lmax = max(l)
rmin = min(r)
if lmax > rmin:
    print(0)
else:
    gate = set(range(lmax,rmin+1))
    ans = list(set(num) & gate)
    print(len(ans))

Submission status: Accepted

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

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())
 
if max(L) > min(R):
    print("0")
else:
    print(min(R) - max(L) + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
List = []
for i in range (M):
  List.append(list(map(int, input().split())))
resList = [0]*N
for i in range(M):
  resList[List[i][0]-1] +=1
  if List[i][1]<N:
    resList[List[i][1]] +=-1
for j in range(1,N):
  resList[j] = resList[j] + resList[j-1]
print(resList.count(M))

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

Submission status: Accepted

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


for i in range(M-1):
      L1, R1 = map(int, input().split())
      if R1 < L or R < L1:
        print(0)
        exit()
      else:
        if L <= L1 <= R:
          L = L1
          
        elif L <= R1 <= R:
          R = R1
        
          
print(R-L+1)

Submission status: Accepted

Solution code:
N,T = map(int,input().split())
X = [list(map(int,input().split())) for n in range(T)]
M = N
m = 0

for a,b in X:
  M = min(M,b)
  m = max(m,a)
  
if M<m:
  print(0)
else:
  print(M-m+1)

Submission status: Accepted

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

Submission status: Accepted

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

u = 0
o = 10 ** 9
for i in range(M):
    u = max(u, I[i][0])
    o = min(o, I[i][1])

if u > o:
    print(0)
else:
    print(o-u+1)

Submission status: Accepted

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

for i in range(M):
  L, R = map(int, input().split())
  
  if i == 0:
    top = R
    bottom = L
  
  top = min(top, R)
  bottom = max(bottom, L)

if top >= bottom:
  print(top-bottom+1)
else:
  print(0)

Submission status: Accepted

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

bottom=0
top=10**5
for i in range(m):
  a,b=map(int,input().split())
  bottom=max(bottom,a)
  top=min(top,b)
  
if top<bottom:
  print(0)
else:
  print(top-bottom+1)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    l_lst = [0 for _ in range(m)]
    r_lst = [0 for _ in range(m)]

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

    left = max(l_lst)
    right = min(r_lst)

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


if __name__ == "__main__":
    main()