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:
def main():
    n, m = map(int, input().split())
    lr_lst = [list(map(int, input().split())) for _ in range(m)]
    left = 0
    right = n + 1

    for i in range(m):
        if left < lr_lst[i][0]:
            left = lr_lst[i][0]
        if lr_lst[i][1] < right:
            right = lr_lst[i][1]

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
LR = list()
al = 0
ar = 0
for i in range(M):
    LR.append( list(map(int,input().split())) )
    L = LR[i][0]
    R = LR[i][1]
    if i == 0:
        al = L
        ar = R
    if al < L:
        al = L
    if ar > R:
        ar = R
    if ar < L or al > R:
        print(0)
        exit()
print(ar-al+1)

Submission status: Accepted

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

bottom = 0
top = N+1

for i in range(M):
  L,R = map(int,input().split())
  bottom = max(bottom, L)
  top = min(top,R)
  
ans = top - bottom + 1
print(ans if ans>=0 else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

mi = 0
ma = n
for i in range(m):
  mi = max(mi, lr[i][0])
  ma = min(ma, lr[i][1])
  
  
if ma-mi>=0:
  print(ma-mi+1)
else:
  print(0)

Submission status: Accepted

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


def main():
    N, M = map(int, input().split())
    min_id = 0
    max_id = 10**5
    for _ in range(M):
        L, R = map(int, input().split())
        if min_id < L:
            min_id = L
        if max_id > R:
            max_id = R
    if min_id > max_id:
        print(0)
    else:
        print(max_id - min_id + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Lmi, Lma = [], []
for i in range(M):
  mi, ma = map(int, input().split())
  Lmi.append(mi)
  Lma.append(ma)
r = max(min(Lma) - max(Lmi) + 1, 0)
print(r)

Submission status: Accepted

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

min, max = map(int, input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_left = 0
min_right = n
for _ in range(m):
    l_tmp, r_tmp = map(int, input().strip().split())
    if max_left < l_tmp:
        max_left = l_tmp
    if r_tmp < min_right:
        min_right = r_tmp

if min_right < max_left:
    print(0)
else:
    print(min_right - max_left + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = input().split()
N = int(a[1])
mini = []
maxi = []

for i in range(N):
  b = input().split()
  mini.append(b[0])
  maxi.append(b[1])
  
c = [int(s) for s in mini]
d = [int(s) for s in maxi]

c = sorted(c)
d = sorted(d, reverse=True)
e = min(d) - max(c)

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

Submission status: Accepted

Solution code:
import math
s = input().split()
n=int(s[0])
m=int(s[1])
l=0
r=float('inf')
for i in range(m):
    s = list(map(int,input().split()))
    l=max(l,s[0])
    r=min(r,s[1])
a=r-l
if a>=0:
    print(a+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
minimum = 1
maximum = N 
for i in range(M):
    L,R = map(int,input().split())
    minimum = max(minimum,L)
    maximum = min(maximum,R)
    if minimum > maximum:
        print(0)
        exit(0)

print(maximum - minimum + 1)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N , M = [int(item) for item in input().split()]
    LRs = [[int(item) for item in input().split()] for _ in range(M)]

    L_max = 0
    R_min = N

    for L, R in LRs:
        L_max = max(L_max, L)
        R_min = min(R_min, R)

    delta = R_min - L_max
    if delta >= 0:
        print(delta + 1)
    else:
        print(0)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
mini = 0
maxi = 10**9
for i in range(m):
    l,r = map(int, input().split())
    if mini < l:
        mini = l
    if r < maxi:
        maxi = r
t = maxi -mini + 1
if maxi < mini:
    print(0)
else:
    print(t)

Submission status: Accepted

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

L,R = list(zip(*LR))#転置
#print(L)
#print(R)
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; cnt = 0
for i in range(m):
    L[i],R[i] = map(int,input().split())
ml = max(L); mr = min(R)
print(mr-ml+1) if ml <= mr 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)
ll = max(L)
rr = min(R)
print(max(0,rr-ll + 1))

Submission status: Accepted

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

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

print(len(range(L,R+1)))

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N, M = getNM()
# l ~ rのどれかを持っていれば通過できる
L = 0
R = float('inf')

for i in range(M):
    l, r = getNM()
    L = max(L, l)
    R = min(R, r)

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

Submission status: Accepted

Solution code:
n , m = map(int, input().split())
count = 0
low = 0
high  = n
for _ in range(m):
    l,r=map(int, input().split())
    low = max(l, low)
    high = min(high, r)
    #print(low, high)

print(max(high-low+1, 0))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
r=[]
for i in range(m):
    templ,tempr=map(int,input().split())
    l.append(templ)
    r.append(tempr)
l_max=max(l)
r_min=min(r)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def resolve():
    import sys
    import string
    input = sys.stdin.readline
    # row = [int(x) for x in input().rstrip().split(" ")]
    # n = int(input().rstrip())
    n, m = [int(x) for x in input().rstrip().split(" ")]
    left = 1
    right = n
    for i in range(m):
        l, r = [int(x) for x in input().rstrip().split(" ")]
        if left < l:
            left = l
        if right > r:
            right = r

        if right < left:
            print(0)
            return

    print(right - left + 1)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())                       # 区間の数

imos = [0]*(10**5+2)

for _ in range(M):
    a, b = map(int, input().split())    # 区間は閉区間 [a,b] で与えられる
    imos[a] += 1
    imos[b+1] -= 1                      # 半開区間を作るために b+1 としておく点に注意。区間を使った回数を聞かれた場合はまた変わるので注意。

res = [0]
for k in imos:
    res.append(res[-1] + k)

print(res.count(M))

Submission status: Accepted

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

for i in range(m):
    c.append(list(map(int,input().split())))

s=(list(map(lambda x:x[0],c)))
b=(list(map(lambda x:x[1],c)))

maxs=max(s)
minb=min(b)

if minb<maxs:
    print('0')
else:
    ans=minb-maxs+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)

ans = min(r) - max(l) + 1

print(max(ans, 0))

Submission status: Accepted

Solution code:
def solve():
    import sys
    n, m, *LR = map(int, sys.stdin.read().split())
    L = LR[::2]
    R = LR[1::2]
    l = max(L)
    r = min(R)
    print(max(0, r - l + 1))


solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(lambda n: int(n), input().split(" ")))
L = []
R = []
for i in range(M):
  tmp = input().split(" ")
  L.append(int(tmp[0]))
  R.append(int(tmp[1]))

maxL = max(L)
minR = min(R)

print(max([minR - maxL + 1, 0]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M=mi()

    L,R=-INF,INF
    for i in range(M):
        l,r = mi()
        if L < l:
            L = l
        if r < R:
            R = r
        
        if R < L:
            print(0)
            exit()
    
    print(R-L+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)

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

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

# n,mが10^5なのでO(n^2)は間に合わない
"""
ans = 0
for i in range(1,n+1):
    flg = True
    for j in range(m):
        if i < l[j][0] or i > l[j][1]:
            flg = False
    if (flg):
        ans+=1
print(ans)
"""

#lをO(m)で走査して上限、下限をあらかじめ確定しておく
up = 10**5+1
lo = 0

for i in range(m):
    if lo < l[i][0]:
        lo = l[i][0]
    if  up > l[i][1]:
        up = l[i][1]

"""
#わざわざ回す必要がない
ans = 0
for i in range(1,n+1):
    if i >= lo and i <= up:
        ans+=1
"""
if (up - lo >= 0):
    print(up -lo+1)
else:
    print(0)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

# n,mが10^5なのでO(n^2)は間に合わない
"""
ans = 0
for i in range(1,n+1):
    flg = True
    for j in range(m):
        if i < l[j][0] or i > l[j][1]:
            flg = False
    if (flg):
        ans+=1
print(ans)
"""

#lをO(m)で走査して上限、下限をあらかじめ確定しておく
up = 10**5+1
lo = 0

for i in range(m):
    if lo < l[i][0]:
        lo = l[i][0]
    if  up > l[i][1]:
        up = l[i][1]

ans = 0
for i in range(1,n+1):
    if i >= lo and i <= up:
        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 += [L]
    r += [R]

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

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

Submission status: Accepted

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


x = min(R)-max(L)


print(0 if x <0 else x+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L_list = []
R_list = []
for o 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)
    
if L_max <= R_min:
    ans = R_min - L_max + 1
else:
    ans = 0
print(ans)

Submission status: Accepted

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

low = 1
high = n

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

print(max(high - low + 1, 0))

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N,M=MI()
    L=[0]*(N+1)
    for _ in range(M):
        l,r=MI()
        l-=1
        r-=1
        L[l]+=1
        L[r+1]-=1
        
    for i in range(N):
        L[i+1]+=L[i]
        
    ans=0
    
    for i in range(N):
        if L[i]>=M:
            ans+=1
            
    print(ans)
    

main()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
s,t=0,100001
for i in range(m):
    l,m=map(int,input().split())
    if l>s:
        s=l
    if m<t:
        t=m

ans=t-s+1
if ans<0:
    ans=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
max_l = 0
min_r = n
ans = n
for i in range(m):
  l, r = map(int, input().split())
  if ans != 0:
    if l <= min_r or r <= max_l:
      max_l = max(max_l, l)
      min_r = min(min_r, r)
    else:
      ans = 0
if ans != 0:
  ans = min_r - max_l + 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)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
max_ = 0
min_ = n+1
for i in range(m):
  a,b=map(int, input().split())
  max_ = max(max_, a)
  min_ = min(min_, b)
#print(max_, min_)
if(min_ < max_):print(0)
else:print(min_-max_+1)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lst_l = []
lst_r = []
for _ in range(m):
    l,r = map(int,input().split())
    lst_l.append(l)
    lst_r.append(r)

max_l = max(lst_l)
min_r = min(lst_r)

count = 0
for i in range(n):
    if max_l <= i+1 <= min_r:
        count += 1

print(count)

Submission status: Accepted

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

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

ans = R_min - L_max
ans += 1

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split(' '))
l = []
start = 0
end = n+1
count = 0

for i in range(m):
    l.append(list(map(int,input().split(' '))))

for i in l:
    if i[0] >= start:
        start = i[0]
    if i[1] <= end:
        end = i[1]
if start > end:
    print(0)
else:
    print(end-start+1)

Submission status: Accepted

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

low = 1
high = 10**5

for l,r in lr:
    if low <= l:
        low = l 
    if r <= high:
        high = r 

if high < low:
    print(0)
    exit()
print(high - low + 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, input().split())
c = [0 for i in range(N+1)]
for x in range(M):
    L, R = map(int, input().split())
    c[L-1] += 1
    c[R] -= 1
r = 0
t = 0
for x in c[:-1]:
    t += x
    if t == M:
        r += 1
print(r)

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)

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

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)
ans=min(b)-max(a)+1
print(ans if ans>=0 else 0)

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, m = inintm()

