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:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    #N = int(input())
    N, M = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    S = tuple(tuple(map(int, input().split())) for i in range(M)) #改行行列
    acc=[0]*N
    for l,r in S:
        l-=1;r-=1
        acc[l]+=1
        if r+1<N:
            acc[r+1]-=1
    acc=accumulate(acc)
    count=0
    for a in acc:
        if a==M:
            count+=1
    print(count)







if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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_)
  
if r>=l:
  print(r-l+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

i = list(map(int, input().split()))
N = i[0]
M = i[1]

l = np.array([list(map(int, input().split())) for i in range(M)])

limit1 = max(l[:, 0])
limit2 = min(l[:, 1])

print(max(limit2 - limit1 + 1, 0))

Submission status: Accepted

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

L = [None] * M
R = [None] * M

for i in range(M):
    l, r = map(int, input().split())
    L[i] = l
    R[i] = 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:
def solve():
    N, M = map(int, input().split())
    Count = [0] * (N + 1)
    for i in range(M):
        l, r = map(int, input().split())
        Count[l-1] += 1
        Count[r] -= 1
    ans = 0
    if Count[0] == M: ans = 1
    for i in range(1, N):
        Count[i] += Count[i-1]
        if Count[i] == M: ans += 1
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#n=int(input())
#s,t=input().split()
n,m = map(int, input().split())
#l=list(map(int, input().split()))
l=[0]*100005
r=[1000005]*100005
z=1000010
for i in range(m):
   l[i],r[i]=map(int,input().split()) 
mi=max(l)
ma=min(r)



if mi>ma:
    print(0)
else:
    print(ma-mi+1)

Submission status: Accepted

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

lr = []

ming = 0
maxg = 10 **6

for i in range(m):
    a,b = map(int,input().split(" "))
    lr.append((a,b))

for i in lr:
    if i[0] > ming:
        ming = i[0]
    if i[1] < maxg:
        maxg = i[1]

print(max([0, maxg - ming + 1]))

Submission status: Accepted

Solution code:
import numpy as np
n, k = map(int,input().split())
gates = [0]*(n+1)
for i in range(k):
  l,r = map(int,input().split())
  gates[l-1] += 1
  gates[r] -= 1
print(np.count_nonzero(np.cumsum(gates) == k))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

ans,ansf = 1,n

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

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 組合せnCr
def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

# ここから書き始める
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[i] -= 1
    r[i] -= 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

print(card.count(M))

Submission status: Accepted

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

n, m = [int(w) for w in input().split()]

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

Submission status: Accepted

Solution code:
class Imos_1:
    def __init__(self,N):
        self.len=N
        self.list=[0]*(N+1)

    def Add(self,F,T,C):
        self.list[F]+=C
        self.list[T+1]-=C

    def Cumulative_Sum(self):
        Y=[0]*(self.len)
        S=0
        for i in range(self.len):
            S+=self.list[i]
            Y[i]=S

        return Y

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

I=Imos_1(N)
for _ in range(M):
    L,R=map(int,input().split())
    I.Add(L-1,R-1,1)

J=I.Cumulative_Sum()
print(J.count(M))

Submission status: Accepted

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

gates = [0] * (n + 2)

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

from itertools import accumulate
gates = list(accumulate(gates))

print(gates.count(m))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
min_L = 1
max_R = 1 * 10 ** 7
for i in range(M):
    L,R=map(int,input().split())
    min_L = max(L,min_L)
    max_R = min(R,max_R)

if max_R >= min_L :  
    print(max_R - min_L + 1)
else :
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

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

Submission status: Accepted

Solution code:
N,M=list(map(int,input().split()))
l=1
r=N
for m in range(M):
    l_,r_ = list(map(int,input().split()))
    l = max(l,l_)
    r = min(r,r_)
    
ans = r-l+1

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

Submission status: Accepted

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

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

L = 0
R = N
for i in range(M):
    L = max(L, list_LR[i][0])
    R = min(R, list_LR[i][1])
if L > R:
    print(0)
else:
    print(R-L+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)

ls.sort()
rs.sort()

ans = rs[0] - ls[-1] + 1
ans = max(0,ans)

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
## C - Prison
N, M = map(int, input().split())
valL = 0
valR = 10 ** 5
ans = 0
for m in range(M):
    L, R = map(int, input().split())
    valL = max(valL, L)
    valR = min(valR, R)
if valL > valR:
    ans = 0
else:
    ans = valR - valL + 1
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
Authority = [list(map(int, input().split())) for _ in range(m)]
cards = list(range(1, n + 2))
l_list = []
r_list = []
for i in range(m):
    l_list.append(Authority[i][0])
    r_list.append(Authority[i][1])
        
print(len(cards[max(l_list):min(r_list) + 1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l_max = 0
r_min = 100001
for i in range(m):
    l, r = map(int, input().split())
    l_max = max(l_max, l)
    r_min = min(r_min, r)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n,m=map(int,input().split())
t=[0 for i in range(n)]
for i in range(m):
    l,r=map(int,input().split())
    t[l-1]+=1
    if r<n:
        t[r]-=1
ans=list(accumulate(t))
tmp=0
for i in range(n):
    if ans[i]==m:
        tmp+=1
print(tmp)

Submission status: Accepted

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

L = 1
R = N

for m in range(M):
    L_, R_ = map(int,input().split())
    if L < L_:
        L = L_
    if R_ < R:
        R = R_

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

Submission status: Accepted

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

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

import numpy as np
card = list(np.cumsum(card))
print(card.count(m))

Submission status: Accepted

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

card = [0]*(n+2)
for _ in range(m):
    l, r = map(int, input().split())
    card[l] +=1
    card[r+1] -=1
count =0
import numpy as np
card = np.cumsum(card)
for i in card:
    if i ==m:
        count+=1

print(count)

Submission status: Accepted

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

for i in range(m):
  if min < LR[i][0]:
    min = LR[i][0]
  if max > LR[i][1]:
    max = LR[i][1]

ans = max - min + 1

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
l = -1
r = N+1

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

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

Submission status: Accepted

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

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

res = 0
gate = 0
for i in range(N):
    gate += card[i]
    if gate == M:
        res += 1

print(res)

Submission status: Accepted

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

if l <= r:
    ans = r-l+1
else:
    ans = 0

print(ans)

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)
x=max(l)
y=min(r)
ans=(max(y-x+1,0))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,m=f()
L,R=0,n
for i in range(m):
  l,r=f()
  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())

L, R = 0, float("inf")

for _ in range(M):
    l, r = map(int, input().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 = 0, float("inf")

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

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

print(count)

Submission status: Accepted

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

    left = 1
    right = N

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

        if left < l:
            left = l
        if right > r:
            right = r

    ans = right - left

    print(ans + 1 if ans >= 0 else 0)


main()

Submission status: Accepted

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

left = 1
right = N

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

    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

print(ans + 1 if ans >= 0 else 0)

Submission status: Accepted

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

left = 1
right = N

for l, r in LR:
    if left < l:
        left = l
    if right > r:
        right = r

ans = right - left

print(ans + 1 if ans >= 0 else 0)

Submission status: Accepted

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

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print(ans + 1 if ans >= 0 else 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(0, right - left + 1))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
##かーどについての初期配列
head =0
tail = n
for i in range(m):
    l,r = map(int,input().split())
    head = max(head,l)
    tail = min(r,tail)
    
if tail < head:
    print(0)
else:
    print(tail-head+1)

Submission status: Accepted

Solution code:
hoge = input().split()
N = int(hoge[0])
M = int(hoge[1])
 
mi = 0
ma = N
 
for j in range(M):
    i = input().split(" ")
    l = int(i[0])
    r = int(i[1])
    if l >= mi:
        mi = l
    if r <= ma:
        ma = r
r = ma - mi + 1
if r > 0:
    print(r)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
low = 0
high = 100001

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

Submission status: Accepted

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

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

ansl=-1
ansr=10**6

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

print(max(ansr-ansl+1,0))

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
LR=set(range(N+1))
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:
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for _ in range(M)]

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

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

Submission status: Accepted

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

count_dict = {}

gate = set(range(10**5 + 1))

L = 1
R = 10**5

for i in range (M):
    l = LR[i][0]
    r = LR[i][1]

    if l > L:
        L = l
    if r < R:
        R = r

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

Submission status: Accepted

Solution code:
hoge = input().split()
N = int(hoge[0])
M = int(hoge[1])

mi = 0
ma = N

for j in range(M):
    i = input().split(" ")
    l = int(i[0])
    r = int(i[1])
    if l >= mi:
        mi = l
    if r <= ma:
        ma = r
r = ma - mi + 1
if r > 0:
    print(r)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
table=[0]*(n+2)
for i in range(m):
    l,r=map(int,input().split())
    table[l]+=1
    table[r+1]-=1
from itertools import accumulate
from collections import Counter as co
var=co((accumulate(table)))

if m not in var:print(0)
else:print(var[m])

Submission status: Accepted

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

cards = [lrs[0][0], lrs[0][1]] # [min, max]

for lr in lrs[1:]:
    if (lr[1] < cards[0]) or (lr[0] > cards[1]):
        print(0)
        exit()

    cards[0] = max(cards[0], lr[0])
    cards[1] = min(cards[1], lr[1])
    

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

L = []; R = []
for _ 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
sys.setrecursionlimit(10**6)

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

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

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

if minR < maxL:
    ans = 0
else:
    ans = minR-maxL+1
print(ans)

Submission status: Accepted

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

l = 1
r = n

for i, j in LR:
  l = max(l, i)
  r = min(r, j)
  
print(max(0, r - l + 1))

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
lsl = []
lsr = []
for i in range(m):
    l, r = map(int,input().split())
    lsl.append(l)
    lsr.append(r)
lmax = max(lsl)
rmin = min(lsr)

if lmax > rmin:
    print(0)
else:
    ans1 = set(range(1,n + 1))
    ans2 = set(range(lmax,rmin + 1))
    ans = ans1 & ans2
    print(len(ans))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
gate=[list(map(int,input().split())) for i in range(m)]
small=[]
tall=[]
for i in range(m):
    small+=[gate[i][0]]
    tall+=[gate[i][1]]
smallmax=(max(small))
tallmin=(min(tall))
if tallmin-smallmax>=0:
    print(tallmin-smallmax+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N,M = (int(i) for i in input().split())
ans_L =[]
ans_R =[]
for i in range(M):
    L,R = (int(x) for x in input().split())
    ans_L.append(L)
    ans_R.append(R)
#
ans =0
max_L =max(ans_L)
min_R =min(ans_R)
if max_L > min_R:
    ans =0
else:
    ans =min_R -max_L +1
print(ans)

Submission status: Accepted

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n,m = i2()
l = []
r = []
for i in range(m):
	L,R = i2()
	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 = list()
R = list()

for a in range(m):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

ans = 0
Lmax = max(L)
Rmin = min(R)

for a in range(n):
    if Lmax <= a+1 and a+1 <= Rmin:
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = max(min_r - max_l + 1, 0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count=[0]+[0]*N+[0]
for l,r in lr:
    count[l]+=1
    count[r+1]-=1

i=1
while i<N+2:
    count[i]=count[i]+count[i-1]
    i+=1

print(sum(c==M for c in count))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[]
M=[]
for i in range(m):
  l,m=map(int,input().split())
  L.append(l)
  M.append(m)
  
a=max(L)
b=min(M)
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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, M, *LR=map(int, read().split())
    Lmax = max(LR[::2])
    Rmin = min(LR[1::2])
    
    print(max(0, Rmin-Lmax + 1))
    return


if __name__ == '__main__':
    main()