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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	N, M = map(int, input().split(" "))
	#N = int(input())
	#S = input()

	LR = []
	min_i = -1
	max_i = -1
	min_a = -1
	max_a = -1
	for i in range(M):
		L, R = map(int, input().split(" "))
		LR.append((L, R))
		if i == 0:
			min_i = L
			max_i = R
			min_a = L
			max_a = R
		else:
			if L > min_i:
				min_i = L
			if max_i > R:
				max_i = R

	ans = 0
	for i in range(min_i, max_i+1):
		ans += 1

	print(ans)


if __name__ == "__main__":
	main()

Submission status: Accepted

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

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

for i in range(M):
    imos[s[i]] += 1
    imos[t[i]+1] -= 1

for i in range(10**5+1):
    imos[i+1] += imos[i]

count = 0
for i in range(10**5+2):
    if imos[i] == M:
        count += 1

print(count)

Submission status: Accepted

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

l_max = 1
r_min = n
for l, r in lrs:
    l_max = max(l, l_max)
    r_min = min(r, r_min)

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

Submission status: Accepted

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

l = []
r = []
for i in range(m):
    temp = input().split()
    l.append(int(temp[0]))
    r.append(int(temp[1]))

a = max(l)
b = min(r)

ans = b - a + 1
if ans <= 0:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
from sys import exit

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_max = max(L)
r_min = min(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())
l_max = 0
r_min = float('inf')
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(0)
else:
    print(r_min - l_max + 1)

Submission status: Accepted

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

LR = []
for _ in range(M):
    l, r = map(int, input().split())
    LR.append((l, r))
    
cards = LR[:]

import sys
INT_MAX = sys.maxsize

left_id = 0
right_id = INT_MAX
for l, r in cards:
    left_id = max(left_id, l)
    right_id = min(right_id, r)
    
if left_id <= right_id:
    ans = right_id + 1 - left_id
else:
    ans = 0
print(ans)

Submission status: Accepted

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

N,M = [int(i) for i in input().split()]
L_max = 0
R_min = 10**9
for _ in range(M):
    L,R = [int(i) for i in input().split()]
    L_max = max(L_max,L)
    R_min = min(R_min,R)

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

Submission status: Accepted

Solution code:
n, m = [int(_) for _ in input().split()]
start, end = (1, n)

for i in range(m):
    l, r = [int(_) for _ in input().split()]
    if start < l:
        start = l
    if end > r:
        end = r