min_id = 10**5+1
max_id = 0

for _ in range(m):
    l, r = inintm()
    min_id = min(min_id, r)
    max_id = max(max_id, l)

print(max(0, min_id-max_id+1))

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
alist=[]
blist=[]
for i in range(m):
  a,b=map(int, input().split())
  alist.append(a)
  blist.append(b)
maxa=max(alist)
minb=min(blist)
if minb>=maxa:
  print(minb-maxa+1)
else:
  print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
x = [0] * (N + 1)

for i in range(M):
  l, r = map(int, input().split())
  x[l - 1] += 1
  x[r] -= 1
  
for i in range(1, N + 1):
  x[i] += x[i - 1]
  
#print(x)
print(x.count(M))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M= map(int, input().split())
L,R= map(int, input().split())
flag = True
for i in range(M-1):
    a,b= map(int, input().split())
    if a >= L and b <= R:
        L = a
        R = b
    elif L <= b and R >= b:
        R = b
    elif L <= a and R >= a:
        L = a
    elif a < L and R < b:
        L = L
    else:
        flag = False
        break
if flag:
 print(R-L+1)
else:
    print(0)

Submission status: Accepted

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

llist = []
rlist = []
for i in range(m):
    l,r = map(int,input().split())
    llist += [l]
    rlist += [r]

