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:
# -*- coding: utf-8 -*-
import sys;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

# 整数の入力
#n = int(input())

# スペース区切りの整数の入力
#a = [x for x in map(int, input().split())]
n, m = map(int, input().split())

# 改行区切りの整数の入力
#a = []
#for x in range(n):
#  a.append(int(input()))

# 文字列の入力
#s = input()

# 出力
#print("{} {}".format(a+b+c, s))
#a.sort()
#print(set(a), file=sys.stderr)
#print(a, file=sys.stderr)

#surfixes = ("dream", "dreamer", "erase", "eraser")

def doit():
  ma = 0
  mi = 100000 + 1
  for i in range(m):
    l, r = map(int, input().split())
    print(l, r, file=sys.stderr)
    ma = max(ma, l)
    mi = min(mi, r)
    print(ma, mi, file=sys.stderr)
  if ma > mi:
    print("0")
  else:
    print(mi - ma + 1)
  
try:
  doit()
except Exception as e:
  print(e, file=sys.stderr)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,m=map(int,input().split(" "))
mae=0
usiro=100000
for i in range(m):
    s,t=map(int,input().split(" "))
    if(mae<=s):
        mae=s
    if(usiro>=t):
        usiro=t
#print(mae,usiro)
ans=usiro-mae+1
if(ans<0):
    ans=0
print(ans)

Submission status: Accepted

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

L_list = []
R_list = []

for i in range(M):
    L_i,R_i = map(int, input().split() )
    L_list.append(L_i)
    R_list.append(R_i)

result = max([min(R_list)- max(L_list) + 1, 0])

print(str(result))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

head = 1
tail = n

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

if head > tail:
    print(0)
else:
    print(tail - head + 1)

Submission status: Accepted

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

listL = []
listR = []
card = N
for i in range(M):
    l, r = input().split()
    L = int(l)
    R = int(r)

    listL.append(L)
    listR.append(R)

maxL = max(listL)
minR = min(listR)

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

Submission status: Accepted

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

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

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)

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

Submission status: Accepted

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

for i in range(N):
    if (0 < i):
        t[i] += t[i-1]

t = t[:-1]
m = max(t)
if m == M:
    print(t.count(m))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

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

inputs = input().split(' ')
N = int(inputs[0])
M = int(inputs[1])

maxL = 1
minR = 100000
for _ in range(M):
    inputs = input().split(' ')
    L = int(inputs[0])
    R = int(inputs[1])
    if L > maxL:
        maxL = L
    if R < minR:
        minR = R

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

Submission status: Accepted

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

gate = []

l, r = map(int, input().split())


for i in range(M-1):
    tmpl, tmpr = map(int, input().split())
    if l < tmpl:
        l = tmpl
    if tmpr < r:
        r = tmpr


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):
    Li, Ri = map(int, input().split(' '))
    L.append(Li)
    R.append(Ri)

Lmax = 1
Rmin = N
for i in range(M):
    if Lmax < L[i]:
        Lmax = L[i]
    if Rmin > R[i]:
        Rmin = R[i]

num = Rmin - Lmax + 1
if num < 0:
    num = 0
print(num)

Submission status: Accepted

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

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

    LR1 = LR[0]
    maxL = LR1[0]
    minR = LR1[1]

    for tmp_LR in LR[1::]:
        tmp_L = tmp_LR[0]
        tmp_R = tmp_LR[1]

        if tmp_L >= maxL:
            maxL = tmp_L

        if tmp_R <= minR:
            minR = tmp_R

    my_list = range(maxL, minR + 1)

    answer = len(my_list)
    print(answer)

Submission status: Accepted

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

l0 = L[0][0]
l1 = L[0][1]
for i in L:
    if l0 < i[0]:
        l0 = i[0]

    if l1 > i[1]:
        l1 = i[1]

if l1 < l0:
    print(0)
else:
    print(l1 - l0 + 1)

Submission status: Accepted

Solution code:
ss=input().split(" ")
N=int(ss[0])
M=int(ss[1])
x=1
y=N
for i in range(M):
  tt=input().split(" ")
  if int(tt[0])>x:
    x=int(tt[0])
  if int(tt[1])<y:
    y=int(tt[1])
last=int(y-x+1)
if last>0:
  print(last)
