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())
left = 0
right = n
ls = list(range(1,n+1))

for i in range(m):
    l,r = map(int, input().split())
    if l - 1 > left:
        left = l - 1
    if r < right:
        right = r
print(len(ls[left:right]))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  
  l=[0]*(n+1)
  for _ in range(k):
    a,b=LI()
    l[a-1]+=1
    l[b]-=1

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

  ans=0
  for x in l[:-1]:
    if x==k:
      ans+=1

  return ans

print(main())

Submission status: Accepted

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

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
L, R = [None] * M, [None] * 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:
# -*- coding: utf-8 -*-

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

l = max(L)
r = min(R)

ans = max(r - l + 1, 0)

print(ans)

Submission status: Accepted

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

lmax = 0
rmin = N

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

print(max(rmin-lmax+1,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L = []
R = []
LR = [list(map(int,input().split())) for _ in range(M)]
for i in range(M):
    L.append(LR[i][0])
countL = 0
countL = max(L)
for j in range(M):
    R.append(LR[j][1])
countR = 0
countR = min(R)
if countR - countL + 1 <= 0:
    print(0)
else:
    print(countR-countL+1)

Submission status: Accepted

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

Submission status: Accepted

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

for _ in range(m):
    l, r = map(int, input().split())
    ls.append(l)
    rs.append(r)

l_max = max(ls)
r_min = min(rs)
master_card_num = r_min - l_max

if master_card_num < 0:
    print(0)
else:
    print(master_card_num + 1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = -1
right = n+1

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=map(int,input().split())
x=[]
listA=[]
for i in range(0,M):
  x=list(map(int,input().split()))
  listA.append(x)
listB=[]
listC=[]
for i in range(0,M):
  listB.append(listA[i][0])
  listC.append(listA[i][1])
x=0
if max(listB)>min(listC):
  print(0)
else:
  print(abs(max(listB)-min(listC))+1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
imos=[0]*(10**5+10)
maxr=-1
for _ in range(m):
    l,r=map(int,input().split())
    imos[l]+=1
    imos[r+1]-=1
    maxr=max(maxr,r)

for i in range(maxr+1):
    imos[i+1]+=imos[i]

ans=0

for i in range(maxr+1):
    if imos[i]==m:
        ans+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
maxl=1
minr=n
while m:
  l,r=map(int,input().split())
  maxl=max(maxl,l)
  minr=min(minr,r)
  m-=1
print(max(0,minr-maxl+1))

Submission status: Accepted

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

for _ in range(M-1):
    l, r = map(int, input().split())
    if L < l:
        L = l

    if R > r:
        R = r

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lm = []
for _ in range(m):
    lm.append(tuple(map(int,input().split())))
l = lm[0][0]
r = lm[0][1]
for x in range(1,m):
    if lm[x][0] > l:
        l = lm[x][0]
    if lm[x][1] < r:
        r = lm[x][1]
    
if l > r:
    print(0)
else:
    print(r-l+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())
a=L[0]
for i in range(M):
  if L[i]>a:
    a=L[i]
  else:
    a=a
b=R[0]
for i in range(M):
  if R[i]<b:
    b=R[i]
  else:
    b=b
if b-a<0:
  print("0")
else:
  print(b-a+1)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

l_max = 0
r_min = 10**6
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_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())
cnt=0
ln=[]
rn=[]
for i in range (m):
    l,r=map(int,input().split())
    ln.append(l)
    rn.append(r)
ln.sort(reverse=True)
rn.sort()
for i in range (n):
    if ln[0]<=i+1 and rn[0]>=i+1:
        cnt=cnt+1
print(cnt)

Submission status: Accepted

Solution code:
def prison(left, right):
    if left <= right:
        print(right-left+1)
    else:
        print(0)
        
N, M = map(int, input().split())
left_limit=0
right_limit=N
 
for i in range(M):
    LEFT, RIGHT = map(int, input().split())
    if LEFT > left_limit:
        left_limit = LEFT
    if RIGHT < right_limit:
        right_limit = RIGHT

prison(left_limit, right_limit)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import stdin
import numpy as np

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

L = np.array(L)
R = np.array(R)

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())
left=0
right=N

for i in range(M):
    L, R = map(int, input().split())
    if L > left:
        left = L
    if R < right:
        right = R

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

Submission status: Accepted

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

for i in range(0, len(l_lst)):
    for j in range(0, len(l_lst[i])):
        l_lst[i][j] = int(l_lst[i][j])

max_lst = list()
min_lst = list()
for i in range(0, len(l_lst)):
    max_lst.append(l_lst[i][0])
    min_lst.append(l_lst[i][1])

max_int = max(max_lst)
min_int = min(min_lst)

cnt = 0
for i in range(1, n+1):
    if max_int <= i and i <= min_int:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
#C
N, M = map(int, input().split())
Table = [list(map(int, input().split())) for _ in range(M)]
Table.sort(key=lambda x:x[0])
Table.sort(key=lambda x:x[1])
#print(f'Table:{Table}')
first = Table[0][0]
end = Table[0][1]
Table.pop(0)
ans = 1
#print(f'first:{first} end:{end}')
for task in Table:
    #print(f'task:{task}')
    num = task[0]
    #print(f'num:{num}')
    if num <= end:
        if num > first:
            first = num
    else:
        ans = 0
        break
    #print(f'first:{first}')
if ans == 0:
    print(ans)
else:
    print(end-first+1)

Submission status: Accepted

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

min_value = 1
max_value = n

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

Answer = max(max_value - min_value +1,0)

print(Answer)

Submission status: Accepted

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

Submission status: Accepted

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

LN, RN = 1,N
for i in range(M):
  L,R = list(map(int, input().split()))
  LN = max(LN,L)
  RN = min(RN,R)
  
print(max(RN-LN+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())
cnt = 0
l_max = max(l[:m])
r_min = min(r[:m])
for i in range(n):
    if l_max <= i+1 <= r_min:
        cnt += 1
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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)

l = max(left)
r = min(right)

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

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

Submission status: Accepted

Solution code:
data = list(map(lambda x: int(x),input().split(" ")))

cardNum = []

for n in range(data[1]):
	cardNum.append(list(map(lambda x: int(x),input().split(" "))))


card = [0]*(data[0])

check = [0,1000000]

for n in range(data[1]):
	if check[0] < cardNum[n][0]:
		check[0] = cardNum[n][0]
	if check[1] > cardNum[n][1]:
		check[1] = cardNum[n][1]

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

Submission status: Accepted

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

L,R = map(int,input().split(' '))
for i in range(M-1):
  inL,inR = map(int,input().split(' '))
  if inL > L:
    L = inL
  if inR < R:
    R = inR
    
print(max(0,R-L+1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ans = 0
L_list = []
R_list = []

for _ in range(M):
    l, r = map(int, input().split())
    L_list.append(l)
    R_list.append(r)

L = max(L_list)
R = min(R_list)

for i in range(1, N+1):
    if L <= i <= R:
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
r, l = 10 ** 5 + 1, 0
for i in range(m):
    li, ri = list(map(int, input().split()))
    if li > l:
        l = li
    if ri < r:
        r = ri

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

Submission status: Accepted

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

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

ans = min(rr) - max(ll) + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

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

max_L = -1
min_R = 10**5 + 1
for _ in range(M):
    L, R = map(int, 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())
lr = [list(map(int,input().split())) for _ in range(m)]
min_id = 1
max_id = n
for v in lr:
  min_id = max(min_id,v[0])
  max_id = min(max_id,v[1])
if max_id < min_id:
  out = 0
else:
  out = max_id - min_id +1  
print(out)

Submission status: Accepted

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

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

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

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)

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

print(ans)

Submission status: Accepted

Solution code:
def kasanari(A, B, C, D):
    if B < C or A > D:
        return 0
    elif B <= D:
        if A >= C:
            return [A, B]
        else:
            return [C, B]
    else:
        if A >= C:
            return [A, D]
        else:
            return [C, D]

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

tmp = [L[0][0], L[0][1]]
L.pop(0)
for i in L:
    L1 = tmp[0]
    R1 = tmp[1]
    tmp = kasanari(L1, R1, i[0], i[1])
    if tmp == 0:
        print(0)
        exit()
print(tmp[1]-tmp[0]+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
ids = [list(map(int, input().split())) for _ in range(m)]
l = []
r = []
for id in ids:
        l.append(id[0])
        r.append(id[1])
if max(l) <= min(r):
        print(min(r) - max(l) + 1)
else:
        print(0)

Submission status: Accepted

Solution code:
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print(0)
    break
else:
  print(pair[1]-pair[0]+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)
Lmax = max(L)
Rmin = min(R)
if Lmax>Rmin:
    print(0)
else:
    print(Rmin-Lmax+1)

Submission status: Accepted

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

for i in range(M-1):
    a,b= map(int, input().split())
    if a<L and R<b:
        N=N
    elif L<=a<=R:
        L=a
    elif L<=b<=R:
        R=b
    else:
        print(0)
        exit(0)
print(R-L+1)

Submission status: Accepted

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

for i in range(M):
        l.append(A[i][0])
for i in range(M):
        r.append(A[i][1])

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

Submission status: Accepted

Solution code:
import numpy as np

a = input().split(" ")
N = int(a[0])
M = int(a[1])
gate = []
for i in range(M):
    a = input().split(" ")
    gate.append([int(a[0]),int(a[1])])

gate = np.array(gate)

max = gate.max(axis=0)[0]
min = gate.min(axis=0)[1]

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

Submission status: Accepted

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

for tan in range(M):
    if(L[tan]>min):
        min=L[tan]
    if(R[tan]<max):
        max=R[tan]
    if(L[tan]>max):
        kotae=0
        break
    if(R[tan]<min):
        kotae=0
        break
    kotae=max-min+1
print(kotae)

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)
  if L > R:
    print(0)
    exit()
print(R-L+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def main():
    n, m = map(int, input().split())

    A = []
    B = []

    for _ in range(m):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)

         
    l = max(A)
    r = min(B)
    #print(l, r)

    if r >= l:
        return r - l + 1
    else:
        return 0

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



"""
for i in range(1, n + 1):
        for j in range(m):
            if gate[j][0] <= i and i <= gate[j][1]:
                if j == len(gate) - 1:
                    result += 1
                continue
            else:
                break
"""

Submission status: Accepted

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

for i in range(m):
    li, ri = map(int, input().split())
    left = max(left, li)
    right = min(right, ri)
    
print(max(0, right - left + 1))

Submission status: Accepted

Solution code:
n,m = input().split()
LR =  [list(map(int,input().split())) for i in range(int(m))]
s = max([i[0] for i in LR])
e = min([i[1] for i in LR])
print(max(e-s+1,0))

Submission status: Accepted

Solution code:
inf = input().split()
N = int(inf[0])
M = int(inf[1])
ls = []
m = 0
pair = [0,N]
for i in range(M):
  inf = input().split()
  L = int(inf[0])
  R = int(inf[1])
  pair[0] = max(pair[0], L)
  pair[1] = min(pair[1], R)
  if pair[0]>pair[1]:
    print(0)
    break
else:
  print(pair[1]-pair[0]+1)

Submission status: Accepted

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

l = [0]*m
r = [0]*m
b = [0]*(n + 2)

count = 0

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

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

for i in range(1,n+1):
        if(i >= l_max and i <= r_min ):
                count += 1
        
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N,M = na()
GATA = [0]*(N+1)
for i in range(M):
    L,R = na()
    GATA[L-1]+=1
    GATA[R]-=1
ans = 0
tt = 0
for i,t in enumerate(GATA):
    tt += t
    if tt == M:
        ans+=1
print(ans)

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)
    
tans=min(rl)-max(ll)+1
if tans>=0:print(tans)
else:print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().rstrip().split())
L_max = -10**10
R_min = 10**10
for i in range(M):
    L,R = map(int, input().split())
    if L_max < L:
        L_max = L
    if R < R_min:
        R_min = R
print(R_min-L_max+1 if L_max <= R_min else 0)

Submission status: Accepted

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

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

sidel = 1
sider = n
for lr in range_lr:
    sidel = max(sidel, lr[0])
    sider = min(sider, lr[1])
print(sider-sidel+1 if sider-sidel>=0 else 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())

lt=max(L)
rt=min(R)

print(max(0,rt-lt+1))

Submission status: Accepted

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

left = 0
right = n

for i in li:
  left = max(i[0], left)
  right = min(i[1], right)
  
if right < left:
  print(0)
else:
  print(right - left + 1)

Submission status: Accepted

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

l = []
r = []

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LR = []
for _ in range(M):
    LR.append(list(map(int, input().split())))
mn, mx = 0, int(1e5)
for i, j in LR:
    if i >= mn:
        mn = i
    if j <= mx:
        mx = j
    
if mx < mn:
    print(0)
else:
    print(mx - mn + 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())
max_L = max(L)
min_R = min(R)
if(min_R - max_L + 1 > 0):
    print(min_R - max_L + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

l_max = 1
r_min = N
for m in range(M):
    left, right = map(int, input().split())
    l_max = max(l_max, left)
    r_min = min(r_min, right)

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
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:
n,m = map(int,input().split())
card = []
l = []
min_num = 0
max_num = 100000

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

for i in card:
	if i[0] > min_num:
		min_num = i[0]
	if i[1] < max_num:
		max_num = i[1]
	# print("min_num =",min_num,"max_num =",max_num)

if min_num > max_num:
	print(0)
else:
	print(max_num-min_num+1)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

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

def main():
  n,m=LI()

  l=[]
  for _ in ' '*m:
    a,b=LI()
    l.append([a,1])
    l.append([b,-1])

  l=sorted(l,key=lambda x:x[1],reverse=True)
  l=sorted(l,key=lambda x:x[0])

  sm=st=0
  for x in l:
    sm+=x[1]
    if sm==m:
      st=x[0]
    if sm!=m and st!=0:
      return x[0]-st+1

  return 0

print(main())

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
i_max = n
i_min = 0
for x in range(m):
    l, r = map(int, input().split())
    i_max = min(r, i_max)
    i_min = max(l, i_min)

print(max(0,i_max - i_min + 1))

Submission status: Accepted

Solution code:
def enter_card(L, R, f_point, e_point):
    if f_point < L:
        f_point = L
    if e_point > R:
        e_point = R
    return f_point, e_point

def open_list(L, R):
    if L > R:
        return 0
    else:
        return R - L + 1
   

N, M = map(int, input().split(' '))
f_point = 1
e_point = N
for i in range(M):
    L,  R = map(int, input().split(' '))
    f_point, e_point = enter_card(L, R, f_point, e_point)
a = open_list(f_point, e_point)
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())
lr = [[int(i) for i in input().split()] for i in range(m)]
lr.sort(key=lambda x:x[0])
low = 1
high = n
for l,e in lr:
    if low < l:
        low = l
    if high > e:
        high = e
if high >= low:
    print(high-low+1)
else:
    print("0")

Submission status: Accepted

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

left = 1
right = N
# 1-indexed
# [left, right]

for _ in range(M):
    L, R = map(int, input().split())
    if L > left:
        left = L
    if R < right:
        right = R
    if right - left + 1 <= 0:
        break
print(max(0, right - left + 1))

Submission status: Accepted

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


class CountedAcID():
    def __init__(self, gates):
        self.gates = gates
        self.scope = [1, N]

    def run(self):
        self.__get_range()
        self.total = self.__return_num()


    def __get_range(self):
        for element in self.gates:
            self.scope[0] = self.__get_left(element[0], self.scope[0])
            self.scope[1] = self.__get_right(element[1], self.scope[1])

    def __get_left(self, element, left):
        if element > left:
            return element
        else:
            return left

    def __get_right(self, element, right):
        if element < right:
            return element
        else:
            return right

    def __return_num(self):
        if self.scope[1] > N:
            return N - self.scope[0] + 1

        else:
            return self.scope[1] - self.scope[0] + 1


c = CountedAcID(gates)
c.run()
num = c.total

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

Submission status: Accepted

Solution code:
# C - Prison
# https://atcoder.jp/contests/abc127/tasks/abc127_c

n, m = map(int, input().split())
gates = [list(map(int, input().split())) for _ in range(m)]
# gates = []
# for i in range(m):
#     gates.append(list(map(int, input().split())))


scope = [1, n]

for element in gates:
    """
    ここで範囲の左右を取得する
    Lは0番目の最大値
    Rは1番目の最小値を検索する
    """

    if element[0] > scope[0]:
        scope[0] = element[0]

    if element[1] < scope[1]:
        scope[1] = element[1]

"""
範囲の最大値がnを超えていた場合について
最小値~nまでの範囲で処理を行う

超えていない場合について
普通にR-Lを行う
"""


def return_num():
    if scope[1] > n:
        return n - scope[0] + 1

    else:
        return scope[1] - scope[0] + 1


num = return_num()

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

# print(scope)

Submission status: Accepted

Solution code:
def main():
    n = [int(i) for i in input().split()]
    mi = []
    ma = []
    for i in range(n[1]):
        j = [int(i) for i in input().split()]
        mi.append(j[0])
        ma.append(j[1])
    ans = min(ma)-max(mi)+1
    if ans < 0:
        ans = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

intv = [1,N]
for i in range(M):
  L,R=map(int,input().split())
  if intv[0] < L:
    intv[0] = L
  if R < intv[1]:
    intv[1] = R

#print(intv)
print(max(intv[1]-intv[0]+1,0))

Submission status: Accepted

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

li_w = list()

for _ in range(m):
    li_w.append(tuple(map(int, input().split())))

w_l = 0
w_r = 999999

for w in li_w:
    w_l = max(w_l, w[0])
    w_r = min(w_r, w[1])


if w_r >= w_l:
    print(w_r - w_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)
l=max(l_lst)
r=min(r_lst)
if l<=r:
    print(r-l+1)
else:
    print(0)

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:
        l_max = l
        r_min = r
    else:
        l_max = max(l_max, l)
        r_min = min(r_min, r)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = [-1]*(m)
r = [-1]*m
for i in range(m):
    a,b = map(int,input().split())
    l[i]=a
    r[i]=b
right = n
left = 0 
#print(r)
#print(l)
count = 0
k = 0
for i in range(m):
    right = min(r[i],right)
    left = max(l[i],left)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
left = 0
right = 10**5
for _ 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 = map(int, input().split())
my_list = []
for i in range(M):
    line = input().split()
    my_list.append((int(line[0]), int(line[1])))

max_l = 0
min_r = 10 ** 6

for l,r in my_list:
    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:
from itertools import accumulate
from collections import Counter


def solve(string):
    n, m, *lr = map(int, string.split())
    t = [0] * (n + 2)
    for l, r in zip(*[iter(lr)] * 2):
        t[l] += 1
        t[r + 1] -= 1
    *t, = accumulate(t)
    c = Counter(t)
    return str(c[m]) if m in c.keys() else "0"


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(m)])))