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:
def main():
    N, M = map(int, input().split())
    lr = [list(map(int, list(input().split()))) for i in range(M)]
    # N, M = 4, 2
    # lr = [[1, 3], [2, 4]]
    ll = []
    rl = []
    for line in lr:
        ll.append(line[0])
        rl.append(line[1])
    print(min(rl) - max(ll) + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import numpy as np
N, M = map(int, input().split())
l_max = 0
r_min = 1000000000
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

print(r_min - l_max + 1)

Submission status: Wrong Answer

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

def merge(P1, P2):
    L1, R1 = P1
    L2, R2 = P2
    return (max(L1, L2), min(R1, R2))

data = []
for i in range(0, M):
    data.append(map(int, input().split()))

import math
logM = math.floor(math.log2(M))

totyuu = data
# 2つずつくみでしょりしたい
for i in range(0, logM):
    half = []
    if len(totyuu)%2==1:
        half.append(totyuu[-1])
    for j in range(0, int(math.floor(len(totyuu)/2))):
        # じっさいににこずつ
        half.append(merge(totyuu[2*j], totyuu[2*j+1]))
    # halfに半分入っている
    # totyuuをおきかえる
    totyuu = half
data = totyuu

for i in range(0, len(data)):
  tmpL, tmpR = data[i]
  L = max(1, tmpL)
  R = min(tmpR, R)
  if L>R:
    print(0)
    exit()
print(max(0, R-L+1))

Submission status: Wrong Answer

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(R[0]-L[M-1]+1)

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]

if ans_max < ans_min:
    print(0)
elif n < ans_min:
    print(0)
elif n < ans_max:
    print(n - ans_min + 1)
else:
    print(ans_max - ans_min + 1)

Submission status: Wrong Answer

Solution code:
import numpy

N, M= map(int, input().split())
l = []
for i in range(M):
  L, R = map(int, input().split())
  l.append([L, R])
a = numpy.array(l)

print(numpy.amin(a, axis=0)[1] - numpy.amax(a, axis=0)[0] + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split(" "))
l_init,r_init=map(int,input().split(" "))
ans_l=l_init
ans_r=r_init
for i in range(m-1):
	l,r=map(int,input().split(" "))
	next_ans_l=ans_l
	next_ans_r=ans_r
	if ans_l <= l and l <= ans_r:
		next_ans_l=l
	if ans_l <= r and r <= ans_r:
		next_ans_r=r
	ans_l=next_ans_l
	ans_r=next_ans_r
ans=ans_r-ans_l
if ans>n:
	print(n)
