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

max_l = -1*float('INF')
min_r = float('INF')
for i in range(m):
    max_l = max(max_l,l[i][0])
    min_r = min(min_r,l[i][1])

ans = min_r - max_l + 1

if(ans < 0):
    print(0)
    exit()

if(ans <= n):
    print(ans)
else:
    print(n)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,M = LI()
left = 0
right = 10000000

for _ in range(M):
    l,r = LI()
    left = max(left,l)
    right = min(right,r)

count = 0
for i in range(1,N+1):
    if left <= i <= right:
        count += 1
print(count)

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

    if(r < L or R < l):
        print(0)
        exit()
    else:
        L = max(l,L)
        R = min(r,R)

print(R-L+1)

Submission status: Accepted

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

ans_1=max(l_list)
ans_2=min(r_list)
print(max(ans_2-ans_1+1,0))

Submission status: Accepted

Solution code:

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

MAX = N + 2
array = [0] * (MAX)

for l, r in X:
    array[l] += 1
    array[r + 1] -= 1

for i in range(MAX - 1):
    array[i + 1] += array[i]

print(sum(v == M for v in array))

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
ansl = 1
ansr = N
for _ in range(M):
    l, r = map(int, sys.stdin.readline().split())
    ansl = max(ansl, l)
    ansr = min(ansr, r)
ans = max(0, ansr - ansl + 1)
print(ans)

Submission status: Accepted

Solution code:
# imos法
n, m = map(int, input().split())
l = []
r = []
imos = [0]*(n+1)
for i in range(m):
    L, R = map(int, input().split())
    l.append(L)
    r.append(R)

for i in range(m):
    imos[l[i]-1] += 1
    imos[r[i]] -= 1

cum_sum = [imos[0]]
for i in range(1, len(imos)):
    cum_sum.append(cum_sum[i - 1] + imos[i])

ans = 0
for count in cum_sum:
    if count == m: ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n, m = MI()
    l, r = MI()
    for _ in range(m-1):
        li, ri = MI()
        l = max(l, li)
        r = min(r, ri)
    ans = max(0, r-l+1)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

left = 1
right = n+1

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

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

Submission status: Accepted

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

from itertools import accumulate
ac = list(accumulate(cards))

ans = 0
for a in ac:
    if a == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m,*a=map(int,open(0).read().split())
b=min(a[1::2])-max(a[::2])+1
print(b if b>0 else 0)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# abc127_c.py
# https://atcoder.jp/contests/abc127/tasks/abc127_c

# C - Prison /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# N枚の ID カードと M個のゲートがあります。
# i番目のゲートは Li,Li+1,...,Ri 番目の ID カードのうちどれか 1枚を持っていれば通過できます。
# 1枚だけで全てのゲートを通過できる ID カードは何枚あるでしょうか。

# 制約
#     入力は全て整数である。
#     1≤N≤105
#     1≤M≤105
#     1≤Li≤Ri≤N

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N M
# L1 R1
# L2 R2
# ⋮
# LM RM

# 出力
# 1枚だけで全てのゲートを通過できる ID カードの枚数を出力せよ。

# 入力例 1
# 4 2
# 1 3
# 2 4

# 出力例 1
# 2

# 以下のように、1
# 枚だけで全てのゲートを通過できる ID カードは 2枚です。
#     1番目の ID カードでは 2番目のゲートを通過できません。
#     2番目の ID カードでは全てのゲートを通過できます。
#     3番目の ID カードでは全てのゲートを通過できます。
#     4番目の ID カードでは 1番目のゲートを通過できません。

# 入力例 2
# 10 3
# 3 6
# 5 7
# 6 9

# 出力例 2
# 1

# 入力例 3
# 100000 1
# 1 100000