else:
  print(0)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
min_max = 0
max_min = 1000000

for i in range(m):
    l, r = map(int, input().split())
    if l > min_max:
        min_max = l

    if r < max_min:
        max_min = r

print(len(range(min_max, max_min + 1)))

Submission status: Accepted

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


def main():
    from itertools import accumulate

    n, m = map(int, input().split())
    imos = [0 for _ in range(n + 1)]

    for i in range(m):
        li, ri = map(int, input().split())
        imos[li - 1] += 1
        imos[ri] += -1

    count = 0

    for i in list(accumulate(imos)):
        if i >= m:
            count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
_, m = map(int, input().split())
segs = [tuple(map(int, input().split())) for _ in range(m)]
left, right = segs[0]
for l, r in segs[1:]:
    left = max(left, l)
    right = min(right, r)
print(max(0, right - left + 1))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(M):
    l, r = map(int, input().split())
    
    if l > L:
        L = l
#        print('L ' + str(L))
    if r < R:
        R = r
#        print('R ' + str(R))
        
if L > R:
    print(0)
else:
    print(R - L + 1)

Submission status: Accepted

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

Submission status: Accepted

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

L, R = map(int, input().split())

for i in range(1, M):
    hoge, fuga = map(int, input().split())
    L = max(L, hoge)
    R = min(R, fuga)


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

Submission status: Accepted

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

hoge = []
for i in range(0,M):
    a = [int(x) for x in input().split()]
    hoge.append(a)
    
s = min([x[1] for x in hoge]) - max([x[0] for x in hoge]) +1
if s <= 0:
	print(0)
else:
	print(s)

Submission status: Accepted

Solution code:
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

min_idx = 1
max_idx = 1e+5

for _ in range(M):
    l,r = [int(s) for s in input().split(' ')]
    min_idx = max(min_idx, l)
    max_idx = min(max_idx, r)
if max_idx >= min_idx:
    print(min(N, int(max_idx - min_idx + 1)))
else:
    print(0)

Submission status: Accepted

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

gates = [list(map(lambda x: int(x), input().split())) for i in range(m)]

first_max = 0
second_min = 100001
for gate in gates:
  first_max = max(first_max, gate[0])
  second_min = min(second_min, gate[1])

answer = second_min - first_max + 1
if (answer <= 0):
  print(0)
else:
  print(answer)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l=[0]*m
r=[0]*m
for x in range(m):
    l[x],r[x] = map(int,input().split())
lm=max(l)
rm=min(r)
#print(lm)
#print(rm)
if (rm-lm)<0:
    print("0")
else:
    print(rm-lm+1)

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) + 1 - max(L)
print(ans if ans >= 0 else 0)

Submission status: Accepted

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

def main():
    dic = {}
    for i in range(1, N+1):
        dic[i] = 0
    Ls = []
    Rs = []
    for i in range(M):
        d = input().split()
        l = int(d[0])
        r = int(d[1])
        Ls.append(l)
        Rs.append(r)
    lm = max(Ls)
    rm = min(Rs)
    if rm - lm >= 0:
        print(rm - lm + 1)
    else:
        print(0)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

gate = np.array(gate)

l = gate[:, 0].max()
r = gate[:, 1].min()

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

Submission status: Accepted

Solution code:
#ABC127C-Prison.py

from sys import stdin

input = stdin.readline

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

L = []
R = []

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

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


if maxL <= N and maxL <= minR:
    if N >= minR:
        print(minR-maxL+1)
    else:
        print(N-maxL+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)]

l_list = [LR[i][0] for i in range(M)]
r_list = [LR[i][1] for i in range(M)]

l = max(l_list)
r = min(r_list)

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[]
ans0=0
ans1=n
for i in range(m):
        l.append(list(map(int,input().split())))
for j in range(m):
        ans0=max(ans0,l[j][0])
        ans1=min(ans1,l[j][1])
if ans1-ans0+1<=0:
        print("0")
else:
        print(ans1-ans0+1)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
low = array[0][0]
up = array[0][1]

flag = True
for i in range(1,M):
    if array[i][0] > up:
        flag = False
        break
    if array[i][0] > low:
        low = array[i][0]
    if array[i][1] < up:
        up = array[i][1]
    
