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:
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

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

N, M = l_inpl()
D = [0] * (N+1)
for _ in range(M):
    li, ri = l_inpl()
    li, ri = li-1, ri-1
    D[li] += 1
    D[ri+1] -= 1

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

ans_l = D[:len(D)-1]
print(ans_l.count(M))

Submission status: Accepted

Solution code:
o=lambda: map(int,input().split())
n,m=o()
p=[list(o())for _ in range(m)]
l=max(map(lambda x:x[0],p))
r=min(map(lambda x:x[1],p))
print(len(range(l,r+1)))

Submission status: Accepted

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

for l,r in LR:
    imos[l] += 1
    imos[r+1] -= 1

for j in range(1,n+1):
    imos[j] += imos[j-1]
    
print(imos[:-1].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())

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
gate=[1]*N
start=0
end=N
for i in range(M):
  L,R=map(int,input().split())
  if start<L:
    start=L
  if end>R:
    end=R
if end<start:
  print(0)
else:
  print(end-start+1)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,M=MAP()

imos=[0]*(N+1)
for i in range(M):
    l,r=MAP()
    imos[l-1]+=1
    imos[r]-=1

acc=list(accumulate(imos))

mx=max(acc)
if mx==M:
    print(acc.count(mx))
else:
    print(0)

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

if lmax > rmin:
    print(0)
else:
    print(rmin-lmax+1)

Submission status: Accepted

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

table=[0]*(n+2)
for i in range(m):
    table[a[i][0]]+=1
    table[a[i][1]+1]-=1

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

ans=0
for i in table:
    if i==m:
        ans+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

anscards = [0 for i in range(N)]
total = cards[0]
for i in range(N):
    anscards[i] = total
    total += cards[i+1]      
    
print(anscards.count(M))

Submission status: Accepted

Solution code:
import sys
rr = lambda: sys.stdin.readline().strip()
rri = lambda: int(rr())
rrs = lambda: [int(_) for _ in rr().split()]

N, M = rrs()
data = [rrs() for _ in range(M)]

L = data[0][0]
R = data[0][1]

for d in data:
    if L < d[0]:
        L = d[0]
    if R > d[1]:
        R = d[1]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#a, b, c, d = (int(i) for i in input().split())  

#a = [int(i) for i in input().split()] 

N, M = (int(i) for i in input().split())  
L_list = []
R_list = []
for i in range(M):
    L, R = (int(i) for i in input().split())
    L_list.append(L)
    R_list.append(R)
#print(L_list)
#print(R_list)
L_max = max(L_list)
R_min = min(R_list)
for i in range(M):
    pass

if R_min-L_max+1 < 0:
    print("0")
else:
    
    print(R_min-L_max+1)
# if A <= 5:
#     print(0)
# elif 5 < A and A <= 12:
#     print(int(B/2))
# else:
#     print(B)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
LR = [tuple(map(int, input().split())) for i in range(M)]
L, R = zip(*LR)
ans = min(R) - max(L) + 1
if ans < 0:
    ans = 0
print(ans)

Submission status: Accepted

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

A = [0, 1000000]
for i in range(M) :
    L, R = map(int, input().split(' '))
    A[0] = max(A[0], L)
    A[1] = min(A[1], R)

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

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_list.append(l)
    r_list.append(r)

n_card=min(r_list)-max(l_list)+1

if n_card<=0:
    print(0)
else:
    print(n_card)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
minR = N + 1
maxL = 0
for _ in range(M):
    l, r = map(int, input().split())
    minR = min(r, minR)
    maxL = max(l, maxL)

print(max(0, minR-maxL+1))

Submission status: Accepted

Solution code:
# coding: utf-8

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

start = 1
end = N

for i in range(M):
    L,R = list(map(int,input().split()))
    start = max(start, L)
    end = min(end, R)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, M = LI()
    LR = []
    for _ in range(M):
        LR.append(LI())
    left = 0
    right = INF
    for l, r in LR:
        left = max(left, l)
        right = min(right, r)
    ans = max(right - left + 1, 0)
    return ans

print(main())

Submission status: Accepted

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

max_l = max(lr[i][0] for i in range(m))
min_r = min(lr[i][1] for i in range(m))

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
card = [0 for i in range(n + 1)]

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

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Left= 0
Right = N + 1
for i in range(M):
    Left = max(Left,LR[i][0])
    Right = min(Right,LR[i][1])
    
