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 numpy as np

n, m = map(int, input().strip().split())
data = [map(int, input().strip().split()) for _ in range(m)]

left = np.zeros(n + 1, dtype=np.int32)
right = np.zeros(n + 1, dtype=np.int32)

for l, r in data:
    left[l] += 1
    right[r] += 1

card = 0
count = 0
for i in range(n + 1):
    count += left[i]
    if count == m:
        card += 1
    count -= right[i]

print(card)

Submission status: Accepted

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

mxl = 0
mnr = n

for i in range(m):
	if ls_lr[i][0] > mxl:
		mxl = ls_lr[i][0]
	if ls_lr[i][1] < mnr:
		mnr = ls_lr[i][1]

print(max(mnr - mxl + 1, 0))

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
l=[]
zero=[]
one=[]
for i in range(m):
  l.append(list(map(int, input().split())))
for i in range(m):
  zero.append(l[i][0])
  one.append(l[i][1])
print(max(min(one)-max(zero)+1,0))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

l, r = map(int, input().split())
for _ in range(m - 1):
    li, ri = map(int, input().split())
    if ri < l:
        print(0)
        exit()
    elif r < li:
        print(0)
        exit()
    elif l >= li and r >= ri:
        r = ri
    elif l < li and r >= ri:
        l, r = li, ri
    elif l < li and r < ri:
        l = li

print(r - l + 1)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

# (x,y)みたいな構造体をN個まとめてリストにするとき
L = []
R = []
for i in range(M):
    l,r = map(int, input().split())
    L.append(l)
    R.append(r)

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
left = 1
right = 10**5

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())

min, max = map(int, input().split())

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



lis = [i for i in range(min, max+1)]
print(len(lis))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L, R = 1, N
for i in range(M):
  l, r = map(int, input().split())
  L = max(L, l)
  R = min(R, r)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(0, m)]
sum = [0] * (n + 1)
for (l, r) in lr:
  sum[l - 1] += 1
  sum[r] -= 1

cur = 0
ans = 0
for i in range(0, n + 1):
  cur += sum[i]
  if cur == m:
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
max_l = 1
min_r = N
for _ in range(M):
    l, r = map(int, input().split())
    max_l = max(max_l, l)
    min_r = min(min_r, r)

