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 = []
R = []
lan = 0
ran = 0
ans = 0
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

i = 0
for i in range(M):
    for j in range(M):
        if L[i] >= L[j] and L[i] <= R[j]:
            lan += 1
        else:
            lan = 0
            ran = 0
            break
        if R[i] >= L[j] and R[i] <= R[j]:
            ran += 1
        else:
            lan = 0
            ran = 0
            break
    else:
        if lan == M:
            ans += 1
        if ran == M:
            ans += 1
        lan = 0
        ran = 0
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
#入力取得
N, M = map(int, input().split())

#最大と最小を選べば良い

left_max = 0
right_min = N


for i in range(M):

    tmp_left,tmp_right = map(int, input().split())


    if tmp_left > left_max:
        left_max = tmp_left

    if tmp_right < right_min:
        right_min = tmp_right



result = right_min - left_max

if result <= 0:
    print(0)
else:
    print(result + 1)

Submission status: Wrong Answer

Solution code:
nm = input().split()
args = []
minValue = -1
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:
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 func(L, R):
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)

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

print(len(count_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

MIN = [LR[i][0] for i in range(M)]
MAX = [LR[i][1] for i in range(M)]

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ls = []
for i in range(m):
    l, r = map(int, input().split())
    ln = l
    while r >= ln:
        ls.append(ln)
        ln += 1
ls_duplicate = [x for x in set(ls) if ls.count(x) > 1]
print(len(ls_duplicate))

Submission status: Wrong Answer

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


print(min(gol) -max(sta) +1)

Submission status: Wrong Answer

Solution code:
import collections
import math 
from operator import itemgetter
N,M = map(int, input().split())
t =[]
dp = [0]*(N+1)
for i in range(M):
    n,m = map(int, input().split())
    dp[n-1] = +1
    dp[m] -=1
s = 0
for i in range(N):
    dp[i+1]+= dp[i] 
    if dp[i] == M:s+=1
print(s)

Submission status: Wrong Answer

Solution code:
#入力取得
N, M = map(int, input().split())

#最大と最小を選べば良い

left_max = 0
right_min = N


for i in range(M):

    tmp_left,tmp_right = map(int, input().split())


    if tmp_left > left_max:
        left_max = tmp_left

    if tmp_right < right_min:
        right_min = tmp_right


result = right_min - left_max + 1
print(result)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def main():
    N, M = i2nn()
    L0, R0 = i2nn()
    for i in range(1, M):
        L1, R1 = i2nn()
        L0 = max(L0, L1)
        R0 = min(R0, R1)
    if L0 > R0:
        L0 = R0 - 1
    print(R0 - L0 + 1)
    return

main()

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::]])
    A = np.intersect1d(A,tmp)

print(len(A))

Submission status: Wrong Answer

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

L = []
R = []
l = 0
r = 10 ** 6
for i in range(M):
    a,b = map(int, input().split())
    L.append(a)
    R.append(b)

for i in range(M):
    if l < L[i]:
        l = L[i]
    if r > R[i]:
        r = R[i]

ans = r-l+1
print(ans)

Submission status: Wrong Answer

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

MIN = [LR[i][0] for i in range(M)]
MAX = [LR[i][1] for i in range(M)]

print(min(MAX) - max(MIN) + 1)

Submission status: Wrong Answer

Solution code:
import sys
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
    if min>=max:
        print(0)
        sys.exit()
print(max-min+1)

Submission status: Wrong Answer

Solution code:
N, M = input().split(" ")
#S = []
if M == 1:
    a, b = input().split(" ")
    print(int(b)-int(a)+1)
else:    
    a, b = input().split(" ")
    aM, bm = int(a),int(b)
    #sets = set([i for i in range(int(a),int(b)+1)])
    for m in range(int(M)-1):
        a, b = input().split(" ")
        aM = max(aM,int(a))
        bm = min(bm,int(b))
        #sets = sets & set([i for i in range(int(a),int(b)+1)])
    print(int(bm)-int(aM)+1)

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(1,M):
    if arukadouka == True:
        if anslist[0] > list[i][1]:
            arukadouka = False
        elif anslist[1] < list[i][0]:
            arukadouka = False
        elif list[i][0] == list[i][1]:
            anslist[0] = list[i][0]
            anslist[1] = list[i][1]
        else:
            if (anslist[0]<=list[i][0]) and (anslist[1]<=list[i][1]):
                anslist[0] = list[i][0]
            elif (anslist[0]<=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[0] = list[i][0]
                anslist[1] = list[i][1]
            elif (anslist[0]>=list[i][0]) and (anslist[1]<=list[i][1]):
                pass
            elif (anslist[0]>=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[1] = list[i][1]
        cnt += 1
print(anslist[1]-anslist[0]+1)

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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

def main():
    ID_gate = re.split(" +", input())
    ID = ID_gate[0]
    gate = ID_gate[1]
    min = 0
    max = 1000000
    for n in range(int(gate)):
        l_r = re.split(" +", input())
        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)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

min_num = 1
max_num = n
for i in range(m):
  l,r = map(int, input().split())
  if min_num < l:
    min_num = l
  if max_num > r:
    max_num = r

print (max_num-min_num + 1)

Submission status: Wrong Answer

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

num_card = 0

for i in range(0, N):
    for j in range(0, M-1):
        #print(LR_list[j])
        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())




