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:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n,m = map(int,input().split())
# 列ベクトルとかの受け取り
lists = [list(map(int,input().split())) for _ in range(m)]

maxL = 1
minR = n
for i in range(m):
    maxL = max(maxL,lists[i][0])
    minR = min(minR,lists[i][1])

ans = minR - maxL +1
if ans <= 0:
    ans = 0

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

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)
    
L_max = max(L_list)
R_min = min(R_list)

if R_min >= L_max:
  print(R_min - L_max + 1)
else:
  print(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:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *

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

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
ID = [LIST() for _ in range(M)]
ID_t = [list(x) for x in zip(*ID)]

tmp = min(ID_t[1]) - max(ID_t[0])

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

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M = list(map(int, sys.stdin.buffer.readline().split()))
    mi = 1
    mx = N

    for m in range(M):
        L, R = map(int, sys.stdin.buffer.readline().split())
        mi = max(mi, L)
        mx = min(mx, R)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for _ in range(M):
    L_i,R_i = map(int,input().split())
    if L_i > min:
        min = L_i
    if R_i < max:
        max = R_i

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

Submission status: Accepted

Solution code:
N,M =  [int(x) for x in input().split()]
Min = 0
Max = N
for _ in range(M):
    L_i,R_i = map(int, input().split())
    if L_i > Min:
        Min = L_i
    if R_i < Max:
        Max = R_i
        
if Min > Max:
    print("0")
else:
    print(Max-Min+1)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ans = [1, n]
for i in range(m):
    l,r = map(int,input().split())
    if r < ans[0] or ans[1] < l:
        print(0)
        exit()
    elif ans[0] <= l and r <= ans[1]:
        ans = [l,r]
    else:
        ans = [max(ans[0], l), min(ans[1], r)]
print(ans[1]-ans[0] + 1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split(' '))
counts = [0 for _ in range(N+2)]

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

ret = 0
for i in range(1, N+2):
    counts[i] += counts[i-1]
    if counts[i] == M:
        ret += 1

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = list(map(int , input().split()))
sc = [list(map(int, input().split())) for i in range(A[1])]
a = []
b = []
for i in sc:
    a.append(i[0])
    b.append(i[1])
if max(a) <= min(b):
    ans = min(b) - max(a) + 1
else:
    ans = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mini, maxi = 0, 10**5+1

for _ in range(m):
    l, r = map(int, input().split())
    if l>mini:
        mini = l
    if r<maxi:
        maxi = r

print(max((maxi-mini+1), 0))

Submission status: Accepted

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

ans =  right - left + 1
if ans <= 0:
  ans = 0
print( ans )

Submission status: Accepted

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

ll, rr = 1, n

for _ in range(m):
    l, r = [int(i) for i in input().split()]
    ll = max(l, ll)
    rr = min(r, rr)
print(max(0, rr-ll+1))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

import numpy as np

N,M = map(int,readline().split())
LR = np.array(read().split(),np.int32)

L = LR[::2]; R = LR[1::2]

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

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)
if l > R:
    print(0)
else:
    print(R - L + 1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
key = [0] * (N+1)
tmp_R = 10 ** 5
tmp_L = 1
for _ in range(M):
    L,R = map(int,input().split())
    if L >= tmp_L:
        tmp_L = L
    if R <= tmp_R:
        tmp_R = R
ans = tmp_R - tmp_L + 1
if ans < 0:
    print(0)
else:
    print(ans)

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 max(l) > min(r):
    print(0)
else:
    print(min(r) - max(l) + 1)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
A, B = map(int, input().split())
L = 0
R = A
for i in range(B):
  x,y = map(int, input().split())
  L = max(L,x)
  R = min(R,y)
ans = R-L+1 if R>=L else 0

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l, r = 1, 10 ** 5

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

ans = r - l + 1 if r >= l else 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)

minmin = max(L)
maxmax = min(R)

ans = maxmax - minmin + 1
if ans < 0:
  ans = 0
print(ans)

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 l > left:
        left = l
    if r < right:
        right = r

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split(" "))
Min = 1
Max = N

for _ in range(M):
    L,R = map(int,input().split(" "))
    if L>Min:
        Min=L
    if R<Max:
        Max=R