print(max(0,min(rlist)-max(llist)+1))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[0]*m
R=[0]*m
table=[0]*(n+2)
for i in range(m):
   L[i],R[i]=map(int,input().split())
   table[L[i]]+=1
   table[R[i]+1]-=1
s=[0]*(n+2)#累積和をするリスト
for i in range(1,n+2):
   s[i]=s[i-1]+table[i]
print(s.count(m))

Submission status: Accepted

Solution code:
g=lambda:map(int,input().split())
_,m=g()
ml,mr=0,10**5
for _ in range(m):
    l,r=g()
    ml=max(ml,l)
    mr=min(mr,r)
print([0,mr-ml+1][mr>=ml])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for _ in range(m - 1):
    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())
low=1
high=n 
for _ in range(m):
    l,r=map(int,input().split())
    low=max(l,low)
    high=min(r,high)
if high-low+1>=0:
    print(high-low+1)
else:
  print(0)

Submission status: Accepted

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

L = []
R = []
L_append = L.append
R_append = R.append
for l, r in LR:
    L.append(l)
    R.append(r)

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


n, m = nm()
l, r = nm()
for i in range(m - 1):
    l_t, r_t = nm()
    l, r = max(l, l_t), min(r, r_t)
if r >= l:
    print(r - l + 1)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
