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:
N, M = map(int,input().split())

L_max = 0
R_min = 100000
flag = False
for i in range(M):
    L, R = map(int,input().split())
    if R_min >= L:        
        if L_max <= L:
            L_max = L
    
        if R_min >= R:
            R_min = R
    else:
        flag = True

if flag:
    print(0)
else:
    print(R_min-L_max+1)

Submission status: Accepted

Solution code:
n,m =list(map(int,input().split()))
lis = []
max_begin = 0
min_end = n
for i in range(m):
    begin,end =list(map(int,input().split()))
    
    if begin >= max_begin:
        max_begin = begin
    if end <= min_end:
        min_end = end

if min_end-max_begin >= 0:
    print(min_end-max_begin+1)
else:
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m = map(int, raw_input().split())
# 文字列の入力
# 出力
gate = [[0,0]]*m
for i in range(m):
    gate[i]=raw_input().split()

cnt = 0
min_r = n
max_l = 0
for i in range(m):
    g0 = int(gate[i][0])
    g1 = int(gate[i][1])

    if max_l < g0:
        max_l = g0

    if min_r > g1:
        min_r = g1

for j in range(n):
    j=j+1
    if j >= max_l and min_r >= j:
            cnt += 1

print cnt

Submission status: Accepted

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

for i in range(m):
    k = input().split()
    l.append(int(k[0]))
    r.append(int(k[1]))

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

Submission status: Accepted

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

Submission status: Accepted

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

minimum = 0
maximum = 10**8
for _ in range(M):
    L, R = map(int, input().split())
    minimum = L if minimum < L else minimum
    maximum = R if R < maximum else maximum
maximum = N if N < maximum else maximum

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

Submission status: Accepted

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

L_list = []
R_list = []
for i in range(M):
	L, R = input().split(' ')
	L_list.append(int(L))
	R_list.append(int(R))

L_max = max(L_list)
R_min = min(R_list)
if L_max <= R_min:
	print(len(range(L_max, R_min+1)))
else:
	print(0)

Submission status: Accepted

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

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

print(rb-lb+1 if lb <= rb else 0)

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)

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

Submission status: Accepted

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

keys = [True] * N

l1 = 1
r1 = N

for i in range(M):
    l, r = map(int, input().split())
    if (l1 < l):
        l1 = l
    if (r < r1):
        r1 = r

if (r1 - l1 + 1 > 0):
    print(r1 - l1 + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# IMPORT ###############################################################################################################
import sys                                                      # システム
# sys.setrecursionlimit(10**6)                                    # 再帰処理の上限変更
# from collections import deque, Counter                          # キュー・個数カウント
# from itertools   import accumulate, permutations, combinations  # 累積和・順列・組み合わせ
# from operator    import itemgetter                              # ソート補助
# from bisect      import bisect_left, bisect_right, bisect       # 二分探索
# from heapq       import heappop, heappush                       # プライオリティキュー
# from fractions   import gcd                                     # 最大公約数
# from math        import ceil, floor, sqrt, cos, sin, tan, pi    # 数学処理
# from copy        import deepcopy                                # オブジェクトコピー
# import numpy         as np                                      # 数値計算

# MAIN #################################################################################################################
def main():
    # Init #############################################################################################################
    im = InputManager()
    # Input ############################################################################################################
    n, m = im.inputMultiple()
    left = 0
    right = n
    for _ in range(m):
        l, r = im.inputMultiple()
        left = max(left, l - 1)
        right = min(right, r - 1)
    # Logic ############################################################################################################

    # Output ###########################################################################################################
    if (right - left) >= 0:
        print(right - left + 1)
    else:
        print(0)
    # Return ###########################################################################################################
    return

# INPUT MANAGER ########################################################################################################
class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]

# CALL MAIN ############################################################################################################
main()

Submission status: Accepted

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

Lmax = 0
Rmin = N
for i in range(M):
    L, R = list(map(int, input().split()))
    Lmax = max(Lmax, L)
    Rmin = min(Rmin, R)

