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

from collections import defaultdict


def main():
	Ls = []
	cardDic = defaultdict(int)
	Rs = []
	for i in range(M):
		inputs= list(map(int,input().split()))
		Ls.append(inputs[0])
		cardDic[inputs[0]] += 1
		Rs.append(inputs[1])
		cardDic[inputs[1]] += 1

	if M == 1:
		print(N)
		return
	result = []
	for i in range(1, M + 1):
		Li = Ls[i - 1]
		LiPlusOne = Li + 1
		Ri = Rs[i - 1]
		res = set([Li, LiPlusOne, Ri])
		print(res)
		if len(result) == 0:
			result.extend(list(res))
		result = list(set(result) & res)

	answer = 0
	for i in result:
		if i in cardDic:
			answer += 1
	print(answer)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

answer = 0

upper = a
under = 0

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

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

Submission status: Wrong Answer

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

print(min(r[0::])-max(l[0::])+1)

Submission status: Wrong Answer

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

cards = np.zeros(N) 
L = np.zeros(M)
R = np.zeros(M)
for j in range(M):
    L[j], R[j] = map(int, input().split())

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

Submission status: Wrong Answer

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

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

inputs =  list(map(int, input().split()))
start = inputs[0]
end = inputs[1]

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

if N < end:
    end = N

if N < start:
    start = end+1

print(end - start + 1)

Submission status: Wrong Answer

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

L = [0] * M
R = [0] * M

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

ans = range(1,N)

for i in range(M):
  ans = [j for j in ans if L[i]<=j and R[i]>=j]

print(len(ans))

Submission status: Wrong Answer

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

L =[]
R =[]

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

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

Submission status: 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
maximum = N if N < maximum else maximum

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
minNum = 1
maxNum = N
for lr in range(M):
  l, r = map(int, input().split())
  if minNum <= l and r <= maxNum:
    minNum = l
    maxNum = r
  elif minNum <= l <= maxNum and maxNum <= r:
    minNum = l
  elif l <= minNum and minNum <= r <= maxNum:
    maxNum = r
print(maxNum - minNum +1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

L = []
R = []
for i in range(m):
    li,mi = map(int,input().split())
    L.append(li)
    R.append(mi)

Lmax = L[0]
Rmin = R[0]
for i in range(m - 1):
    if Lmax < L[i + 1]:
        Lmax = L[i + 1]
    if Rmin > R[i + 1]:
        Rmin = R[i + 1]

if Lmax > n:
    print(0)
elif Rmin > n:
    print(n - Lmax + 1)
else:
    print(Rmin - Lmax + 1)

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)
A=0
for i in range(N):
	count=0
	for j in range(M):
		if R[j]>=i & i>=L[j]:
			count+=1
	if count==M:
		A+=1
print(A)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lst = []
for i in range(m):
    j , k = map(int, input().split())
    lst.append([j, k])

#m回の間、idカードの重複を数える

s = {i for i in range(lst[0][0], lst[0][1])}
for i in range(1, m-1):
    s = s & {k for k in range(lst[i][0], lst[i][1])}

s = list(s)
print(len(s))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(M):
    tL,tR = list(map(int,input().split()))
    if tL > L:
        L = tL
    if tR < R:
        R = tR

print(R-L+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "))
min_l = 0
max_l = n
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:
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)
  if L>R:
    print(0)
    exit()
print(max(0, R-L+1))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
cards=[i for i in range(1,n+1)]
for i in range(m):
  a,b=map(int,input().split())
  l=[i for i in range(a,b+1)]
  cards=list(set(l)&set(cards))
  print(cards)
print(len(cards))

Submission status: Wrong Answer

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

minR = min(R)
maxL = max(L)
if minR <= maxL:
  count = 0
else:
  count = minR - maxL + 1
      
print('%d' % count)

Submission status: Wrong Answer

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

for i in range(M):
    tL,tR = list(map(int,input().split()))
    if tL > L:
        L = tL
    if tR < R:
        R = tR
    print(L,R)
print(R-L+1)

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
a = [0] * (n + 2)
for i in range(m):
    x,y = map(int,input().split())
    a[x - 1] = 1
    a[y] = -1
for i in range(n + 1):
    a[i + 1] += a[i]
count = 0
mx = max(a)
for i in range(n):
    if a[i] == mx:
        count += 1
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

answer = 0

upper = a
under = 0

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

print(upper - under + 1)

Submission status: Wrong Answer

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

llist = []
rlist = []

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

print(min(rlist) - max(llist) + 1)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

print str(_R-_L+1)

Submission status: Wrong Answer

Solution code:
# n=int(input())  #数値入力 「N」だけの入力のとき
# a,b=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
# c=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
# s=[list(map(int,list(input()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# a=100
# b=0.987654321
# print('{0:06d}-{1:6f}'.format(a,b))  # 0埋めのときの出力
# 000100-0.987654

