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_L,r_L = [],[]

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

    l_L.append(l)
    r_L.append(r)

r = min(r_L)
l = max(l_L)

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

Submission status: Accepted

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

for llrr in lr:
  min2 = max(min2, llrr[0])
  max2 = min(max2, llrr[1])

if max2-min2 >= 0:
  print(max2-min2+1)
else:
  print(0)

Submission status: Accepted

Solution code:
import numpy as np
[n,m]=[int(x) for x in input().split()]
LR_T=np.array([[int(x)-1 for x in input().split()] for _ in range(m)]).T

print(max(np.min(LR_T[1])-np.max(LR_T[0])+1,0))

Submission status: Accepted

Solution code:
import sys

N, M = map(int,sys.stdin.readline().split())
L = 1
R = N

for _ in range(M):
  l, r = map(int, sys.stdin.readline().split())

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
min_n = 0
max_n = 10**5

for i in range(M):
    min_n = max(min_n, LR[i][0])
    max_n = min(max_n, LR[i][1])

print(max(max_n-min_n+1, 0))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    LR = open(0).read().split()[2:]
    max_L = max(map(int, LR[::2]))
    min_M = min(map(int, LR[1::2]))
    print(min_M - max_L + 1 if max_L <= min_M else 0)
    return


main()

Submission status: Accepted

Solution code:
def main():
    LRm = open(0).read().split()[2:]
    max_L = max(map(int, LRm[::2]))
    min_M = min(map(int, LRm[1::2]))
    print(min_M - max_L + 1 if max_L <= min_M else 0)
    return


main()

Submission status: Accepted

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

L = []
R = []

for i in range(M):
    inp = input().split()
    L.append(int(inp[0]))
    R.append(int(inp[1]))
    