print(max(Rmin - Lmax +1, 0))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
ID_min = 1
ID_max = N
for i in range(M):
    L_tmp,R_tmp = map(int,input().split())
    if ID_max < L_tmp or ID_min > R_tmp:
        ID_max = 0
        ID_min = 1
        break
    if L_tmp > ID_min:
        ID_min = L_tmp
    if R_tmp < ID_max:
        ID_max = R_tmp
print(ID_max - ID_min + 1)

Submission status: Accepted

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

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

ans = list(range(max(L), min(R) + 1))


print(len(ans))

Submission status: Accepted

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

L =[]
R =[]

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

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

Submission status: Accepted

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

import numpy as np
s = list(map(int, input().split()))
N = s[0]
M = s[1]

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

print(max(np.min(s[:,1]) - np.max(s[:,0]) +1, 0))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
ans = -1
if m > 1:
    for i in range(m-1):
        _l, _r = map(int, input().split())
        if (l < _l <= r):
            l = _l
        if (l <= _r < r):
            r = _r
        if _r < l or r < _l:
            ans = 0
            print(ans)
            break

if ans == -1:
    print(r-l+1)

Submission status: Accepted

Solution code:
data = [int(s) for s in  input().split()]
l = 0
r = 10000000
flag = 0

for i in range(data[1]):
    gate = [int(s) for s in input().split()]
    
    if l < gate[0]:
        l = gate[0]

    if l > r:
        print(0)
        flag = 1
        break
        

    if r > gate[1]:
        r = gate[1]
    if r < l:
        print(0)
        flag = 1
        break
if flag == 0:
    print(r-l +1)
else:
    pass

Submission status: Accepted

Solution code:
import sys

def parse(inp):
    n,m = map(int,next(inp).split())
    lr =[]
    for _ in range(m):
        lr.append(list(map(int,next(inp).split())))
    
    return n,m,lr
  
def main(inp):
    n,m,lr = inp
    
    b1 = 1
    b1 = ((b1 << (lr[0][1]-lr[0][0]+1)) - 1) << lr[0][0]-1 
    
    b2 = 1
    for i in range(1,m):
        b2 = ((b2 << (lr[i][1]-lr[i][0]+1)) - 1) << lr[i][0]-1
        b1 = b1 & b2
        b2 = 1
    
    mask = (1 << n) - 1
    b1 = b1 & mask
    return bin(b1).count("1")
  
print(main(parse(sys.stdin)))

Submission status: Accepted

Solution code:
import numpy as np

N, M = map(int, input().split())
ls = np.empty(M)
rs = np.empty(M)


for i in range(M):
    l, r = map(int, input().split())
    ls[i] = l
    rs[i] = r

if 0 <= (np.min(rs) - np.max(ls)):
    print(int(np.min(rs) - np.max(ls) + 1))
else:
    print(0)

Submission status: Accepted

Solution code:
#coding:utf-8
n, m = map(int, input().split())
l, r = 0, 10**6
for i in range(m):
    a, b = map(int, input().split())
    l = max(l, a)
    r = min(r, b)
print(r - l  + 1 if l <= r else 0)

Submission status: Accepted

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

N, M = map(int, raw_input().split())
_L = 1
_R = N

for i in range(M):
    L, R = map(int, raw_input().split())
    if _L < L:
        _L = L
    if R < _R:
        _R = R 

print str(max(_R-_L+1,0))

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
min_gate = 0
max_gate = sys.maxsize
for l, r in lr:
    min_gate = max(min_gate, l)
    max_gate = min(max_gate, r)
