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, 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, groupby
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

N, M = MAP()

LR = [LIST() for _ in range(M)]
LR = np.transpose(LR)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
rc = [list(map(int,input().split())) for i in range(k)]
transpose = [x for x in zip(*rc)]

ans = min(transpose[1]) - max(transpose[0])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

min1=1
max1=N

for j in gate:
  if j[0]>min1:
    min1=j[0]
  if j[1]<max1:
    max1=j[1]
if max1>=min1:
  print(max1-min1+1)
else:
  print(0)

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    L, R = 0, 1001001
    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))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

start=1
end=n
for line in input_lines:
    if line[0]>start:
        start=line[0]
    if line[1]<end:
        end=line[1]
print(len([i for i in range(start,end+1)]))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

accNums = list(accumulate(nums))
#print('accNums:', accNums)

ans = accNums.count(M)
print(ans)

Submission status: Accepted

Solution code:
import sys
N,M=map(int, sys.stdin.readline().split())
ll=float("-inf")
rr=float("inf")
for _ in range(M):
    l,r=map(int, sys.stdin.readline().split())
    ll=max(ll,l)
    rr=min(rr,r)

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

Submission status: Accepted

Solution code:
# solution
import io

nim,mike=map(int,input().split())

kite,qwe=1,nim

for i in range(mike):
    pop,ioi=map(int,input().split())
    kite,qwe=max(kite,pop),min(qwe,ioi)

print(max(qwe-kite+1,0))

Submission status: Accepted

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

card_min = 0
card_max = N
ans = N
#print(card_min, card_max)
for l, r in LR:
    if l > card_min:
        card_min = l
    if r < card_max:
        card_max = r
    #print(card_min, card_max)
    ans = card_max - card_min + 1
    if ans <= 0:
        ans = 0
        break
print(ans)

Submission status: Accepted

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

while True:
  try:
    tl,tr = map(int,input().split())
    l.append(tl)
    r.append(tr)
  except:
    break

R = min(r)
L = max(l)
cnt = 0
for i in range(1,n+1):
  if i>=L and i<=R:
    cnt +=1
    
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M=map(int, input().split())
w=[list(map(int, input().split())) for i in range(M)]
R_min=10**6
L_max=0
cnt=0

for i in range(M):
    if w[i][1]<=R_min:
        R_min=w[i][1]

for i in range(M):
    if w[i][0]>=L_max:
        L_max=w[i][0]

for num in range(1, N+1):
    if L_max<= num <= R_min:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import sys
import numpy as np

n, m, *lr = map(int, sys.stdin.read().split())
l, r = np.array(lr).reshape(-1, 2).T

def main():
    res = max(0, r.min() - l.max() + 1)
    print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ma = 0
mi = float("INF")
for i in range(0,m):
    ma = max(ma,A[i][0])
    mi = min(mi,A[i][1])

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(m)]
l_max = 0
r_min = 100000000
for i in range(m):
    if (l_max < a[i][0]):
        l_max = a[i][0]
for i in range(m):
    if (r_min > a[i][1]):
        r_min = a[i][1]
print(max(r_min-l_max+1, 0))

Submission status: Accepted

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

import itertools

n, m = map(int, readline().split())
lr = [list(map(int, readline().split())) for i in range(m)]
memo = [0] * (n + 1)
for l, r in lr:
    memo[l - 1] += 1
    memo[r] -= 1
cumsum = list(itertools.accumulate(memo))
cnt = 0
for i in range(n):
    if cumsum[i] == m:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split(" ")]
ans_L = 0
ans_R = 1e5+1

for _ in range(M):
    L,R = [int(i) for i in input().split(" ")]
    if ans_L < L:
        ans_L = L
    if ans_R > R:
        ans_R = R

if (ans_R - ans_L) >= 0:
    print(ans_R - ans_L + 1)
else:
    print(0)

Submission status: Accepted

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

low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = map(int,input().split())
    
    low = max(low,ll)
    high = min(high,rr)

