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: Wrong Answer

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

gate_s = sorted(gate,key=lambda x:x[0])
min_r = 1000000
max_l = 0

for i in range(M):
  #print(gate_s[i])
  if gate_s[i][1] < min_r:
    min_r = gate_s[i][1]
    #print(min_r)
  if gate_s[i][0] > max_l:
    max_l = gate_s[i][0]
    #print(max_l)
if N >= min_r-max_l+1:
  print(min_r-max_l+1)
else:
  print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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(min(R)-max(L)+1)

Submission status: Wrong Answer

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)

min_haba = set(range(max_L, min_R + 1))
ids = set()
for L, M in haba:
    if max_L - 1 < L and R < min_R:
        h = set(range(L, R + 1))
        ids = ids.union(h.intersection(min_haba))

print(len(ids))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = min_r - max_l + 1

# Output
print(ans)

Submission status: Wrong Answer

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 N < l0:
    print(0)
elif N < l1:
    print(N - l0 + 1)
else:
    print(l1 - l0 + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L, R = map(int, input().split())
if M == 1:
    print(R - L + 1)
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: Wrong Answer

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

l = 1
r = n



for list in lr:
    if list[0] > l:
        l = list[0]
    if list[1] < r:
        r = list[1]


print(r-l+1)

Submission status: Wrong Answer

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)

print(rs - ls + 1)

Submission status: Wrong Answer

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)))
print(minr - maxl +1)

Submission status: Wrong Answer

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())
counter = 0
print(min(R) - max(L) + 1)

Submission status: Wrong Answer

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

N, M = int(N), int(M)

mini = 1
maxi = N
for i in range(M):
    L, R = input().split()
    mini, maxi = max(mini, int(L)), min(maxi, int(R))
print(maxi-mini+1)

Submission status: Wrong Answer

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)
    if N >= rmin:
        ans = rmin - lmax + 1
    else:
        ans = N - lmax + 1
    print(ans)

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

Submission status: Wrong Answer

Solution code:
data = [int(s) for s in  input().split()]
l = 0
r = 10000000
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)
        break

    if r > gate[1]:
        r = gate[1]

print(r-l +1)

Submission status: Wrong Answer

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
  
print(j - i + 1)

Submission status: Wrong Answer

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

print(rm-lm+1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
Ls, Rs = [], []
for i in range(M):
    n, m = list(map(int, input().split()))
    Ls.append(n)
    Rs.append(m)

ll = Ls[0]
rr  =Rs[0]
for i in range(M):
    ll = max(ll, Ls[i])
    rr = min(rr, Rs[i])

print(rr - ll + 1)

Submission status: Wrong Answer

Solution code:
str = input().split()
N = int(str[0])
M = int(str[1])
L = []
R = []
for i in range(M):
    str = input().split()
    L.append(int(str[0]))
    R.append(int(str[1]))

mini = 1
maxi = N
    
for i in range(M):
    if mini <= L[i]:
        mini = L[i]
    if maxi >= R[i]:
        maxi = R[i]
        
if (maxi - mini) != 0:
    print(maxi - mini+1)
else:
    print(maxi - mini)

Submission status: Wrong Answer

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)

result = minR - maxL + 1
print(result)

Submission status: Wrong Answer

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

# 最初の 2つの値を初期の下限、上限を定めた範囲とする
minV, maxV = LR[0]

# 次の行以降について、下限と上限の範囲が狭まるならば更新していく
for m in range(1, M):
    if minV < LR[m][0]:
        minV = LR[m][0]
    if LR[m][1] < maxV:
        maxV = LR[m][1]