else:
	print(ans+1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m = map(int, stdin.readline().split())
mn = 0
mx = n

for i in range(m):
    l, r = map(int, stdin.readline().split())
    mn = max(mn, l)
    mx = min(mx, r)
print(mx - mn + 1)

Submission status: Wrong Answer

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

data = []
l = 1
r = 10**5
for m in range(M):
    li, ri = map(int, input().split(' '))
    if li > l:
      l = li
    if ri < r:
      r = ri

if l<= N <= r:
  cnt = (N - l) + 1
elif r < N:
  cnt = (r - l) + 1
else:
  cnt = 0

print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import heapq                        
hq_left = []
hq_right = []

#スペースなどで区切られた入力が複数行に続くとき
listA=[] #appendのために宣言が必要
N,M= map(int,input().split())

for i in range(M):
    listA.append(list(map(int,input().split())))


candidate_list = [i for i in range(1,N+1)]

for i in range(M):
    hq_left.append(listA[i][0])
    hq_right.append(listA[i][1])
    
if max(hq_left)-min(hq_right) <0:
    print(0)
else:
    print(max(hq_left)-min(hq_right)+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
print(Rmin-Lmax+1)

Submission status: Wrong Answer

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

l_p = 1
r_p = n

for j in range(m):
  l, r = map(int,input().split())
  if l > l_p:
    l_p = l
  if r < r_p:
    r_p = r

a = r_p - l_p + 1

print(a)

Submission status: Wrong Answer

Solution code:
from itertools import chain

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

if M == 1:
  print(N)
  exit()

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

for i in range(len(L)):
  L[i].insert(1,L[i][0]+1)
  
S = list(chain.from_iterable(L))

r = 0
for j in set(S):
  if S.count(j) == M:
    r += 1

print(r)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
  
count = 0
for i in range(1, N+1):
  if LR[M-1][0] >= LR[0][1]:
    if LR[0][0] <= i <= LR[M-1][1] and LR[0][1] <= i <= LR[M-1][1]:
      count += 1
  else:
    if LR[0][0] <= i <= LR[0][1] and LR[M-1][0] <= i <= LR[M-1][1]:
      count += 1

print(count)

Submission status: Wrong Answer

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

Lli = []
Rli =[]
for i in range(M):
    tmp = list(map(int,input().split()))
    Lli.append(tmp[0])
    Rli.append(tmp[1])

print(min(Rli)-max(Lli)+1)

Submission status: Wrong Answer

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

L_max = 1
R_min = 10000

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

ans = set(range(L_max,R_min+1))

print(len(ans))

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(lambda x: int(x)-1, input().split())

sum = min(R) - max(L) + 1

print(sum)

Submission status: Wrong Answer

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

s = (input().split(' '))

n,m = [int(i) for i in s]

l_list = []
r_list = []

for i in range(m):
    s = (input().split(' '))
    l,r = [int(i) for i in s]
    l_list.append(l)
    r_list.append(r)

range = (max(l_list), min(r_list))

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

Submission status: Wrong Answer

Solution code:
import numpy as np
a=[]#入力画面の二文字を別々の文字に格納
a=input().split(" ")
N=int(a[0])
M=int(a[1])
# 2次元配列
G = []
for i in range(M):
    array = list(map(int, input().strip().split()))
    G.append(array)
b=[x[0] for x in G]
c=[x[1] for x in G]

B=max(b)
C=min(c)
y=C-B+1
print(y)

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]

if n < ans_min:
    print(0)
elif n < ans_max:
    print(n - ans_min + 1)
else:
    print(ans_max - ans_min + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
lLst = [0] * M
rLst = [0] * M
ans = 1
for i in range(M):
    lLst[i], rLst[i] = map(int, input().split())

if lLst[M - 1] < rLst[0]:
    ans += rLst[0] - lLst[M - 1]


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L_max = 99999999999
R_min = 0
li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i-1][0] < li[i][0] ):
        L_max = li[i][0]
        if (li[i-1][1] > li[i][1]):
            R_min = li[i][1]
    elif (li[i - 1][1] > li[i][1]):
            R_min = li[i][1]

if (li[M-1][0] < li[M][0] ):
        L_max = li[M][0]
        if (li[M-1][1] > li[M][1]):
            R_min = li[M][1]
elif (li[M - 1][1] > li[M][1]):
            R_min = li[M][1]

if (L_max > N) and (L_max > R_min):
    print(0)
elif(R_min < N):
    print(R_min - L_max + 1)
else:
    print(N + 1 - L_max)

Submission status: Wrong Answer

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

x = e[0]
for i in e:
  s, e = x[0], x[1]
  if x[0] < i[0]:
    s = i[0]
  if x[1] > i[1]:
    e = i[1]
  if s > e:
    break

if s > e:
  print(0)
