Problem:


Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000



Sample input:
4 2
1 3
2 4


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

card = [0] *N

for i in range(M):
    l, r = map(int,input().split())
    card[l-1] += 1
    if (r+1)-1 < N:
        card[r] -= 1
        
cumsum = list(itertools.accumulate(card))

print(cumsum.count(M))

Submission status: Accepted

Solution code:
import math

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

_left, _right = 1, n

for l, r in lrL:
    _left = max(_left, l)
    _right = min(_right, r)

print(max(0, _right - _left + 1))

Submission status: Accepted

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

l=-float('inf')
r=float('inf')
for i in range(m):
  L,R=[int(x) for x in input().rstrip().split()]
  if l<L:
    l=L
  if R<r:
    r=R
if l<=r:
  print(r-l+1)
else:
  print(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)


res = min(R) - max(L) + 1
if res < 0:
    print(0)
else:
    print(res)

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)

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

Submission status: Accepted

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

l = []
r = []

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


ans = rmin - lmax + 1
if (ans < 0):
  ans = 0

print(ans)

Submission status: Accepted

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

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

ans = 0
l = max(L)
r = min(R)
for i in range(N):
    if i+1 >= l and i+1 <= r:
        ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(N):
  A[i+1] += A[i]

print(A.count(M))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n,m=map(int,input().split())
    l=-1
    r=10**5+1
    for _ in range(m):
        l2,r2=map(int,input().split())
        l=max(l,l2)
        r=min(r,r2)
    print(max(0, min(r,n)-l+1))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ok = [0, pow(10, 5)]

for i in range(M):
    l, r = map(int, input().split())
    if l >= ok[0]:
        ok[0] = l
    if r <= ok [1]:
        ok[1] = r
if ok[0] > ok[1]:
    print('0')
else:
    print(ok[1] - ok[0] +1)

Submission status: Accepted

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    N,M=map(int,input().split())
    lr=[tuple(map(int,input().split())) for i in range(M)]
    imos=[0]*(N+2)
    cnt=0
    for i in lr:
        imos[i[0]]+=1
        imos[i[1]+1]-=1
    for j in range(N+1):
        imos[j+1]+=imos[j]
    for i in imos:
        if i == M:
            cnt+=1
    print(cnt)
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
# 範囲の幅を狭めていく

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

# 初期条件
left = 1
right = n

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

# leftよりrightが小さくなってしまう場合が存在
# そのときは0
print(max(right-left+1, 0))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
## Library
 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
 
## input
 
# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
 
N,M=map(int, input().split())
LR = [list(map(int,input().split())) for i in range(M)]
 
Lmin = 1
Rmax = N

for i in range(M):
    if Lmin < LR[i][0]:
        Lmin = LR[i][0]
    if LR[i][1] < Rmax:
        Rmax = LR[i][1]
    if Rmax < Lmin:
        print(0)
        sys.exit()

print(Rmax-Lmin+1)

Submission status: Accepted

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

print(max(min(Y)-max(X)+1,0))

Submission status: Accepted

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

left = 1
right = n

for i in range(m):
    a, b = map(int, input().split())
    if a > left:
        left = a
    if b < right:
        right = b
    
    if left > right:
        print(0)
        exit()

print(right-left+1)

Submission status: Accepted

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

in_min = 1
in_max = N

for i in range(M):
	l, r = map(int, input().split())
	# 最小値(今の最小値と取得値のうち、大きいほう)
	in_min = max([in_min, l])
	# 最大値(今の最大値と取得値のうち、小さいほう)
	in_max = min([in_max, r])

# 個数を出力
if(in_max < in_min):
	# 最小値が大きくなった場合は、通らない
	print(0)
else:
	cnt = in_max - in_min +1
	print(cnt)

Submission status: Accepted

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

lmax = 0
rmin = int(1e10)

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

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

Submission status: Accepted

Solution code:
def solve(lx, rx):
    diff = min(rx) - max(lx)
    ans = diff+1 if diff >= 0 else 0
    return ans