ans = end - start + 1
print(ans if ans > 0 else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

max_l = float("-inf")
min_r = float("inf")

for l, r in lr:
    max_l = max(max_l, l)
    min_r = min(min_r, r + 1)

ans = min_r-max_l
print("0" if ans <= 0 else ans if ans < n else n)

Submission status: Accepted

Solution code:
import numpy as np

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

lr_np = np.array(lr)

l = lr_np[:, 0]
r = lr_np[:, 1]

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt=0
mini,maxim=list(map(int,input().split()))
for _ in range(M-1):
    a,b=list(map(int,input().split()))
    if a>maxim:
        cnt=1
    else:
        mini=max(a,mini)
        maxim=min(b,maxim)

if cnt==0:
    print(maxim-mini+1)
else:
    print("0")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_l = 1
min_r = N

for i in range(M):
    l, r = map(int, input().split())
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    if max_l > min_r:
        print(0)
        exit()

print(min_r - max_l + 1)

Submission status: Accepted

Solution code:
argN,argM = list(map(int,input().split()))

aryL = [0] * argM
aryR = [0] * argM

for i in range(argM):
  argL,argR = list(map(int,input().split()))
  aryL[i] = argL
  aryR[i] = argR

maxL = max(aryL)
minR = min(aryR)
print(str(max(minR - maxL + 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())

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

Submission status: Accepted

Solution code:
N, M = [int(n) for n in input().split()]
left, right = 1, N

for _ in range(M):
    L, R = [int(n) for n in input().split()]
    if left < L:
        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 = -1
R = 100000
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())
dp = [0 for i in range(n+2)]
for i in range(m):
    a,b = map(int,input().split())
    if a > b:a,b = b,a
    dp[a] += 1
    dp[b+1] -= 1
ios = [0 for i in range(n+3)]
for i in range(len(dp)):
    ios[i+1] = ios[i] + dp[i]
ans = 0
for i in range(len(ios)):
    if ios[i] == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


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

l_max = 1
r_min = N

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

    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:
from fractions import *
N, M = map(int, input().split())

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

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

Submission status: Accepted

Solution code:
import math
N,M = list(map(int,input().split(" ")))
ll = -10e9
rr = (10e9)
for i in range(M):
    l,r = list(map(int, input().split(" ")))
    if l > ll:
        ll = l
    if r  <  rr :
        rr = r
ans = max(rr - ll + 1,0)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
L = 1
R = n

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

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

Submission status: Accepted

Solution code:
N,M=list(map(int,input().split()))
s=1
m=N
for i in range(M):
    a,b=list(map(int,input().split()))
    if s<a:
        s=a
    if m>b:
        m=b
    #ans &=tmp
ans=m-s+1
if ans>0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
# スペース区切りの整数の入力
LR=[]
for i in range(M):
    LR.append(list(map(int, input().split())))
left=[]
right=[N]
for i in range(M):
    left.append(LR[i][0])
    right.append(LR[i][1])
ans=min(right)-max(left)+1
if ans<0:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
l = -1
r = 1000000000
for _ in range(M):
    ll,rr = list(map(int,input().split()))
    if (ll > r) or (rr < l):
        print(0)
        exit(0)
    else:
        l = max(l,ll)
        r = min(r,rr)
print(r-l+1)

Submission status: Accepted

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

a, b = max(l), min(r)
a = b-a
print(a+1 if a >= 0 else 0)

Submission status: Accepted

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

L, R = gates[0]
for l,r in gates[1:]:
  if r < L or R < l:
    L, R = 0, 0
    break
  if L < l <= R:
    L = l
  if L <= r < R:
    R = r

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
 
N,M = map(int,input().split())

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

mae = 0
usiro = 10**6

for i in range(M):
    if LR[i][0] > mae:
        mae = LR[i][0]
    if LR[i][1] < usiro:
        usiro = LR[i][1]

if usiro - mae < 0:
    print(0)
else:
    print(usiro - mae + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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


borderL = L[0]
borderR = R[0]
for i in range(1, M) :
    if L[i] > borderL :
        borderL = L[i]
    if R[i] < borderR :
        borderR = R[i]

if borderR < borderL :
    print(0)
else :
    print(borderR - borderL + 1)

Submission status: Accepted

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

lr = [list(map(int, input().split())) for i in range(m)]
left = lr[0][0]
right = lr[0][1]

for i in range(1, m):
    left = max(lr[i][0], left)
    right = min(lr[i][1], right)

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L_max = -1
R_min = N
for _ in range(M):
	l, r = map(int, input().split())
	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:
# -*- coding: utf-8 -*-
"""
Created on Sat May 25 20:58:38 2019

@author: USER
"""

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

min_p = 1
max_p = N
for lr in LR:
    min_p = max(min_p, lr[0])
    max_p = min(max_p, lr[1])
    
print(max(0, max_p-min_p+1))

Submission status: Accepted

Solution code:
n, m = (int(_) for _ in input().split())
currs = 0
currg = 100000000
for i in range(m):
    s, g = (int(_) for _ in input().split())
    if s > currs:
        currs = s
    if g < currg:
        currg = g
if currg < currs:
    print(0)
else:
    print(currg - currs + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque

INF = 10 ** 10

N, M = [int(x) for x in stdin.readline().rstrip().split()]

LR = [list(map(int, stdin.readline().rstrip().split())) for _ in range(M)]

data = [i for i in range(N+1)]
max_L = 0
min_R = INF

for L, R in LR:
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print(len(data[max_L:min_R+1]))

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
L0, R0 = map(int, input().split())

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

Submission status: Accepted

Solution code:

N,M=[int(c) for c in input().split()]
Lmax=0
Rmin=int(1e5)
for i in range(M):
	L,R=[int(c) for c in input().split()]
	if(L>Lmax):
		Lmax=L
	if(R<Rmin):
		Rmin=R

print(max(Rmin-Lmax+1,0))
#LmaxとRminの間に入っているのがいくつなのか

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# sys.stdin.readline()
import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
id = [0]*(n+1)

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

res = 0
x = 0
for i in range(n+1):
    x += id[i]
    if x==m:
        res += 1

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
syou,dai = map(int,input().split())
for i in range(m-1):
    l,r = map(int,input().split())
    if syou < l:
        syou = l
    if r < dai:
        dai = r

if dai > n:
    dai = n
    
if syou <= dai:
    print(dai - syou + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
minMax = N
maxMin = 0
for i in range(M):
    L,R = map(int, input().split())
    if L > maxMin:
        maxMin = L
    if R < minMax:
        minMax = R

print(0) if minMax - maxMin + 1 <= 0 else print(minMax - maxMin + 1)

Submission status: Accepted

Solution code:
# ただの典型いもす

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

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

count = 0
for i in range(n):
    if arr[i] == m:
        count += 1

print(count)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max = 1
r_min = N
for _ in range(M):
  l, r = map(int, input().split())
  l_max = l if l > l_max else l_max
  r_min = r if r < r_min else r_min
if r_min - l_max >= 0:
  print(r_min - l_max + 1) 
else:
  print("0")

Submission status: Accepted

Solution code:
n,m = [int(x) for x in input().split(" ")]
lmax = 0
rmin = 1000000
for i in range(m):
    l,r = [int(x) for x in input().split(" ")]
    
    if(lmax<l):
        lmax = l
    
    if(rmin>r):
        rmin = r

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

Submission status: Accepted

Solution code:
from itertools import accumulate
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)

m = max(R)
imo = [0] * (m+2)
for l,r in zip(L,R):
    imo[l] += 1
    imo[r+1] -= 1
imo = list(accumulate(imo))

m = max(imo)
if m < M:
    print(0)
else:
    ans = 0
    for c in range(M,m+1):
        ans += imo.count(c)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(m)]
a.sort()
l = -float('inf')
r = float('inf')
for x, y in a:
    if x <= r:
        l = max(l, x)
        r = min(r, y)
    else:
        print(0)
        exit()
print(r-l+1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
max_id, min_id = N, 1
for _ in range(M):
    l, r = map(int, input().split())
    if (max_id < l) or (min_id > r):
        max_id, min_id = 0, 1
        break
    max_id = min(max_id, r)
    min_id = max(min_id, l)
print(max_id - min_id + 1)

Submission status: Accepted

Solution code:

n, m = [ int(v) for v in input().split() ]
l1, r1 = [ int(v) for v in input().split() ]

def connect(a,b,c,d):
	x = max(a,c)
	y = min(b,d)
	if y < x:
		return 0
	else:
		return (x,y)
ans2 = 1
ans = (l1, r1)
for i in range(m-1):
	l, r = [ int(v) for v in input().split() ]
	if connect(l,r,l1,r1) != 0:
		l1, r1 = connect(l,r,l1,r1)
		ans = (l1, r1)
	else:
		ans2 = 0
		break

if ans2 == 0:
	ans = 0
else:
	ans = ans[1] - ans[0] +1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(M):
    cards = input().split()
    lefts.append(int(cards[0]))
    rights.append(int(cards[1]))

lmax = max(lefts)
rmin = min(rights)

ans = rmin - lmax + 1

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

Submission status: Accepted

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

ll = 0
rr = int(10e5+1)
for _ in range(M):
    l, r = map(int, input().split())
    ll = max(ll, l)
    rr = min(rr, r)
if rr - ll + 1 <= 0:
    print(0)
else:
    print(rr - ll + 1)

Submission status: Accepted

Solution code:
N,M = (int(i) for i in input().split())
mx,mn = 0,float('inf')
for l,r in [[int(i) for i in input().split()] for i in range(M)]:
    mx = max(l,mx)
    mn = min(r,mn)
if mn-mx < 0:
    print(0)
else:
    print(mn-mx+1)

Submission status: Accepted

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

LM=0
RM=1000000

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

	if(L>=LM):
		LM=L

	if(RM>=R):
		RM=R

if (RM-LM<0):
	print(0)
else:
	print(RM-LM+1)

Submission status: Accepted

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

l = 1
r = 10**5
for s in range(M):
    if A[s][0] > l:
        l = A[s][0]
    if A[s][1] < r:
        r = A[s][1]
if r < l:
    print(0)
else:
    print(r-l+1)

Submission status: Accepted

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

[N, M] = [int(i) for i in input().split()]
LR = []
for m in range(M):
    LR.append((int(i) for i in input().split()))

max_l = 0
min_r = 100000
for m in range(M):
    (l, r) = LR[m]
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r

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

lMax = float("-inf")
rMin = float("inf")
for i in range(m):
	l, r = map(int, input().split())
	lMax = max(l, lMax)
	rMin = min(r, rMin)

if lMax > rMin:
	print(0)
else:
	print(rMin - lMax + 1)

Submission status: Accepted

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 

N, M = [ int(x) for x in input().split() ]

nm = []
for _i in range(M):
    nm.append([ int(x) for x in input().split() ])

ans = [1,N]

for _nm in nm:
    if ans[0] < _nm[0]:
        ans[0] = _nm[0]
    if ans[1] > _nm[1]:
        ans[1] = _nm[1]

print(max(ans[1] - ans[0] + 1,0))

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ans = 0
L = -100000000
R = 100000000
for i in range(m):
    l,r = map(int, input().split())
    if l > L:
        L = l
    if r < R:
        R = r

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

Submission status: Accepted

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

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

for i  in range(N+1):
    if i==0:
        data[i]=data[i]
    else:
        data[i]+=data[i-1]
print(data.count(M))

Submission status: Accepted

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

l_list = [_lr[0] for _lr in lr]
r_list = [_lr[1] for _lr in lr]

a = min(r_list)
b = max(l_list)
print(max(a - b + 1, 0))

Submission status: Accepted

Solution code:
from sys import exit
n,m = map(int,input().split())
gate=[]
rc=[0,n]
for i in range(m):
    mg=list(map(int,input().split()))
    if rc[1]<mg[0] or rc[0]>mg[1]:
        print(0)
        exit()
    rc[0]= rc[0] if rc[0]>mg[0] else mg[0]
    rc[1] = rc[1] if rc[1]<mg[1] else mg[1]
print(rc[1]-rc[0]+1)

Submission status: Accepted

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

Submission status: Accepted

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

c.reverse()
for i in range(n):
    if c[i] != 0:
        a = i
        break
for i in range(n):
    if d[i] != 0:
        b = i
        break

if b-n+a+2 < 0:
    print(0)
else:
    print(b-n+a+2)

Submission status: Accepted

Solution code:
N ,M = (int(x) for x in input().split())
L = 0
R = N
for i in range(M):
    L_in ,R_in = (int(x) for x in input().split())
    if L < L_in:
        L = L_in
    if R > R_in:
        R = R_in

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

Submission status: Accepted

Solution code:
def read():
    N, M = list(map(int, input().strip().split()))
    L = []
    R = []
    for m in range(M):
        l, r = list(map(int, input().strip().split()))
        L.append(l)
        R.append(r)
    return N, M, L, R


def solve(N, M, L, R):
    l_max = max(L)
    r_min = min(R)
    n_cards = r_min - l_max + 1
    return n_cards if n_cards > 0 else 0


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Accepted

Solution code:
n = [int(i) for i in input().split(" ")]
l = []
r = []
for j in range(n[1]):
    k = input().split(" ")
    l.append(int(k[0]))
    r.append(int(k[1]))

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

Submission status: Accepted

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

ans = k_max - k_min + 1
if (ans < 0):
    ans = 0
print(ans)

Submission status: Accepted

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

min = 0
max = 1000000000
for L, R in LR:
  if(int(L) > min):
    min = int(L)
  if(int(R) < max):
    max = int(R)
if(max - min >= 0):
  print(max - min + 1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

lr = []
for _ in range(M):
    lr.append(tuple(map(int, input().split())))

ll, rr = lr[0]
if len(lr) == 1:
    print(rr - ll + 1)
    exit(0)
for l, r in lr[1:]:
    if rr < l:
        print(0)
        exit(0)
    if ll <= l <= rr:
        ll = l

    if r < ll:
        print(0)
        exit(0)
    if ll <= r <= rr:
        rr = r

print(rr - ll + 1)

Submission status: Accepted

Solution code:
n,m= map(int, input().split())
L,R = [],[]
for i in range(m):
    li,ri = map(int, input().split())
    L.append(li)
    R.append(ri)
print(max(min(R) - max(L) + 1, 0))

Submission status: Accepted

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

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

#ゲートの数だけループ
maxL = 1
minR = 1000000
for m in range(M):
  l, r = map(int, input().split())
  maxL = max(maxL, l)
  minR = min(minR, r)
  
print(max(0, minR - (maxL - 1)))

Submission status: Accepted

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

L, R = [int(i) for i in input().strip().split(' ')]
for _ in range(M - 1):
  l, r = [int(i) for i in input().strip().split(' ')]
  L = max(L, l)
  R = min(R, r)

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

Submission status: Accepted

Solution code:
import sys
import os


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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
id_cards = set(i for i in range(N))

l_lim = 1
r_lim = N
for j in range(M):
    li, ri = map(int, input().split())
    if l_lim < li:
        l_lim = li
    if r_lim > ri:
        r_lim = ri
if r_lim >= l_lim:
    print(r_lim-l_lim + 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.append(l)
  rList.append(r)
result = min(rList) - max(lList) + 1
if result > 0:
  print(result)
else:
  print(0)

Submission status: Accepted

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

Ct = list(map(list, zip(*C)))
L = max(Ct[0])
R = min(Ct[1])
print(max(R-L+1, 0))

Submission status: Accepted

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

l = lr[0][0]
r = lr[0][1]

for i in range(1, m):
    l = max(l, lr[i][0])
    r = min(r, lr[i][1])
    if l > r:
        print(0)
        sys.exit()

print(r - l + 1)

Submission status: Accepted

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

Submission status: Accepted

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

L = [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())

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

Submission status: Accepted

Solution code:
from io import StringIO
from collections import *
from functools import *
import sys


def main(inputs):
    # n = int(next(inputs))
    # n, k = map(int, next(inputs).split())
    # xs = list(map(int, next(inputs).split()))
    n, m = map(int, next(inputs).split())
    xs = [ list(map(int, next(inputs).split())) for _ in range(m) ]
    ls, rs = zip(*xs)
    l = max(ls)
    r = min(rs)
    if l > r:
        return 0
    return r - l + 1


def gen_inputs(str_=None):
    inputs = StringIO(str_) if str_ else sys.stdin
    while True:
        a = inputs.readline().rstrip()
        yield a
        pass
    pass


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

Submission status: Accepted

Solution code:
"""
A,B=map(int, input().split())

print(B if A >= 13 else 0 if A <= 5 else int(B/2))
"""
"""
r,D,x2000=map(int, input().split())
xNext=x2000*r-D
for i in range(10):
  print(xNext)
  xNext=xNext*r-D
"""
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)

D=[0]*(N+1)
for i in range(M):
  D[L[i]-1] += 1
  D[R[i]] -= 1
S=[0]*(N+1)
S[0] = D[0]
for i in range(1,N):
  S[i] = S[i-1] + D[i]
cnt=0
for i in range(N):
  if S[i] == M: cnt += 1
print(cnt)

Submission status: Accepted

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

n_max = data[0]
m = data[1]

l_max = 0
r_min = n_max

for i in range(m):
    data = list(map(int, input().split()))
    l_max = max(data[0], l_max)
    r_min = min(data[1], r_min)

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
# import math
# import random
# import fractions
# import sys
# sys.setrecursionlimit(100000)
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = stdin.readline().rstrip().split()[0]
N, M = [int(x) for x in stdin.readline().rstrip().split()]

L_max = 0
R_min = N
for i in range(M):
  L, R = [int(x) for x in stdin.readline().rstrip().split()]
  if L > L_max:
    L_max = L
  if R < R_min:
    R_min = R

print(max(0,R_min-L_max+1))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Ls = []
Rs = []
for _ in range(M):
    L, R = map(int, input().split())
    Ls.append(L)
    Rs.append(R+1)
L_max = max(Ls)
R_min = min(Rs)
print(max(R_min - L_max, 0))