# 出力例 3
# 100000


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    # N = int(lines[0])
    N, M = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    valueses = list()
    for i in range(M):
        valueses.append(list(map(int, lines[i+1].split())))

    ll = None
    rr = None

    log(f'valueses=[{valueses}]')

    for i in range(M):
        l = valueses[i][0]
        r = valueses[i][1]
        if ll is None:
            ll = l
        elif ll < l:
            ll = l
        else:
            log(f'll=[{l}], l=[{l}]')
        if rr is None:
            rr = r
        elif rr > r:
            rr = r
        else:
            log(f'rr=[{rr}], r=[{r}]')

    log(f'l=[{l}]')
    log(f'r=[{r}]')

    return [max(rr-ll+1, 0)]


# 引数を取得
def get_input_lines():
    line = input()
    N, M = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(M):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4 2', '1 3', '2 4']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['10 3','3 6', '5 7', '6 9']
        lines_export = [1]
    if pattern == 3:
        lines_input = ['100000 1', '1 100000']
        lines_export = [100000]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
a = []
b = []
for i in range(M):
    L, R = map(int,input().split())
    a.append(L)
    b.append(R)
a = sorted(a)
b = sorted(b)
A = a[M - 1]
B = b[0]
if A <= B:
    print(B - A + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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=[]
r=[]
for j in range(m):
    templ,tempr=map(int,input().split())
    l.append(templ)
    r.append(tempr)
ans=min(r)-max(l)+1
if ans<0:
    ans=0
print(ans)

Submission status: Accepted

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

for _ in range(M):
    L, R = map(int, input().split())
    lefts.append(L)
    rights.append(R)

lefts.sort()
rights.sort()

if lefts[-1] > rights[0]:
    print(0)
else:
    print(rights[0] - lefts[-1] + 1)

Submission status: Accepted

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

min_l = 0
max_l = 10**6

for _ in range(m):
    L, R = map(int, input().split())
    min_l = max(min_l, L)
    max_l = min(max_l, R)

print(max_l - min_l + 1 if max_l - min_l + 1 > 0 else 0)

Submission status: Accepted

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

min = lst[0][0]
max = lst[0][1]
for v in lst:
    if v[0] > min:
        min = v[0]
    if v[1] < max:
        max = v[1]
if min <= max:
    print(max-min + 1)
elif min > max:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ls = []
rs = []
for _ in range(m):
  l,r = map(int, input().split())
  ls.append(l)
  rs.append(r)
  
ans = min(rs) - max(ls) + 1

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

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split()]
c = [1, N]
for _ in range(M):
    a,b = [int(i) for i in input().split()]
    a,b = min(a,b), max(a,b)
    if c[0] <= a <= b <= c[1]:
        c = [a,b]
    elif a < c[0] <= b < c[1]:
        c = [c[0], b]
    elif c[0] < a <= c[1] < b:
        c = [a, c[1]]
    elif a <= b < c[0]:
        print(0)
        break
    elif c[1] < a <= b:
        print(0)
        break
else:
    print(c[1] - c[0] + 1)

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
LR = []
for i in range(M):
	LR.append(rl())
cnts = [0]*(N+1)
for l, r in LR:
	cnts[l-1] += 1
	cnts[r] -= 1
ans = 0
s = 0
for i in range(N):
	s += cnts[i]
	if s == M:
		ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

N,M=input2()
L,R=[],[]
for i in range(M):
	l,r=input2()
	L.append(l)
	R.append(r)

MIN=0
MAX=N

for j in range(M):
	if L[j]>MIN:
		MIN=L[j]
	if R[j]<MAX:
		MAX=R[j]

if MAX<MIN:
	print(0)
else:
	print((MAX-MIN)+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
right = N
left = 1
for _ in range(M):
  a,b = map(int,input().split())
  right = min(right,b) 
  left = max(left,a)

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

Submission status: Accepted

Solution code:
def readinput():
    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)
    return n,m,l,r

def main(n,m,l,r):
    lmax=1
    rmin=n
    for i in range(m):
        lmax=max(lmax,l[i])
        rmin=min(rmin,r[i])
    return max(rmin-lmax+1,0)

if __name__=='__main__':
    n,m,l,r=readinput()
    ans=main(n,m,l,r)
    print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(m)] #複数行複数入力を多重リストに