if Max-Min>=0:
    print(Max-Min+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n = list(map(int, input().split()))
N = n[0]
M = n[1]
X = []
Y = []
for i in range(M):
    a = list(map(int, input().split()))
    X.append(a[0])
    Y.append(a[1])
v=0
w=0
x=max(X)
y=min(Y)
if y>=x:
    print(y-x+1)
else:
    print(0)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ans = [-1,10**6]

for i in range(m):
    tmp = list(map(int,input().split()))
    ans[0] = max(ans[0],min(tmp))
    ans[1] = min(ans[1],max(tmp))

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

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

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

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 R < right:
        right = R
    if left > right:
        print(0)
        exit()
print(right - left + 1)

Submission status: Accepted

Solution code:
import numpy as np
n,m=map(int,input().split())
X=1
Y=n
for i in range(m):
    L,R=map(int,input().split())
    X=max(X,L)
    Y=min(Y,R)
print(max(0,Y-X+1))

Submission status: Accepted

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

tmp1 = 0
tmp2 = pow(10, 6)
for i in range(m):
    tmp1 = max(lr[i][0], tmp1)
    tmp2 = min(lr[i][1], tmp2)
print(max(0, tmp2 - tmp1 + 1))

Submission status: Accepted

Solution code:
import sys
N, M = map(int, input().split())
L_lis = []
R_lis = []

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

ans = 0
for i in range(max(L_lis),(min(R_lis) + 1)):
    ans = ans + 1

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = 0, 100001
for i in range(m):
    x, y = map(int, input().split())
    if l <= x:
        l = x
    if r >= y:
        r = y
print(len([x for x in 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)]

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

ans = 0
if L_max <= R_min:
    ans = R_min - L_max + 1

print(ans)

Submission status: Accepted

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

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

print(a.count(m))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C - Prison
N,M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
a_max = 0
b_min = 10**9
for i in range(M):
    if b_min >= L[i][1]:
        b_min = L[i][1]
    else:
        pass
    if a_max <= L[i][0]:
        a_max = L[i][0]
    else:
        pass

if a_max > b_min:
    print(0)
else:
    print(b_min - a_max + 1)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n, m = map(int, input().split())
    lmax = 0
    rmin = n + 1
    for i in range(m):
        ltmp, rtmp = map(int, input().split())
        if ltmp > lmax:
            lmax = ltmp
        if rtmp < rmin:
            rmin = rtmp

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

Submission status: Accepted

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

min_num=0
max_num=10**5
for l,r in lr:
    if l>min_num:min_num=l
    if r<max_num:max_num=r

print(max(max_num-min_num+1,0))

Submission status: Accepted

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

left = 1
right = n
for i in range(m):
    k = list_score[i]
    if left < k[0]:
        left = k[0]
    if right > k[1]:
        right = k[1]

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

Submission status: Accepted

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

for l, r in LR:
  ans[l - 1] += 1
  ans[r] -= 1
  
accu =[0] * (n + 1)
for i in range(n):
  accu[i + 1] += ans[i] + accu[i]
  
ans = accu.count(m)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int,input().split())
lr = []
for i in range(m):
    lr.append([int(i) for i in input().split()])

if m == 1:
    print(lr[0][1] - lr[0][0] + 1)
    exit()

tmp1 = 0
tmp2 = pow(10, 6)
for i in range(m - 1):
    tmp1 = max(lr[i][0], lr[i + 1][0], tmp1)
    tmp2 = min(lr[i][1], lr[i + 1][1], tmp2)
#print(tmp1, tmp2)
ans = max(tmp2 - tmp1 + 1, 0)
print(ans)

Submission status: Accepted

Solution code:
#解説AC
n,m = map(int,input().split())
lists = []
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)
ans = len(list(range(ll,rr+1)))
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lists = []
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)
ans = len(list(range(ll,rr+1)))
print(ans)

Submission status: Accepted

Solution code:
m,n=map(int,input().split())
my_min=-float("INF")
my_max=float("INF")
for i in range(n):
    a,b=map(int,input().split())
    if a>my_min:
        my_min=a
    if b<my_max:
        my_max=b
if my_max-my_min<0:
    print(0)
else:
    print(my_max-my_min+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n, m = MAP()
ml = -1
mr = n
for _ in range(m):
    l, r = MAP()
    ml = max(ml, l)
    mr = min(mr, r)
print(max(mr - ml + 1, 0))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, M = (int(i) for i in input().split())
    A = [[int(i) for i in input().split()] for j in range(M)]
    S = [0]*(N+2)
    for le, ri in A:
        S[le] += 1
        S[ri+1] += -1
    ans = 0
    for i in range(N):
        S[i+1] += S[i]
        if S[i+1] == M:
            ans += 1
    # print(S)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    count = [0] * (N + 1)
    for _ in range(M):
        l, r = map(int, input().split())
        count[l - 1] += 1
        count[r] -= 1
    ans = (1 if count[0] == M else 0)
    for i in range(1, N):
        count[i] += count[i - 1]
        if count[i] == M: ans += 1
    print(ans)

    return 0

if __name__ =="__main__":
    solve()

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10**7)
n,m = map(int,stdin.readline().rstrip().split())
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(m)]
mi = 0
ma = n
for i,j in li:
    mi = max(mi,i)
    ma = min(ma,j)
if mi > ma:
    print(0)