if flag:
    print(up - low + 1)
else:
    print(0)

Submission status: Accepted

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

maxf = 1
minf = a

for i in range(b):
    c, d = map(int, input().split())
    if(maxf < c):
        maxf = c
    if(minf > d):
        minf = d

if(minf-maxf < 0):
    print(0)
else:
    print(minf -maxf + 1)

Submission status: Accepted

Solution code:
a = input().split()
low = 0
high = int(a[0])
for i in range(int(a[1])):
    b = input().split()
    if int(b[0]) > low:
        low = int(b[0])
    if int(b[1]) < high:
        high = int(b[1])

r = high - low
if r < 0:
    print("0")
    exit()
print(r + 1)

Submission status: Accepted

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

N,M=map(int,input().split())
LRs = []
Min=-1
Max=10**5
for _ in range(M):
  L,R=map(int,input().split())
  Min = L if L > Min else Min
  Max = R if Max > R else Max
if Max-Min+1 > 0:
  print(Max-Min+1)
else:
  print(0)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

for i in range(m):
    low,high =[int(i) for i in input().split()]
    if(under<low):
        under=low
    if(top>high):
        top=high

if(top<under):
    print(0)
else:
    print(top-under+1)

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    N, M = map(int, input().split())
    LR = [tuple(map(int, input().split())) for _ in [0] * M]

    l, r = list(zip(*LR))
    lmax = max(l)
    rmin = min(r)
    ans = 0
    for i in range(lmax, rmin + 1):
        if i <= N:
            ans += 1
    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Accepted

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

l, r = 1, N

for val 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:
# coding: utf-8

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

l, r = 0, N
for l_r in L_R:
    if l < l_r[0]:
        l = l_r[0]
    if l_r[1] < r:
        r = l_r[1]

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

Submission status: Accepted

Solution code:
import sys

def solve(n, m, l, r):
    l_max = max(l)
    r_min = min(r)
    if l_max > r_min:
        return 0
    else:
        return r_min - l_max + 1