if __name__ == '__main__':
    n, m = map(int, input().split())
    lx = []
    rx = []

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

    print(solve(lx, rx))

Submission status: Accepted

Solution code:
N, M, *_LR = map(int, open(0).read().split())
LR = list(zip(*[map(int, iter(_LR))]*2))

L_max = 1
R_min = N

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

card_num = R_min - L_max + 1

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lmax = 0
rmin = 1000000
for _ 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 = list(map(int,input().split()))
L = []
R = []
for i in range(M):
    in1, in2 = list(map(int,input().split()))
    L.append(in1)
    R.append(in2)
ans = max(0, min(R) - max(L) + 1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, m = map(int,input().split()) 
    startlis = []
    endlis = []

    for i in range(m):
        a, b = map(int, input().split())
        startlis.append(a)
        endlis.append(b)
    
    start = max(startlis)
    end = min(endlis)

    print(max(0,end - start + 1))





    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    l_max = 0
    r_min = float('inf')
    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 + 1 - l_max, 0))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print (r[1] - r[0] + 1) if r else 0

Submission status: Accepted

Solution code:
n,m = map(int, raw_input().split(' '))
def intersection(A,B):
	a,b  = max(A[0],B[0]), min(A[1],B[1])
	return [a,b] if a <= b else []

r = [1,n+1]
for _ in range(m):
	a,b = map(int, raw_input().split(' '))
	r = intersection(r, [a,b])
	if len(r) == 0: break
print (r[1] - r[0] + 1) if r else 0

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l,r=1,pow(10,5)
for _ in range(m):
  l_,r_=map(int,input().split())
  l=max(l,l_)
  r=min(r,r_)
print(max(0,r-l+1))

Submission status: Accepted

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

left=1
right=n

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

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

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N, M = map(int, input().split())
    card = [0 for _ in range(N + 1)]

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

    for i in range(N):
        card[i + 1] += card[i]

    # print(card)
    print(card.count(M))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(l,r):
  l_max = max(l)
  r_min = min(r)
  return max(r_min - l_max + 1, 0)
  
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(solve(L,R))

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
l = 0
r = inf
for _ in range(m):
  a, b = rm()
  l = max(a, l)
  r = min(b, r)
c = r-l+1
if c >= 1:
  print(c)
else:
  print(0)

Submission status: Accepted

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

# In[7]:


N,M = map(int, input().split())
lr = []
l_max = 1
r_min = N
for _ in range(M):
    lr.append(list(map(int, input().split())))
    l_max = max(l_max, lr[-1][0])
    r_min = min(r_min, lr[-1][1])

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


# In[ ]:

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = []
for i in range(M):
    L, R = map(int, input().split())
    if i == 0:
        Lmax = L
        Rmin = R
    else:
        Lmax = max(L, Lmax)
        Rmin = min(R, Rmin)

# L = set(A[0])
# for i in range(1, M):
#     L = L & set(A[i])

if Rmin-Lmax < 0:
    print(0)
else:
    print(Rmin-Lmax+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
maxL = 0
minR = 10 ** 9
for i 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:
def resolve():
    N, M = list(map(int, input().split()))
    interval = []
    l, r = 0, 10**5
    for i in range(M):
        L, R = list(map(int, input().split()))
        l = max(l, L)
        r = min(r, R)

    interval = set(list(range(l, r + 1)))
    interval = interval & set(range(1, N + 1))

    print(len(interval))

    return


resolve()

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)
if minR - maxL >= 0:
    print(minR - maxL + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
MIN = 0
MAX = 110000
        
for i in range(M):
    L, R = map(int, input().split())
    MIN = max(MIN, L)
    MAX = min(MAX, R)
    #print(MIN, MAX)

ans = MAX-MIN+1 if MAX >= MIN else 0
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

left = 1
right = n

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]


def solve():
    n, m = [int(x) for x in input().split()]
    a, b = 0, 1e9
    for _ in range(m):
        l, r = [int(x) for x in input().split()]
        a = max(a, l)
        b = min(b, r)
    print(max(0, b - a + 1))
        
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()