else:
    print(ma-mi+1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
MAXL = -1
MINR = float("inf")
for _ in range(M):
    L,R = map(int,input().split())
    MAXL = max(MAXL, L)
    MINR = min(MINR, R)

print(max(0, MINR - MAXL + 1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

max_l = 1
min_r = n
for l, r in lr:
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    if min_r - max_l < 0:
        print(0)
        exit()
print(min_r - max_l + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_c
# 脳死でimosで殴っても良い


class Imos1d:
    def __init__(self, N: int):
        '''
        [0]*N の配列に対して、区間の加算を管理する。
        '''
        self.ls = [0] * (N + 1)  # 配列外参照を防ぐため多くとっておく
        self.N = N

    def add(self, l, r, x):
        '''
        [l,r)の区間にxを足し込む O(1)
        '''
        self.ls[l] += x
        self.ls[min(r, self.N)] -= x  # 配列外参照は余分に作ったところにまとめておく(どうせ使わない)

    def get_result(self):
        '''
        O(N) かけて、区間の加算結果を取得する
        '''
        from itertools import accumulate
        return list(accumulate(self.ls[:-1]))


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


N, M = read_ints()
imos = Imos1d(N)  # 各idカードはいくつのゲートを通ることができるのか
for _ in range(M):
    l, r = read_ints()
    imos.add(l - 1, r, 1)
print(sum([M == x for x in imos.get_result()]))

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

ans = min(right) - max(left) + 1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

ID = list()
for i in range(M):
    ID.append(list(map(int, input().split())))

nega = 0
posi = N

check = 1
for (L, R) in ID:
    if L > nega:
        nega = L
    if R < posi:
        posi = R
    if R < nega or posi < L:
        check = 0

if check == 1:
    print(posi-nega+1)
else:
    print(0)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

L = []
R = []

for i in range(M):
    Li, Ri = map(int, raw_input().split())
    L.append(Li)
    R.append(Ri)

ans = min(R) - max(L) + 1

if ans > 0:
    print ans
else:
    print 0

Submission status: Accepted

Solution code:
N,M=[int(x) for x in input().rstrip().split()]
l=[]
r=[]
for i in range(M):
    b=input().rstrip().split()
    l.append(int(b[0]))
    r.append(int(b[1]))

L=max(l)
R=min(r)

if R<L:
    print(0)
else:

    print(R-L+1)

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split()]
La = -1
Ra = 10**9+7
for i in range(M):
    L,R = [int(j) for j in input().split()]
    La= max(L,La)
    Ra = min(R,Ra)
if Ra - La +1 <=0:
    print(0)
if Ra - La + 1>0:
    print(Ra-La+1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
L_max, R_min = map(int, input().split())
for i in range(m-1):
    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(' ')]

min_num = 0
max_num = N
for i in range(M):
    low, high = [int(k) for k in input().split()]
    if low > min_num:
        min_num = low
    if high < max_num:
        max_num = high
if max_num - min_num < 0:
    print(0)
else:
    print(max_num - min_num + 1)

Submission status: Accepted

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

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

if high - low + 1 > 0 :
    print(high - low + 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):
    L[i],R[i] = map(int, input().split())

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

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)

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
LR = [[i for i in map(int,input().split())] for j in range(M)]
start = 1
end = N
for i in range(M):
  if start < LR[i][0]: start = LR[i][0]
  if end > LR[i][1]: end = LR[i][1]
    
ans = end-start+1
if ans < 0: ans = 0
print(ans)

Submission status: Accepted

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

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

x = min(b)
y = max(a)
if x >= y:
  print(x-y+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

ans_l = 0
ans_r = 100001

for i in range(m):
    tmp_l,tmp_r = map(int,input().split())
    if ans_l < tmp_l:
        ans_l = tmp_l
    if tmp_r < ans_r:
        ans_r = tmp_r
        
if ans_l <= ans_r:
    print(ans_r-ans_l+1)
else:
    print(0)

Submission status: Accepted

Solution code:
from sys import stdin

n, m = (int(x) for x in stdin.readline().rstrip().split())
lr = [stdin.readline().rstrip().split() for _ in range(m)]
l = max([int(i[0]) for i in lr])
r = min([int(i[1]) for i in lr])

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in LR:
    #print(i)
    if i[0]>l:
        l=i[0]
    if i[1]<r:
        r=i[1]

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

Submission status: Accepted

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


def solve(N: int, M: int, L: "List[int]", R: "List[int]"):
    left = 1
    right = N
    for i in range(M):
        if left < L[i]:
            left = L[i]
        if right > R[i]:
            right = R[i]
    if right < left:
        print(0)
        return
    print(right - left + 1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, M, L, R)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans=min(ri)-max(left)+1
if ans<=0:
    ans=0
print(ans)

Submission status: Accepted

Solution code:
#abc127 c
n,m=map(int,input().split())
l_max=0
r_mini=n
for i in range(m):
    l,r=map(int,input().split())
    l_max=max(l_max,l)
    r_mini=min(r_mini,r)
ans=r_mini-l_max+1
if ans<0:
    ans=0
print(ans)

Submission status: Accepted

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

l_max = 0
r_min = 10 ** 6
for _ in range(m):
    l, r = map(int, input().split())
    if l_max < l:
        l_max = l
    if r < r_min:
        r_min = r
if l_max <= r_min:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left = -1
right = 10**10

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

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