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())
a = set(range(1,n))
for i in range(m):
    l,r = map(int,input().split()) 
    a &= set(range(l,r+1))
    
print(len(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l = -float('inf')
r = float('inf')
for i in range(m):
  l2 , r2 = map(int, input().split())
  if l < l2:
    l = l2
  if r2 < r:
    r = r2
print(r-l+1)

Submission status: Wrong Answer

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

for _ in range(M):
    L, R = map(int, input().split())
    L_ans = max(L, L_ans) # Lの中で一番大きいもの抽出
    R_ans = min(R, R_ans) # Rは最小
    print(L_ans, R_ans)

    if L_ans > R_ans:
        print(0)
        exit()

print(R_ans - L_ans + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
gate = [list(map(int,input().split())) for i in range(M)]
small = gate[M-1][0]
big = gate[0][1]
if small <=  big:
    ans = big - small + 1
else:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

small = gate[M-1][0]
big = gate[0][1]
con = 0

for i in range(small, big+1):
    con += 1
print(con)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
for _ in range(M-1):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

score = r-l+1
if score:
    print(score)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
for _ in range(M-1):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

if r-1 >= 0:
    print(r-l+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
for _ in range(M-1):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

if r-1 > 0:
    print(r-l+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
for _ in range(M-1):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

print(r-l+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l = 0
r = 1000000
for _ in range(M):
    new_l, new_r = map(int, input().split())
    if l < new_l:
        l = new_l
    if new_r < r:
        r = new_r

print(r-l+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lim = list()
res = list()
for i in range(1,n+1):
    res.append(i)
for n in range(0,m):
    lim.append(list(map(int,input().split())))
for k in lim:
    try:
        if k[0] <= res[0]:
            a = 0
        #try:
        else:
            a = int(res.index(k[0]))
    #except ValueError:
        del res[:a]
    #try:
        if k[1] >= res[-1]:
            b = res[-1]
    #except ValueError:
        else:
            b = int(res.index(k[1]))
        del res[b+1:]
    except:
        pass

print(len(res))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

minv = 100000
maxv = 1
for lr in LR:
  maxv = max(maxv, lr[0])
  minv = min(minv, lr[1])

print(minv - maxv + 1)

Submission status: Wrong Answer

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

min_n = 0
max_n = 10**5+1
for l,r in LRlist:
    min_n = l if l>min_n else min_n
    max_n = r if r<max_n else max_n

print((max_n-min_n)+1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy3

#import numpy as np
#import scipy, scipy.special

# Python 3 modules: https://docs.python.org/3/library/

import math, itertools, operator
from math import *
from math import factorial as fact
from itertools import permutations, combinations
from functools import reduce
# Use scipy for stats: https://docs.scipy.org/doc/scipy/reference/stats.html
# Before Python 3.5
from fractions import gcd

inf = float('inf')

def lcm(a, b):
    return a * b // gcd(a,b)

def perm(a, b):
    import scipy.special
    return scipy.special.perm(a, b, exact = True)

def comb(a, b):
    import scipy.special
    return scipy.special.comb(a, b, exact = True)

def dfact(n):
    import scipy.special
    return scipy.special.factorial2(n, exact = True)

def prod(l):
    return reduce(operator.mul, l)

def input_ints(inp = input, sep = ' ', conv = int):
    return map(conv, inp().split(sep = sep))

################################################################################

N, M = input_ints()
L, R = input_ints()
for i in range(M - 1):
    l, r = input_ints()
    if L < l:
        L = l
    if r < R:
        R = r
print(R - L + 1)

Submission status: Wrong Answer

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


l = [0] * m
r = [0] * m

for i in range(m):
    l[i], r[i] = map(int, input().split())
    
    
l_max = 0
r_min = 1000000000000     
for i in range(m):
    a = max(l[i],r[i])
    b = min(l[i],r[i])
    if a<r_min:
        r_min = a
    if b>l_max:
        l_max = b

print(r_min-l_max+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())
    
num = min(r)-max(l)
print(num+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
minC = 1
maxC = n
index=0
while index < m:
    l, r = map(int, input().split())
    if l > minC:
        minC = l
    if r < maxC:
        maxC = r
    index+=1
print(maxC-minC+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L, R = [], []
for _ in range(m):
    li, ri = map(int, input().split())
    L.append(li)
    R.append(ri)

# ans = 0

# for i in range(1, n+1):
#     flag = True
#     for li, ri in zip(L, R):
#         if not (i >= li and i <= ri):
#             flag = False
#             break
#     if flag:
#         ans += 1

# print(ans)

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L, R = [], []
for _ in range(m):
    li, ri = map(int, input().split())
    L.append(li)
    R.append(ri)

# ans = 0

# for i in range(1, n+1):
#     flag = True
#     for li, ri in zip(L, R):
#         if not (i >= li and i <= ri):
#             flag = False
#             break
#     if flag:
#         ans += 1

# print(ans)

if min(min(R), n) == 0:
        print(0)
else:
        print(min(min(R), n) - max(L) +1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L, R = [], []
for _ in range(m):
    li, ri = map(int, input().split())
    L.append(li)
    R.append(ri)

# ans = 0

# for i in range(1, n+1):
#     flag = True
#     for li, ri in zip(L, R):
#         if not (i >= li and i <= ri):
#             flag = False
#             break
#     if flag:
#         ans += 1

# print(ans)

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
gate_list = []
while True:
  try:
    gate_list.append(list(map(int,input().split())))
  except:
    break

left_ = []
right_ = []

for i in gate_list:
  left_.append(i[0])
  right_.append(i[1])

min_ = max(left_)
max_ = min(right_)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(r-l+1)

Submission status: Wrong Answer

Solution code:
c = []
d = []
n,m = map(int, input().split())
for i in range(m):
    a,b = map(int,input().split())
    c.append(a)
    d.append(b)
minl = max(c)   
maxr = min(d)
print(maxr-minl+1)

Submission status: Wrong Answer

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

print(min(rs)-max(ls)+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
tmp = list(map(int, range(l-1, r)))
for i in range(m-1):
    l, r = map(int, input().split())
    for j in range(l-1, r):
        if j in tmp:
            tmp.remove(j)

print(len(tmp))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
l, r = map(int, input().split())
tmp = list(map(int, range(l-1, r)))
for i in range(m-1):
    l, r = map(int, input().split())
    for j in range(max(l-1, min(tmp)), min(r, max(tmp))):
        if j in tmp:
            tmp.remove(j)

print(len(tmp))

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

low = 1
high = n

for i in range(m):
        lr_temp = lr[i]
        if low < lr_temp[0]:
                low = lr_temp[0]
        if high > lr_temp[1]:
                high = lr_temp[1]

ans = high - low + 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
flag = 0
if M != 1:
    for i in range(1,M):
        lnew, rnew = map(int, input().split())
        if r < lnew or l > rnew:
            flag = 1
            break
        else:
            l = max(l, lnew)
            r = min(r, rnew)
    if flag:
        res = 0
    else:
        res = r - l + 1
    print(res)
else:
    print(N)

Submission status: Wrong Answer

Solution code:
import numpy as np

nm = input().split()
n = int(nm[0])
m = int(nm[1])

min = 0
max = np.inf

for i in range(m):
    a = input().split()
    if int(a[0])>=min:
        min = int(a[0])

    if int(a[1])<=max:
        max = int(a[1])

print(max-min+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
cand = set(range(l, r+1))
if M != 1:
    for i in range(1,M):
        l, r = map(int, input().split())
        cand = cand & set(range(l, r+1))
    print(len(cand))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
ls = [[int(x) for x in input().split()]for i in range(m)]
max_num = []
min_num = []
ans = 0
for i in range(m):
  max_num.append(ls[i][0])
  min_num.append(ls[i][1])
M = max(max_num)
N = min(min_num)
for i in range(n):
    if M<=i<=N:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
ID_num, gate_num = tuple([int(i) for i in input().split(" ")])
min_list = []
max_list = []
for i in range(gate_num):
    min_ID, max_ID = tuple([int(i) for i in input().split(" ")])
    min_list.append(min_ID)
    max_list.append(max_ID)
M = min(max_list)
m = max(min_list)
if M > m:
    print(min(max_list) - max(min_list) + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
ID_num, gate_num = tuple([int(i) for i in input().split(" ")])
min_list = []
max_list = []
for i in range(gate_num):
    min_ID, max_ID = tuple([int(i) for i in input().split(" ")])
    min_list.append(min_ID)
    max_list.append(max_ID)
print(min(max_list) - max(min_list) + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
        for i in range(m):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
    
    lr = []
    for i in range(1, m+1):
        token = lines[i].split(" ")
        l = int(token[0])
        r = int(token[1])
        lr.append({"l":l, "r":r})

    return (n, m, lr)


def solve(n, m, lr):

    if len(lr) == 1:
        l = lr[0]["l"]
        r = lr[0]["r"]
        return r - l + 1

    l = lr[0]["l"]
    r = lr[0]["r"]
    c = {"l":l, "r":r}
    count = 0
    for i in range(1, m):

        if lr[i]["l"] <= c["r"] and c["l"] <= lr[i]["r"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = max(c["l"],  lr[i]["l"])
            count = count + 1
        elif lr[i]["r"] <= c["l"] and c["r"] <= lr[i]["l"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = max(c["l"],  lr[i]["l"])
            count = count + 1

        if debug:
            log("c=%s" % c)

    result = c["r"] - c["l"] + 1

    if debug:
        log("count=%s" % count)
        log("result=%s" % result)

    if count == 0:
        return 0
    

    return result



def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

Solution code:
N, M = [int(t) for t in input().split()]
l = [[0]*2 for i in range(M)]

l_max = 0
r_min = N
for i in range(M):
    L, R = [int(t) for t in input().split()]
    l_max = L if l_max < L else l_max
    r_min = R if r_min > R else r_min

print(r_min-l_max+1)

Submission status: Wrong Answer

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

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
        for i in range(m):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
    
    lr = []
    for i in range(1, m+1):
        token = lines[i].split(" ")
        l = int(token[0])
        r = int(token[1])
        lr.append({"l":l, "r":r})

    return (n, m, lr)


def solve(n, m, lr):

    if len(lr) == 1:
        l = lr[0]["l"]
        r = lr[0]["r"]
        return r - l + 1

    l = lr[0]["l"]
    r = lr[0]["r"]
    c = {"l":l, "r":r}
    count = 0
    for i in range(1, m):

        if lr[i]["l"] <= c["r"] and c["l"] <= lr[i]["r"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = max(c["l"],  lr[i]["l"])
            count = count + 1

        if lr[i]["r"] <= c["l"] and c["r"] <= lr[i]["l"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = max(c["l"],  lr[i]["l"])
            count = count + 1

        if debug:
            log("c=%s" % c)

    result = c["r"] - c["l"] + 1

    if debug:
        log("count=%s" % count)
        log("result=%s" % result)

    if count == 0:
        return 0
    

    return result



def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
        for i in range(m):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split(" ")
        n = int(token[0])
        m = int(token[1])
    
    lr = []
    for i in range(1, m+1):
        token = lines[i].split(" ")
        l = int(token[0])
        r = int(token[1])
        lr.append({"l":l, "r":r})

    return (n, m, lr)


def solve(n, m, lr):

    if len(lr) == 1:
        l = lr[0]["l"]
        r = lr[0]["r"]
        return r - l + 1

    l = lr[0]["l"]
    r = lr[0]["r"]
    c = {"l":l, "r":r}
    count = 0
    for i in range(1, m):

        if lr[i]["l"] <= c["r"] and c["l"] <= lr[i]["r"]:
            c["r"] = min(c["r"],  lr[i]["r"])
            c["l"] = max(c["l"],  lr[i]["l"])
            count = count + 1

        if debug:
            log("c=%s" % c)

    result = c["r"] - c["l"] + 1

    if debug:
        log("count=%s" % count)
        log("result=%s" % result)

    if count == 0:
        return 0
    

    return result



def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

r=n
l=1

for i in range(1,m+1):
	lt, rt = map(int, input().split())
	if lt > l:
		l=lt
	if rt < r:
		r=rt
	#print(l,r)

print(r-l+1)

Submission status: Wrong Answer

Solution code:
def prison():
    N, M = (int(i) for i in input().split())
    L_max = 0
    R_min = float("inf")
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)

    return R_min-L_max+1


print(prison())

Submission status: Wrong Answer

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

left_max = -1
right_min = n+1

for i in range(m):
    s,t = map(int,input().split())
    left_max = max(left_max,s)
    right_min = min(right_min, t)
    
print(0, right_min - left_max +1)

Submission status: Wrong Answer

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

left_max = -1
right_min = n+1

for i in range(m):
    s,t = map(int,input().split())
    left_max = max(left_max,s)
    right_min = min(right_min, t)
    
print(right_min - left_max +1)

Submission status: Wrong Answer

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


def prison():
    _, M = (int(i) for i in input().split())
    L_max = 0
    R_min = 10**5
    for _ in range(M):
        L, R = (int(i) for i in input().split())
        L_max = max(L, L_max)
        R_min = min(R, R_min)

    return R-L+1


print(prison())

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l,r=0,10**5
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())
l,r=0,n
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())
LR = [list(map(int, input().split()))[:2] for m in range(M)]

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

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

# 上限と下限の間に存在する整数値
w = maxV - minV + 1
if w > N:
    print(0)
else:
    print(w)

Submission status: Wrong Answer

Solution code:
# 解説を見ての解答
from sys import stdin

n, m = map(int, stdin.readline().rstrip().split())
lr = [list(map(int, stdin.readline().rstrip().split())) for i in range(m)]

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

print(r-l+1)

Submission status: Wrong Answer

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

left, right = 0, int(1e6)
for i in range(M):
    L, R = map(int, input().split())
    left = max(L, left)
    right = min(R, right)

print(int(right - left) + 1)

Submission status: Wrong Answer

Solution code:
# ABC127C - Prison
# l-max < i < r-min -> count i
n, m = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
l, r = lst[0][0], lst[0][1]
for i in lst[1:]:
    l = max(l, i[0])
    r = min(r, i[1])
print(r - l + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
l=[]
L,R=map(int,input().split())
l.extend(list(range(L,R+1))) 
for i in range(M-1):
    L,R=map(int,input().split())
    x=list(range(L,R+1))
    for y in l:
        if not y in x:l.remove(y)
print (len(l))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a,b=map(int,input().split())
for i in range(m-1):
  c,d=map(int,input().split())
  a=max(a,c)
  b=min(b,d)
print(b-a+1)

Submission status: Wrong Answer

Solution code:
import sys
N, M = map(int,input().split())
first = 1
second = N
for i in range (M):
  A, B = map(int,input().split())
  if not(first <= A <= second) and not(first <= B <= second):
    print(0)
    sys.exit(0)
  first = max(first,A)
  second = min(second,B)
print(second - first + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
first = 1
second = N
for i in range (M):
  A, B = map(int,input().split())
  first = max(first,A)
  second = min(second,B)
print(second - first + 1)

Submission status: Wrong Answer

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

Lma = 0
Rmi = 0
for L, R in LR:
  Lma = max(L, Lma)
  Rmi = min(R, Rmi)

print(max(0, Rmi - Lma + 1))

Submission status: Wrong Answer

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

l=1
r=N

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

Submission status: Wrong Answer

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

Lma = 0
Rmi = 0
for L, R in LR:
  Lma = max(L, Lma)
  Rmi = min(R, Rmi)

print(max(0, Lma - Rmi + 1))

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)
  
l = max(l_list)
r = min(r_list)

if r < l:
  print(0)
else:
  print(l-r+1)

Submission status: Wrong Answer

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

p=[]
q=[]

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

p1=max(p)
q1=min(q)

if p1>=q1:
    print(q1-p1+1)
else:
    print(0)

Submission status: Wrong Answer

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

p=[]
q=[]

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

p1=max(p)
q1=min(q)

if p1>=q1:
    print(q1-p1)
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
n ,m = map(int, input().split())
L = []
R = []
flg = False
for i in range(m):
    l, r = map(int, input().split())
    if r - l <= 0:
        flg = True 
    L.append(l)
    R.append(r)
if flg:
    print(0)
else:
    print(min(R) - max(L) + 1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
t_list = [[0] * 2 for _ in range(M)]
ans = 0
is_change = False
for i in range(M):
  t_list[i] = list(map(int, input().split()))
bottom = t_list[i][0]
up = t_list[i][1]
for i in range(M):
  t_bottom = t_list[i][0]
  t_up = t_list[i][1]
  if t_bottom > bottom and t_bottom <= up:
    bottom = t_bottom
    is_change = True
  if t_up < up and t_up >= bottom :
    up = t_up
    is_change = True
ans = up-bottom+1
if is_change:
  print(ans)
else:
  print("0")

Submission status: Wrong Answer

Solution code:
import sys
n ,m = map(int, input().split())
L = []
R = []
for i in range(m):
    l, r = map(int, input().split())
    if r - l <= 0:
        print(0)
        sys.exit(0) 
    L.append(l)
    R.append(r)

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

Submission status: Wrong Answer

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

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=LR[-1,0]
R=LR[0,1]

if R-L<0:
    print(0)
else:
    print(int(abs(R-L)+1))

Submission status: Wrong Answer

Solution code:
import sys
n ,m = map(int, input().split())
L = []
R = []
for i in range(m):
    l, r = map(int, input().split())
    if r - l <= 0:
        print(0)
        sys.exit(0) 
    L.append(l)
    R.append(r)

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

Submission status: Wrong Answer

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

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=np.max(LR[:,0])
R=np.min(LR[:,1])


el=np.sum(np.logical_and(LR>L,R>LR))


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

Submission status: Wrong Answer

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

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=np.max(LR[:,0])
R=np.min(LR[:,1])


el=np.sum(np.logical_and(LR>L,R>LR))


print(int(abs(R-L)+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:
import sys
import numpy as np
N,M= (int(i) for i in input().split(" "))  

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=np.max(LR[-1,:])
R=np.min(LR[0,:])


el=np.sum(np.logical_and(LR>L,R>LR))


print(int(abs(R-L)+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)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
t_list = [[0] * 2 for _ in range(M)]
for i in range(M):
  t_list[i] = list(map(int, input().split()))
bottom = t_list[i][0]
up = t_list[i][1]
for i in range(M):
  t_bottom = t_list[i][0]
  t_up = t_list[i][1]
  if t_bottom > bottom and t_bottom <= up:
    bottom = t_bottom
  if t_up < up and t_up :
    up = t_up
print(up - bottom + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
k=list(map(int,input().split()))
if m==1:
    print(k[1]-k[0]+1)
else:
    a=set(list(range(k[0],k[1]+1)))
    for i in range(m-1):
        r=list(map(int,input().split()))
        d=a & set(list(range(r[0],r[1])))
    print(len(d))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
k=list(map(int,input().split()))
if m==1:
    print(k[1])
else:
    a=set(list(range(k[0],k[1]+1)))
    for i in range(m-1):
        r=list(map(int,input().split()))
        d=a & set(list(range(r[0],r[1])))
    print(len(d))

Submission status: Wrong Answer

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

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=LR[-1,0]
R=LR[0,1]


el=np.sum(np.logical_and(LR>L,R>LR))


print(int(R-L+1))

Submission status: Wrong Answer

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

LR= np.array([[int(i) for i in input().split()] for i in range(M)]) 

L=LR[-1,0]
R=LR[0,1]


el=np.sum(np.logical_and(LR>L,R>LR))


print(int(abs(R-L)+1))

Submission status: Wrong Answer

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

p = []
q = []

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

p1 = max(p)
q1 = min(q)

if p.__len__() == 1:
    print(q1)
elif q1 >= p1:
    print(q1 - p1 + 1)
else:
    print(0)