if high>=low:
    print(high-low+1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = []
r = []
low = 0
high = 10 ** 5 + 1
for i in range(m):
    ll,rr = map(int,input().split())
    l.append(ll)
    r.append(rr)
    low = max(low,ll)
    high = min(high,rr)

if high>=low:
    print(high-low+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = -1
right = n+1

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

L = [L_R[i][0] for i in range(M)]
R = [L_R[i][1] for i in range(M)]

L_max = max(L)
R_min = min(R)

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

print(ans)

Submission status: Accepted

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

l = []
r = []

for i in range(m):
  l.append(a[i][0])
  r.append(a[i][1])

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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    left, right = 0, N-1
    for _ in range(M):
        l, r = map(int, input().split())
        left = max(left, l-1)
        right = min(right, r-1)
    
    print(max(0, right - left + 1))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

tmp1, tmp2 = 0, 10**5

for i in range(M):
    L, R = map(int, input().split())
    tmp1, tmp2 = max(L, tmp1), min(R, tmp2)


if tmp2-tmp1 >= 0:
    print(tmp2-tmp1+1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def myAnswer(N,M,L,R) -> int:
   MAXL=max(L)
   MINR =min(R)
   ans = [i for i in range(1,N+1) if(MAXL<= i and MINR >= i)]
   return len(ans)


def modelAnswer():
   tmp=1
def main():
   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(myAnswer(N,M,L[:],R[:]))
   

if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:

def myAnswer(N,M,L,R) -> int:
   total = 0
   MAXL=max(L)
   MINR =min(R)
   for i in range(1,N+1):
      if(MAXL<= i and MINR >= i):
         total += 1
   return total


def modelAnswer():
   tmp=1
def main():
   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(myAnswer(N,M,L[:],R[:]))
   

if __name__ == '__main__':
   main()

Submission status: Accepted

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

for i in range(M):
  lsub.append(LR[i][0])
  rsub.append(LR[i][1])

L = max(lsub)
R = min(rsub)

ans = min(R, N) - L + 1

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ans = 0
l_max = 0
r_min = n+1
for i in range(m):
  l, r = map(int, input().split())
  if r < r_min:
    r_min = r
  if l > l_max:
    l_max = l
for i in range(1, n+1):
  if i >= l_max and i <= r_min:
    ans += 1
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())
    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())

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(max(0, right-left+1))

Submission status: Accepted

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

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

for i in range(m):
    l = max(l, list[i][0])
    r = min(r, list[i][1])

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

Submission status: Accepted

Solution code:
def main():
    ll = []
    rl = []
    n,m = map(int,input().split())
    for _ in range(m):
        l,r = map(int, input().split())
        ll.append(l)
        rl.append(r)
    l = max(ll)
    r = min(rl)
    print(max(0,r-l+1))

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,numpy,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N,M = LI()
LR = [LI() for _ in range(M)]
a = 0
b = float('INF')
for x in LR:
    a = max(x[0],a)
    b = min(x[1],b)
print(b-a+1 if b-a+1>=0 else 0)

Submission status: Accepted

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

Submission status: Accepted

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

left = 0
right = 100000

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

result = right - left + 1

print(result if result >= 0 else 0)

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().strip().split()]
a = []
for _ in range(m):
    a.append([int(i) for i in input().strip().split()])
a.sort(key=lambda s: s[1])
result = a[0]
for i in range(1, len(a)):
    if result[1] >= a[i][0]:
        result = [max(result[0], a[i][0]), min(result[1], a[i][1])]
    else:
        result = []
        break
print(result[1] - result[0] + 1 if len(result) else 0)

Submission status: Accepted

Solution code:
from itertools import accumulate
n, m = map(int, input().split())
l = [list(map(int, input().split())) for i in range(m)]

# 10**5=100000では、配列は0~99999。いもす法で必要な分は100001
ans = [0] * (10 ** 5 + 2)
for i, j in l:
    ans[i] += 1
    ans[j + 1] -= 1

print(list(accumulate(ans)).count(m))

Submission status: Accepted

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


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

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# indeednow_A_b
import sys
from sys import stdin
N, M = [int(x) for x in stdin.readline().rstrip().split()]
L = [0 for i in range(M)]
R = [0 for i in range(M)]
for i in range(M):
    L[i], R[i] = [int(x) for x in stdin.readline().rstrip().split()]
# print(L, R)
ans = set()
ans1 = 0
ans2 = 10**6
for k in range(M):
    if ans2 < L[k]:
        print("0")
        sys.exit()
    if ans1 < L[k]:
        ans1 = L[k]
    if ans2 > R[k]:
        ans2 = R[k]

    # temp = set()
    # for l in range(L[k], R[k]+1):
    #     temp.add(l)
    # if len(ans) != 0:
    #     ans = temp & ans
    # else:
    #     ans = temp
print(ans2+1-ans1)

Submission status: Accepted