else:
  print(e-s+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

num=0
max=100000
min=1
for i in range(M):
    a,b=map(int,input().split())
    if(max>=b):
        max=b
    if(a>=min):
        min=a
num=max-min+1
if(num>=N):
    print(N)
else:
    print(num)

Submission status: Wrong Answer

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

seki = set(range(LR[0][0],LR[0][1]+1))
max = LR[0][1]
for i in range(M):
    if max>=LR[i][0]:
        max = LR[i][1]
        seki = seki & set(range(LR[i][0],LR[i][1]+1))
print(len(seki))

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
min = 1
max = n
for var in range(0, m):
  a, b= map(int, input().split())
  if min < a:
    min = a

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
isstr=input()
n,m = [int(i) for i in isstr.split(' ')]
l=[0]*m
r=[0]*m
for i in range(m):
    l[i],r[i]=[int(i) for i in input().split(' ')]
count=0
for i in range(n):
  if i>=min(l) and i<=max(r):
    count+=1
print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

N, M = [int(x) for x in stdin.readline().rstrip().split()]

#[競プロ等におけるpython3の標準入力 \- Qiita](https://qiita.com/zenrshon/items/c4f3849552348b3dbe67)
L = []
R = []
for i in range(M):
    l,r=[int(i) for i in input().split()]
    L.append(l)
    R.append(r)

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

Submission status: Wrong Answer

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

gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
max_under_th = max([l[0] for l in gate_cond])
min_upper_th = min([l[1] for l in gate_cond])
if min_upper_th - max_under_th > 0:
    print(min_upper_th - max_under_th + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
card_num, gate_num = map(int, input().split())

ans_start, ans_end = map(int, input().split())
for _ in range(gate_num-1):
    s, e = map(int, input().split())
    if e < ans_start or ans_end < s:
        continue
    elif s < ans_start and ans_start < e < ans_end:
        ans_end = e
    elif ans_start < s < ans_end and ans_end < e:
        ans_start = s

print(ans_end - ans_start + 1)

Submission status: Wrong Answer

Solution code:
x = input()
li = x.split()
n = int(li[0])
m = int(li[1])
o = 0
f = []
for k in range (m):
  y = input()
  e = y.split()
  l = int(e[0])
  r = int(e[1])
  f.append(l)
  f.append(r)
for j in range (n):
  s = int(j) + 1
  ans = 0
  a = f[0]
  d = f[1]
  if(a <= s and s <= d):
    ans += 1
  b = f[2 * (m-1)]
  c = f[2 * (m-1) + 1]
  if(b <= s and s <= c):
    ans += 1
  if(ans == 2):
    o += 1

print(o)

Submission status: Wrong Answer

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

gates = list(range(1, N))
for m in range(M):
  L, R = map(int, input().split())
  gates = [g for g in gates if L <= g and g <= R]
  if len(gates) < 1:
    break

print(len(gates))

Submission status: Wrong Answer

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

N = inputs[0]
M = inputs[1]

start = 1
end = N

for i in range(M):
    inputs = list(map(int, input().split()))
    if start < inputs[0]:
        start = inputs[0]
    if inputs[1] < end:
        end = inputs[1]

print(end - start + 1)

Submission status: Wrong Answer

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

for j in range(m):
  l, r = map(int,input().split())
  l_lis.append(l)
  r_lis.append(r)

a = min(r_lis) - max(l_lis) + 1

print(a)

Submission status: Wrong Answer

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

inu = []
neko = []
for i in range(m):
	x = list(range(res[i][0],res[i][1]+1))
	inu.append(x)
    
for j in range(m):
	neko += inu[j]

dup = [x for x in set(neko) if neko.count(x) == m]
print(dup)
print(len(dup))

Submission status: Wrong Answer

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

ok = []
for L, R in LR:
  ok.append(list(range(L-1, R)))
if len(ok) == 1:
  cnt = N
else:
  cnt = 0
  for i in range(N):
    tmp = 0
    for o in ok:
      if i in o:
        tmp += 1
    if tmp == M:
      cnt += 1
print(cnt)

Submission status: Wrong Answer

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

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

a1 = max(L)
a2 = min(R)

ans = a2-a1+1

print(ans)

Submission status: Wrong Answer

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

def merge(P1, P2):
    L1, R1 = P1
    L2, R2 = P2
    return (max(L1, L2), min(R1, R2))

data = []
for i in range(0, M):
    data.append(map(int, input().split()))

import math
logM = math.floor(math.log2(M))
import copy

half = []
# 2つずつくみでしょりしたい
for i in range(0, logM):
    if len(data)%2==1:
        half.append(data[-1])
    for j in range(0, int(math.floor(len(data)/2))):
        # じっさいににこずつ
        half.append(merge(data[2*j], data[2*j+1]))
    # dataをおきかえる
    data = copy.deepcopy(half)

for i in range(0, len(data)):
  tmpL, tmpR = data[i]
  L = max(1, tmpL)
  R = min(tmpR, R)
  if L>R:
    print(0)
    exit()
print(max(0, R-L+1))

Submission status: Wrong Answer

Solution code:
import collections
a = list(map(int,input().split()))
k = []
t=0
for i in range(a[1]):
    b,c = map(int,input().split())
    for j in range(c-b+1):
        k.append(b+j)
d = collections.Counter(k)
f = d.values()
f = list(f)
if max(f) == a[1]:
    print(f.count(max(f)))

Submission status: Wrong Answer

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

a=[]
b=[]
for i in range(M):

    X, Y= map(int, input().split())
    a.append(X)
    b.append(Y)
    
    if a[0]<a[i]:
      a[0]=a[i]
      
    if b[0]>b[i]:
      b[0]=b[i]
      
print(b[0]-a[0]+1)

Submission status: Wrong Answer

Solution code:
nm = input().split()
minArr = []
maxArr = []

for m in range(int(nm[1])):
    tmp = input().split()
    minArr.append(int(tmp[0]))
    maxArr.append(int(tmp[1]))

print(min(maxArr) - max(minArr)+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
min = 1
max = n
for var in range(0, m):
  a, b= map(int, input().split())
  if min < a:
    min = a

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

Submission status: Wrong Answer

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

X = {i+1 for i in range(N)}

for i in range(M):
    print({j for j in range(ls[i][0],ls[i][1]+1)})
    X = X & {j for j in range(ls[i][0],ls[i][1]+1)}

print(len(X))

Submission status: Wrong Answer

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

seki = set(range(LR[0][0],LR[0][1]+1))
max = 1
for i in range(M):
    if max>=LR[i][0]:
        max = LR[i][1]
        seki = seki & set(range(LR[i][0],LR[i][1]+1))
print(len(seki))

Submission status: Wrong Answer

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

num=0
max=100000
min=0
for i in range(M):
    a,b=map(int,input().split())
    if(max>=b):
        max=b
    if(a>=min):
        min=a
num=max-min+1
if(num>=N):
    print(N)
else:
    print(num)

Submission status: Wrong Answer

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

con = 0

for i in range(N):
    if i < N/2:
        for j in range(M-1, 0, -1):
            if L[j] > i or i > R[j]:
                break
        else:
            con += 1
    else:
        for j in range(M):
            if L[j] > i or i > R[j]:
                break
        else:
            con += 1


print(con)

Submission status: Wrong Answer

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

max = 0
min = 0

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

    if min <= L:
        min = L
    elif min == 0:
        min = L

    if max >= R:
        max = R
    elif max == 0:
        max = R

ans = max - min + 1

print(ans)

Submission status: Wrong Answer

Solution code:
list_l = []
list_R = []
ID,gate = map(int,input().split())
s = 0
t = 0
for i in range(gate):
  l,r = map(int,input().split())
  list_l.append(l)
  list_R.append(r)

for i in range(ID):
  for j in range(gate):
    if list_l[j] <= i <= list_R[j]:
      s += 1
    else:
      None
  if s == gate:
    t += 1
  s = 0
print(t)

Submission status: Wrong Answer

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

num=0
max=100000
min=0
for i in range(M):
    a,b=map(int,input().split())
    if(max>=b):
        max=b
    if(a>=min):
        min=a
num=max-min+1
if(num>N):
    print(N)
else:
    print(num)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
for m in range(M):
    L,R=map(int,input().split())
count=0
for n in range(1,N+1,1):
    if n>=L and n<=R:
        count=count+1
    else :
        pass

Submission status: Wrong Answer

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

gate_cond = [[int(x) for x in input().split()] for _ in range(m)]
max_under_th = max([l[0] for l in gate_cond])
min_upper_th = min([l[1] for l in gate_cond])

print(min_upper_th - max_under_th + 1)

Submission status: Wrong Answer

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

x = e[0]
for i in e:
  s, e = x[0], x[1]
  if x[0] < i[0]:
    s = i[0]
  if x[1] > i[1]:
    e = i[1]

if s > e:
  print(0)
else:
  print(e-s+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
gates = []
for i in range(m):
	l,r = map(int,input().split())
	gates.append((l,r))
res = 0
L,R = gates[0]
if len(gates) < 2 :
	if L<=n<=R :
		print(1)
	else :
		print(0)
else :
	for i in range(L,R+1) :
		flag =False
		for l,r in gates[1:] :
			if l<=i<=r :
				flag =True
			else :
				flag = False
		res += 1 if flag else 0
	print(res)

Submission status: Wrong Answer

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

max_Li = -1
min_Ri = 10000000
for i in range(M):
    L_i, R_i = map(int, input().split())
    max_Li = max(max_Li, L_i)
    min_Ri = min(min_Ri, R_i)


print(min_Ri - max_Li + 1)

Submission status: Wrong Answer

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
ids = set(range(1, n+1))
l_1 = 1
r_1 = n

for gate in args[1:]:
  l, r = map(int, gate.split(' '))  
  if l > l_1:
    l_1 = l
  if r < r_1:
    r_1 = r
  
print(r_1 - l_1 + 1)

Submission status: Wrong Answer

Solution code:
import collections
x = input().split()
n = int(x[0])
m = int(x[1])
a = list()
for i in range(m):
  y = input().split()
  l = int(y[0])
  r = int(y[1])
  for i in range(l,r+1):
    a.append(i)
c = collections.Counter(a)
b = 0
for i in range(n):
  if c[i] == m:
    b += 1
print(b)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

lr_mat = np.zeros((m, 2))

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

left_max = max(lr_mat[:, 0])
right_min = min(lr_mat[:, 1])

cnt = 0
for i in range(n):
    if(left_max <= i) and (i <= right_min):
        cnt += 1

print(cnt)

Submission status: Wrong Answer

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)
count = 0
for i in range(1,n+1):
        if l[m-1] <= i <= r[0]: 
            count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
  
count = 0
for i in range(1, N+1):
  if LR[0][0] <= i <= LR[0][1] and LR[M-1][0] <= i <= LR[M-1][1]:
    count += 1

print(count)

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

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "))
min_l = 0
max_l = 10**5
for _ in range(m):
    l, r = map(int, input().split(" "))
    if min_l < l:
        min_l = l
    if r < max_l:
        max_l = r
if max_l <= min_l:
    print(0)
else:
    print(max_l - min_l + 1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

n, m = sys.stdin.readline().rstrip().split(' ')
l, r = sys.stdin.readline().rstrip().split(' ')
l, r = int(l), int(r)
result = np.ones(r)
result[:l-1] = 0
min_r = r-1
for _ in range(int(m)-1):
  l, r = sys.stdin.readline().rstrip().split(' ')
  l, r = int(l)-1, int(r)-1
  result[:l] = 0
  if min_r >= r:
    result[r:] = 0
    min_r = r
print(int(result.sum()))

Submission status: Wrong Answer

Solution code:
nm = input().split()
args = []
minValue = 0
maxValue = 100001
for m in range(int(nm[1])):
    tmp = input().split()
    if (int(tmp[0]) >= minValue):
        minValue = int(tmp[0])
    if (int(tmp[1]) <= maxValue):
        maxValue = int(tmp[1])

if minValue == 0 or maxValue == 10001:
    print(0)
else:
    print(maxValue - minValue + 1)

Submission status: Wrong Answer

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

maxNum = 0
minNum = 0

for i in range(m):
    a, b = map(int, input().split())
    if maxNum == 0:
        maxNum = b
    else:
        if maxNum > b:
            maxNum = b

    if minNum == 0:
        minNum = a
    else:
        if minNum < a:
            minNum = a

if maxNum - minNum >= 0:
    print(maxNum - minNum + 1)

Submission status: Wrong Answer

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

max=100000
min=0
for i in range(M):
    a,b=map(int,input().split())
    if(max>=b):
        max=b
    if(a>=min):
        min=a

num=max-min+1


if(num>N):
    print(N)
else:
    print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ls = []
count = 0
while count < m:
    temp = list(map(int, input().split()))
    ls.append(temp)
    count = count + 1
a_max = 1
b_min = n
for s in ls:
    a = s[0]
    b = s[1]
    if a > a_max:
        a_max = a
    if b < b_min:
        b_min = b
print(int(b_min - a_max) + 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N, M = makeMultiInteger(input_string[0])
    S = makeIntMatrix(input_string[1:])
    c = 0
    ra = range(1, N+1)
        
    min_range = ra
    min_l = ra[0]
    min_r = ra[-1]
    for s in S:
        l = s[0]
        r = s[1]
        if l > min_range[0]:
            min_l = l
        if r < min_range[-1]:
            min_r = r
        min_range = range(min_l, min_r+1)
        if len(min_range) == 0:
            return 0
    if ra[-1] > min_range[-1]:
        return len(min_range)
    else:
        return len(ra)



def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
lmax,mmin = 0,1000000
for i in range(M):
    l,m = map(int,input().split())
    if lmax < l:
        lmax = l
    if mmin > m:
        mmin = m

if lmax == mmin:
    ans = 1
else:
    ans = mmin-lmax
    ans += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

seki = set(range(LR[0][0],LR[0][1]+1))
max = LR[0][1]
for i in range(M):
    if max<=LR[i][0]:
        max = LR[i][1]
        seki = seki & set(range(LR[i][0],LR[i][1]+1))
print(len(seki))

Submission status: Wrong Answer

Solution code:
nm = input().split()
args = []
minValue = 0
maxValue = 100001
for m in range(int(nm[1])):
    tmp = input().split()
    if (int(tmp[0]) >= minValue):
        minValue = int(tmp[0])
    if (int(tmp[1]) <= maxValue):
        maxValue = int(tmp[1])

print(maxValue - minValue + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

count = np.zeros(n)

for i in range(m):
    l_, r_ = map(int, input().split())
    count[l_:r_+1]+=1

print(np.sum(count==m))

Submission status: Wrong Answer

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

N = inputs[0]
M = inputs[1]

start = 0
end = N

for i in range(M):
    inputs = list(map(int, input().split()))
    if start < inputs[0]:
        start = inputs[0]
    if inputs[1] < end:
        end = inputs[1]

print(end - start + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

num_card = 0

for i in range(0, N):
    for j in range(0, M-1):
        if not (i >= LR_list[j][0] and i <= LR_list[j][1]):
            break
    else:
        num_card += 1

print(num_card)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L_max = 99999999999
R_min = 0
li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i-1][0] < li[i][0] ):
        L_max = li[i][0]
        if (li[i-1][1] > li[i][1]):
            R_min = li[i][1]
    elif (li[i - 1][1] > li[i][1]):
            R_min = li[i][1]

if (li[M-1][0] < li[M][0] ):
        L_max = li[M][0]
        if (li[M-1][1] > li[M][1]):
            R_min = li[M][1]
elif (li[M - 1][1] > li[M][1]):
            R_min = li[M][1]

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

Submission status: Wrong Answer

Solution code:
#from collections import defaultdict
n,m=map(int,input().split())
mi = 0
ma = 10**5+1
for i in range(m):
  s,e = map(int,input().split())
  if s > mi:
    mi = s
#    print('mi',mi)
  if e < ma:
    ma = e
#    print('ma',ma)
print(abs(ma-mi)+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
LL, RR = map(int, input().split())
for i in range(M-1):
    L, R = map(int, input().split())
    LL = max(L, LL)
    RR = min(R, RR)
print(RR-LL+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split(' '))
left = float('-inf')
right = float('inf')
for i in range(M):
  L,R = map(int,input().split(' '))
  if left <= L:
    left = L
  if R <= right:
    right = R
  if right- left == 0:
    print(1)
    exit()
  if (right - left) < 0:
    print(0)
    exit()
print(right-left+1)

Submission status: Wrong Answer

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

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

if m!=1:
    for i in range(n):
        for j in range(m):
            L=l[j:]
            R=r[:j+1]
            if L.count(i+1)>0 or R.count(i+1)>0: cnt+=1
            if cnt==m: key+=1
        cnt=0
else: key=n

print(key)

Submission status: Wrong Answer

Solution code:


v = [int(d) for d in input().split()]

LM = []
for d in range(v[1]):
   LM.append([int(d) for d in input().split()])

L = [d[0] for d in LM]
M = [d[1] for d in LM]
L.sort()
M.sort()
if M[0] <= L[-1]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')