for i in range(M):
    st,sp = map(int,input().split())
    if i == 0:
        N_List = [st,sp]
    else:
        if st > N_List[0]:
            N_List[0] = st
        if sp < N_List[1]:
            N_List[1] = sp   
print(len(list(range(N_List[0],N_List[1]+1))))

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    acc = [0] * (N+1)
    for _ in range(M):
        left, right = map(int, input().split())
        acc[left-1] += 1
        acc[right] -= 1

    cnt = 0
    for a in accumulate(acc):
        if a == M:
            cnt += 1

    return cnt


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

st = 0
en = N

for _ in range(M):
    l, r = map(int, input().split())
    st = max(st, l)
    en = min(en, r)

print(max(en - st + 1, 0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N,M = map(int,input().split())
A = (N+2)*[0]

for m in range(M):
  L,R = map(int,input().split())
  A[L]+=1
  A[R+1]-=1

A = np.cumsum(A)
print(sum(a==M for a in A))

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
l=0
r=N
for _ in range(M):
  ll,rr=map(int,input().split())
  l = max(l,ll)
  r = min(r,rr)
ans = r - l + 1
print(ans if ans > 0 else 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(r-l+1,0))

Submission status: Accepted

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

N, M = map(int, input().split())
L = []
R = []
for i in range(M):
    l, r, = map(int, input().split())
    L.append(l)
    R.append(r)
max_l = 0
min_r = N
for i in range(M):
    if max_l < L[i]:
        max_l = L[i]
    if min_r > R[i]:
        min_r = R[i]
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())
cardid_2_num_map = [0] * (10**5 + 2)

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

for i in range(1, n + 1):
    cardid_2_num_map[i] += cardid_2_num_map[i - 1]

max_sum_num = max(cardid_2_num_map)
count_max_card = cardid_2_num_map.count(max_sum_num)

if max_sum_num == m:
    print(count_max_card)
else:
    print(0)

Submission status: Accepted

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

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

L = []
R = []

for LR in LRs:
    L.append(LR[0])
    R.append(LR[1])

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

Submission status: Accepted

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

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)
ans = min_R - max_L + 1
if ans > 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l_max = 1
r_min = 10 ** 5
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(0, r_min - l_max + 1))