#最大と最小を選べば良い

left_max = 0


for i in range(M):

    tmp_left,tmp_right = map(int, input().split())

    if i == 0:
        right_min = tmp_right

    if tmp_left > left_max:
        left_max = tmp_left

    if tmp_right < right_min:
        right_min = tmp_right


result = right_min - left_max + 1
print(result)

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]

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

Submission status: Wrong Answer

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=0
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(' '))
l_ = []
r_ = []
for i 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(" "))
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())
a=0
b=float('inf')
for i in range(m):
    l,r=map(int,input().split())
    if l >=a:
        a=l
    if r <=b:
        b=r
if a==b:
    print(1)
else:
    print(b-a+1)

Submission status: Wrong Answer

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

i = 0
for i in range(M):
    for j in range(M):
        if L[i] >= L[j] and L[i] <= R[j]:
            lan += 1
        if R[i] >= L[j] and R[i] <= R[j]:
            ran += 1
    else:
        if lan == M:
            ans += 1
        if ran == M:
            ans += 1
        lan = 0
        ran = 0
else:
    print(ans)

Submission status: Wrong Answer

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

MAX=np.max(list,axis=1)
MIN=np.min(list,axis=1)
print(MAX[0]-MIN[-1]+1)

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]

if max-min+1 > 0:
  print(max - min + 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 = 0
b_min = 10**5
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:
N, M= map(int, input().split())

L_list = []
R_list = []

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

ans = min(R_list) - max(L_list) + 1
print(ans)

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

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)

print(r[0]-l[a[1]-1]+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
    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
    #print(min_range)
    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:
# -*- coding: utf-8 -*-
# abc127/abc127_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def main():
    N, M = i2nn()
    L0, R0 = i2nn()
    for i in range(1, M):
        L1, R1 = i2nn()
        L0 = max(L0, L1)
        R0 = min(R0, R1)
    print(R0 - L0 + 1)
    return

main()

Submission status: Wrong Answer

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

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

    if 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())
lr = [list(map(int, input().split())) for i in range(m)]
L,R = [],[]
for i in lr:
  l,r = i[0],i[1]
  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())
L = []
R = []
lan = 0
ran = 0
ans = 0
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

print(R)
print(L)
i = 0
for i in range(M):
    for j in range(M):
        if L[i] >= L[j] and L[i] <= R[j]:
            lan += 1
        if R[i] >= L[j] and R[i] <= R[j]:
            ran += 1
    else:
        if lan == M:
            ans += 1
        if ran == M:
            ans += 1
        lan = 0
        ran = 0
else:
    print(ans)

Submission status: Wrong Answer

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


for i in range(M):
	a,b=map(int, raw_input().split())
	if i==0:	r=b
	if i==M-1: l=a