import numpy as np
import math
import copy

N,M=map(int, input().split())
A=np.array([i for i in range(N+1)[1::]])

for _ in range(M):
    L,R=map(int, input().split())
    tmp=np.array([i for i in range(R+1)[L::]])
    if(max(tmp) < min(A) or max(A) < min(tmp)):
        continue
    A = np.intersect1d(A,tmp)

print(len(A))

Submission status: Wrong Answer

Solution code:
M,N = map(int,input().split())
ans=set(range(1,M+1))
for s in range(N):
  S,Z = map(int,input().split())
  K=set()
  for a in range(M):
    if S <= a <= Z:
      K.add(a)
  ans = ans & K
print(len(ans))

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:'+str(min_r))
  if gate_s[i][0] > max_l:
    max_l = gate_s[i][0]
    #print('max_l:'+str(max_l))
    
if N < (min_r-max_l+1):
  print(N)
else:
  print(min_r-max_l+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
L,H=map(int,input().split())
c=0

while c<M-1:
    l,h=map(int,input().split())
    if L<l<=H:
        L=l
    elif l>H:
        print(0)
        break
    if L<h<H:
        H=h
    c+=1
print(H-L+1)

Submission status: Wrong Answer

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

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

inputs =  list(map(int, input().split()))
start = inputs[0]
end = inputs[1]

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

if N < end:
    end = N

if N < start:
    start = end-1

print(end - start + 1)

Submission status: Wrong Answer

Solution code:
import sys
args = sys.stdin.readlines()
n, m = map(int, args[0].split(' '))
gates = []
for gate in args[1:]:
  l, r = map(int, gate.split(' '))
  gates.append((l, r))
  
ids = gates[0]
for gate in gates[1:]:
  l, r = gate
  ids = [x for x in ids if x in range(l, r+1)]
  
print(len(ids))

Submission status: Wrong Answer

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

L = []
R = []
for i in range(M):
  L.append(LR[i][0])
  R.append(LR[i][1])
  
count = 0
flag = 0
for i in range(1, N+1):
  flag = 0
  for j in range(M):
    if (not i in L[j:]) and (not i in R[:j+1]):
      break
    else:
      flag += 1
    if flag == M:
      count += 1

print(count)

Submission status: Wrong Answer

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

Lmax = 0
Rmin = 100000
for i in range(m):
	L,R = map(int,input().split())
	if Lmax < L:
		Lmax = L
	if Rmin > R:
		Rmin = R
if Rmin > Lmax:
	print("0")
else:
	print("%d" % (Rmin - Lmax +1))

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(min(maximum - minimum + 1, N))

Submission status: Wrong Answer

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

L =[]
R =[]

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


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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
cards=[i for i in range(1,n+1)]
for i in range(m):
  a,b=map(int,input().split())
  l=[i for i in range(a,b+1)]
  cards=list(set(l)&set(cards))
print(cards)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
l_r = []
# i番目のゲートはl~rのIDカードを持っていれば通過できる
l_r = [input().split() for i in range(m)]
l_r.sort(key=lambda x:x[0])
l_max = int(l_r[-1][0])
l_r.sort(key=lambda x:x[1])
r_min = int(l_r[0][1])
if r_min - l_max + 1 > 0:
    print(r_min - l_max + 1)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = (int(x) for x in input().split())
L = []
R = []
for i in range(M):
	l, r = (int(x) for x in input().split())
	L.append(l)
	R.append(r)
	
lmax = max(L)
rmin = min(R)
ans = lmax - rmin + 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#python 3.5.2

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

s_ids = []
m_ids = []

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

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

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

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

print str(_R-_L+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
X = np.ones((N))
for i in range(M):
    L, R = map(int, input().split())
    if L >1 : X[:L]=0
    X[R+1:]=0
print(np.sum(X, dtype=np.int64))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#input
a = list(map(int,input().split())) #input from array of integer like "1 2 3"

l = []
r = []
for i in range(a[1]):
  ll,rr = (int(x) for x in input().split())
  l.append(ll)
  r.append(rr)


if a[1] == 1:
  print(r[0])
  exit()

r.reverse()

x = []
del l[:a[1]-1]
del r[:a[1]-1]
lr = l + r
ulist = list(set(lr))

print(len(ulist))

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, m = map(int, input().split())
a = [0] * (10 ** 5 + 10)
for i in range(m):
    x,y = map(int,input().split())
    a[x] = 1
    a[y + 1] = -1
for i in range(n + 3):
    a[i + 1] += a[i]
count = 0
mx = max(a)
for i in range(1, n + 1):
    if a[i] == mx:
        count += 1
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n,m = [int(i) for i in input().split()]

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

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

print(max-min+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def main():
    ID_gate = input().split(" ")
    ID = ID_gate[0]
    gate = ID_gate[1]
    min = 0
    max = 1000000
    for n in range(int(gate)):
        l_r = input().split(" ")
        if min < int(l_r[0]):
            min = int(l_r[0])
        if int(l_r[1]) < max:
            max = int(l_r[1])
    print(max - min + 1)


def xinext(r, xi, D):
    return r * xi - D

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

s_ids = []
m_ids = []

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

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

print(max_s_id)
print(min_m_id)

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
X = np.ones((N))
for i in range(M):
    Y = np.zeros((N))
    L, R = map(int, input().split())
    if L >1 : X[:L]=0
    X[R+1:]=0
print(np.sum(X, dtype=np.int64))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

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

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

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
l_r = []
# i番目のゲートはl~rのIDカードを持っていれば通過できる
l_r = [input().split() for i in range(m)]
l_r.sort(key=lambda x:x[0])
l_max = int(l_r[-1][0])
l_r.sort(key=lambda x:x[1])
r_min = int(l_r[0][1])
print(r_min - l_max + 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(min(R)-max(L)+1)

Submission status: Wrong Answer

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

left = 1
right = N

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

print(right-left+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
ID = []
for i in range(M):
    K = list(map(int, input().split())) 
    ID.append(list(range(K[0], K[1]+1)))
if len(ID) == 1:
    print(len(list(range(K[0], K[1])))+1)
else:
    match = set(ID[0]) & set(ID[1])
    for i in range(M):
        match = set(ID[i]) & match
        print(len(match))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
list = []
for i in range(M):
    L, R = map(int, input().split())
    list.append([L,R])
anslist = [list[0][0],list[0][1]]
arukadouka = True
cnt = 0
for i in range(M-1):
    if arukadouka == True:
        if anslist[0] > list[i+1][1]:
            arukadouka = False
        elif anslist[1] < list[i+1][0]:
            arukadouka = False
        else:
            if (anslist[0]<=list[i+1][0]) and (anslist[1]<=list[i+1][1]):
                anslist[0] = list[i+1][0]
            elif (anslist[0]<=list[i+1][0]) and (anslist[1]>=list[i+1][1]):
                anslist[0] = list[i+1][0]
                anslist[1] = list[i+1][1]
            elif (anslist[0]>=list[i+1][0]) and (anslist[1]<=list[i+1][1]):
                pass
            elif (anslist[0]>=list[i+1][0]) and (anslist[1]>=list[i+1][1]):
                anslist[1] = list[i+1][1]
        cnt += 1
print(anslist[1]-anslist[0]+1)

Submission status: Wrong Answer

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

gate = []

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


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

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

Submission status: Wrong Answer

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

L = 0
R = N

for _ in range(M):
    l, r = list(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 = list(map(int, input().split()))
max_l = 1
min_r = 100000
for _ in range(m):
    l, r = list(map(int, input().split()))
    if l > max_l:
        max_l = l
    if r < min_r:
        min_r = r
ans = min_r - max_l + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
li1 = []
li2 = []
for i in range(M):
    L, R = map(int,input().split())
    li1.append([L,R])
    li2.append([R,L])
li1.sort()
li2.sort(reverse=True)
print((li2[M-1][0])-(li1[M-1][0])+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 = list(set(L_list))
M_list = list(set(M_list))
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)
if min == max:
    ans = 1
else:
    ans = n[min:max]
    ans = len(ans)+1
print(ans)

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

Submission status: Wrong Answer

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

l_max = 1
r_min = N
for i in range(0,M):
    l,r = map(int,input().split())
    if l_max < l:
        l_max = l
    if r_min > r:
        r_min = r
print(r_min-l_max+1)

Submission status: Wrong Answer

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

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

inputs =  list(map(int, input().split()))
start = inputs[0]
end = inputs[1]

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = (int(i) for i in input().split())
l, r = [0]*m, [0]*m
for i in range(m):
    l[i], r[i] = (int(i) for i in input().split())
print(min(r) - max(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())
if M==1:
  print(R[0]-L[0]+1)
else:
  for i in range(M-1):
    L[i+1]=max(L[i],L[i+1])
    R[i+1]=min(R[i],R[i+1])
  print(R[M-1]-L[M-1]+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)

print(min_R + 1 - max_L)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: 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 L[-1] <= v[0]:
    if M[0] <= v[0]:
        print(M[0]-L[-1]+1)
        exit()
    else:
        print(v[0]-L[-1]+1)
        exit()
else:
    print('0')
    exit()

Submission status: Wrong Answer

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

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

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

print(minR - maxL + 1)

Submission status: Wrong Answer

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

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

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