if __name__ == '__main__':

    n, m = map(int, sys.stdin.readline().strip().split(" "))
    l = []
    r = []
    for i in range(m):
        l_tmp, r_tmp = map(int, sys.stdin.readline().strip().split(" "))
        l.append(l_tmp)
        r.append(r_tmp)
    print solve(n, m, l, r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
# L = [int(x) for x in input().split()]
N, M = map(int, input().split())
# S = input()

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

ans = 0
tmp = 0
for i in range(1, N + 1):
    tmp += ok[i]
    
    if tmp == M:
        ans += 1

print(ans)

Submission status: Accepted

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

maxl = max(lr[j][0] for j in range(len(lr)))
minr = min(lr[j][1] for j in range(len(lr)))
if n > minr:
    print(max(minr - maxl + 1,0))
else:
    print(max(n - maxl + 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)

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

Submission status: Accepted

Solution code:
def prison(N: int, M: int, queries: list)->list:
    l, r = 1, N

    for L, R in queries:
        l = max(l, L)
        r = min(r, R)

    return max(0, r-l+1)


if __name__ == "__main__":
    M = 0
    N, M = map(int, input().split())
    queries = [tuple(int(s) for s in input().split()) for _ in range(M)]

    ans = prison(N, M, queries)
    print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())        
                                        
x = []                                  
for i in range(M):                      
    l,r = map(int, input().split())     
    x.append([l, r])                    
                                        
                                        
cand = list(range(1, N+1))              
                                        
for i, p in enumerate(x):               
    l,r = p                             
    if (r < cand[0]) or (l > cand[-1]): 
        del cand[0:]                    
        break                           
                                        
    if cand[0] < l:                     
        del cand[:(l - cand[0])]        
    if cand[-1] > r:                    
        del cand[(r-cand[0]+1):]        
                                        
print(len(cand))

Submission status: Accepted

Solution code:
import fractions
n, m = map(int, input().split())
l = []
r = []
for i in range(m):
    x, y = map(int, input().split())
    l.append(x)
    r.append(y)
ans = 0
M = max(l)
m = min(r)
for i in range(1,n+1):
    if M <= i and i <= m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,m= input().split()
n = int(n)
m = int(m)
g=[input().split() for i in range(m)]
av =[-1,1000000]
for i in range(m):
  av[0] = max(int(g[i][0]),av[0])
  av[1] = min(int(g[i][1]),av[1])
print(max(av[1] -av[0]+1,0))

Submission status: Accepted

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

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



ans = -1
l_min , r_max = list(map(int, input().split()))
for i in range(M-1):
    l, r = list(map(int, input().split()))
    if r_max < l or r < l_min:
        ans = 0
        break

    if l_min <= l and l <= r_max:
        l_min = l

    if l_min <= r and r <= r_max:
        r_max = r


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

Submission status: Accepted

Solution code:
N,M = [int(i) for i in input().split()]
L_max ,R_min = [int(i) for i in input().split()]
for _ in range(M-1):
    temp = [int(i) for i in input().split()]
    if L_max < temp[0]:
        L_max = temp[0]
    if R_min > temp[1]:
        R_min = temp[1]
print(max(R_min - L_max + 1,0))

Submission status: Accepted

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

for i in range(M):
    L, R = map(int, input().split())
    L_l.append(L)
    R_l.append(R)

cont = min(R_l) - max(L_l) + 1
if cont <= 0:
    print(0)
else:
    print(cont)

Submission status: Accepted

Solution code:
def ando(x, y):
    return (max(x[0], y[0]), min(x[1], y[1]))

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

r = (0, N)
for _ in range(M):
    lr = tuple(map(int, input().split()))
    r = ando(r, lr)

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

Submission status: Accepted

Solution code:
N, M = [int(i) for i in raw_input ().split()]
X, Y = [int(i) for i in raw_input ().split()]
for i in range(M - 1):
    x, y = [int(i) for i in raw_input ().split()]
    X = max(X, x)
    Y = min(Y, y)
print max(0, Y - X + 1)

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

# s = input()
# n = int(input())
# A = list(map(int, input().split()))
n, m = map(int, input().split())

l = 0
r = n+1

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

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

Submission status: Accepted

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

Submission status: Accepted

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


ans=0
Gates=[0]*(N+2)

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

for i in range(N+1):
    Gates[i+1]+=Gates[i]

for i in range(1,N+1):
    if Gates[i]==M:
        ans+=1

print(ans)

Submission status: Accepted

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

arr = [0, n+1]

for i in range(m):
    a , b = map(int, input().split())
    if arr[0] < a:
        arr[0] = a
    if arr[1] > b:
        arr[1] = b

ans = arr[1] - arr[0] + 1

if ans >= 0:
    print(arr[1] - arr[0] + 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)	

num = min(Rs) - max(Ls) + 1

if num < 0:
	num = 0

print(num)

Submission status: Accepted

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

for i in range(M):
	a, b = map(int, input().split())
	list_s.append(a)
	list_e.append(b)

res = min(list_e) -  max(list_s) + 1
if res < 0:
	res = 0
print(res)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
head, tail = map(int, input().split())
for i in range(1, M):
    if head > tail:
        print(0)
        exit()
    x, y = map(int, input().split())
    if x > head:
        head = x
    if y < tail:
        tail = y

print(tail - head + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
i = 0
j = n + 1

for k in range(m):
  ti, tj = map(int, input().split())
  if ti > i:
    i = ti
  if tj < j:
    j = tj

if j == 0 and i == 0:
  print(0)
else:
  print(max(j - i + 1, 0))

Submission status: Accepted

Solution code:

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

    for i in range(M):
        L[i], R[i] = map(int, input().split())
    ans = range(max(L), min(R) + 1)
    print(len(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = list(map(int,input().split()))
l = [0]*s[1]
r = [0]*s[1]
for i in range(s[1]):
    l[i], r[i] = list(map(int,input().split()))
    
lmax=0
rmin=s[0]
for i in range(s[1]):
    if l[i] > lmax:
        lmax = l[i]
    if r[i] < rmin:
        rmin = r[i]
if lmax > rmin:
    print(0)
else:
    print(-lmax+rmin+1)

Submission status: Accepted

Solution code:
from collections import Counter

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

print(min_r - max_l + 1 if min_r - max_l >= 0 else 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(0, R-L+1))

Submission status: Accepted

Solution code:
def main():
    n,m=list(map(int,input().split(' ')))
    l_max=0
    r_min=n
    for i in range(m):
        l,r=list(map(int,input().split(' ')))
        if l>l_max:
            l_max=l
        if r < r_min:
            r_min=r
    if r_min>=l_max:
        print(r_min-l_max+1)
    else:
        print(0)
if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
min = 0
max = n

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

    if l > min:
        min = l
    
    if r < max:
        max = r

ans = max - min + 1

if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
N,M = map(int , input().split())
A=[map(int , input().split()) for _ in range(M)]
L_min = 0
R_max = N
for L,R in A:
	L_min = max(L_min , L)
	R_max = min(R_max , R)

if R_max - L_min+1 >=0:
	print(R_max - L_min+1)
else:
	print(0)

Submission status: Accepted

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

for i in range(m):
    b,c = map(int, input().split())
    low.append(b)
    high.append(c)
    
print(max(min(high)-max(low)+1, 0))

Submission status: Accepted

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

import sys
input = sys.stdin.readline
lmax,rmin = 1,N

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

print(max(0,rmin-lmax+1))

Submission status: Accepted

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

L = [i for i,j in LR_list]
R = [j for i,j in LR_list]
maxL = max(L)
minR = min(R)
ans = minR - maxL + 1
if ans >= 0:
    print(ans)
else:
    print("0")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,m = get_nums_l()

l_max = 0
r_min = n
for i in range(m):
    l,r = get_nums_l()
    if l_max < l:
        l_max = l
    if r_min > r:
        r_min = r

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

Submission status: Accepted

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

shita = 1
ue = 100000
for i in range(0, M):
    Li, Ri = map(int, input().split())
    if shita < Li:
        shita = Li
    if Ri < ue:
        ue = Ri

print(max(ue - shita + 1, 0))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
G = []
for i in range(M):
    G.append(list(map(int, input().split())))
tG = list(zip(*G))
A = max(tG[0])
B = min(tG[1])

ans = B - A + 1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

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

for i in range(m-1):
    l2,r2 = map(int, input().split())
    if l<l2:
        l=l2
    if r>r2:
        r=r2
if r-l+1>0:
    print(r-l+1)
else:
    print(0)

Submission status: Accepted

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

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

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

pass_min = 1
pass_max = n

for li, ri in zip(l, r):
    if li > pass_min:
        pass_min = li
    if ri < pass_max:
        pass_max = ri
        
if pass_max >= pass_min:
    print(pass_max - pass_min + 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(0,min(Rs)-max(Ls)+1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

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

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

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

k = 0
ans = 0
for i in l:
        k += i
        if k == M:
                ans += 1
print(ans)

Submission status: Accepted

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

e=[]
f=[]

for _ in range(b):
    c, d = [int(i) for i in input().split()]
    e.append(c)
    f.append(d)

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

Submission status: Accepted

Solution code:
from itertools import accumulate as ac

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


array = [0] * (N + 2) # 最初と最後に 0 を入れるので、サイズを2だけ大きくする。
def imosu(array):
    for _ in range(M):
        l, r = map(int, input().split())
        array[l] += 1
        array[r+1] -= 1
    return list(ac(array))
ans_lst = imosu(array)
print(ans_lst.count(M))

Submission status: Accepted

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

min = 0
max = 1000000

for i in range(M):
    L, M = map(int,input().split())
    if min < L:
        min = L
    if max > M:
        max  = M

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

Submission status: Accepted

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

l_list = list()
r_list = list()
for _ in range(m):
  l, r = map(int, input().split())
  l_list.append(l)
  r_list.append(r)
  
ll = max(l_list)
rr = min(r_list)
if (rr-ll+1) <=0:
  print(0)
else:
  print(rr-ll+1)

Submission status: Accepted

Solution code:
# 2019/05/25
# AtCoder Beginner Contest 127 - C

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

# Search Max L & Min R
max_l = -1
min_r = n + 1
for i in range(m):
    l, r = map(int,input().split())
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r

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

# Output
print(ans)

Submission status: Accepted

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

min_, max_ = map(int, input().split())

for _ in range(M-1):
    LL, MM = map(int, input().split())
    min_ = max(min_, LL)
    max_ = min(max_, MM)


if min_ <= max_:
    print(max_ - min_ + 1)
else:
    print(0)