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_r = list(list(int(i) for i in input().split()) for i in range(m))

x = []
y = []

for i in range(m):
    x.append(l_r[i][0])
    y.append(l_r[i][1])

x1 = max(x)
y1 = min(y)

if x1 <= y1:
    print(y1 - x1 + 1)
else:
    print('0')

Submission status: Accepted

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

ls = []
rs = []
for l, r in lrs:
    ls.append(l)
    rs.append(r)
l = max(ls)
r = min(rs)
if l <= r:
    print(r - l + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
LR = np.array([list(map(int, input().split())) for _ in range(M)])
lmax, rmin = LR.max(axis=0)[0], LR.min(axis=0)[1]
print(max(0, rmin-lmax+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import copy
import fractions
import numpy as np

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

data = getSomeInputListInt(m)

lower = []
upper = []

for i in range(m):
    lower.append(data[i][0])
    upper.append(data[i][1])

lowerNum = max(lower)
upperNum = min(upper)

ans = max(0, upperNum - lowerNum + 1)


print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

n3 = []
n4 = []

t1 = 0
t2 = 0
for i1 in range(n2):
    t1, t2 = ([int(i) for i in input().split()])
    n3.append(t1)
    n4.append(t2)

m1 = max(n3)
m2 = min(n4)

r1 = m2 - m1 + 1

if r1 < 0:
    r1 = 0

print(r1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = [0] * M, [0] * M
Lmax, Rmin = 0, 1e10
for i in range(M):
    L, R = map(int, input().split())
    if L > Lmax:
        Lmax = L
    if R < Rmin:
        Rmin = R

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
min_num = 0
max_num = 10**5
           
for i in range(m):
  min_temp, max_temp = map(int, input().split())
  if min_temp > min_num:
    min_num = min_temp
  if max_temp < max_num:
    max_num = max_temp
print(max(max_num - min_num + 1, 0))

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
l = [list(map(int,input().split())) for i in range(n[1])]
minp = l[0][0]
maxp = l[0][1]
for t in range(n[1]):
    minp = max(minp,l[t][0])
    maxp = min(maxp,l[t][1])
print(max(0,maxp-minp+1))

Submission status: Accepted

Solution code:
import numpy as np
N, M = [int(x) for x in input().split()]
ok = np.zeros(N+2, np.int64)

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

cum = np.cumsum(ok)
print(np.sum(cum==M))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(len(range(s,e+1)))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
maxi = 10**5
mini = 0
for _ in range(m):
    s,t = map(int,input().split())
    maxi = min(maxi,t)
    mini = max(mini,s)
print(maxi - mini +1) if maxi - mini +1 > 0 else print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
mNum=0
MNum=10**10
for i in range(M):
    tm,tM = map(int, input().split())
    if(mNum<tm):
        mNum=tm
    if(MNum>tM):
        MNum=tM
print(max(MNum-mNum+1,0))

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
mn = 0
mx = N
f = False
for i in range(M):
    tmn, tmx = list(map(int, input().split()))
    if mn < tmn:
        mn = tmn
    if tmx < mx:
        mx = tmx
    if mx < mn:
        f = True
        break
if f:
    print(0)
else:
    print(mx - mn + 1)

Submission status: Accepted

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

n, m = map(int, input().split())
L, R = 0, n

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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
S = [None]*m
L = 0
R = n

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

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

Submission status: Accepted

Solution code:
import numpy as np
input1 = input()

door_num = int(input1.split()[1])
lefts = []
rights = []
for i in range(door_num):
    a = input()
    new_inputs = [int(i) for i in a.split()]
    lefts.append(new_inputs[0])
    rights.append(new_inputs[1])
    #max_left = np.max([new_inputs[0], max_left])
    #min_right = np.min([new_inputs[1], min_right])
    #if not min_right >= max_left:
    #    break
min_right = np.min(rights)
max_left = np.max(lefts)

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

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

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

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

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())
LL = max(L)
RR = min(R)
ans = 0
for i in range(N):
    if LL <= i + 1 <= RR:
        ans += 1
    else:
        pass
print(ans)

Submission status: Accepted

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

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

if r_min < l_max:
    ans = 0
else:
    ans = r_min - l_max + 1

print(ans)

Submission status: Accepted

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

l_tmp = 0
r_tmp = n+1
for i in range(m):
  l, r = map(int, input().split())
  l_tmp = l if l > l_tmp else l_tmp
  r_tmp = r if r < r_tmp else r_tmp

if r_tmp-l_tmp >= 0:
  print(r_tmp-l_tmp+1)
else:
  print(0)

Submission status: Accepted

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

mi = -1
ma = 10**5 + 1
for i in range(M):
    l = lr[i][0]
    r = lr[i][1]
    if mi <= l:
        mi = l
    if r <= ma:
        ma = r

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

Submission status: Accepted

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

for i in range(1,n+1):
    card[i] += card[i-1]
ans = 0
for i in card:
    if i == m:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def main(): 
    N,M = map(int,input().split())
    LR=[list(map(int,input().split())) for _ in range(M)]
    max = 0
    min = N
    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(0)
    else:
        print(min-max+1)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split(' ')]
lr = [(int(l), int(r)) for l, r in [input().split(' ') for _ in range(m)]]

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

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

Submission status: Accepted

Solution code:
def prison(n,m,security):
    l = 0
    r = 1
    through = [False]*(n+1)
    gate = [1,n]
    for i in range(m):
        if security[i][r] < gate[r]:
            gate[r] = security[i][r]

        if  gate[l] < security[i][l]:
            gate[l] = security[i][l]


    for i in range(1,n+1):
        if gate[l] <= i and i <= gate[r]:
            through[i] = True
        else:
            through[i] = False


#        print("i=%d, %s" %(i,through[i]))

    sucess = filter(lambda x:x == True, through)
    return len(sucess)

if __name__ == '__main__':
    n,m = map(int,raw_input().split())

    security = [list(map(int,raw_input().split())) for _ in range(m)]

    print prison(n,m,security)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=0
R=100001
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())
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(ans if ans >= 0 else 0)

Submission status: Accepted

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

    left, right = LR[0]
    for i in range(1, M):
        l, r = LR[i]
        if left > r or right < l:
            return 0
        elif left <= r:
            left = max(left, l)
            right = min(right, r)
    return right - left + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ll=[]
rl=[]
for i in range(m):
    l,r=map(int, input().split())
    ll.append(l)
    rl.append(r)
#print(ll)
#print(rl)
max_l=max(ll)
min_r=min(rl)
ans=min_r-max_l+1
if ans<=0:
    print(0)
else:
    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)
  