print r-l+1

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N = int(s[0])
    M = int(s[1])
    L = []
    R = []
    for i in range(M):
        s = input().rstrip().split(' ')
        L.append(int(s[0]))
        R.append(int(s[1]))

    res = list(range(1,N+1))
    res_min = 1
    res_max = N

    for i in range(M):
        res_min = max(res_min,L[i])
        res_max = min(R[i],res_max)

    print(res_max-res_min+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
la, ra = map(int, input().split())
for i in range(1, m):
    lm, rm = map(int, input().split())
    overlap = False
    if la <= lm <= ra:
        la = lm
        overlap = True
    if la <= rm <= ra:
        ra = rm
        overlap = True
    if not overlap:
        print(0)
        exit(0)

print(ra - la + 1)

Submission status: Wrong Answer

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

s_list = list()
e_list = list()

for m in range(M):
    s, e = map(int, input().strip().split())
    s_list.append(s)
    e_list.append(e)

print(min(e_list)-max(s_list)+1)

Submission status: Wrong Answer

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

MAX=np.max(list,axis=0)
MIN=np.min(list,axis=0)
print(MIN[1]-MAX[0]+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(" "))
num_list = [0] * 100002
L_list = []
R_list = []
for i in range(M):
    L,R = map(int,input().split(" "))
    L_list.append(L)
    R_list.append(R)
    for j in range(L,R+1):
        num_list[j] += 1
if max(num_list) == 0:
    print(0)
else:
    print(min(R_list) - max(L_list) +1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=0
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)
	elif n>a and n>=b:
  		print(b-a)
	else:
  		print(0)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
for _ in range(M):
    a, b = map(int, input().split())
print(N//M)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# IMPORT ###############################################################################################################
import sys                                                      # システム
# sys.setrecursionlimit(10**6)                                    # 再帰処理の上限変更
# from collections import deque, Counter                          # キュー・個数カウント
# from itertools   import accumulate, permutations, combinations  # 累積和・順列・組み合わせ
# from operator    import itemgetter                              # ソート補助
# from bisect      import bisect_left, bisect_right, bisect       # 二分探索
# from heapq       import heappop, heappush                       # プライオリティキュー
# from fractions   import gcd                                     # 最大公約数
# from math        import ceil, floor, sqrt, cos, sin, tan, pi    # 数学処理
# from copy        import deepcopy                                # オブジェクトコピー
# import numpy         as np                                      # 数値計算

# MAIN #################################################################################################################
def main():
    # Init #############################################################################################################
    im = InputManager()
    # Input ############################################################################################################
    n, m = im.inputMultiple()
    left = 0
    right = n
    for _ in range(m):
        l, r = im.inputMultiple()
        left = max(left, l - 1)
        right = min(right, r - 1)
    # Logic ############################################################################################################

    # Output ###########################################################################################################
    print(right - left + 1)
    # Return ###########################################################################################################
    return

# INPUT MANAGER ########################################################################################################
class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]

# CALL MAIN ############################################################################################################
main()

Submission status: Wrong Answer

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

mi, mx = map(int, input().split())
for i in range(m-1):
    l, r = map(int, input().split())
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r

print(mx - mi + 1)

Submission status: Wrong Answer

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

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(1,M):
    if arukadouka == True:
        if anslist[0] > list[i][1]:
            arukadouka = False
        elif anslist[1] < list[i][0]:
            arukadouka = False
        else:
            if (anslist[0]<=list[i][0]) and (anslist[1]<=list[i][1]):
                anslist[0] = list[i][0]
            elif (anslist[0]<=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[0] = list[i][0]
                anslist[1] = list[i][1]
            elif (anslist[0]>=list[i][0]) and (anslist[1]<=list[i][1]):
                pass
            elif (anslist[0]>=list[i][0]) and (anslist[1]>=list[i][1]):
                anslist[1] = list[i][1]
        cnt += 1
print(anslist[1]-anslist[0]+1)

Submission status: Wrong Answer

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

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 if r <= l else 0)

Submission status: Wrong Answer

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

L_list.sort()
M_list.sort()
lmax = L_list[-1]
mmin = M_list[0]
if lmax == mmin:
    ans = 1
else:
    ans = mmin-lmax
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
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)
b = 0
for i in range(n):
  if a.count(i) == m:
    b += 1
print(b)

Submission status: Wrong Answer

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

start, end = 1, n
for _ in range(m):
  l, r = map(int, input().split())
  if r < start or l > end:
    start, end = 1, 0
    break
  elif l <= start and start <= r <= end:
    end = r
  elif start <= l <= end and end <= r:
    start = l
  else:
    start, end = l, r

print(end - start + 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(0, m):
    s = s & {k for k in range(lst[i][0], lst[i][1])}

s = list(s)
print(len(s)+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)
  
print(min(R)-max(L)+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L_max, R_min = 0,100000
for i in range(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:
N, M = map(int, input().split())
a1 = 0
b1 = N
for i in range(M):
    a, b = map(int, input().split())
    if a1 <= a:
        a1 =a
    if b1 >= b:
        b1 = b

print(b1-a1+1)

Submission status: Wrong Answer

Solution code:
n,m=(int(i) for i in input().split())
a=0
b=0
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 n>a and n<b:
  print(n-a)
elif n>a and n>=b:
  print(b-a)
else:
  print(0)

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)
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+1)

Submission status: Wrong Answer

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

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

print(r-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("left {} right{}".format(left,right))

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

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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
s = list(map(int, input().split()))
N = s[0]
M = s[1]

s = np.array([list(map(int, input().split())) for i in range(M)])

print(np.min(s[:,1]) - np.max(s[:,0]) +1)

Submission status: Wrong Answer

Solution code:
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N,M = map(int, lines.pop(0).split(' '))
    min_n = 0
    max_n = N
    for i in range(M):
        a,b = map(int, lines.pop(0).split(' '))
        min_n = max(a, min_n)
        max_n = min(b, max_n)
    print( max_n - min_n + 1)

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

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 = rmin - lmax + 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

keys = [True] * N

l1 = 1
r1 = N

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

print(r1 - l1 + 1)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
N = int(N)
M = int(M)
L_list =[]
R_list = []
for h in range(M):
    L, R = input().split()
    L_list.append(L)
    R_list.append(R)

L_list = map(int,L_list)
R_list = map(int,R_list)

L = max(L_list)
R = min(R_list)
print(R-L+1)