l1=[]
l2=[]
for i in l:
  l1.append(i[0])
  l2.append(i[1])
a=max(l1)
b=min(l2)
if b-a>=0:
  print(b-a+1)
else:
  print("0")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = [0] * m, [0] * m
for i in range(m):
    li, ri = map(int, input().split())
    l[i] = li
    r[i] = ri
maxl = max(l)
minr = min(r)
if minr < maxl:
    print(0)
else:
    print(minr - maxl + 1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
n, m = map(int, input().split())


cards = np.zeros(n+1, dtype=int)

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

cum = cards.cumsum()

print(len(cum[cum==m]))

Submission status: Accepted

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

Submission status: Accepted

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

max_l = 0
min_r = 100000
for _ in range(M):
  l, r = map(int, input().split())
  if max_l < l:
    max_l = l
  if min_r > r:
    min_r = r

id = min_r-max_l+1
print(0 if id <0 else id)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m=map(int, input().split())
l1 = [[] for _ in range(n)]
mn=1
ma=n
for i in range(1, m+1):
  l, r=map(int, input().split())
  mn=max(mn, l)
  ma=min(ma, r)
  if ma<mn:
    print(0)
    exit()
print(ma-mn+1)

Submission status: Accepted

Solution code:
k, g = map(int, input().split())
id = [map(int, input().split()) for i in range(g)]
l, r = list(zip(*id))
print(max(min(r) - max(l) + 1, 0))

Submission status: Accepted

Solution code:
k, g = list(map(int, input().split()))
id = [list(map(int, input().split())) for i in range(g)]
l, r = list(zip(*id))
print(min(r) - max(l) + 1) if min(r) - max(l) >= 0 else print(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)
s=0
t=n+1
for i in range(m):
    if l[i]>s:
        s=l[i]
    if r[i]<t:
        t=r[i]
if s>t:
    print(0)
else:
    print(t-s+1)

Submission status: Accepted

Solution code:
N, M = input().split(' ')
N = int(N)
M = int(M)
min = 1
max = N
for i in range(M):
  l, m = input().split(' ')
  l = int(l)
  m = int(m)
  if min>m or max<l:
    print(0)
    break 
  if min < l:
    min = l
  if max > m:
    max = m
else:
  print(max-min+1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
for x in range(m):
  s=list(map(int,input().split()))
  a.append(s)
  
min1=1
max1=n

for y in range(m):
  l=a[y][0]
  r=a[y][1]
  if min1<l:
    min1=l
  if max1>r:
    max1=r
    
if max1-min1>=0:
  print(max1-min1+1)
  
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, M = map(int, input().split())
    L = 1
    R = 10 ** 5 + 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#36
N,M = map(int,input().split())
li = []
ri = []
num = 0


for i in range(M):
    L,R = map(int,input().split())
    li.append(L)
    ri.append(R)
L = max(li)
R = min(ri)
num = R-L+1
if num <0:
    num= 0
    
print(num)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
L = []
R = []
N, M = map(int, input().split())
for _ 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())
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(0,r_min-l_max+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

L = [0] * M
R = [0] * M


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

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

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())
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())
max_l = 0
min_r = 10**5
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
res = min_r - max_l + 1
if res<0:
  res = 0
print(res)

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())
  if l > max_l:
    max_l = l
  if r < min_r:
    min_r = r
max_l = min(n+1, max_l)
min_r = min(n+1, min_r)
res = min_r - max_l + 1
if res<0:
  res = 0
print(res)

Submission status: Accepted

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

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

L_max=0
R_min=10**6
for i in range(m):
    if gate[i][0]>L_max:
        L_max=gate[i][0]

for i in range(m):
    if gate[i][1]<R_min:
        R_min=gate[i][1]


ans=0

for i in range(1, n+1):
    if L_max<=i<=R_min:
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 12, 2020 15:41:56 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