# 上限と下限の間に存在する整数値
print(maxV - minV + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if ue - shita <= 0:
    print(0)
else:
    print(ue - shita + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc127/tasks/abc127_c

NM = [int(i) for i in input().split()]
LR = []
for i in range(NM[1]):
    LR.append([int(i) for i in input().split()])

mi = LR[0][0]
ma = LR[0][1]
for i in range(NM[1]):
    if LR[i][0] > mi:
        mi = LR[i][0]
    if LR[i][1] < ma:
        ma = LR[i][1]

if ma > NM[0]:
    ma = NM[0]
if mi > NM[0]:
    mi = ma + 1
print(ma - mi + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,raw_input().split())
L = 1
R = N
for a in range(M):
  l,r = map(int,raw_input().split())
  if L < l: L=l
  if R > r: R=r
print R-L+1

Submission status: Wrong Answer

Solution code:
l = raw_input().split()
N = int(l[0])
M = int(l[1])

L = []
R = []

for i in range(M):
    l = raw_input().split()
    L.append(int(l[0]))
    R.append(int(l[1]))

l = 0
r = 0
for i in range(M - 1):
    if L[M - 1] == L[M - i - 1]:
        l = M - i - 1
    if R[0] == R[i]:
        r = i
print(R[r] - L[l] + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(maximum - minimum + 1)

Submission status: Wrong Answer

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

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

print(R - L + 1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import bisect
N,M = map(int,input().split())
n = list(range(1,N+1))
L_list,M_list = [],[]
for i in range(M):
    l,m = map(int,input().split())
    L_list.append(l)
    M_list.append(m)

L_list.sort()
M_list.sort()
lmax = L_list[-1]
mmin = M_list[0]
min = bisect.bisect_left(n,lmax)
max = bisect.bisect_left(n,mmin)
ans = n[min:max]
print(len(ans)+1)

Submission status: Wrong Answer

Solution code:
import sys

def solve(n, m, l, r):
    l_max = max(l)
    r_min = min(r)
    if n < l_max:
        return 0
    elif l_max <= n < r_min:
        return n - l_max + 1
    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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
i = input().split(" ")

N = int(i[0])
M = int(i[1])

mi = 0
ma = N

for j in range(M):
	i = input().split(" ")
	l = int(i[0])
	r = int(i[1])
	if l >= mi:
		mi = l
	if r <= ma:
		ma = r
	print(mi, ma)

r = ma - mi + 1

if r > 0:
	print(r)
else:
	print(0)

Submission status: Wrong Answer

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

import sys

input = sys.stdin.readline

def main():
    N, M = [int(e) for e in input().split()]
    min_l = 1
    min_r = 100000
    for l in sys.stdin:
        l, r = [int(e) for e in l.strip().split()]
        if l > min_l:
            min_l = l
        if r < min_r:
            min_r = r
    print(min_r + 1 - min_l)

main()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
gate=[input() for __ in range(M)]
s,e=map(int,gate[0].split())
for n in gate:
    pre_s,pre_e=map(int,n.split())
    s=(s if s>pre_s else pre_s)
    e=(e if e<pre_e else pre_e)

print(e+1-s)

Submission status: Wrong Answer

Solution code:

l = 0
r = 100001

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

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

print(r-l+1)

Submission status: Wrong Answer

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

# N,M=10,3
# L=[1,2]
# R=[3,4]
print(min(R)-max(L)+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc127_c

N, M = map(int, input().split())
pairs = [[int(c) for c in input().split()] for _ in range(M)]
pairs.sort(key=lambda pair: pair[1])

_, right = pairs[0]
res = -1
for (left, _) in pairs:
    res = max(left, res)

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

Submission status: Wrong Answer

Solution code:
t = input().split()
n = int(t[0])
m = int(t[1])

low = 0
high = n
for i in range(0, m):
  tmp = input().split()
  if int(tmp[0]) > low:
    low = int(tmp[0])
  if int(tmp[1]) < high:
    high = int(tmp[1])

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

Submission status: Wrong Answer

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])
    print(lr2[0][1] - lr1[-1][0] + 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

l_max = 1
r_min = n
for l, r in lrs:
    l_max = max(l, l_max)
    r_min = min(r, r_min)

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

Submission status: Wrong Answer

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

n,m = input().split()

n = int(n)
m = int(m)

min = 1
max = n
for i in range(m):
    _min,_max = input().split()
    _min = int(_min)
    _max = int(_max)

    if min < _min:
        min = _min

    if max > _max:
        max = _max

print(max - min + 1)

Submission status: Wrong Answer

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

L = []
R = []
for i in range(M):
    L1,R1=[int(i) for i in input().split()]
    L.append(L1)
    R.append(R1)
lmax = max(L)
rmax = min(R)
print (rmax-lmax+1)
#for i in range(0,M-1):
#    if L[i+1] >= L[i]:
#        lmax = L[i+1]
#    else:
#        lmax = lmax

Submission status: Wrong Answer

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

ans_min = lr[0][0]
ans_max = lr[0][1]

for i in range(m - 1):
    t = i + 1
    if lr[t][0] > lr[t - 1][0]:
        ans_min = lr[t][0]
    if lr[t][1] < lr[t - 1][1]:
        ans_max = lr[t][1]

ans = ans_max - ans_min + 1

if ans_max < ans_min:
    print(0)
elif ans > n:
    print(n)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
K = []
F = 0
for i in range(M):
  A,B = map(int,input().split())
  F = max(A,F)
  K.append(B)
counter = 0
for i in range(len(K)):
  if (K[i]>=F):
    counter+=1
print(counter)

Submission status: Wrong Answer

Solution code:
str = input().split()
N = int(str[0])
M = int(str[1])
L = []
R = []
for i in range(M):
    str = input().split()
    L.append(int(str[0]))
    R.append(int(str[1]))

mini = 1
maxi = N
    
for i in range(M):
    if mini <= L[i]:
        mini = L[i]
    if maxi >= R[i]:
        maxi = R[i]
        
print(maxi - mini+1)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
max_l = 0
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 = n - (n - min_r)
ans = ans - max_l + 1
print(ans)

Submission status: Wrong Answer

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

lr_min=1145141919
lr_max=0

for i in range(m):
    lr_max=max(lr_max,lr[i][0])
    lr_min=min(lr_min,lr[i][1])

print(lr_min-lr_max+1)

Submission status: Wrong Answer

Solution code:

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

L_li = []
R_li = []

for i in range(M):
    Li, Ri = list(map(int,input().split()))
    L_li.append(Li)
    R_li.append(Ri)


if N < max(L_li):
    ans = 0
elif min(R_li) < N:
    ans = 0
else:
    ans = min(R_li)-max(L_li)+1
    
print(str(ans))

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]



n, m = na()
l, r = [0] * m, [0] * m
for i in range(m):
    l[i], r[i] = na()

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

Submission status: Wrong Answer

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

tmp1 = 0
tmp2 = 10 ** 9
for t in target:
    tmp1 = max(t[0],tmp1)
    tmp2 = min(t[1],tmp2)
    #print(tmp1)
    #print(tmp2)

print(tmp2-tmp1 + 1)

Submission status: Wrong Answer

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

N,M = [int(i) for i in input().split()]
L_max = 0
R_min = 10**9
for _ in range(M):
    L,R = [int(i) for i in input().split()]
    L_max = max(L_max,L)
    R_min = min(R_min,R)

print(R_min-L_max+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
for num in range(M):
    l, r = map(int, input().split())
    if num == 0:
        minimam = l
        maximam = r
    else:
        minimam = max(minimam, l)
        maximam = min(maximam, r)
print(maximam - minimam + 1)

Submission status: Wrong Answer

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(min(Rs)-max(Ls)+1)

Submission status: Wrong Answer

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)

print(r-l+1)

Submission status: Wrong Answer

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 = 0
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: Wrong Answer

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

Submission status: Wrong Answer

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, 100000000)
for _ in range(M):
    lr = tuple(map(int, input().split()))
    r = ando(r, lr)

print(r[1] - r[0] + 1)

Submission status: Wrong Answer

Solution code:
def main():
    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)
    lmax = max(L)
    rmin = min(R)
    print(int(rmin - lmax + 1))


main()

Submission status: Wrong Answer

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)

