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

l = 1
r = N
flag = False
for i in range(M):
    l_1, r_1 = list(map(int, input().split()))
    if l_1 > l:
        l = l_1
    if r_1 < r:
        r = r_1
    if l >= r:
        flag = True
        break
    
if flag:
    print(0)
else:
    print(r - l + 1)

Submission status: Wrong Answer

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

Lmax = 0
Rmin = 1000000
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())
L1, R1= map(int, input().split())

for x in range(M-1):
    L, R = map(int, input().split())
    if L > L1:
        L1 = int(L)
    if R < R1:
        R1 = int(R)
        
print(R1-L1+1)

Submission status: Wrong Answer

Solution code:
number = input().split()
i = 1
card_list = []

for i in range(int(number[1])):
    card = input().split()
    LR = int(card[1]) - int(card[0]) + 1
    j = 0
    if number[1] == "1":
        print(number[0])
        break
    while LR > 0:
        card_list.append(int(card[0]) + j)
        j += 1
        LR -= 1
        print("teest")
    i += 1
    print("test")

card_lists = [x for x in set(card_list) if card_list.count(x) > int(number[1]) - 1]

if number[1] != "1":
    print(len(card_lists))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
a = []
for i in range(M):
    a.append(list(map(int, input().split())))
id_card = []
for i in range(M):
    for j in range(N):
        if j < a[i][0] or j > a[i][1]:
            id_card.append(j)
print(N - len(set(id_card)))

Submission status: Wrong Answer

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

keys = [0] * N
count = N

for i in range(N):
    keys[i] = i + 1

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

    for i in range(N):
        if keys != 0:
            if keys[i] < L or keys[i] > R:
                keys[i] = 0
                count -= 1

print(count)

Submission status: Wrong Answer

Solution code:
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()

LR = [IL() for i in range(M)]

l = 0
r = MAX_INT
for L,R in LR:
    if l < L:
        l = L
    if r > R:
        r = R
print(r-l+1)

Submission status: Wrong Answer

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

    min_card = 1
    max_card = n

    for i in range(m):
        i_list = input().split()
        l = int(i_list[0])
        r = int(i_list[1])
        if l > min_card:
            min_card = l
        if r < max_card:
            max_card = r

    print(max_card - min_card + 1)

if __name__ == '__main__':
    calc()

Submission status: Wrong Answer

Solution code:
def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))

N, M = s_input()
L, R = [], []

for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])

L_max = max(L)
R_min = min(R)