print(max(0, min_r - max_l + 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
maxl = 0
minr = n
for i in range(m):
  l, r = map(int, input().split())
  maxl = max(maxl, l)
  minr = min(minr, r)
if maxl <= minr:
  print(minr - 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, R = list(map(list, zip(*LR)))
print(max(0, min(R)-max(L)+1))

Submission status: Accepted

Solution code:
n,m=map(int, input().split())
list_R=[]
list_L=[]
for _ in range(m):
    L,R=map(int,input().split())
    list_L.append(L)
    list_R.append(R)
L=max(list_L)
R=min(list_R)

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

Submission status: Accepted

Solution code:
#ABC-127-C
N, M = map(int, input().split())
L_list  = []
R_list = []
for _ in range(M):
    Li, Ri = map(int, input().split())
    L_list.append(Li)
    R_list.append(Ri)

max_L = max(L_list)
min_R = min(R_list)

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())
bottom, top = 0, n
for i in range(m):
    l, r = map(int, input().split())
    bottom = max(bottom, l)
    top = min(top, r)
print(max(0, top-bottom+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_c
N, M = list(map(int, input().split()))
L = 0
R = N
for _ in range(M):
    l, r = list(map(int, input().split()))
    L = max(L, l)
    R = min(R, r)

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

Submission status: Accepted

Solution code:
def main():
	import itertools
	n,m = map(int,input().split())
	l = [list(map(int,input().split())) for i in range(m)]
	gaterange = []
	#gate1とgate2の共通部分を出す
	li = []
	ri = []
	for i in range(m):
		li.append(l[i][0])
		ri.append(l[i][1])
	print(len(list(range(max(li)-1,min(ri)))))
	




if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
N, M= map(int, input().split())
L_list=list()
R_list=list()
for i in range(M):
	L, R = map(int, input().split())
	L_list.append(L)
	R_list.append(R)
	
result=min(R_list)-max(L_list)+1

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

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
n, m = [int(i) for i in sys.stdin.readline().split()]
ls = [0 for i in range(n+1)]
for i in range(m):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    l, r = l - 1, r - 1
    ls[l] += 1
    ls[r + 1] -= 1
ls = [i for i in accumulate(ls)]
cnt = 0
for l in ls[:-1]:
    if l == m:
        cnt += 1
print(cnt)

Submission status: Accepted

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

st = 1
en = N
for _ in range(M):
    L, R = map(int, input().split())
    st = max(st, L)
    en = min(en, R)

ans = en - st
if ans < 0:
    print(0)
else:
    print(ans + 1)

Submission status: Accepted

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

x=[]
y=[]

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

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

Submission status: Accepted

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

lmax = 0
rmin = 10 ** 5
for i in range(m):
    lmax = max(lmax,l[i])
    rmin = min(rmin,r[i])

if lmax <= rmin:
    print(rmin - lmax + 1)
else:
    print("0")

Submission status: Accepted

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

lp = 1
rp = n

for i in range(m):
    if lp < lr[i][0]:
        lp = lr[i][0]
    if rp > lr[i][1]:
        rp = lr[i][1]
else:
    if rp - lp < 0:
        ans = 0
    else:
        ans = rp-lp+1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

print(max((R-L+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())
  l = max(l, L)
  r = min(r, R)

ans = 0
for i in range(1,n+1) :
  if l <= i <= r :
    ans += 1
print(ans)

Submission status: Accepted

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

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

under = 1
top = 10**6+10
for i in range(m):
    if LRs[i][0] > under:
        under = LRs[i][0]
    if LRs[i][1] < top:
        top = LRs[i][1]

if not top >= under:
    print(0)
else:

    print(top - under + 1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
N, M=input().split()
N=int(N)
M=int(M)
i=0
ID_list=[]
ID_sublist=[]
gate_possible_list=[]
#センテンスを暗記するのではなく、まずinput()を書いて、膨らます感じで記述する。すると#思考の流れ通りに書ける。素晴らしい！
for i in range(N):
 ID_list.append(i+1)
#print(ID_list)
#for i in range(N)
gate_list =  [list(map(int, input().split(" ")))for i in range(M)]
#センテンスを暗記するのではなく、まずinput()を書いて、膨らます感じで記述する。すると#思考の流れ通りに書ける。素晴らしい！

#x=[]
#y=[]
#t, x, y = [0]*(N+1), [0]*(N+1), [0]*(N+1)#N+1個の空のリストを作成(原点を入れて##るから一つ増える)
#delta_t=0
#delta_x=0
#delta_y=0
#l=0
#t[0],x[0],y[0]=[0, 0, 0]
#とりあえず入力までは完成した．
#for i in range(N):
# t[i+1], x[i+1], y[i+1] = map(int, input().split())
#child_i =  list(map(int, input().split()))
#w_len=len(w)
#w_list=list(w)
#print(len(w_list))
#print(w_list)
#child_il=sorted(child_i)
#print(child_il)#リストの右端のほうが大きい
i=0
j=0
k=0
l=0
m=0
mini=gate_list[0][0]
maxi=gate_list[0][1]
for i in range(M):
  if mini <gate_list[i][0]:
      mini=gate_list[i][0]
i=0
for i in range(M):
  if maxi >gate_list[i][1]:
      maxi=gate_list[i][1]
#print(maxi, mini)
if maxi-mini<0:
    print(0)
else:
 print(maxi-mini+1)


      
#for i in range(M):#ゲートの個数分回す
# for j in range(gate_list[i][1]-gate_list[i][0]+1):
#     gate_possible_list.append(gate_list[i][0]+j)
 #print(gate_possible_list)
# for k in range(len(ID_list)):
#     for j in range(len(gate_possible_list)):
#      if ID_list[k]==gate_possible_list[j]:
#         ID_sublist.append(gate_possible_list[j])
#      else:
#          pass
#
# gate_possible_list=[]
# ID_list=[]
# for m in range(len(ID_sublist)):
#  ID_list.append(ID_sublist[m])
# #print(ID_list)
# ID_sublist=[]
#print(len(ID_list))
#これ時間かかりまくり

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
conditions = [ tuple(map(int,input().split())) for _ in range(m)]
max_l = 0
min_r = float("INF")
for l,r in conditions:
  max_l = max(max_l,l)
  min_r = min(min_r,r)
if min_r < max_l:
  print(0)
else:
  print(min_r - max_l + 1)

Submission status: Accepted

Solution code:
from sys import stdin
n,m = map(int,stdin.readline().split())
#ll = [list(map(int,input().split())) for _ in [0]*m]
#l=[]
#r=[]
#for i in range(m):
#  l.append(ll[i][0])
#  r.append(ll[i][1])
#l = max(l)
#r = min(r)

l=0
r=n
for _ in range(m):
  a,b = map(int,stdin.readline().split())
  l = l if l>a else a
  r = r if r<b else b
print(max([0,r-l+1]))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
#ll = [list(map(int,input().split())) for _ in [0]*m]
#l=[]
#r=[]
#for i in range(m):
#  l.append(ll[i][0])
#  r.append(ll[i][1])
#l = max(l)
#r = min(r)

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

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ll = [list(map(int,input().split())) for _ in [0]*m]
l=[]
r=[]
for i in range(m):
  l.append(ll[i][0])
  r.append(ll[i][1])
l = max(l)
r = min(r)

  
#for _ in range(m):
#  a,b = map(int,input().split())
#  l = max([a,l])
#  r = min([b,r])
print(max([0,r-l+1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC127 C - Prison
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n,m=nm()
l,r=0,10000000

for i in range(m):
    cl,cr=nm()
    l=max(cl,l)
    r=min(cr,r)

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

Submission status: Accepted

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

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

l_max = 1
r_min = N

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def fetch_one_line():
    return stdin.readline().rstrip()


def fetch_int_input():
    return [int(s) for s in fetch_one_line().split()]


def fetch_inputs(times):
    return [fetch_one_line() for _ in range(times)]


def fetch_int_inputs(times):
    return [fetch_int_input() for _ in range(times)]


N, M = fetch_int_input()
gate_len = fetch_int_inputs(M)
# range_card = [[False] * N for _ in range(M)]
l, r = 0, N
for gl in gate_len:
    if l < gl[0]:
        l = gl[0]
    if r > gl[1]:
        r = gl[1]

# range_card = list(zip(*range_card))
# print(sum([all(r) for r in range_card]))
result = r - l + 1
if result < 0: result = 0

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
l_max = -float("inf")
r_min = float("inf")

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>N:
    r_min = N
print(len(range(l_max, r_min+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
max_value, min_value = -1, -1
for i in range(m):
  tmp_min_value, tmp_max_value = map(int, input().split())
  min_value = max([min_value, tmp_min_value]) if min_value >= 0 else tmp_min_value
  max_value = min([max_value, tmp_max_value]) if max_value >= 0 else tmp_max_value

min_value = min_value if min_value <= n else n
max_value = max_value if max_value <= n else n

ret = max_value - min_value + 1
if ret < 0:
  ret = 0
print(ret)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
llis=[]
rlis=[]
for i in range(m):
  l,r=map(int,input().split())
  llis.append(l)
  rlis.append(r)
llis.sort()
ok=True
for i in range(m):
  if llis[m-1]>rlis[i]:
    ok=False
if ok:
  print(min(rlis)-llis[m-1]+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for i in range(m)]
a = 0
b = 100001
for i in range(m):
	a = max(a, lr[i][0])
	b = min(b, lr[i][1])
if b-a < 0:
	print (0)
else:
	print (b-a+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())
    if l > ll:
        ll = l
    if r < rr:
        rr = r
print(max(rr - ll + 1, 0))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
left = 1
right = n
for _ in range(m):
    l, r = map(int, input().split())
    left = max(left, l)
    right = min(right, r)
print(max(right-left+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)

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    tl, tr = map(int, input().split())
    for i in range(M-1):
        L, R = map(int, input().split())
        if tl < L:
            tl = L
        if tr > R:
            tr = R
    if tr >= tl:
        print(tr - tl + 1)
    else:
        print(0)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

tmp1 = 0
tmp2 = float('inf')
val = [[] for i in range(M)]
ans = 0
for i in range(M):
    L,R = map(int, input().split())
    val[i].append(L)
    val[i].append(R)
    if R < tmp1:
        print(ans)
        sys.exit()
    if L > tmp1:
        tmp1 = L
    if R < tmp2:
        tmp2 = R
if tmp2-tmp1+1 < 0:
    print(0)
else:
    print(tmp2-tmp1+1)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
array = []
for i in range(M):
    a = list(map(int,input().split()))
    array.append(a)
maxa = max([i[1] for i in array])
array2 = [0 for i in range(maxa+2)]
for i in array:
    array2[i[0]] += 1
    array2[i[1]+1] -= 1
for i in range(len(array2)-1):
    array2[i+1] += array2[i]
print(array2.count(M))

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def MAP(): return [int(s) for s in input().split()]

N,M = MAP()
G = [MAP() for _ in range(M)]

l = 0
r = 10**5+1
for a,b in G:
	if a>l: l=a
	if b<r: r=b

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

Submission status: Accepted

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

a = {i for i in range(1, N+1)}
L_list = []
R_list = []
for i in range(M):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)
    # print(a, set([i for i in range(L, R+1)]))
    # a = a & {i for i in range(L, R+1)}
    # print(a, set([i for i in range(L, R+1)]))

print(max(min(R_list) - max(L_list) + 1, 0))

Submission status: Accepted

Solution code:
def main():
    LR = open(0).read().split()[2:]
    MAX_L = max(map(int, LR[::2]))
    MIN_R = min(map(int, LR[1::2]))
    print(MIN_R - MAX_L + 1 if MAX_L <= MIN_R else 0)
    return


main()

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+1)
L.sort()
R.sort()
ans=R[0]-L[-1]
print(0 if ans<0 else ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
mnr = []
mxr = []
for i in range(1,M+1):
  mn , mx = map(int,input().split())
  mnr.append(mn)
  mxr.append(mx)
x = max(min(mxr)-max(mnr) +1,0)
print(x)

Submission status: Accepted

Solution code:
# coding: utf-8
N, M = map(int, input().split())
L, R = 0, 10**6
card = []
for i in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r)
print(R - L + 1 if L <= R else 0)

Submission status: Accepted

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

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:
#ABC 127C
import sys
n, m = map(int, sys.stdin.readline().split())

L = 0
R = n
for _ in range(m):
    Li, Ri = map(int, sys.stdin.readline().split())
    if L < Li:
        L = Li
    if Ri < R:
        R = Ri

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
t=[list(map(int,input().split())) for _ in range(m)]
tmp=[1,n]
for i in range(m):
  l,r=t[i]
  if tmp[0]<l:
    tmp[0]=l
  if r<tmp[1]:
    tmp[1]=r
print(max(0,tmp[1]-tmp[0]+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())

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans_l = 1
ans_r = n

for i in range(m):
  l, r = (int(i) for i in input().split())
  if l > ans_l:
    ans_l = l
    
  if r < ans_r:
    ans_r = r

print(max(0, ans_r - ans_l + 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())

min = L[0]
max = R[0]

for j in range(1,M):
    if min < L[j]:
        min = L[j]
    if max > R[j]:
        max = R[j]
if max-min+1 >= 0:
	print(max-min+1)
else:
  print(0)

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(
            think(4, [[1, 3], [2, 4]]),
            2
        )

    def test_2(self):
        self.assertEqual(
            think(10, [[3, 6], [5, 7], [6, 9]]),
            1
        )

    def test_3(self):
        self.assertEqual(
            think(100000, [[1, 100000]]),
            100000
        )


def solve():
    n, list_of_l_and_r = read()
    result = think(n, list_of_l_and_r)
    write(result)


def read():
    n, m = read_int(2)
    list_of_l_and_r = []
    for _ in range(m):
        list_of_l_and_r.append(read_int(2))
    return n, list_of_l_and_r


def read_int(n):
    return read_type(int, n)


def read_float(n):
    return read_type(float, n)


def read_type(t, n):
    return list(map(lambda x: t(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, list_of_l_and_r):
    left = 1
    right = n
    for l, r in list_of_l_and_r:
        if right < l or r < left:
            return 0
        left = max(l, left)
        right = min(r, right)
    return right - left + 1


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
l=0
r=10**5+1
for i in range(m):
    a,b=map(int,input().split())
    l=max(a,l)
    r=min(b,r)
res=r-l+1
if res>=0:
    print(res)
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(m)]
left = lr[0][0]
right = lr[0][-1]
cnt = 0

for i in lr:
  if i[0] > left:
    left = i[0]
  
  if i[-1] < right:
    right = i[-1]

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

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n, m = rli()
    bl, br = 1, n
    for i in range(m):
        l, r = rli()
        bl = max(bl, l)
        br = min(br, r)
    print(max(br-bl+1, 0))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  n, m = list(map(int, input().split()))
  l = 0
  r = 100001
  for i in range(m):
    a, b = map(int, input().split())
    if l < a:
      l = a
    if b < r:
      r = b

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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M =map(int,input().split())
left = 0
right = 10**5 + 1
for i in range(M):
    l,r = map(int,input().split())
    left = max(l,left)
    right = min(r,right)
ans = right - left + 1
if(right - left < 0):
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
a=[]
b=[]
for i in range(M):
  L,R=map(int,input().split())
  a.append(L)
  b.append(R)
c=sorted(a)
d=sorted(b)
if d[0]-c[M-1]>=0:
  print(d[0]-c[M-1]+1)
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print (sum(1 for x in l if x == M))

Submission status: Accepted

Solution code:
import heapq

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

left = heapq.nlargest(1, head)[0]
right = heapq.nsmallest(1, tail)[0]
#print(right, left)

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

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
A = []
B = []
for _ in range(M):
    temp = list(map(int,input().split()))
    A.append(temp[0])
    B.append(temp[1])
cnt = 0
minA = max(A)
maxA = min(B)
if maxA - minA + 1 > 0:
    print(maxA - minA + 1)
else:
    print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
L=[list(map(int,input().split())) for _ in range(m)]
 
lm=0
rm=float('inf')
for i in range(m):
      lm=max(L[i][0],lm)
      rm=min(L[i][1],rm)
print(max(0,rm-lm+1))