def main():
  N, M = list(map(int, input().split()))

  min_ = -float('inf')
  max_ = float('inf')
  for _ in range(M):
    L, R = map(int, input().split())
    min_ = max(min_, L)
    max_ = min(max_, R)

  ans = 0
  for i in range(1, N+1):
    (max_ - min_ + 1)
    if min_<=i<=max_:
      ans += 1
  print(ans)


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

Submission status: Accepted

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

x = max(l)
y = min(r)

p = 0
for i in range(1,n+1):
    if x <= i and i <= y:
        p += 1

print(p)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

ids = list(range(1, N+1))

L_max = 0
R_min = 10000000000000000000
for _ in range(M):
    (L, R) = map(int, input().split(" "))
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

ids = [i for i in ids if L_max <= i <= R_min]
print(len(ids))

Submission status: Accepted

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

min_r = min(r)
max_l = max(l)

if min_r >= max_l:
    print(min_r - max_l + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M =list(map(int,input().split()))
la=1
rm=N
for i in range(M):
  a,b=list(map(int,input().split()))
  la=max(la,a)
  rm=min(rm,b)
print(max(0,rm-la+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
data=input().split()
N=int(data[0])
M=int(data[1])
data_gate=[input().split() for i in range(M)]
data_min=[]
data_max=[]
for i in range(M):
    data_min.append(int(data_gate[i][0]))
    data_max.append(int(data_gate[i][1]))
m=max(data_min)
M=min(data_max)
if M>=m:
    ans=M-m+1
else:
    ans=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

maxL = max(L)
minR = min(R)

num = 0 
for j in range(maxL,minR+1):
    num +=1

print(num)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
L, R, flg = 1, N, True
for i in range(M):
    _L, _R = list(map(int, input().split()))
    if L <= _L and _R <= R:
        L = _L
        R = _R
    elif _L <= L and R <= _R:
        continue
    elif L <= _L <= R <= _R:
        L = _L
    elif _L <= L <= _R <= R:
        R = _R
    else:
        flg = False
if flg:
    print(R - L + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    print(max(0, R-L+1))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    n, m = map(int, input().split())
    L, R = 1, n
    for _ in range(m):
        l, m = map(int, input().split())
        if l > L: L = l
        if m < R: R = m
    if R-L+1 < 0:
        print(0)
    else:
        print(R-L+1)
    
if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:

def solve():
    dp = [0] * N
    L = max(l)
    R = min(r)
    if L <= R:
        i = list(range(L,R+1))
        ans = len(i)
    else:
        ans = 0
    print(ans)

if __name__ == "__main__":
    N,M = list(map(int, input().split()))
#    LR = [list(map(int, input().split())) for _ in range(M)]
    l,r = [0]*M, [0]*M
    for c in range(M):
        tmp = list(map(int, input().split()))
        l[c] = tmp[0]
        r[c] = tmp[1]
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

P = [0]*(N+1)

for i in range (0, M):
	A, B = map(int, input().split())
	P[A-1]+=1
	P[B]-=1
    
for i in range (1, N+1):
	P[i]+=P[i-1]
    
P = sorted(P, reverse = True)

count = 0

for i in range (0, N+1):
	if P[i] == M:
		count+=1
	else:
		print(count)
		exit()

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_max = max(L)
R_min = min(R)

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

Submission status: Accepted

Solution code:
N,M = (int(x) for x in input().split())
Now = [0]*N
maxL = 1
minR = N
for T in range(0,M):
    L,R = (int(x) for x in input().split())
    if maxL<L:
        maxL = L
    if minR>R:
        minR = R
if maxL>minR:
    print(0)
elif maxL==minR:
    print(1)
elif maxL<minR:
    print(minR-maxL+1)

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,m=mp()
a=-float("inf")
b=float("inf")
for i in range(m):
    l,r=mp()
    a=max(a,l)
    b=min(b,r)
print(max(b-a+1,0))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
(n, m,), *g = [[*map(int, i.split())] for i in open(0)]
ma = n
mi = 0
for l, r in g:
    mi = max(mi, l)
    ma = min(ma, r)

print(max(ma-mi+1,0))