"""

1 4
7
RRRR
1 1 0 1
----
4

1 4
47
RCRR
1 1 0 1
----
-1

2 2
10
RR
RR
1 7
4 1
----
4

2 2
10
RR
RR
7 4
7 4
----
-1

3 3
8
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


3 3
1
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


"""

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
card =[list(map(int,input().split())) for i in range(m)]
r = card[0][0]
R = card[0][1]
for i in range(m):
    if  r < card[i][0]:
        r = card[i][0]
for i in range(m):
    if card[i][1] < R:
        R = card[i][1]
ans = max(0,R - r + 1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N,M = map(int, input().split())
    l=-100000
    r=1000000
    for i in range(M):
        L,R =map(int,input().split())
        if l<L:
            l=L
        if R<r:
            r=R
    a = r-l+1
    if a<0:
        a=0
    print(a)
main()

Submission status: Accepted

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

#n,m = 5,3
#a = [[3,6],[5,7],[6,9]]

L = a[0][0]
R = a[0][1]

for i in range(1,m):
  L = max(L,a[i][0])
  R = min(R,a[i][1])

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

Submission status: Accepted

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

left = 0
right = float("inf")
for i in range(M):
    L, R = map(int, input().split())
    left = max(left, L)
    right = min(right, R)

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

Submission status: Accepted

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

minG=1
maxG=N
for i in range(M):
    L,R=map(int,input().split())
    if L>minG:
        minG=L
    if R<maxG:
        maxG=R

if minG<=maxG:
    print(maxG-minG+1)
else:
    print(0)

Submission status: Accepted

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

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(r_min-l_max+1,0))

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,m=n1()
LR=n3(m)

m=0
M=10**10
for l,r in LR:
    m=max(m,l)
    M=min(M,r)

p0(M>=m,M-m+1,0)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,m=n1()
LR=n3(m)

m=0
M=10**10
for l,r in LR:
    m=max(m,l)
    M=min(M,r)
ans=M-m