print(max(0, max_gate - min_gate + 1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N,M = map(int, lines.pop(0).split(' '))
    min_n = 0
    max_n = N
    for i in range(M):
        a,b = map(int, lines.pop(0).split(' '))
        min_n = max(a, min_n)
        max_n = min(b, max_n)
    print( max(0, max_n - min_n + 1 ) )

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l = []
r = []
for i in range(1, m + 1):
    s, t = map(int, input().split())
    l.append(s)
    r.append(t)
l.sort()
r.sort()
tmp = range(l[-1], r[0] + 1)
se = set(tmp)
print(len(se))

Submission status: Accepted

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

s_ids = []
m_ids = []

for i in range(Q):
    s_ids.append(l[i][0])
    m_ids.append(l[i][1])

max_s_id = max(s_ids)
min_m_id = min(m_ids)

# diff = abs(max_s_id - min_m_id)
# print(diff+1)

if max_s_id <= min_m_id:
    print(abs(max_s_id - min_m_id) +1)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
L=[]
R=[]
Nlist = list(range(1,N+1))
for i in range(M):
  L_n,R_n = map(int,input().split())
  L.append(L_n)
  R.append(R_n)

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

Submission status: Accepted

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

answer = 0

upper = a
under = 0

for i in range(b):
  c,d=map(int,input().split())
  if d <= upper:
    upper = d
  if under <= c:
    under = c

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#python 3.5.2

N, M = [int(i) for i in input().split()]
LRs = [[int(j) for j in input().split()] for i in range(M)]
LRs = list(map(list, zip(*LRs)))  # 転置
Lmax = max(LRs[0])
Rmin = min(LRs[1])
Rmin = min(Rmin, N)
num = Rmin - Lmax + 1 if Rmin >= Lmax else 0 
print(num)

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 right-left+1 > 0:
    print(right-left+1)
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    N,M = map(int, input().split())
    lr = []
    for _ in range(M):
        l,r = map(int, input().split())
        lr.append((l,r))
    lr1 = sorted(lr, key=lambda x: x[0])
    lr2 = sorted(lr, key=lambda x: x[1])
    res = lr2[0][1] - lr1[-1][0] + 1
    if res < 0:
        res = 0
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(M):
    L, R = map(int,input().split())
    if L > minL:
        minL = L

    if R < maxR:
        maxR = R

if maxR-minL+1 > 0:
    print(maxR-minL+1)
else:
    print(0)

Submission status: Accepted

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

list_left = []
list_right = []
for i in range(m):
    l,r=map(int, input().split())
    list_left.append(l)
    list_right.append(r)
test=[i for i in range(max(list_left),min(list_right)+1)]
print(len(test))

Submission status: Accepted

Solution code:
from sys import stdin
N,M=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(M):
    data.append([int(x) for x in stdin.readline().rstrip().split()])
min_=0
max_=10**5

for j in data:
    if j[0] > min_:
        min_ = j[0]
    if j[1] < max_:
        max_ = j[1]
ans = max_ - min_
if ans < 0:
  print(0)
else:
  print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
li1 = []
li2 = []
for i in range(M):
    L, R = map(int,input().split())
    li1.append([L,R])
    li2.append([R,L])
li1.sort()
li2.sort(reverse=True)
if (li2[M-1][0])<(li1[M-1][0]):
    print("0")
    exit()
print((li2[M-1][0])-(li1[M-1][0])+1)

Submission status: Accepted

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

L, R = 1, N
for _ in range(M):
    Li, Ri = map(int, input().split())
    if L <= Li:
        L = Li
    if Ri <= R:
        R = Ri

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

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")
#-------------------------------------------------------------------

N, M = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

L=0
R=N
for i in range(M):
	l, r = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

	L=max(L,l)
	R=min(R,r)
	
	p("l,r,L,R",l,r,L,R)

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

print(ans)

p("l,r,L,R",l,r,L,R)
p(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# abc 127 c
# input
a, b = map(int, input().split())
mi, mx = map(int, input().split())
o = 0
for i in range(1, b):
    n, m = map(int, input().split())
    if n > mx:
        o = 1
        break
    else:
        if mi < n:
            mi = n
        if mx > m:
            mx = m
if o == 1:
    print(0)
else:
    print(mx-mi+1)

Submission status: Accepted

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

n = -1
for i in range(0,M):
    # gate i can be opened by following card set
    (l, r) = (int(j) for j in input().split(' '))
    #print(cards, set(range(l,r+1)))
    if l > r0 or r < l0:
        n = 0
        break
    (l0,r0) = (max(l,l0), min(r,r0))

if n == -1:
    n = r0-l0 +1
    
print(n)

Submission status: Accepted

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

llist = []
rlist = []

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

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

Submission status: Accepted

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

for m in range(M):
    l, r = map(int, input().split(" "))
    if l > min_l:
        min_l = l
    if r < max_r:
        max_r = r

ans = (max_r-min_l)+1
if ans < 0:
    ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Lmax = 1
Rmin = N

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
n,m = map(int, input().split())
l=[]
r=[]
for i in range(m):
    l1,r1=map(int, input().split())
    l.append(l1)
    r.append(r1)
maxl = max(l)
minr = min(r)
if minr-maxl+1 > 0:
    print(minr-maxl+1)
else:
    print(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(l,left)
    right=min(r,right)
print(max(0,right-left+1))

Submission status: Accepted

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

ll = 1
rr = N

for i in range(M):
    l, r = map(int, input().split())
    ll = max(ll, l)
    rr = min(rr, r)

print(max(0, rr - ll + 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)

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

Submission status: Accepted

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

L=0
R=N
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 = (int(i) for i in input().split())
CL = 1
CR = N
for i in range(M):
	L, R = (int(i) for i in input().split())
	if CL < L:
          CL = L
	if CR > R:
          CR = R

C = CR-CL
if C < 0:
	print(0)
else:
	print(C + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    idnum,gatenum = [int(element) for element in input().split(" ")]
    minid = 0
    maxid = idnum

    for gete in range(gatenum):
        condmin,condmax = [int(element) for element in input().split(" ")]
        if(minid<condmin):
            minid = condmin
        if(maxid>condmax):
            maxid = condmax

    if(maxid<minid):
        print(0)
    else:
        print(maxid-minid+1)

if __name__ == "__main__":
    main()

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

Submission status: Accepted

Solution code:
N, M = map(int,input().split())
p = [list(map(int,input().split())) for i in range(M)]
lmax, rmin = 0, 10**7
for l, r in p:
    lmax = max(lmax, l)
    rmin = min(rmin, r)
if lmax <= rmin:
    print(rmin-lmax+1)
else:
    print(0)

Submission status: Accepted

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

haba = list()
max_L = 0
min_R = 10 ** 5 + 1
for i in range(M):
    L, R = map(int, input().split())
    haba.append([L, R])
    max_L = max(max_L, L)
    min_R = min(min_R, R)

print(max(min_R + 1 - max_L, 0))

Submission status: Accepted

Solution code:
# coding: UTF-8
# sample code

v = list(map(int, input().split()))
n, m = v[0], v[1]

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

min = 1
max = n
for i in range(m):
	if l[i] > min:
		min = l[i]
	if max > r[i]:
		max = r[i]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
lm=0
rm=100000

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

if rm-lm<0:
    print(0)
else:
    if lm<=n:
        print(rm-lm+1)
    else:
        print(n-rm+1)

Submission status: Accepted

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

L_max = max(L)
R_min = min(R)
ok = 0
for i in range(N):
  if L_max <= i+1 <= R_min:
    ok += 1

print(ok)

Submission status: Accepted

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

through_list = []
for i in range(M):
    L,R = map(int,input().split())
    through_list.append([L,R])
through_list.sort()
ans_range = through_list[0]
for i in range(M-1):
    ans_range = [max(ans_range[0],through_list[i+1][0]),min(ans_range[1],through_list[i+1][1])]
ans = ans_range[1]-ans_range[0]+1 
ans = max(0,ans)
print(ans)

Submission status: Accepted

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

a_max = -1
b_min = 999999999999999

for i in range(m):
    a, b = map(int, input().split())
    
    a_max = max(a_max, a)
    b_min = min(b_min, b)

ans = max(b_min - a_max + 1, 0)
print(ans)

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)

max_L = max(L)
min_R = min(R)

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())
L_max = 0
R_min = N
for i in range(M):
    L,R = map(int, input().split())
    if L > L_max:
        L_max = L
    if R < R_min:
        R_min = R

if R_min-L_max < 0:
    print(0)
else:
    print(R_min-L_max+1)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n,m =na()
ls = 1
rs = n
for i in range(m):
    l, r = na()
    ls = max(ls, l)
    rs = min(rs, r)
if ls > rs:
    print(0)
else:
    print(rs - ls + 1)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
head, tail = map(int, input().split())
flag=True
for i in range(m-1):
    l,r = map(int, input().split())
    if head<l and l<=tail:
        head=l
    elif l>tail:
        flag=False
        break
    if tail>r and head<=r:
        tail = r
    elif r<head:
        flag=False
        break
print(tail-head+1 if flag==True else 0)

Submission status: Accepted

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

lmax = 0
rmin = N
for i in range(M):
    L, R = map(int, input().split())
    lmax = max(L, lmax)
    rmin = min(R, rmin)

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

Submission status: Accepted

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

L = 1
R = N

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
t = [tuple(map(int, input().split())) for i in range(m)]
t1 = [tt[0] for tt in t]
t2 = [tt[1] for tt in t]
r = min(t2) - max(t1) + 1
r = r if r >= 0 else 0
print(r)

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

Submission status: Accepted

Solution code:
L, N = map(int, input().strip().split())
s1, e1 = 0, 10**5
for i in range(N):
    s, e = map(int, input().strip().split())
    s1, e1 = max(s, s1), min(e, e1)
    
print(max(0, e1 - s1+1))

Submission status: Accepted

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

count = [0 for i in range(N+1)]

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

ans = 0
tmp = 0

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

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
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("{}".format(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
max_l = 1
min_r = 100000
for _ in range(m):
    l, r = list(map(int, input().split()))
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
ans = min_r - max_l + 1
if ans < 0:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split()) #nがIDカード,mがゲートの数
lst = [list(map(int, input().split())) for i in range(m)]

left = 1
right = n
for i in range(m):
  if left < lst[i][0]:
    left = lst[i][0]
  if right > lst[i][1]:
    right = lst[i][1]
if left <= right:
  print(right-left+1)
else:
  print(0)

Submission status: Accepted

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

import math

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

s = 0
t = 10**5+1
for i in range(m):
    a,b = list(map(int,input().split(" ")))
    if s < a:
        s = a
    if b < t:
        t = b
if t < s:
    print(0)
else:
    print(t-s+1)

Submission status: Accepted

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

Submission status: Accepted

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

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

    L = []
    R = []
    id = list(range(N+1))
    for _ in range(M):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
import math
import string
import fractions
import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop

# from itertools import accumulate
# from collections import deque
import random




def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


def main():
    n,m = map(int, input().split())
    # g =[[] for i in range(m+1)]
    # for i in range(1,m+1):          # mコのgateを作る
    #     g[i]=[0 for i in range(100001)]
    left=-100000
    right=sys.maxsize
    for i in range(m):
        t1,t2 = map(int, input().split())
        # eprint(t1)
        # eprint(t2)
        if left<t1:
            # eprint(left)
            # eprint(t1)
            left =t1
        if t2<right:
            right = t2
        # eprint("left: "+str(left) + "   right: "+str(right) + "   n: "+str(n))

    if right<left or n<left:
        print(0)
    else :                      # 少なくともleft<right
        if n<right:
            print(n+1 - left)
        else:                    #
            print(right+1 - left)
        
            
        
            
        
        

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

result = [1, n]

for card in gates:
    if result[0] < card[0]:
        result[0] = card[0]
    if result[1] > card[1]:
        result[1] = card[1]

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