print(R_min - L_max + 1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
a = []
b = [] * M
r = []
for i in range(M):
    x, y = map(int, input().split())
    for j in range(x, y):
        a.append(j)
    b.append(a)
    a = []
for i in range(M - 1):
    r = list(set(b[i]) & set(b[i + 1]))

if M >= 2:
    print(len(r))

else:
    print(len(b[0]) + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
card = []
z=0

for i in range(M):
    L,R=map(int, input().split())
    for j in range(N):
        if L<=j+1<=R:
            card.append(j+1) 
l=list(set(card))
            
for x in l:
    if card.count(x)==M:
        z+=1
print(z//M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
N, M = map(int, input().split())
LR = []
LR2 = []
count = 0
min = 1
max = N
for i in range(M):
    l, r = map(int, input().split())
    LR2.append(l)
    LR2.append(r)
    LR3 = copy.copy(LR2)
    LR.append(LR3)
    LR2.clear()

for i in LR:
    if min < i[0]:
        min = i[0]
    if max > i[1]:
        max = i[1]
ans = max - min + 1
print(ans)

Submission status: Wrong Answer

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

for i in range(M):
    L,R=map(int,input().split())
    if L>start:
        start=L
    if R<end:
        end=R
    #print(start,end)
print(end-start+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)
ans=min(R)-max(L)+1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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:
    G = []
    for i in range(M):
        G1 = []
        for j in range(len(L[i:])):
            G1.append(L[i:][j])
        for k in range(len(R[:(i+1)])):
            G1.append(R[:(i+1)][k])
        G.append(G1)

    print(G)
    count_list = [0]*N
    for i in range(1, N+1):
        for j in range(M):
            if not G[j-1].count(i):
                count_list[i-1] += 1

    print(count_list.count(0))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

keys = [0] * N
count = N

for i in range(N):
    keys[i] = i + 1

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

    for i in range(N):
        if keys[i] < L or keys[i] > R:
            keys[i] = 0
            count -= 1

print(count)

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]
else:
    if lLst[M-1] == rLst[M - 1]:
        ans = N

print(ans)

Submission status: Wrong Answer

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

A, B = map(int, input().split())

for i in range(M-1):
    A2, B2 = map(int, input().split())

    if (A < A2 and B2 < A) or (B < A2 and B< B2):
        A = 0
        B = 0
    elif (A2 <= A and A <= B):
        B = B2

    elif (A2 <= B and B <= B2):
        A = A2
    else:
        A = A2
        B = B2


if A == 0 and B == 0:
  print(0)
else:
  print(B-A+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#C問題
import numpy as np
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)
if min(R_list) - max(L_list) <=0:
    print(0)
else:
    print(min(R_list) - max(L_list)+1)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
list_a = list(range(1, a, 1))
for num in range(b):
    c, d = map(int, input().split())
    for item in list_a:
        if item < c or item > d:
            list_a.remove(item)
                
print(len(list_a))

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
listA = [list(map(int,input().split())) for i in range(m)]
listB=[]
for h in range(1,n+1):
    listB.append(h)
for j in range(m):
    for k in range(1,n+1):
        if k<listA[j][0] or k>listA[j][1]:
            listB[k-1]=0
setA=set(listB)
print(len(setA)-1)
print(setA)

Submission status: Wrong Answer

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

for i in range(m):
    left.append(lr[i][0])
    right.append(lr[i][1])

ans = min(right) - max(left) + 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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(N)
else:
    G = []
    for i in range(M):
        G1 = []
        for j in range(len(L[i:])):
            G1.append(L[i:][j])
        for k in range(len(R[:(i+1)])):
            G1.append(R[:(i+1)][k])
        G.append(G1)

    print(G)
    count_list = [0]*N
    for i in range(1, N+1):
        for j in range(M):
            if not G[j-1].count(i):
                count_list[i-1] += 1

    print(count_list.count(0))

Submission status: Wrong Answer

Solution code:
n,m=input().split()
n=int(n)
m=int(m)
i=0
x=list()
y=list()
ans=0
g=0
ll=[]
rr=[]


while i!=m:
    i=i+1
    l,r=input().split()
    l=int(l)
    r=int(r)
    ll.append(l)
    rr.append(r)
l=max(ll)
r=min(rr)

ans=r-l+1
print(ans)

Submission status: Wrong Answer

Solution code:
n,m= map(int,input().split())
l = []
for i in range(m):
    a,b = map(int,input().split())
    l +=[a+j for j in range(b-a+1)]
l = [i for i in range(len(l)) if l.count(i) == m]
print(len(l))

Submission status: Wrong Answer

Solution code:
import sys

n, m = map(int, input().split())
nums = []

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
#    nums = list(filter(lambda x: l <= x <= r, nums))
    del nums[:l - 1]
    del nums[r + 1:]

    if len(nums) == 0:
        print(0)
        sys.exit()

print(len(nums))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
g=[]
for i in range(m):
  l=list(map(int,input().split()))
  g.append(l)
g=sorted(g)
if m == 1:
  print(g[0][1]-g[0][0]+1)
else:
  for j in range(m-1):
    d=[]
    d.append(g[j][1])
    d.append(g[j+1][0])
    if j == 0:
      unq=set(d)
    else:
      unq=unq&set(d)

  print(len(list(unq)))

Submission status: Wrong Answer

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

n, m = map(int, input().split())
l = []
r = []
answer = 0
for i in range(m):
    a, b = map(int, input().split())
    l.append(a)
    r.append(b)
min_ok = max(l)
max_ok = min(r)
answer = max_ok - min_ok + 1
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**6
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i > min_num else min_num
  max_num = n_j if n_j < max_num else max_num

if max_num - min_num > 0:
  print(0)
else:
  print(max_num - min_num +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())

def prison(N,M,L,R):
  max_val=min(R)
  min_val=L[M-1]
  num=max_val-min_val+1
  if num<=0:
    num=0
  return(num)

print(prison(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 L[-1] == R[0]:
    count = 1
else:
    count = R[0] - L[M - 1] + 1

print(count)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
a,b=map(int,input().split())
ans=0
l_=[int(n) for n in range(a,b+1)]
 
for i in range(M-1):
  c,d=map(int,input().split())
  if max(c,d)< a or min(c,d) > b:
    pass
  else:
    l__=[n for n in range(c,d+1)]
    l_=list(set(l_)&set(l__))
    ans+=1
    
print(ans)

Submission status: Wrong Answer

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

for i in range(m):
    a,b = map(int, input().split())
    f.append(a)
    e.append(b)
ans = f + e
t_ans = []
print(ans[m] - ans[m-1] + 1)

Submission status: Wrong Answer

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

for i in range(M):
    l, r = map(int, input().split())
    ls.append(set(list(range(l, r + 1))))

S = set(list(range(N)))
for i in range(len(ls)):
    S = S & set(ls[i])

print(len(list(S)))

Submission status: Wrong Answer

Solution code:

lst_1 = input().split()

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

lst_A = []

count = 0

ans_min = 1
ans_max = N

for i in range(M):
    lst_A.append(input().split())

for m in range(M):

    com_min = int(lst_A[m][0])

    if com_min > ans_min:
        ans_min = com_min

    com_max = int(lst_A[m][1])

    if com_max < ans_max:
        ans_max = com_max

ans = ans_max - ans_min + 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
nm = input().split()
n = int(nm[0])
m = int(nm[1])
lr = [input().split() for i in range(m)]
l = [int(lr[i][0]) for i in range(m)]
r = [int(lr[i][1]) for i in range(m)]
if m == 1:
  print(r[0]-l[0]+1)
  sys.exit()
cl = []
cl2 = []
tmp = [i for i in range(n)]
cl = [tmp[l[i]:r[i]+1] for i in range(m)]

for i in range(m):
  cl2 = cl2 + cl[i]
l = cl2
dup = [x for x in set(l) if l.count(x) == m]
print(len(dup))

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):
    temp = l[i]
    min = min if min>temp else temp
    temp = r[i]
    max = max if max<temp else temp
    #print(min, max)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(' '))
L_list=[0]
R_list=[0]

for i in range(M):
    l,r = map(int, input().split(' '))
    L_list.append(l)
    R_list.append(r)


L_list.append(0)
R_list.append(0)

all_list=[]
num_list = range(1,len(L_list))

#Listを足しこむ
cnt = 0
for l,n in zip(L_list,num_list):
    all_list.append(l)
    #print(n)
    all_list.append(R_list[n])
    tmp = l
    while(tmp < R_list[n]):
        #print(L_list)
        all_list.append(tmp)
        tmp += 1
    if(all_list.count(n)>0):
        cnt += 1
    all_list = []

print(cnt)

Submission status: Wrong Answer

Solution code:
N,M = [int(x) for x in input().split()]
List = [list(map(int,input().split())) for i in range(M)]
left = List[0][0]
right = List[0][1]
for j in range(M-1):

    l = List[j+1][0]
    r = List[j+1][1]

    if l > left:
        left = l
    if r < right:
        right = r
    if right <= left:
        print(0)
        break
else:
    print(right-left+1)

Submission status: Wrong Answer

Solution code:
#   C



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


mini, maxi = map(int, input().split())

for i in range(1, M):
    L, R = map(int, input().split())
    if L > mini:
        mini = L
    if R < maxi:
        maxi
    if mini > maxi:
        break
if mini > maxi:
    print(0)
else:
    print(maxi - mini + 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
print(ans+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())

def prison(N,M,L,R):
  max_val=R[0]
  min_val=L[M-1]
  if max_val>=R[M-1]:
    max_val=R[M-1]
  num=max_val-min_val+1
  if num<=0:
    num=0
  return(num)

print(prison(N,M,L,R))

Submission status: Wrong Answer

Solution code:

lst_1 = input().split()

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

lst_A = []

count = 0

ans_min = 1
ans_max = N

for i in range(M):
    lst_A.append(input().split())

print(lst_A)

for m in range(M):

    com_min = int(lst_A[m][0])

    if com_min > ans_min:
        ans_min = com_min

    com_max = int(lst_A[m][1])

    if com_max < ans_max:
        ans_max = com_max

ans = ans_max - ans_min + 1

print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
OUT=set()
for m in range(M):
    L,R=map(int,input().split())

    for n in range(1,N+1,1):
        print('check',n)
        if n>=L and n<=R:
            pass
        else :
            OUT.add(n)
print(N-len(OUT))

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)
'''
if NM[0] >= ma:
    print(ma-mi+1)
elif NM[0] < ma and NM[0] > mi:
    print(NM[0]-mi+1)
elif NM[0] <= mi:
    print(0)

Submission status: Wrong Answer

Solution code:
a,b=map(int, input().split())
c,d=map(int, input().split())
start=c
end=d


for i in range(b-1):
  e,f=map(int, input().split())
  if e>start:
    start=e
  if f<end:
    end=f
    
out= end - start +1
print(out)

Submission status: Wrong Answer

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

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:
import copy

n, m = [int(i) for i in input().split()]
cnt = 0
l = []
r = []
for i in range(m):
    a, b = [int(i) for i in input().split()]
    l.append(a)
    r.append(b)
ans = min(r)-max(l)+1
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
conunt = 0
for i in range(M):
    a, b = map(int, input().split())
print(N%M)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**6
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i > min_num else min_num
  max_num = n_j if n_j < max_num else max_num

print(max_num - min_num +1)

Submission status: Wrong Answer

Solution code:
#   C



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


mini, maxi = map(int, input().split())

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

print(maxi - mini + 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m = map(int, stdin.readline().split())
mn = 1
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())
conunt = 0
for i in range(M):
    a, b = map(int, input().split())
    if i > a and i > b:
      conunt = conunt +1
print(N%M-conunt)

Submission status: Wrong Answer

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

ans = [0] * (N+1)

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

    for j in range(l,r+1):
        ans[j] += 1

maxv = max(ans)
cnt = ans.count(maxv)
print(cnt)

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][0] < li[i+1][0] ):
        L_max = li[i][0]
        if (li[i][1] > li[i+1][1]):
            R_min = li[i+1][1]
    elif (li[i ][1] > li[i+1][1]):
            R_min = li[i+1][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=map(int,input().split())

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

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

def prison(N,M,L,R):
  max_val=R[0]
  min_val=L[M-1]
  if max_val>=R[M-1]:
    max_val=R[M-1]
  return(max_val-min_val+1)

print(prison(N,M,L,R))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**5
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i >= min_num else min_num
  max_num = n_j if n_j <= max_num else max_num

print(max_num - min_num +1)

Submission status: Wrong Answer

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

gate = np.zeros(N+1)
for i in range(0, M):
    L, R = [int(j) for j in input().split()]
    gate[L-1] += 1
    gate[R] -=1

count = 0
for i in range(1, len(gate)):
    gate[i] = gate[i] + gate[i-1]
    if gate[i] == M:
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
lr=[]
result=0
for i in range(m):
  lr.append(input().split())
for k in range(1,n+1):
  for h in range(m):
    if int(k)<=int(lr[h][0]) or int(k)>=int(lr[h][1]):
      break
    result+=1
print(result)

Submission status: Wrong Answer

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

if m == 1:
  print(g[0][1]-g[0][0]+1)
else:
  for j in range(m-1):
    d=[]
    d.append(g[j][1])
    d.append(g[j+1][0])
    if j == 0:
      unq=set(d)
    else:
      unq=unq&set(d)

  print(len(list(unq)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().rstrip().split()]

    _l = 0
    _r = n

    for i in range(m):
        l,r = [int(x) for x in stdin.readline().rstrip().split()]
        _l = max(l, _l)
        _r = min(r, _r)

    print(_r - _l + 1)
    
if __name__=='__main__':
    main()

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)

ls = []    
for i in range(M):
    ls.append(set(list(range(L[i], R[i] + 1))))

S = set(list(range(N)))
for i in range(len(ls)):
    S = S & set(ls[i])

print(len(list(S)))

Submission status: Wrong Answer

Solution code:
N,M = [int(x) for x in input().split()]
List = [list(map(int,input().split())) for i in range(M)]
left = List[0][0]
right = List[0][1]
for j in range(M-1):

    l = List[j+1][0]
    r = List[j+1][1]

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

Lmax = 0
Rmin = N

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

print(Rmin - Lmax + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
li = input().split()
n = int(li[0])
m = int(li[1])
# IDカードn枚，　ゲートm個
# 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])
answer = 0
# for i in range(n):
#     if l_max <= i + 1 and i + 1 <= r_min:
#         answer += 1
diff = r_min - l_max + 1
if diff > 0:
    print(diff)
else:
    print(0)

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:
#C問題
import numpy as np
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:
from sys import exit, stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
LR = []
ans = [] 
for i in range(M):
    l, r = [int(_) for _ in stdin.readline().rstrip().split()]
    LR.append([l, r])
    if M == 1:
        print(r-l+1)
        exit()
    if i == 1:
        ans = [LR[i][0], LR[i-1][1]]
    elif i >= 2:
        ans = [LR[i][0], ans[1]]
print(ans[1]-ans[0]+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]:
        seki = seki & set(range(max,LR[i][1]+1))
        max = LR[i][1]
        
print(len(seki))

Submission status: Wrong Answer

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

l_=[int(n) for n in range(a,b+1)]

for i in range(M-1):
  c,d=map(int,input().split())
  if max(c,d)< a or min(c,d) > b:
    pass
  else:
    l__=[n for n in range(c,d+1)]
    l_=list(set(l_)&set(l__))
  
  
print(len(l_))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
maxL = 0
minM = N
for x in range(M):
    l, m = map(int, input().split())
    maxL = max(maxL, l)
    minM = min(minM, m)

print(min(minM - maxL + 1, 0))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import numpy as np
N, M = map(int, input().split())
l_max = 1
r_min = 100000
for i in range(M):
    l, r = map(int, input().split())
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r
    if r_min < l_max:
        break
print(r_min - l_max + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,M = map(int,input().split())
min_num,max_num = 0,10**5
for i in range(M):
  n_i,n_j = map(int,input().split())
  min_num = n_i if n_i > min_num else min_num
  max_num = n_j if n_j < max_num else max_num

print(max_num - min_num +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(max,LR[i][1]+1))
print(len(seki))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
lr=[]
result=0
gates=0
for i in range(m):
  lr.append(input().split())
for k in range(1,n+1):
  gates=0
  for h in range(m):
    if not(int(k)>=int(lr[h][0]) and int(k)<=int(lr[h][1])):
      break
  result+=1
print(result)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
maxL = 0
minM = N
for x in range(M):
    l, m = map(int, input().split())
    maxL = max(maxL, l)
    minM = min(minM, m)

print(minM - maxL + 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(len(candidate_list[max(hq_left):min(hq_right)])+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())

    if(R < min(A) or max(A) < L):
        print(0)
        break

    tmp=np.array([i for i in range(R+1)[L::]])

    evaluate = (A == tmp)
    if type(evaluate) == bool:
        if evaluate:
            continue
    else:
        if evaluate.all():
            continue

    A = np.intersect1d(A,tmp)

print(len(A))

Submission status: Wrong Answer

Solution code:
N, M=map(int, input().split())
LR=list(sorted([list(map(int, input().split())) for _ in range(M)]))
a=LR[-1][0]
LR=list(sorted(LR,  key=lambda x: x[1]))
b=LR[0][1]
print(b-a+1)

Submission status: Wrong Answer

Solution code:
import collections
N, M = map(int, input().split())
S = [list(map(int, input().split())) for i in range(M)]
smax = max([s[0] for s in S])
smin = min([s[1] for s in S])
print(smin - smax + 1)



#S2 = [set(range(s[0], s[1] + 1)) for s in S]
#S3 = set.intersection(*S2)
#print(len(S3))

#ans = set(range(S[0][0], S[0][1] + 1))
#for s in S[1:] :
#    a = set(range(s[0], s[1] + 1))
#    ans = ans & a
#    if len(ans) == 0 :
#        break
#print(len(ans))

Submission status: Wrong Answer

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

for i in range(n):
    nums.append(i + 1)

for i in range(m):
    l, r = map(int, input().split())
    nums = list(filter(lambda x: l <= x <= r, nums))
    if len(nums) == 0:
        print(0)
        break

print(len(nums))

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 min>max:
    print(0)
else:
    print(ans)