if Right - Left < 0:
    print(0)
else:
    print(Right - Left + 1)

Submission status: Accepted

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

LL = []
RR = []

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

lmax = max(LL)
rmin = min(RR)

ans = rmin - lmax + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lr_array = [[int(x) for x in input().split()] for x in range(m)]
res_l  = 0
res_r  = float("inf")
for l, r in lr_array:
    res_l = max(res_l, l)
    res_r = min(res_r, r)
if res_l == res_r:
    print(1)
elif res_l > res_r:
    print(0)
else:
    print(res_r-res_l+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3.4

# abc127_c

import itertools
import functools

def main():
    # Input
    n, m = [int(s) for s in input().split()]
    lrs = [[int(s) for s in input().split()] for _ in range(m)]
    ls = [lr[0] for lr in lrs]
    rs = [lr[1] for lr in lrs]
    # Get ans
    l = functools.reduce(max, ls)
    r = functools.reduce(min, rs)
    ans = r-l+1 if l<=r else 0
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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



"""
import sys


def solve(N, M, cards):
    left, right = 1, N
    for l, r in cards:
        if l > left:
            left = l
        if r < right:
            right = r

    return max(0, right-left+1)


def main(args):
    N, M = map(int, input().split())
    cards = [[int(i) for i in input().split()] for _ in range(M)]
    ans = solve(N, M, cards)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ll = 0
rr = n
for _ in range(m):
    l, r = map(int, input().split())
    ll = max(ll, l)
    rr = min(rr, r)

if ll <= rr:
    print(rr-ll+1)
else:
    print(0)

Submission status: Accepted

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

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

Submission status: Accepted

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

Min, Max = 1, N

for i in range(M):
    L, R = LR[i]
    Min = max(Min, L)
    Max = min(Max, R)

print(max(Max - Min + 1, 0))

Submission status: Accepted

Solution code:
n,m=[int(x) for x in input().split()]
first_gate=0
last_gate=10**10
for _ in [0]*m:
    small,large=[int(x) for x in input().split()]
    first_gate=max(first_gate,small)
    last_gate=min(last_gate,large)
print(max(0,last_gate- first_gate+1))

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

sys.setrecursionlimit(10**7)

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

Submission status: Accepted

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

L_max = 1
R_min = N

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
L=[]
MM=[]
for i in range(0,M):
  l,m=map(int,input().split())
  L.append(l)
  MM.append(m)
ma=min(MM)
mi=max(L)
ans=ma-mi+1

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split(' '))
l = [list(map(int,input().split())) for i in range(M)]
st = 0
ed = N
flag = False
for i in l:
    if st<i[0]:
        st = i[0]
    if i[1]<ed:
        ed = i[1]
    if st>ed:
        flag = True
        print(0)
        break
if not flag:
    print(ed-st+1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import numpy as np

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

max_l = 0
min_r = int(1e9)
for _ in range(M):
    l, r = map(int, input().rstrip().split(' '))
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    
output = min_r - max_l + 1
if output < 0:
    print(0)
else:
    print(output)

Submission status: Accepted

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

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

ans = 0
for i in range(N):
    if T[i] == M:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
interval = [1, N]
def intersect(ia, ib):
    if not ia or not ib:
        return []
    l = max(ia[0], ib[0])
    r = min(ia[1], ib[1])
    if l > r:
        return []
    else:
        return [l, r]

for i in range(M):
    l, r = tuple(li())
    interval = intersect(interval, [l, r])
if not interval:
    print(0)
else:
    print(interval[1] - interval[0] + 1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

low = 1
high = n

for i in range(0,m):
    if gate[i][0] > high or gate[i][1] < low:
        print(0)
        break
    low = max(low,gate[i][0])
    high = min(high,gate[i][1])
else:
    print(high-low+1)

Submission status: Accepted

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

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

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

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

print(imos.count(M))

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
L = 1
R = N
for _ in range(M):
    Li, Ri = inpl()
    if Li > L:
        L = Li
    if Ri < R:
        R = Ri
print(max(R-L+1,0))

Submission status: Accepted

Solution code:
n,m=[int(_) for _ in input().split()]
ld=1
rd=n
for iii in range(m):
  l,r=[int(_) for _ in input().split()]
  if ld<l:
    ld=l
  if rd>r:
    rd=r
if ld<=rd:
  print(rd-ld+1)
else:
  print(0)

Submission status: Accepted

Solution code:
N,M=(int(x) for x in input().split())
mx=0
mn=10**5+1
for i in range(M):
    L,R=(int(x) for x in input().split())
    mn=min(R,mn)
    mx=max(L,mx)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = [int(x) for x in input().split()]
mini = 0 
maxi = 1.1e+5
for i in range(M):
    l,r = [int(x) for x in input().split()]
    mini = max(mini,l)
    maxi = min(maxi,r)

if maxi - mini +1< 0:
    print(0)
else:
    print(maxi-mini+1)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

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

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

LR.sort(key=lambda x:x[0])


span = LR[0]
for l, r in LR:
    if l > span[1]:
        span = None
        break
    else:
        span[0] = l
        span[1] = min(r, span[1])

if span is None:
    print(0)
else:
    print(span[1] - span[0] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def solve(m, bounds):
    le, ri = bounds[0]
    for i in range(1, m):
        new_le, new_ri = bounds[i]
        le = max(le, new_le)
        ri = min(ri, new_ri)
        if le > ri:
            return 0
    return ri - le + 1


def main():
    n, m = (int(z) for z in input().split())
    bounds = [tuple(int(x) for x in input().split()) for _ in range(m)]
    res = solve(m, bounds)
    print(res)


if __name__ == "__main__":
    main()

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

right = N
left = 0

for i in e:
    if left < i[0]:
        left = i[0]
    if right > i[1]:
        right = i[1]
if right >= left:
    print(right - left + 1)
else:
    print("0")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mi=-1
ma = n
for i in range(m):
    l,r=map(int, input().split())
    mi=max(mi,l)
    ma=min(ma,r)
print(max(0,ma-mi+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_list.append(l)
    r_list.append(r)
if min(r_list) - max(l_list) < 0:
    print(0)
    exit()
else:
    print(min(r_list) - max(l_list) + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

ans = 0
cnt = 0
for i, c in enumerate(cl):
    cnt += c
    if cnt == M:
        ans += 1

#print(cl)
print(ans)

Submission status: Accepted

Solution code:
# from sys import exit
N, M = [int(n) for n in input().split()]
L = [0]*M
R = [0]*M
imos = [0 for _ in range(N+1)]
for i in range(M):
    L[i], R[i] = [int(n) for n in input().split()]
    imos[L[i]-1] += 1
    imos[min(R[i], N)] -= 1

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

ans = 0
for i in range(len(imos)):
    if imos[i] == M:
        ans +=1

print(ans)

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n, m = f()
    g = [ f() for i in range(m)]

    dm = [0] * (n+2)

    for x,y in g:
        dm[x] += 1
        dm[y+1] -= 1

    cnt = 0

    
    for i in range(1, n+1):
        dm[i] = dm[i] + dm[i-1]
        if dm[i] == m:
            cnt += 1

    print(cnt)


solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split(" ")]
l = 1
r = n

for i in range(m):
    li, ri = [int(i) for i in input().split(" ")]
    if l < li:
        l = li
    if ri < r:
        r = ri

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

Submission status: Accepted

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

l = 1
r = N
for i in range(M):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)

ans = r - l + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(M)]
maxx = 100000
minn = 0

for l, r in lr:
    minn = max(l, minn)
    maxx = min(r, maxx)

if maxx - minn >= 0:
    print(maxx - minn + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

s = 1
e = n

for _ in range(m):
  l,r = map(int,input().split())
  
  s = max(s,l)
  e = min(e,r)
  
print(max(e-s+1,0))

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
ll = 1
rr = N

for i in range(M):
    l, r = list(map(int, input().split()))
    if ll <= l:
        ll = l
    if r <= rr:
        rr = r

if rr - ll  + 1> 0:
    print(rr - ll  + 1)
else:
    print(0)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

LMax = LR[0][0]
RMin = LR[0][1]
for l,r in LR[1:]:
  if LMax < l:
    LMax = l
  if RMin > r:
    RMin = r

print(max(0,RMin - LMax + 1))

Submission status: Accepted

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

N, M = map(int, input().split())
L, R = [None for _ in range(M)], [None for _ in range(M)]
for i in range(M):
    L[i], R[i] = map(int, input().split())

# L < R
st = -1
end = N
for i in range(M):
    if L[i] > st:
        st = L[i]
    if R[i] < end:
        end = R[i]

r = end - st + 1
if r < 0:
    r = 0
print(r)

Submission status: Accepted

Solution code:
N,M=list(map(int,input().strip().split(' ')))
Gates=[]
left=[]
right=[]
for _ in range(M):
    a,b=list(map(int,input().strip().split(' ')))
    left+=[a]
    right+=[b]
    Gates+=[(a,b)]
ans=max(min(right)-max(left)+1,0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = [int(i) for i in input().split()]
L, R = 0, N + 10
for l, r in [[int(i) for i in input().split()] for _ in range(M)]:
    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())

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def solve(): 
    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)
    res = max(r-l+1, 0)
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,q = m()

a = [0 for i in range(n)]

for i in range(q):
	j,k = m()
	a[j-1] += 1
	if k != n:
		a[k] -= 1
co = 0
for i in range(1,n):
	a[i] += a[i-1]
for i in range(n):
	if a[i] == q:
		co += 1

print(co)

Submission status: Accepted

Solution code:
N, M = [int(item) for item in input().split()]
now_r = 10000000000
now_l = 0
for i in range(M):
  l, r = [int(item) for item in input().split()]
  now_r = min(r, now_r)
  now_l = max(l, now_l)
  
if (now_l > now_r):
  print (0)
else:
  print (now_r - now_l + 1)

Submission status: Accepted

Solution code:
def run(N, M, L, R):
    maxL = max(L)
    minR = min(R)
    if maxL > minR:
        ret = 0
    else:
        ret = minR - maxL + 1
    return ret


def main():
    N, M = map(int, input().split())
    L = []
    R = []
    for _ in range(M):
        LR = list(map(int, input().split()))
        L.append(LR[0])
        R.append(LR[1])
    print(run(N, M, L, R))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

minimum, maximum = 0, N+1

for i in range(M):
    L, R = list(map(int, input().split()))
    if minimum < L:
        minimum = L
    if R < maximum:
        maximum = R

print(max(maximum-minimum+1, 0))

Submission status: Accepted

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

p=0
q=100000000000000

for i in range(M):
    l,r=map(int,input().split())
    p=max(p,l)
    q=min(q,r)
print(max(q-p+1,0))

Submission status: Accepted

Solution code:
import numpy as np
words = lambda t : list(map(t, input().split()))
n,m = words(int)

l = 0
r = n+1

for i in range(m):
    li,ri = words(int)
    l = max(l,li)
    r = min(r,ri)

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

Submission status: Accepted

Solution code:
import sys

N, M = list(map(int, sys.stdin.readline().strip().split()))

l, r = list(map(int, sys.stdin.readline().strip().split()))

for _ in range(M-1):
    tl, tr = list(map(int, sys.stdin.readline().strip().split()))

    if r > tr:
        r = tr
    if l < tl:
        l = tl

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

Submission status: Accepted

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

L_lst = []
R_lst = []
for i in range(M):
  L_lst.append(LR[i][0])
  R_lst.append(LR[i][1])
  
L_max = max(L_lst)
R_in = min(R_lst)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m = input().split()
n = int(n)
m = int(m)
l = 0
r = n+1
for i in range(m):
    l2,r2 = input().split()
    l2 = int(l2)
    r2 = int(r2)
    if l2 > l:
        l = l2
    if r2 < r:
        r = r2
if (r - l + 1) >= 0:
    print(r-l+1)
else:
    print("0")

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
ls = []
rs = []
for i in range(M):
    L, R = map(int, input().split(' '))
    ls.append(L)
    rs.append(R)

print(max(min(rs) - max(ls) + 1, 0))

Submission status: Accepted

Solution code:
r, m = [int(i) for i in input().split()]
l = []
r = []
for i in range(m):
    lx, rx = [int(j) for j in input().split()]
    l.append(lx)
    r.append(rx)
ans = max(0, min(r) - max(l) + 1)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
p,P=map(int,input().split())
for i in range(m-1):
  a,b=map(int,input().split())
  if p<a:
    p=a
  if b<P:
    P=b
print(max(0,P-p+1))

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())
    if left < l:
        left = l
    if r < right:
        right = r
if right >= left:
    print(str(right-left+1))
else:
    print("0")