if max(L) <= min(R):
    print(min(R)-max(L)+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n,m = [int(x) for x in stdin.readline().rstrip().split()]

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


max_n = 10**7
min_n = -1*10**7

for i in range(m):
    if li[i][0] > max_n or li[i][1] < min_n:
        print(0)
        exit()
    else:
        max_n = min(max_n,li[i][1])
        min_n = max(min_n,li[i][0])

print(max_n - min_n + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L,R = map(int,input().split())
for i in range(M-1):
  L_ans,R_ans = map(int,input().split())
  if L < L_ans:
    L = L_ans
  if R > R_ans:
    R = R_ans
print(len(list(range(L,R+1))))

Submission status: Accepted

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

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([min(r) - max(l) + 1, 0][min(r) - max(l) < 0])

Submission status: Accepted

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

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


ans=min(r)-max(l)+1
if ans < 0:
    ans=0

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
n,m = map(int,input().split())
right = n
left = 0

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[-1]*M
R=[-1]*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:
N,M=map(int,input().split())
L=0
R=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:
import heapq
n, m = map(int,input().split())
l = []
r = []
for i in range(m):
    line = list(map(int,input().split()))
    l.append(-line[0])
    r.append(line[1])
heapq.heapify(l)
heapq.heapify(r)
l_max = - heapq.heappop(l)
r_min = heapq.heappop(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())
max_l = 0
min_r = 100000
for i in range(m):
  l,r = map(int,input().split())
  max_l = max(l, max_l)
  min_r = min(r, min_r)
print(max(0, min_r-max_l+1))

Submission status: Accepted

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

left = []
right = []
for i in range(m):
    a, b = map(int, input().split())
    left.append(a)
    right.append(b)

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

Submission status: Accepted

Solution code:
n, m  = (int(x) for x in input().split())
max_l = 0
min_r = 10000000
for i in range(0, m):
  a, b = (int(x) for x in input().split())
  if(max_l < a):
    max_l = a
  if(b < min_r):
    min_r = b

ret = min_r - max_l + 1
if ret < 0:
  ret = 0

print(ret)

Submission status: Accepted

Solution code:
N , M = map(int ,input().split())
left = 1
right = N
for i in range(M):
    tmp_left , tmp_right = map(int ,input().split())
    if left < tmp_left:
        left = tmp_left
    if right > tmp_right:
        right = tmp_right
ans = right - left + 1
if ans < 0:
    print("0")
else:
    print(right - left + 1)

Submission status: Accepted

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

# Lの最大値以上、Rの最小値以下の整数の個数が答え。負になるときは0を出力
print(max(min(R)-max(L)+1,0))

Submission status: Accepted

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

for i in range(m):
	l=listg[i][0]
	r=listg[i][1]
#	print(l)
#	print(r)
	if l>l_max:
		l_max=l
	if r<r_min:
		r_min=r	

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

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):
    l,r=map(int,input().split())
    if l>L:
        L=l

    if r<R:
        R=r

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l_max = 0
r_min = 10**5 + 1
for i in range(m):
  l, r = map(int, input().split())
  l_max = max(l_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())
max_l = 0
min_r = 100000
for i in range(m):
  l,r = map(int,input().split())
  max_l = max(l, max_l)
  min_r = min(r, min_r)
print(max(0, min_r-max_l+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

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

l = []
r = []
for i in range(m):
    l.append(lr[i][0])
    r.append(lr[i][1])
# print(l)
# print(r)
l_max = max(l)
r_min = min(r)
if r_min>=l_max:
    ans = r_min - l_max + 1
else:
    ans = 0
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
List_LR = [list(map(int,input().split())) for i in range(M)]
L = [List_LR[i][0] for i in range(len(List_LR))]
R = [List_LR[i][1] for i in range(len(List_LR))]
 
L_max = max(L)
R_min = min(R)

ans = R_min - L_max +1
if ans < 0 : ans = 0
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = {li[i][0] for i in range(len(li)) }
R = {li[i][1] for i in range(len(li)) }


mL = max(L)
mR = min(R)

ans = mR - mL + 1
if ans <= 0: ans = 0
print(ans)

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

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
a,b=1,N
for i in range(M):
    L,R=map(int,input().split())
    a,b=max(a,L),min(b,R)
print(max(b-a+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)
if max(L) > min(R):
  print(0)
else:
  print(min(R) - max(L) + 1)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

A=list(accumulate(A))

ans=0
for a in A:
  if a==M:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
cards = [1 for _ in range(n)]
L = 0
R = n-1
for _ in range(m):
    l,r= map(lambda x: int(x) - 1,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())
lst=[[] for _ in range(M)]
dif=10**19
v1,v2=map(int,input().split())
for i in range(M-1):
  v_1,v_2=map(int,input().split())
  v1,v2=max(v1,v_1),min(v2,v_2)
if v2-v1+1<=0:
  print(0)
else:
  print(v2-v1+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_)
Lmax = 0
Rmin = 10**6
for i in range(M):
    Lmax = max(Lmax,L[i])
    Rmin = min(Rmin,R[i])
if Rmin-Lmax < 0:
    print(0)
    exit()
print(Rmin-Lmax+1)

Submission status: Accepted

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from heapq import *
from itertools import *
from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,M = map(int, input().split())
    left = 0
    right = N-1
    for _ in range(M):
        L,R = map(int, input().split())
        left = max(left,L-1)
        right = min(right,R-1)
        if right < left:
            print(0)
            return 0
    print(right-left+1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L_max = 0
R_min = N
for i in range(0,M):
  L_tmp, R_tmp = map(int,input().split())
  if L_max < L_tmp:
    L_max = L_tmp
  if R_min > R_tmp:
    R_min = R_tmp
  
print(R_min - L_max + 1) if R_min >= L_max else print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(m)]
start = 0
end = 9999999999
for i in range(m):
    if l[i][0] > start:
        start = l[i][0]
    if l[i][1] < end:
        end = l[i][1]
print(max(end-start + 1, 0))

Submission status: Accepted

Solution code:
n,m = (int(x) for x in input().split())
l,r = (int(x) for x in input().split())

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4 2
1 3
2 4"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """10 3
3 6
5 7
6 9"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """100000 1
1 100000"""
        output = """100000"""
        self.assertIO(input, output)
def resolve():
    (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)
    lm=max(ll)
    rm=min(rr)
    if lm>rm:
        print(0)
        return
    if N>=rm:
        print(1+rm-lm)
    elif N>=lm:
        print(1+N-lm)
    else:
        print(0)
if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
LL=[]
RR=[]
N,M=map(int,input().split())
for i in range(M):
    L,R=map(int,input().split())
    LL.append(L)
    RR.append(R)
    
L1=max(LL)
R1=min(RR)
if L1>R1:
    print(0)
else:
    print(R1-L1+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())

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

Submission status: Accepted

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

prev_l, prev_r = 1, 100000
for _ in range(m):
    l, r = map(int, input().split()) 
    prev_l, prev_r = max(l, prev_l), min(r, prev_r)

if prev_r < prev_l:
    print(0)
else:
    print(prev_r - prev_l + 1)

Submission status: Accepted

Solution code:
N,M= list(map(int, input().split()))
li=[list(map(int,input().split())) for _ in range(M)]
mi=0
ma=N
for i in li:
    mi=max(mi,i[0])
    ma=min(ma,i[1])
if ma>=mi:
    print(ma-mi+1)
else:
    print(0)

Submission status: Accepted

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


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


if __name__ == '__main__':
    main()

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)
Lm=max(L)
Rm=min(R)
print(Rm-Lm+1 if Rm-Lm>=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())
ans = 0
A = [0] * N
L_max = max(L)
R_min = min(R)
if R_min >= L_max:
    ans = R_min - L_max + 1
else:
    ans = 0
print(ans)

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

Submission status: Accepted

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

cs = [0] * N
for _ in range(M):
    L, R = map(int, input().split())
    cs[L - 1] += 1
    if R != N:
        cs[R] -= 1

for i in range(1, N):
    cs[i] += cs[i - 1]

print(cs.count(M))

Submission status: Accepted

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

cs = [0] * N
for _ in range(M):
  L, R = map(int, input().split())
  cs[L - 1] += 1
  if R != N:
    cs[R] -= 1

for i in range(1, N):
  cs[i] += cs[i - 1]

print(sum(1 for i in range(N) if cs[i] == M))

Submission status: Accepted

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

L_largest = 0
R_min = N
for m in range(M):
    L, R = map(int, input().split())
    if L >= L_largest:
        L_largest = L
    if R <= R_min:
        R_min = R

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ls = [0] * (n+2)
    for i in range(m):
        l, r = list(map(int, input().rstrip('\n').split()))
        ls[l] += 1
        ls[r+1] -= 1
    t = 0
    cnt = 0
    for i in range(len(ls)):
        t += ls[i]
        if t == m:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
N,M,*D = map(int, open(0).read().split())
print(max(min(D[1::2])-max(D[::2])+1, 0))

Submission status: Accepted

Solution code:
import sys

N,M = map(int, input().split())
D = sys.stdin
l = 0
r = N
for d in D:
    L,R = map(int, d.split())
    if L > l:
        l = L
    if R < r:
        r = R
print(max(r-l+1,0))

Submission status: Accepted

Solution code:
import sys
N,M = map(int, input().split())
D = sys.stdin
L = []
R = []
for d in D:
    l,r = map(int, d.split())
    L.append(l)
    R.append(r)
print(max(min(R)-max(L)+1,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
l = 0
r = N
D = [map(int, input().split()) for _ in range(M)]
for d in D:
    L,R = d
    if L > l:
        l = L
    if R < r:
        r = R
print(max(r-l+1,0))

Submission status: Accepted

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

Submission status: Accepted

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

left = 0
right = N
for l, r in zip(L, R):
    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())
G=[[] for _ in range(M)]
for i in range (M):
  L,R=map(int,input().split())
  G[i].append([L,R])
small=[]
for i in range(M):
  small.append(G[i][0][0])
s=max(small)
large=[]
for i in range(M):
  large.append(G[i][0][1])
l=min(large)
if s<=l:
  ans=l-s+1
else:
    ans=0
print(ans)

Submission status: Accepted

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

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

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

    answer = A[0]
    for i in range(1, M):
        if max(answer) < min(A[i]) or max(A[i]) < min(answer):
            print(0)
            exit()

        total = []
        total.extend(answer)
        total.extend(A[i])
        s_total = sorted(total)
        answer = s_total[1:3]

    print(answer[1] - answer[0] + 1)




if __name__ == '__main__':
    main()

Submission status: Accepted

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

ll = 1
rr = 100000

for i in range(M):
    l_next, r_next = map(int, input().split())
    if l_next >= ll:
        ll = l_next
    if r_next <= rr:
        rr = r_next

print(max(rr - ll + 1, 0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
left_max = -1
right_min = 10**10
for i in range(m):
  l, r = list(map(int, input().split()))
  left_max = max([left_max, l])
  right_min = min([right_min, r])
print(len([i for i in range(1, n+1) if left_max <= i and i <= right_min]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = 0
r = n
for _ 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 = map(int, input().split())

ans = 0
L, R = 0, 114514

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

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

Submission status: Accepted

Solution code:
C,G=map(int,input().split())
Lmax=0
Rmin=C+1
for i in range(G):
    L,R=map(int,input().split())
    if L > Lmax :
        Lmax = L
    else:
        pass
    
    if R < Rmin :
        Rmin = R
    else:
        pass
ans=Rmin-Lmax+1
if ans >= 0 :
    print(ans)
else :
    print(0)

Submission status: Accepted

Solution code:
#N枚のIDカードとM個のゲートがある
n,m = map(int, input().split())
L = [0]*m
R = [0]*m
#答えとなる変数の定義
ans = 0
#インフィニティ
infi = 1000000000000000
#Lの最大値とRの最小値を保管する関数
l_max =0
r_min =infi
#入力
for i in range(m):
    L[i],R[i] = map(int, input().split())
    if(l_max<L[i]):
        l_max = L[i]
    if(r_min>R[i]):
        r_min = R[i]

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

Submission status: Accepted

Solution code:
#C
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()
R.sort()
if R[0]>=L[m-1]:
    print(R[0]-L[m-1]+1)
else:
    print(0)

Submission status: Accepted

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

l = 0
r = 100001

for i in range(0,m):
    l = max(g[i][0],l)
    r = min(g[i][1],r)
    if l > r:
        break

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

Submission status: Accepted

Solution code:
N, M =map(int,input().split())
LR = [list(map(int,input().split())) for i in range(M)]
ansMIN = 1
ansMAX = N
for i in range(M):
    ansMIN = max(ansMIN,LR[i][0])
    ansMAX = min(ansMAX,LR[i][1])
print(max(0,ansMAX-ansMIN+1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(m-1):
  l, r = map(int, input().split())
  if mini < l:
    mini = l
  if maxi > r:
    maxi = r
    
if maxi-mini+1 < 0:
  print(0)
else:
  print(maxi-mini+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())
    if Li > L:
        L = Li
    if Ri < R:
        R = Ri

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

Submission status: Accepted

Solution code:
n,m= map(int,input().split())
maxl = 0
minr = n
for i in range(m):
    templ,tempr = map(int,input().split())
    maxl = max(maxl,templ)
    minr = min(minr,tempr)
if minr - maxl >= 0:
    print(minr - maxl + 1)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

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

l_M = 0
r_m = 10**5 +3


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

Submission status: Accepted

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

max_left = 1
min_right = n

for i in range(m):
    left, right = map(int, input().split())
    max_left = max(max_left, left)
    min_right = min(min_right, right)

print(max(min_right - max_left + 1, 0))

Submission status: Accepted

Solution code:
n,m=list(map(int, input().split() ))
Firstlis=[]
Secondlis=[]
for i in range(m):
  s=list(map(int,input().split()))
  S1=s[0]
  S2=s[1]
  Firstlis.append(S1)
  Secondlis.append(S2)
Min=max(Firstlis)
Max=min(Secondlis)
if Max>=Min:
    print(Max-Min+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
maxl=1
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_list = [0]*m
R_list = [0]*m
for i in range(m):
    L, R = map(int, input().split())
    L_list[i] = L
    R_list[i] = R
LR_intersection = min(R_list) - max(L_list)
if LR_intersection >= 0:
    print(LR_intersection+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n, M, *X = map(int, open(0).read().split())
print(max(0, min(X[1::2]) - max(X[::2]) + 1))

Submission status: Accepted

Solution code:

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

current_L = 1
current_R = N
for L,R in LRs:
    if current_L < L:
        current_L = L
    if current_R > R:
        current_R = R


if current_R < current_L:
    print(0)
else:
	print(current_R-current_L+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n,m = MAP()
l,r = [], []
for i in range(m):
    a,b = MAP()
    l.append(a)
    r.append(b)

max_l = max(l)
min_r = min(r)
if min_r<max_l:
    print(0)
    exit()
print(min_r-max_l+1)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    lr = [list(map(int, input().split())) for i in range(m)]
    l = 0
    r = 10 ** 5 + 1
    for i in range(m):
        if lr[i][0] > l:
            l = lr[i][0]

        if lr[i][1] < r:
            r = lr[i][1]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
min_c = 1
max_c = n
for i in range(m):
    l, r = map(int, input().split())
    if l > min_c:
        min_c = l
    if r < max_c:
        max_c = r
if max_c >= min_c:
    print(max_c-min_c+1)
else:
    print(0)

Submission status: Accepted

Solution code:
#coding utf-8
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(L)
r=min(R)
ans=r-l+1
if ans<0:
  print(0)
else:
  print(ans)

Submission status: Accepted

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