p0(M>=m,M-m+1,0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter
import itertools
import fractions
import datetime
from decimal import Decimal
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

N,M=MI()
LR=LLI(M)

maxL=0
minR=11111111111111111

for lr in LR:
    maxL=max(maxL, lr[0])
    minR=min(minR, lr[1])    
            
print(max(minR-maxL+1, 0))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, M = map(int, input().split())
    L_max, R_min = 0, 10**100
    for _ in range(M):
        l, r = map(int, input().split())
        L_max = max(L_max, l)
        R_min = min(R_min, r)
    delta = R_min - L_max + 1
    if delta <= 0:
        delta = 0
    print(delta)
main()

Submission status: Accepted

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

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

print(max(mx-mn+1,0))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
# memo[N+1]はあとで除外する。
memo = [0]*(N+1)

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

memo_sum = memo[0]
for i in range(1, N):
    memo_sum += memo[i]
    memo[i] = memo_sum

ans = 0
for a in memo:
    if a == M:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
apple = []
for _ in range(m):
    apple.append(list(map(int, input().split())))
cat = []
dog = []
for i in apple:
    cat.append(i[0])
    dog.append(i[1])

if min(dog) - max(cat) >= 0:
    print(min(dog) - max(cat) + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
#C
from collections import deque
n,m = map(int,input().split())
gate = deque()
for _ in range(m):
    l,r = map(int,input().split())
    if len(gate) == 0:
        gate.append([l,r])
        #print(gate)
    else:
        check = gate.pop()
        #print(gate)
        if check[0] <= l:
            check[0] = l
            #print(check)
        if check[1] >= r:
            check[1] = r
            #print(check)
        gate.append(check)
     
result = gate.pop()
if result[1] < result[0]:
    print(0)
else:
    print(result[1]-result[0]+1)

Submission status: Accepted

Solution code:
import numpy as np
def solve():
    N,M = map(int, input().split())
    LR = np.array([list(map(int, input().split())) for _ in range(M)])
    Lmax = np.max(LR[:,0])
    Rmin = np.min(LR[:,1])
    ans = Rmin - Lmax + 1 if Rmin - Lmax >=0 else 0
    print(ans)





if __name__ == '__main__':
    solve()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = [], []
for _ 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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
s = [0]*(N+1)
for i in range(M):
  l,r = list(map(int,input().split()))
  s[l] += 1
  if r+1 != N+1:
    s[r+1] -= 1
ruiseki = [0]*(N+1)
cnt = 0
for i in range(1,N+1):
  ruiseki[i] += ruiseki[i-1] + s[i]
  if ruiseki[i] == M:
    cnt +=1
print(cnt)

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

Submission status: Accepted

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

MIN = 0
MAX = float('inf')

for i in range(M):
    MIN = max(MIN, int(gate[i][0]))
    MAX = min(MAX, int(gate[i][1]))

print(max(MAX - MIN + 1, 0))

Submission status: Accepted

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

c_min = 1
c_max = 10 ** 5
for i in range(M):
    LM = list(map(int, input().split()))
    c_min = max(c_min, LM[0])
    c_max = min(c_max, LM[1])

if c_min > c_max:
    print(0)
else:
    print(c_max - c_min + 1)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ls = [i for i in range(m)]
rs = [i for i in range(m)]
cnt = 0
for _ in range(m):
    ls[_],rs[_] = map(int,input().split())

maxl = max(ls)
minr = min(rs)

for nn in range(1,n+1):
    if maxl <= nn and minr >= nn:
        cnt += 1
print(cnt)

Submission status: Accepted

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

max_L = []
min_R = []

cnt = 0
for i in range(m):
    L,R = map(int,input().split())
    max_L.append(L)
    min_R.append(R)
if max(max_L) <= min(min_R):
    print(min(min_R)-max(max_L)+1)
else:
    print(0)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Lmax = 0
Rmin = 10**5 + 1
for n in range(M):
  L, R = map(int, input().split())
  if L > Lmax:
    Lmax = L
  if R < Rmin:
    Rmin = R

gate = Rmin - Lmax + 1
if gate < 0:
  print("0")
else:
  print(gate)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[0]*(10**5+10)
for _ in range(M):
    l,r=map(int,input().split())
    A[l]+=1
    A[r+1]-=1
for i in range(10**5+9):
    A[i+1]+=A[i]
print(A.count(M))

Submission status: Accepted

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

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

left = -1
right = n + 1

for _ in range(m):
    l, r = map(int, input().split())
    if r < left or l > right:
        print(0)
        sys.exit()
    left = max(left, l)
    right = min(right, r)

print(right - left + 1)

Submission status: Accepted

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

for _ in range(M):
  L, R = map(int, input().split())
  ansL = max(ansL, L)
  ansR = min(ansR, R)

print(max(0, ansR - ansL + 1))

Submission status: Accepted

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

l_max=0
r_min=n

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

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

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    l = [list(map(int, input().split())) for i in range(m)]
    lt = [list(x) for x in zip(*l)]
    if min(lt[1]) - max(lt[0]) >= 0:
        print(min(lt[1]) - max(lt[0]) + 1)
    else:
        print(0)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = 0
b = n
for i in range(m):
    l,m = map(int,input().split())
    a = max(a,l)
    b = min(b,m)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
import numpy as np

n, m = map(int, input().split())
data = [0] * m

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


data = np.array(data)
ll = data[:, 0]
rr = data[:, 1]

ans = np.min(rr) - max(ll) + 1
ans = max(0, ans)
print(ans)

'''
n, m = map(int, input().split())
tar = [0] * (1 + 10 ** 5)
tar = np.array(tar)

for i in range(m):
    l, r = map(int, input().split())
    tar[l:r + 1] += 1
    #print(tar[:10])

#tar = tar.tolist()
# print(tar[0:5])
count = Counter(tar)
ans = count[m]
print(ans)
#-----------------------------
n, m = map(int, input().split())

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

    #lについて
    if (l <= ll):
      pass
    elif (ll < l )

ans = max(ll - rr, 0)
print(ans)

'''