print(min(f) - max(e) + 1)

Submission status: Wrong Answer

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]

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

Submission status: Wrong Answer

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

for i in range(1,n+2):
    res[i] += res[i-1]

max_num = max(res)
print(res.count(max_num))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

L_li = []
R_li = []

for i in range(M):
    Li, Ri = list(map(int,input().split()))
    L_li.append(Li)
    R_li.append(Ri)


if max(L_li) < N:
    ans = 0
elif N < min(R_li):
    ans = 0
else:
    ans = min(R_li)-max(L_li)+1
    
print(str(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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(ue - shita + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
nmin = 1
nmax = N
for _ in range(M):
    l, r = map(int, input().split())
    nmin = max(nmin, l)
    nmax = min(nmax, r)
print(nmax - nmin + 1)

Submission status: Wrong Answer

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

mi = 1
ma = N

for i in range(M):
    if mi < List[i][0]:
        mi = List[i][0]
    if ma > List[i][1]:
        ma = List[i][1]

diff = ma - mi + 1      
print("{}".format(diff))

Submission status: Wrong Answer

Solution code:
import sys
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
n, m = MI()
lr = [LI() for _ in range(m)]
l = lr[0][0]
r = lr [0][1]
for i in range(1,m):
    if l <= lr[i][0] <= r:
        l = lr[i][0] 
    if l <= lr[i][1] <= r:
        r = lr[i][1]
print(r - l + 1)

Submission status: Wrong Answer

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)
print(min(list2)-max(list1)+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
str = input().split()
N = int(str[0])
M = int(str[1])
L = []
R = []
for i in range(M):
    str = input().split()
    L.append(int(str[0]))
    R.append(int(str[1]))

mini = 1
maxi = N
    
for i in range(M):
    if mini <= L[i]:
        mini = L[i]
        print("mini")
        print(mini)
    if maxi >= R[i]:
        maxi = R[i]
        print("maxi")
        print(maxi)
        
print(maxi - mini+1)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
LR = [LIST() for _ in range(M)]
lis = [0]*(N+1)

for L, R in LR:
	lis[L] += 1
	lis[R] -= 1

tmp = 0
max_ = 0
range_ = 0
flag = False
for i in lis:
	tmp += i
	if tmp == M:
		flag = True
		range_ += 1
if flag == True:
	print(range_+1)
else:
	print(0)

Submission status: Wrong Answer

Solution code:
i = input().split(" ")

N = int(i[0])
M = int(i[1])

mi = 0
ma = N

for j in range(M):
	i = input().split(" ")
	l = int(i[0])
	r = int(i[1])
	if l > mi:
		mi = l
	if r < ma:
		ma = r
	print(mi, ma)

print(ma - mi + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
n,m=(int(i) for i in input().split())
list_a=[]
list_b=[]
for i in range(m):
  a,b=(int(i) for i in input().split())
  list_a.append(a)
  list_b.append(b)
  
max_a=max(list_a)
min_b=min(list_b)

print(min_b-max_a+1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
Ls, Rs = [], []
for i in range(M):
    n, m = list(map(int, input().split()))
    Ls.append(n)
    Rs.append(m)

ll = Ls[0]
rr  =Rs[-1]
for i in range(M):
    ll = max(ll, Ls[i])
    rr = min(rr, Rs[i])

print(rr - ll + 1)

Submission status: Wrong Answer

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(r - l + 1)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc127/tasks/abc127_c

NM = [int(i) for i in input().split()]
LR = []
for i in range(NM[1]):
    LR.append([int(i) for i in input().split()])

mi = LR[0][0]
ma = LR[0][1]
for i in range(NM[1]):
    if LR[i][0] > mi:
        mi = LR[i][0]
    if LR[i][1] < ma:
        ma = LR[i][1]
n = 0
if ma - mi + 1 > NM[0]:
    n = NM[0]
else:
    n = ma - mi + 1
print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n, m = map(int, input().split())
data = [0 for i in range(n + 1)]
for i in range(m):
    l, r = map(int, input().split())
    for j in range(l, r + 1):
        data[j] += 1

print(data.count(max(data)))

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
a=[]
l,r = map(int, input().split())
ans = [i for i in range(l, r+1)]
for i in range(m-1):
    a=[]
    l,r = map(int, input().split())
    a=[i for i in range(l+1, r+1)]
    for ele in ans:
        if ele not in a:
            ans.remove(ele)
print(len(ans))

Submission status: Wrong Answer

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

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

a = min(l)
b = max(r)

ans = b - a + 1
if ans <= 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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 = 0
pass_max = n

for li, ri in zip(l, r):
    if li > pass_min:
        pass_min = li
    if ri < pass_max:
        pass_max = ri
        
print(pass_max - pass_min + 1)

Submission status: Wrong Answer

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

import sys

input = sys.stdin.readline

def main():
    N, M = [int(e) for e in input().split()]
    min_l = 0
    min_r = 100000000
    for l in sys.stdin:
        l, r = [int(e) for e in l.strip().split()]
        if l > min_l:
            min_l = l
        if r < min_r:
            min_r = r
    print(min_r + 1 - min_l)

main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l = 1
m = N
for i in range(M):
    l_i, m_i = map(int, input().split())
    l = max(l, l_i)
    m = min(m, m_i)
print(m - l + 1)

Submission status: Wrong Answer

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 > N):
    result = N
print(result)

Submission status: Wrong Answer

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: Wrong Answer

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

Submission status: Wrong Answer

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

max_l = 1
min_r = N

for i in range(M):
    l, r = map(int, input().split())
    max_l = max(max_l, l)
    min_r = min(min_r, r)
    if max_l >= min_r:
        print(0)
        exit()

print(min_r - max_l + 1)

Submission status: Wrong Answer

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