if min(R_list) >= max(L_list):
  print(min(R_list)  - max(L_list)+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

start = 1
end = n
for pair in lst:
    start = max(start, pair[0])
    end = min(end, pair[1])
print(end - start + 1) if end - start + 1 >= 0 else print(0)

Submission status: Accepted

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

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

l = set()
r = set()
for i in range(M):
    li,ri = map(int, input().split())
    l.add(li)
    r.add(ri)
x = min(r) - max(l)
if x<0:
    print(0)
else:
    print(x+1)

Submission status: Accepted

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

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

l,r = 1,N
for i in range(M):
    l1,r1 = map(int, input().split())
    if l1>l:
        l = l1
    if r1<r:
        r = r1
if r-l<0:
    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):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)
L.sort(reverse=True)
R.sort()
ans =0
for i in range(n+1):
    if i >=L[0] and i <=R[0]:
        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(0,min(r)-max(l)+1))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(m):
  a,b = [int(i) for i in input().split()]
  lista.append(a)
  listb.append(b)
  
maxa = max(lista)
minb = min(listb)

if minb<maxa:
  print(0)
else:
  print(minb-maxa+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

def main():
    N, M = map(int, input().split())
    INF = float('inf')

    LR = [-INF, INF]
    for _ in range(M):
        L, R = map(int, input().split())
        if LR[0] < L:
            LR[0] = L
        if LR[1] > R:
            LR[1] = R
    if LR[1] < LR[0]:
        return 0
    else:
        return int(LR[1] - LR[0] + 1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
start = []
end = []
for i in range(M):
    L, R = map(int, input().split())
    start.append(L)
    end.append(R)
start_max = max(start)
end_min = min(end)
print(max(0, end_min - start_max + 1))

Submission status: Accepted

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

Submission status: Accepted

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

floor = 0
ceiling = 999999

for i in range(m):
  a, b = map(int, input().split())
  if a > floor:
    floor = a
  if b < ceiling:
    ceiling = b

print(max(0, ceiling - floor + 1))

Submission status: Accepted

Solution code:
n,m=[int(i) for i in input().split()]
datalist=[[int(j) for j in input().split()] for i in range(m)]
# print(datalist)


#先頭と末尾の値を取得
head=1
tail=n

#全てのゲートについて、範囲を狭めていく
for data in datalist:
    
    if data[0]>head:
        head=data[0]
    if data[1]<tail:
        tail=data[1]
        
ans=tail-head+1
if ans<0:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
c,g=map(int,input().split())

st=[]
en=[]

for i in range(g):
	s,e=map(int,input().split())
	st.append(s) 
	en.append(e)

res=min(en)-max(st)+1
print(max(res,0))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

Ld = []
Rd = []

for i in range(M):
	Ld.append(LR[i][0])
	Rd.append(LR[i][1])

if max(Ld) <= min(Rd):
	print(min(Rd) - max(Ld) + 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())
    

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

Submission status: Accepted

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

l, r = 1, 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())
L = []
R = []
for i in range(M):
    l,r = map(int,input().split())
    L.append(l)
    R.append(r)
maxl = max(L)
minr = min(R)
ans = minr-maxl + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = [0 for i in range(M)]
R = [0 for i in range(M)]
for i in range(M):
  L[i], R[i] = map(int, input().split())
  
ans = min(R)-(max(L)-1)

print(max(ans,0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
s=[0]*n
max_=1
min_=n
for _ in range(m):
    b,c=map(int,input().split())
    if b>max_:
        max_=b
    if c<min_:
        min_=c
if min_<max_:
  print('0')
else:
  print(min_-max_+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:
def solve():
    n, m = map(int, input().split())
    lrs = list(list(map(int, input().split())) for _ in range(m))
    max_l, min_r = max([l for l, r in lrs]), min([r for l, r in lrs])
    ans = min_r - max_l + 1
    return ans if ans > 0 else 0


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC 127 c
n, m = [int(el) for el in input().split(' ')]
max_l = 0
min_r = n
for i in range(m):
    l, r = [int(el) for el in input().split(' ')]
    max_l = max(max_l, l)
    min_r = min(min_r, r)

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

Submission status: Accepted

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

x = []
y = []

for i in range(m):
    array = list(map(int, input().split()))
    x.append(array[0])
    y.append(array[1])

a = max(x[:])
b = min(y[:])

print(len(list(range(a, b+1))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lm=0
rm=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())
lm=0
rm=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()) 
s=[list(map(int, input().split())) for i in range(m)]
R=0
L=0
s=sorted(s)
L=s[m-1][0]
s=sorted(s, key=lambda x:x[1])
R=s[0][1]
if R-L>=0:
    print(R-L+1)
else:
    print(0)

Submission status: Accepted

Solution code:
import collections
n,m = map(int,input().split())
r = []
l = []
for i in range(m):
    ll,rr = map(int,input().split())
    r.append(rr)
    l.append(ll)
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())

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

lower = 1
upper = N
for l, u in lst:
    if l > lower:
        lower = l

    if u < upper:
        upper = u

    if upper < lower:
        print(0)
        exit()

print(upper - lower + 1)

Submission status: Accepted

Solution code:
#24 ABC127C
n,m=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(m)]
x=[0 for _ in range(n+1)]
for l,r in lr:
	x[l-1]+=1
	x[r]-=1
for i in range(1,n+1):
	x[i]+=x[i-1]
res=0
for t in x:
	if t==m:
		res+=1
print(res)

Submission status: Accepted

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

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

lma, rmi = max(l), min(r)

if lma > rmi:
    print(0)
else:
    if rmi <= n:
        print(rmi - lma + 1)
    elif lma <= n:
        print(n - lma + 1)
    else:
        print(0)

Submission status: Accepted

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

ans = minR - maxL + 1
print(max(ans, 0))

Submission status: Accepted

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

ans = minR - maxL + 1
print(max(ans, 0))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#list(map(int, input().split()))
import sys
count = 0
n, m = list(map(int, input().split()))
temp_l, temp_r = list(map(int, input().split()))
for i in range(m-1):
  l,r = list(map(int, input().split()))
  if temp_r < l or r < temp_l:
    print(0)
    sys.exit()
  else:
    temp_l = max(temp_l, l)
    temp_r = min(temp_r, r)
print(temp_r - temp_l +1)

Submission status: Accepted

Solution code:
def resolve():
    N,M = map(int,input().split())
    LL,RR = [],[]
    for i in range(M):
        L,R = map(int,input().split())
        LL.append(L)
        RR.append(R)

    bool_id = [1]*N
    lmin = -1
    rmax = 10**10
    for i in range(M):
        lmin = max(lmin,LL[i])
        rmax = min(rmax,RR[i])
    print(max(rmax-lmin+1,0))

resolve()

Submission status: Accepted

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

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

ans = minR - maxL + 1
print(max([ans,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(R) - max(L) + 1

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

Submission status: Accepted

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

l_max = lr[0][0]
r_min = lr[0][1]

for i in range(1,m):
    if l_max < lr[i][0]:
        l_max = lr[i][0]
    if r_min > lr[i][1]:
        r_min = lr[i][1]

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

Submission status: Accepted

Solution code:
#127
n, m = list(map(int, input().split(' ')))
a = []
b = []
for i in range(m):
    x = []
    x = list(map(int, input().split(' ')))
    a.append(x[0])
    b.append(x[1])
result = min(b)-max(a)+1
if result <= 0:
    print(0)
else:
    print(int(result))

Submission status: Accepted

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

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

an = min(rr)-max(ll)+1
print(max(0,an))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

left = max(L)
right = min(R)
ans = right - left + 1
print(ans if ans >= 0 else 0)

Submission status: Accepted

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

cards = [0] * (N + 2)

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

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

Submission status: Accepted

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

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

l_max = 1
r_min = 100000
for i in range(m):
    if l_max < l[i]:
        l_max = l[i]
    if r_min > r[i]:
        r_min = r[i]

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n, m = map(int, input().split())
left = []
right = []
for i in range(m):
    l, r = map(int, input().split())
    left.append(l)
    right.append(r)

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

Submission status: Accepted

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

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

print(s.count(m))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
min_list = []
max_list = []
for i in range(k):
    j, l = list(map(int, input().split()))
    min_list.append(j)
    max_list.append(l)


print(max(0, min(max_list) - max(min_list) + 1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
C=[list(map(int,input().split())) for _ in range(M)]
G=[0]*(N+1)
for l,r in C:
  G[l-1]+=1
  G[r]-=1
gg=0
li=[]
for g in G:
  gg=gg+g
  li.append(gg)
print(li.count(M))

Submission status: Accepted

Solution code:
temp=input().split()
N=int(temp[0])
M=int(temp[1])
Lmax=int(0)
Rmin=int(N-1)

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

Submission status: Accepted

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

a=1
b=10**5
for i in range(m):
    if l[i][0]>=a:
        a=l[i][0]
    if l[i][1]<=b:
        b=l[i][1]

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

Submission status: Accepted

Solution code:
import numpy as np

N, M= (int(i) for i in input().split())  
lr = [[int(i) for i in input().split()] for i in range(M)] 	
lr = np.array(lr)
l = list(lr[:, 0])
r = list(lr[:, 1])
d = min(r)-max(l)+1
if d>0:
	print(d)
else:
	print(0)