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())
gate = [[int(i) for i in input().split()] for _ in range(m)]

minv = gate[0][0]
maxv = gate[0][1]

for x in gate:
  minv = max(minv,x[0])
  maxv = min(maxv,x[1])
  
print(maxv-minv+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

m = max(L)
M = min(R)
print(M-m+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)

if R[0] - L[-1] >= 0:
    print(R[0] - L[-1] + 1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = []
R = []
for _ 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:
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)


print(R[0] - L[-1] + 1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
L = []
R = []
for _ in range(M):
    a,b = map(int,input().split())
    L.append(a)
    R.append(b)
Ls = set(range(L[0],R[0]+1))
Rs = set(range(L[-1],R[-1]+1))

print(len(Ls & Rs))

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)
ans = 0
for j in range(1,n+1):
    cnt = 0
    for i in range(m) :
        if l[i] <= j or r[i] == j:
            cnt += 1
            continue
        break
    if cnt == m:
        ans += 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min_i = 1
max_i = n

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

print(max_i-min_i+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]
f_inf = float("inf")

max_l = 0
min_r = f_inf
for l, r in LR:
    max_l = max(max_l, l)
    min_r = min(min_r, r)

print(min_r - max_l+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n, m = map(int,input().split())
r = float('inf')
l = 0
L = []
for i in range(m):
    l_, r_ = map(int,input().split())
    L.append([l_,r_])
    l = max(l_, l)
    r = min(r_, r)
ans = 0
print(r-l+1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
for i in range(m):
    l,r=map(int,input().split())
    a.append(l)
    b.append(r)
x=max(a)
y=min(b)

print(y-x+1)

Submission status: Wrong Answer

Solution code:

N,M = map(int,input().split())
x,y = [],[]
C = []
for i in range(M):
    L,R = map(int,input().split())
    x.append(L)
    y.append(R)
    C.append(list(range(L,R+1)))

D = 0
for k in range(N):
    K = 0
    for l in range(M):
        if k in C[l]:
            K += 1
    if K == M:
        D += 1
        
print(D)

Submission status: Wrong Answer

Solution code:
#ABC127 C

N,M = map(int,input().split())
x,y = [],[]
C = []
for i in range(M):
    L,R = map(int,input().split())
    x.append(L)
    y.append(R)
    C.append(list(range(L,R+1)))

D = 0
for k in range(N):
    K = 0
    for l in range(M):
        if k in C[l]:
            K += 1
    if K == M:
        D += 1
        
print(D)

Submission status: Wrong Answer

Solution code:
card_num, gate_num = map(int, input().split())
table = []
count = 0

for i in range(gate_num):
    start_num, end_num = map(int, input().split())
    number = end_num - start_num
    table.append(number)

list.sort(table)
if gate_num == 1:
    print(card_num)
elif table[gate_num-1] == table[0]:
    print(table[0])
else:
    print(table[gate_num-1] - table[0])

Submission status: Wrong Answer

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

ans = 0

m = M-1
 
ans = N - m

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l_max=0
r_min=10**5+1
for i in range(m):
    l,r=map(int,input().split())
    l_max=max(l,l_max)
    r_min=min(r,r_min)
print(r_min-l_max+1)

Submission status: Wrong Answer

Solution code:
import math
import collections
import itertools

def YesNo(Bool):
    if(Bool):
        print("Yes")
    else:
        print("No")
    return

def resolve():

    N,M=map(int,input().split())
    L=[0]*M
    R=[0]*M
    Lmax=0
    Rmin=10**10
    for i in range(M):
        L[i],R[i]=map(int,input().split())
        Rmin=min(R[i],Rmin)
        Lmax=max(L[i],Lmax)

    print(Rmin-Lmax+1)

resolve()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = 0
R = N
for i 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:
n,m = map(int,input().split())
lmax, rmin = 0, n

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

Submission status: Wrong Answer

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

L, R = 0, 10 ** 5 + 1
for _ in range(M):
    l, r = map(int, input().split())
    L = max(L, l)
    R = min(R, r+1)


result = min(N, (R-L))
print(result)

Submission status: Wrong Answer

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

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 = [int(x) for x in input().split()]
L = list()
R = list()
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)
print(Rmin-Lmax+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
a = list()
b = list()
for i in range(M):
  tmp_a,tmp_b =  map(int, input().split())
  a.append(tmp_a)
  b.append(tmp_b)
print(min(b)-max(a)+1 if min(b)-max(a) > 0 else 0)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
a = list()
b = list()
for i in range(M):
  tmp_a,tmp_b =  map(int, input().split())
  a.append(tmp_a)
  b.append(tmp_b)
print(min(b)-max(a)+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())
  if L < l:
    L = l
  if R > r:
    R = r
print(1+R-L)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N, M = map(int, input().split())

left = 1
right = M

for i in range(M):
    a,b = map(int, input().split())
    left = max(left,a)
    right = min(right, b)

print(max(0,right-left+1))


# imos = [0] * (N + 2)
# for i in range(M):
#     l, r = map(int, input().split())
#     imos[l] += 1
#     imos[r + 1] -= 1
#
# imos = list(accumulate(imos))
#
# ans = 0
# for im in imos:
#     ans += (im == M)
#
# print(ans)

Submission status: Wrong Answer

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

l = 0
r = N

for i in range(M):
    L, R = map(int, input().split())
    
    if L > l:
        l = L
            
    if R < r:
        r = R
                
    ans = r - l + 1

else:
    print(ans)

Submission status: Wrong Answer

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

l = 0
r = N

for i in range(M):
    L, R = map(int, input().split())
    
    if M == 1:
        print(R-L+1)
        break
        
    else:
        if L > l:
            l = L
            
        if R < r:
            r = R
                
        ans = r - l + 1

else:
    print(ans)

Submission status: Wrong Answer

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

ans = set()

for i in range(M):
    L, R = map(int, input().split())
    
    if M == 1:
        print(R)
        break
        
    else:
        if i == 0:
            ans = set(list(range(L, R + 1)))
                
        else:
            s = set(list(range(L, R + 1)))
            ans = ans & s

else:
    print(len(ans))

Submission status: Wrong Answer

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

ans = set()

for i in range(M):
    L, R = map(int, input().split())
    
    if M == 1:
        print(R)
        break
        
    else:
        if i == 0:
            for n in range(L, R + 1):
                ans.add(n)
                
        else:
            s = set()
            for n in range(L, R + 1):
                s.add(n)
            ans = ans & s

else:
    print(len(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = 1
b = N

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

print(b-a+1)

Submission status: Wrong Answer

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

ll, rr = 1, l
for l, r in lr:
    if ll <= l <= rr or l <= ll <= r:
        ll, rr = max(ll, l), min(rr, r)
print(rr-ll+1)

Submission status: Wrong Answer

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

Lm = -1
Rm = float('inf')

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

print(Rm - Lm + 1)

Submission status: Wrong Answer

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

ans = 0
l_ans = 0
r_ans = float('inf')
for l, r in l_r:
    l_ans = max(l_ans, l)
    r_ans = min(r_ans, r)
print(r_ans-l_ans+1)

Submission status: Wrong Answer

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

L = []

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

res = [L[0][0], L[0][1]]

for i in range(M):
    res = [max(res[0], L[i][0]), min(res[1], L[i][1])]

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 01:27:46 2020

@author: liang
"""

N, M = map(int,input().split())
L = list()
R = list()
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 math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))
L=[]
R=[]
for i in range(len(h)):
    L.append(h[i][0])
for i in range(len(h)):
    R.append(h[i][1])

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

Submission status: Wrong Answer

Solution code:
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))

ans=[]
ha1=[]
ha = [k for i in range(e2) for k in range(1,e1+1) if h[i][0]<=k<=h[i][1]]
sha=sorted(ha)

ma=0
count=1
#最大値求める
for i in range(len(sha)-1):
    if sha[i]==sha[i+1]:
        count+=1
    else:
        ma=max(ma,count)
        count=1
ma=max(ma,count)

print(ma//e2)
# for i in range(1,e1+1):
#     count=0
#     for k in range(e2):
#         if h[k][0]<=i<=h[k][1]:
#             count+=1
#     if count==e2:
#         ans.append(count)

# print(len(ans))

Submission status: Wrong Answer

Solution code:
# import math
# import statistics
# a=input()
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
e1,e2 = map(int,input().split())
#f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
h = []
for i in range(e2):
    h.append(list(map(int,input().split())))

ans=[]

for i in range(1,e1+1):
    count=0
    for k in range(e2):
        if h[k][0]<=i<=h[k][1]:
            count+=1
        else:
            print(0)
            break
    if count==e2:
        ans.append(count)

print(len(ans))

Submission status: Wrong Answer

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

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

ans = []
for i in range(M):
    ans.extend(L[i:])
    ans.extend(R[:i+1])

import numpy as np

splited = np.array_split(ans, M)

S = set(i for i in range(1, N+1))
for s in splited:
    S = S & set(s)

print(len(S))

Submission status: Wrong Answer

Solution code:
#import math
#import itertools
import numpy as np
#from collections import deque

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,m=INTM()
    ls=[]
    rs=[]
    for i in range(m):
        l,r=INTM()
        ls.append(l)
        rs.append(r)
    
    print(min(rs)-max(ls)+1)



    
if __name__=='__main__':
    do()

Submission status: Wrong Answer

Solution code:
#import math
#import itertools
import numpy as np
#from collections import deque

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,m=INTM()
    ct=np.array([0]*n)
    for i in range(m):
        l,r=INTM()
        ct[l-1:r]+=1
    
    la=0
    ra=0
    for i in range(n):
        if ct[i]==m:
            #print(i)
            la = i
            break

    for i in range(n-1,-1,-1):
        if ct[i]==m:
            ra=i
            break
    
    print(ra-la+1)
    #print(la,ra,ct)


    
if __name__=='__main__':
    do()

Submission status: Wrong Answer

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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
if m>=s:
    print(s, m)
else:
    print(0)

Submission status: Wrong Answer

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

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

ans = [1]*n

s = 0
m = n

for i in list:
    if i[0] > s:
        s = i[0]
    if i[1] < m:
        m = i[1]
print(m-s+1)

Submission status: Wrong Answer

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

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

ans = [1]*n

for i in list:
    for j in range(len(ans)):
        if not i[0]<= j <= i[1]:
            ans[j] = 0
print(sum(ans))

Submission status: Wrong Answer

Solution code:
L,M=map(int,input().split())
a=[0]*L
x=[]
y=[]
for i in range(M):
    b,c=map(int,input().split())
    x.append(b)
    y.append(c)
if max(x)<min(y):
    print(-max(x)+min(y)+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
L,M=map(int,input().split())
a=[0]*L
x=[]
y=[]
for i in range(M):
    b,c=map(int,input().split())
    x.append(b)
    y.append(c)
print(-max(x)+min(y)+1)

Submission status: Wrong Answer

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

max = 0
min = n+1
for i in range(m):
    if lr[i][0] > max :
        max = lr[i][0]
    if lr[i][1] < min :
        min = lr[i][1]

print(min-max+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
temp=0
TEMP=N-1
for i in range(M):
    a,b=map(int,input().split())
    if not temp<=a-1<=TEMP and not temp<=b-1<=TEMP:
        print(0)
        exit()
    if a-1>temp:
        temp=a-1
    if TEMP>b-1:
        TEMP=b-1
    
print(TEMP-temp+1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
temp=0
TEMP=N-1
for i in range(M):
    a,b=map(int,input().split())
    if a-1>temp:
        temp=a-1
    if TEMP>b-1:
        TEMP=b-1
print(TEMP-temp+1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(input().split())
haba = a[0]
for i in a:
    if int(haba[0]) < int(i[0]) < int(haba[1]):
        haba[0] = i[0]
    if int(haba[0]) <= int(i[1]) < int(haba[1]):
        haba[1] = i[1]
if n < int(haba[0]):
    ans = 0
elif int(haba[0]) <= n <= int(haba[1]):
    ans = n - int(haba[0]) + 1
else:
    ans = int(haba[1]) - int(haba[0]) + 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(input().split())
haba = a[0]
for i in a:
    if int(haba[0]) < int(i[0]) < int(haba[1]):
        haba[0] = i[0]
    if int(haba[0]) < int(i[1]) < int(haba[1]):
        haba[1] = i[1]
if n < int(haba[0]):
    ans = 0
elif int(haba[0]) <= n <= int(haba[1]):
    ans = n - int(haba[0]) + 1
else:
    ans = int(haba[1]) - int(haba[0]) + 1
print(ans)

Submission status: Wrong Answer

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

max_l, min_r = 0, N

for l, r in LR:
    max_l = max(l, max_l)
    min_r = min(r, min_r)

print(min_r - max_l + 1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    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())

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

if __name__ == '__main__':
    main()

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

keys = []

for i in range(M):
  keys.append(set(range(L[i],R[i]+1)))
  
common = set(range(1, N+1))
for i in range(M-1):
  common = common & keys[i]
    
print(len(common))

Submission status: Wrong Answer

Solution code:
def main():
    x = list(map(int, input().split()))
    n = x[0]
    m = x[1]

    common_section = [1, n]

    for i in range(m):
        a = list(map(int, input().split()))
        if a[0] > common_section[0]:
            common_section[0] = a[0]
        if a[1] < common_section[1]:
            common_section[1] = a[1]

    answer = common_section[1] - common_section[0] + 1
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    x = list(map(int, input().split()))
    n = x[0]
    m = x[1]

    common_section = [1, n+1]

    for i in range(m):
        a = list(map(int, input().split()))
        if a[0] > common_section[0]:
            common_section[0] = a[0]
        if a[1] < common_section[1]:
            common_section[1] = a[1]

    answer = common_section[1] - common_section[0] + 1
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

n = 0
m = 999999

for i in range(M):
  a,b = map(int, input().split())
  n = max(a, n)
  m = min(b , m)

print(m - n + 1)

Submission status: Wrong Answer

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, m = Input()
    max_l = 0
    min_r = 1e6
    for i in range(m):
        l, r = Input()
        max_l = max(max_l, l)
        min_r = min(min_r, r)
    print((min_r+1) - max_l)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


for i in range(M-1):
      L1, R1 = map(int, input().split())
      if R1 < L or R < L1:
        break
      else:
        if L <= L1 <= R:
          L = L1
          
        elif L <= R1 <= R:
          R = R1
        
          
print(R-L+1)

Submission status: Wrong Answer

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


L, R = map(int, input().split())
ar = [i for i in range(L, R+1)]
for i in range(M-1):
      L, R = map(int, input().split())
      if ar[-1] < L or ar[0] > R:
        break
      else:
        if ar[0] < L < ar[-1]:
          ar = [x for x in range(L, ar[-1]+1)]
        elif ar[0] < R <ar[-1]:
          ar = [x for x in range(ar[0], R+1)]
print(len(ar))

Submission status: Wrong Answer

Solution code:
N,T = map(int,input().split())
X = [list(map(int,input().split())) for n in range(T)]
M = N
m = 0

for a,b in X:
  M = min(M,b)
  m = max(m,a)

print(M-m+1)

Submission status: Wrong Answer

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

for i in range(M):
  L, R = map(int, input().split())
  
  if i == 0:
    top = R
    bottom = L
  
  top = min(top, R)
  bottom = max(bottom, L)

print(top-bottom+1)

Submission status: Wrong Answer

Solution code:
import copy

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

l1 = []
l2 = []

for i in range(M):
  L, R = map(int, input().split())
  
  l1 = list(range(L,R+1))
  if len(l2) == 0:
    l2 = copy.copy(l1)
  ans = set(l1) & set(l2)
  l2 = copy.copy(ans)

print(len(ans))

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
Key = [0 for i in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    L -= 1
    R -= 1
    Key[L] += 1
    Key[R+1] -= 1
count = 0
for i in range(len(Key)):
    if i > 0:
        Key[i] += Key[i-1]
        if Key[i] == M:
          count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
Key = [0 for i in range(N+1)]
for _ in range(M):
    L,R = map(int,input().split())
    L -= 1
    R -= 1
    Key[L] += 1
    Key[R+1] -= 1
for i in range(len(Key)):
    if i > 0:
        Key[i] += Key[i-1]
for i in range(len(Key)-1):
    if i > 0:
        if Key[i-1] > 0 and Key[i] == 0 and Key[i+1] > 0:
            print(0)
            exit()
print(Key.count(max(Key)))

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
Key = [0 for i in range(N+2)]
for _ in range(M):
    L,R = map(int,input().split())
    Key[L] += 1
    Key[R+1] -= 1
for i in range(len(Key)):
    if i > 0:
        Key[i] += Key[i-1]
print(Key.count(max(Key)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,M = map(int, input().split())
ansL = 0
ansR = 0
for i in range(M):
    L,R = map(int,input().split())
    if M-1 == 0:
      ansL = L
      ansR = R
      break
    if i == 0:
        ansR = R
    elif i == M-1:
        ansL = L
print(ansR-ansL+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

bottom = 0
top = N+1

for i in range(M):
  L,R = map(int,input().split())
  bottom = max(bottom, L)
  top = min(top,R)
  
print(top - bottom + 1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
Lmax = 0
Rmin = 1e8
for _ in range(M):
    L,R=map(int,input().split())
    if L > Lmax:
        Lmax = L
    if R < Rmin:
        Rmin = R

print(Rmin - Lmax + 1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l_max=0
r_min=10**5 
for i in range(m):
  l,r = map(int,input().split())
  l_max = max(l_max,l)
  r_min = min(r_min,r)
print(r_min - l_max + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

mi = 0
ma = n
for i in range(m):
  mi = max(mi, lr[i][0])
  ma = min(ma, lr[i][1])
  
print(ma-mi+1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
Lmi, Lma = [], []
for i in range(M):
  mi, ma = map(int, input().split())
  Lmi.append(mi)
  Lma.append(ma)
print(min(Lma) - max(Lmi) + 1)

Submission status: Wrong Answer

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

min, max = map(int, input().split())

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

Submission status: Wrong Answer

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

max_left = 0
min_right = n
for _ in range(m):
    l_tmp, r_tmp = map(int, input().strip().split())
    if max_left < l_tmp:
        max_left = l_tmp
    if min_right > r_tmp:
        min_right = r_tmp

print(min_right - max_left + 1)

Submission status: Wrong Answer

Solution code:
s = input().split()
n=int(s[0])
m=int(s[1])
l=0
r=float('inf')
for i in range(m):
    s = list(map(int,input().split()))
    l=max(l,s[0])
    r=min(r,s[1])
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(L)
r = min(R)

print(r-l+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools

N,M = map(int,input().split())
LR = [list(map(int,input().split()))for _ in range(M)]
a = []
b = []
for i in range(M):
    a.append([int(k) for k in range(LR[i][0],LR[i][1]+1)])

b = list(itertools.chain.from_iterable(a))

print(len([x for x in set(b) if b.count(x)>1]))

Submission status: Wrong Answer

Solution code:
n , m = map(int, input().split())
count = 0
low = 0
high  = n
for _ in range(m):
    l,r=map(int, input().split())
    low = max(l, low)
    high = min(high, r)
    #print(low, high)

print(high-low+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
l = []
r = []
for _ 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:
n, m = map(int, input().split())

card = list(range(n))

for i in range(m):
  l,r = map(int, input().split())
  del card[:(l-1)]
  del card[r:]

print(len(card))

Submission status: Wrong Answer

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

card = list(range(n))

for i in range(m):
  l,r = map(int, input().split())
  del card[:(l-1) and m:]

print(len(card))

Submission status: Wrong Answer

Solution code:
def solve():
    import sys
    n, m, *LR = map(int, sys.stdin.read().split())
    L = LR[::2]
    R = LR[1::2]
    l = max(L)
    r = min(R)
    print(r - l + 1)


solve()

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
L, R = 0, M + 1
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 = list(map(int, input().split()))
L, R = 0, N + 1
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:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

# n,mが10^5なのでO(n^2)は間に合わない
"""
ans = 0
for i in range(1,n+1):
    flg = True
    for j in range(m):
        if i < l[j][0] or i > l[j][1]:
            flg = False
    if (flg):
        ans+=1
print(ans)
"""

#lをO(m)で走査して上限、下限をあらかじめ確定しておく
up = 10**5+1
lo = 0

for i in range(m):
    if lo < l[i][0]:
        lo = l[i][0]
    if  up > l[i][1]:
        up = l[i][1]

ans = 0
print(lo,up)
for i in range(1,n+1):
    if i >= lo and i <= up:
        ans+=1
print(ans)

Submission status: Wrong Answer

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


print(0 if x <0 else x+1)