Solution code:
n, m = (int(x) for x in input().split())
l = 0
r = n + 1
for i in range(m):
  ll, rr = (int(x) for x in 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= list(map(int,input().split()))
end = []
start = []
for i in range(M):
    A,B = list(map(int,input().split()))
    end.append(B)
    start.append(A)
end_min = min(end)
start_max = max(start)
if start_max > end_min:
    print(0)
else:
    print(end_min-start_max+1)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
memo=[0]*n
from itertools import accumulate
for i in range(m):
    l,r=map(lambda x:int(x)-1,input().split())
    memo[l]+=1
    if r+1!=n:
        memo[r+1]-=1
ac=list(accumulate(memo))
print(ac.count(m))

Submission status: Accepted

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

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

Submission status: Accepted

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

begin = 1
end = n
for i in range(m):
    a, b = map(int, input().split())
    if begin < a:
        begin = a
    if b < end:
        end = b

if begin <= end:
    print(end - begin + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    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
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    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
    ans=0
    for i in range(n):
        if CARD[i]==m:ans+=1
        CARD[i+1]=CARD[i+1]+CARD[i]

    print(ans)

if __name__=='__main__':
    main()

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
ans=0
for i in range(n):
    if CARD[i]==m:ans+=1
    CARD[i+1]=CARD[i+1]+CARD[i]

print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys

    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

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

saitei = 0
saidai = N

for i in range(M):
    L, R = map(int, input().split())
    saitei = max(saitei, L)
    saidai = min(saidai, R)

print(max(saidai-saitei+1, 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()) 

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

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
L_R = []
for _ in range(M):
    L_R.append(list(map(int, input().split())))
    
l_min = 0
r_max = N
for l,r in L_R:
    if l_min <l:
        l_min = l
    if r_max > r:
        r_max =r
    

if r_max >= l_min:
    print(r_max - l_min + 1)
else:
    print(0)

Submission status: Accepted

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

l, r = [], []
for i in range(m):
    tmpa, tmpb = map(int, input().split())
    l.append(tmpa)
    r.append(tmpb)

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

Submission status: Accepted

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

res = [0]*n
for l, r in lr:
    res[l-1] += 1
    if r < n:
        res[r] -= 1
res_imos =[0]*(n+1)
for i in range(n):
    res_imos[i+1] = res_imos[i]+res[i]
ans = 0
for i in range(n):
    if res_imos[i+1]==m:
        ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Problem C - Prison

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

# initialization
max_left = 0
min_right = N

# update loop
for i in range(M):
    L, R = map(int, input().split())
    max_left = max(max_left, L)
    min_right = min(min_right, R)

# output
if max_left>min_right:
    print(0)
else:
    ans = min_right - max_left + 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

lr.sort(reverse = True)
ansl = lr[0][0]
ansr = 10**9
for i in range(m):
    r = lr[i][1]
    if r < ansr:
        ansr = min(ansr,r)
ans = max(0,ansr-ansl+1)

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l0, r0 = 1, n
for j in range(m):
    l1, r1 = map(int, input().split())
    l0 = max(l0, l1)
    r0 = min(r0, r1)
print(max(0, r0-l0+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
LR = [tuple(map(int,input().split())) for i in range(M)]
maxl = 0
minr = N+1
for l,r in LR:
    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())
ans = [i+1 for i in range(n+1)]
l = []
r = []
for i in range(m):
    l1,r1 = map(int,input().split())
    l.append(l1)
    r.append(r1)
    
left = max(l)
right = min(r)
print(len(ans[left:right+1]))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

card = [0]*N
for _ in range(M):
    l, r = map(lambda x: int(x)-1, input().split())
    card[l] += 1
    if r < N-1:
        card[r+1] -= 1
card = accumulate(card)
# c = list(card)
ans = 0
for ci in card:
    if ci == M:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int, input().split())
K=[0]*(N+1)
for i in range(M):
  L,R=map(int, input().split())
  K[L-1]+=1
  K[R]-=1
#print(K)
R=[0]*N
R[0]=K[0]
for i in range(1,N):
  R[i]=R[i-1]+K[i]
import numpy as np
R=np.array(R)
if R.max()==M:
  print((R==M).sum())
else:
  print(0)

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 = 10**10
for i in li:
    mi = max(mi,i[0])
    ma = min(ma,i[1])
if mi > ma:
    print(0)
else:
    print(ma-mi+1)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = 1
r = 10 ** 5
for _ in range(m):
    li, ri = map(int, input().split())
    l = max(l, li)
    r = min(r, ri)

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

Submission status: Accepted

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

L = 1
R = N
for i in range(M):
    a, b = LR[i]
    L = max(L,a)
    R = min(R,b)
if R-L+1>=0:   
	print(R-L+1)
else:
  	print(0)

Submission status: Accepted

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

max_L = 0
min_R = float("inf")
for i in range(m):
    L, R = LR[i]
    max_L = max(max_L, L)
    min_R = min(min_R, R)
if min_R - max_L < 0:
    print(0)
else:
    print(min_R - max_L + 1)

Submission status: Accepted

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

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

if ans[1] - ans[0] >= 0:
    print(ans[1] - ans[0] + 1)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def resolve():
  n, m = map(int, input().split())
  l = 1
  r = 100000
  for i in range(m):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)

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

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    N, M = LI()
    l = 1; r = N
    for _ in range(M):
        L, R = LI()
        l = max(L,l)
        r = min(R,r)
    ans = max(0,r-l+1)
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return read().rstrip().decode('utf-8')
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examA()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

Solution code:
def main():
    n, m = map(int, input().split())
    gates = [0 for _ in range(n + 1)]
    for _ in range(m):
        l, r = map(lambda x: int(x) - 1, input().split())
        gates[l] += 1
        gates[r + 1] -= 1
    for i in range(1, n + 1):
        gates[i] += gates[i - 1]
    answer = 0
    for i in range(n):
        if gates[i] == m:
            answer += 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

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

left = 1
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)

Submission status: Accepted

Solution code:
inputted = list(map(int, input().split()))
N = inputted[0]
M = inputted[1]

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

left = 0
right = N
for LRi in LR:
    left = max(left, LRi[0])
    right = min(right, LRi[1])

    if left > right: break
    
answer = max(0, right - left + 1)
print(answer)