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 = []
for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)
print(min(R[0]-L[M-1]+1, N))

Submission status: Wrong Answer

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

ans = [0] * (N+1)
count = 0
prev = []
for i in range(M):
    l, r = map(int, input().split())

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

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

Submission status: Wrong Answer

Solution code:
N, M = [int(x) for x in input().split()]
L = 1
R = N
for i in range(M):
    L_,R_ = [int(x) for x in 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())
a = []
for i in range(M):
    a.append(list(map(int, input().split())))
id_card = []
L = []
R = []
for i in range(M):
    L.append(a[i][0])
    R.append(a[i][1])
print(min(R) - max(L) +1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

from sys import stdin

N, M = [int(x) for x in stdin.readline().rstrip().split()]

clear_list = [1] * N
start = 0
end = 0
ans_start = 0
ans_end = 0
    
for i in range(M):
    start, end  = [int(x) for x in stdin.readline().rstrip().split()]


    if ans_start == 0 or ans_start < start: 
        ans_start = start
    if ans_end == 0 or ans_end > end:
        ans_end = end

print(ans_end - ans_start + 1 )

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
l, r = map(int, input().split())
cnt = 0
num = 0
if (l == r):
  cnt += 1
  num = l
for i in range(M-1):
  L, R = map(int, input().split())
  if (cnt == 1 and L == R and num == L):
    num = L
  elif (cnt == 1 and L == R and num != L):
    cnt += 1
    print(0)
    break
  elif (cnt == 0 and L == R):
    cnt += 1
    num = L
  elif ((cnt == 0 or cnt == 1) and L != R):
    if (L >= l):
      l = L
    if (R <= r):
      r = R
if (num != 0 and cnt == 1):
  if (num >= l and num <= r):
    print(1)
  else:
    print(0)
elif(cnt == 0 or cnt == 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())
if M == 1:
  print(N)
else:
  s0 = set(range(L[0],R[0]+1))
  for i in range(1,M):
    ans = s0 & (set(range(L[i],R[i]+1)))
  print(len(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
f = []
for i in range(m):
    a = list(map(int, input().split()))
    f.append(a)
print(f[0][1] - f[m-1][0] + 1)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
Ls, Rs = [], []
for i in range(M):
    n, m = list(map(int, input().split()))
    Ls.append(n)
    Rs.append(m)

# ll = Ls[0]
# rr  =Rs[0]
# for i in range(M):
#     ll = max(ll, Ls[i])
#     rr = min(rr, Rs[i])
#
# print(rr - ll + 1)
print(min(Rs) - max(Ls) + 1)

Submission status: Wrong Answer

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

li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i][0] < li[i+1][0] ):
        L_max = li[i+1][0]
        if (li[i][1] > li[i+1][1]):
            R_min = li[i+1][1]
    elif (li[i ][1] > li[i+1][1]):
            R_min = li[i+1][1]

if (li[M-1][0] < li[M][0] ):
       L_max = li[M][0]
       if (li[M-1][1] > li[M][1]):
          R_min = li[M][1]
elif (li[M-1 ][1] > li[M][1]):
            R_min = li[M][1]

if (L_max > N) or (L_max > R_min):
    print("0")
elif(R_min < N):
    print(R_min - L_max + 1)
else:
    print(N + 1 - L_max)

Submission status: Wrong Answer

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

ans = []
minn = 10000000
maxx = 0
for i in range(m):
    l,r = [int(s) for s in input().split(' ')]
    maxx = max(maxx,l)
    minn = min(minn,r)
print(minn - maxx+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]

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

Submission status: Wrong Answer

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

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

cards = []
for i in range(M):
    Li, Ri = s_input()
    L.extend([Li])
    R.extend([Ri])
    cards.extend(list(range(Li, Ri+1)))
cards = set(cards)

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

card_num = R_min - L_max + 1
for i in range(L_max, R_min + 1):
    if not i in cards:
        max_card_num -= 1

print(card_num)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
a = []
b = []
c = []
for i in range(m):
  a.append(list(map(int, input().split())))
for i in range(m):
  b.append(a[i][0])
  c.append(a[i][1])
  
b_max = max(b)
c_min = min(c)

ans = min(c) - max(b) + 1

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min_i = 0
diff = 0
for i in range(M):
    a_list.append(map(int, raw_input().split()))
    tmp_diff = a_list[i][-1] - a_list[i][0]

    if i==0:
        diff = tmp_diff
    else:
        if diff > tmp_diff:
            diff = tmp_diff
            min_i = i

count = 0
for i in range(a_list[min_i][0], a_list[min_i][-1]+1):
    flag = 0
    for j in range(M):
        if j != min_i:
            if (a_list[j][0] <= i) and (i <= a_list[j][-1]):
                flag = 1
            else:
                flag = 0
                break
    if flag == 1:
        count = count + 1

if M==1:
    print N
else:
    print count

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]

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

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

    print(A,B)


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

Submission status: Wrong Answer

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

r_l, r_r = l_list[0], r_list[0]
for l, r in zip(l_list[1:], r_list[1:]):
    if l <= r_r <= r :
        r_l = l
    elif l >= r_l >= r:
        r_r = r
    else:
        r_l, r_r = 0, -1
        break

print(r_r - r_l + 1)

Submission status: Wrong Answer

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

li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i][0] < li[i+1][0] ):
        L_max = li[i+1][0]
        if (li[i][1] > li[i+1][1]):
            R_min = li[i+1][1]
    elif (li[i ][1] > li[i+1][1]):
            R_min = li[i+1][1]

if (li[M-1][0] < li[M][0] ):
       L_max = li[M][0]
       if (li[M-1][1] > li[M][1]):
          R_min = li[M][1]
elif (li[M-1 ][1] > li[M][1]):
            R_min = li[M][1]

if (L_max > N) and (L_max > R_min):
    print("0")
elif(R_min < N):
    print(R_min - L_max + 1)
else:
    print(N + 1 - L_max)

Submission status: Wrong Answer

Solution code:
x = input()
li = x.split()
n = int(li[0])
m = int(li[1])
o = 0
y = input()
e = y.split()
u = int(e[0])
t = int(e[1])
q = u
w = t
for k in range (m - 1):
  y = input()
  e = y.split()
  l = int(e[0])
  r = int(e[1])
  if (q < l and w < r):
    q = l
    w = r
for j in range (n):
  s = int(j) + 1
  ans = 0
  if(u <= s and s <= t):
    ans += 1
  if(q <= s and s <= w):
    ans += 1
  if(ans == 2):
    o += 1

print(o)

Submission status: Wrong Answer

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

for i in range(0, m):
    ids = [filter(lambda x: x >= gates[i][0], ids)]
    ids = [filter(lambda x: x <= gates[i][1], ids)]

print(len(ids))

Submission status: Wrong Answer

Solution code:
import collections

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

if M == 1:
  print(N)
  exit()

x = []  
for _ in range(M):
  L,R = map(int,input().split())
  if R-L <= 1:
    print(0)
    exit()            
  else:
    x.extend([L,L+1,R])
    
c = collections.Counter(x)

print(len([i[0] for i in c.items() if i[1] == M]))

Submission status: Wrong Answer

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

keys = [0] * N

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

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

    for key in keys:
        if key < L or key > R:
            keys.remove(key)

print(len(keys))

Submission status: Wrong Answer

Solution code:
import collections

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

ok_card = []
for i in range(m):
  hoge = input().split()
  for j in range(int(hoge[1]) - int(hoge[0])+1):
    ok_card.append(int(hoge[0])+j)

big_a = list(set(ok_card))
c = collections.Counter(ok_card)
print(len([i for i in range(len(big_a)) if int(c[i+1]) >= m]))

Submission status: Wrong Answer

Solution code:
N, M= map(int, input().split()) 
L = [0] * M
R = [0] * M
S = set(range(N))
for i in range(M):
    L[i], R[i] = map(int, input().split())
if M > 1:
  s0 = set(range(L[0],R[0]+1))
  for i in range(1,M):
    ans = s0 & (set(range(L[i],R[i]+1)))
  print(len(ans))
else:
  print(N)

Submission status: Wrong Answer

Solution code:
N,M=(int(x) for x in input().split())
L_R = list()

mini = 1000000
maxi = 0
A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
if A < mini:
    mini = A
if B > maxi:
    maxi = B
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,B])
    if A < mini:
        mini = A
    if B > maxi:
        maxi = B

#mini = 1000000
#for i in range(len(L_R)):
#    if L_R[i][0] < mini:
#        mini = L_R[i][0]
#maxi = 0
#for i in range(len(L_R)):
#    if L_R[i][1] > maxi:
#        maxi = L_R[i][1]

num = 0
for x in range(mini,maxi+1):
    n = 0
    for i in range(1,M+1):
        if L_R[i][0] <= x <= L_R[i][1]:
            n += 1
    if n == M:
        num += 1
    else:
        pass
print(mini,maxi)
print(num)

Submission status: Wrong Answer

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

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)
lm=max(l)
rm=min(r)
if lm-rm<0:
  print(0)
else:
  print(lm-rm+1)

Submission status: Wrong Answer

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

l_max=-1
r_min=float("inf")
for i in range(M):
	l,r=map(int, raw_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:
# -*- coding: utf-8 -*-
import sys
N, M= map(int, raw_input().split())
i = 1
a, b = map(int, raw_input().split()) 
while 1:
  if i >= M:
    result = a - b + 1
    print result
    sys.exit()
  c, d = map(int, raw_input().split())  
  a = c
  i = i + 1

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
max=ab[0][1]
min=ab[0][0]

for i in range(1,m):
    if min<ab[i][0]<=max:
        min=ab[i][0]
    if min<=ab[i][1]<max:
        max=ab[i][1]
print(max-min+1)

Submission status: Wrong Answer

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


  
  

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

  
out= end - start +1
print(out)

Submission status: Wrong Answer

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

con = 0

for i in range(N+1):
    for j in range(M):
        if L[j] > i or i > R[j]:
            break
    else:
        con = i
        break
print(R[0]-con+1)

Submission status: Wrong Answer

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

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

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

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


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

    else:
        A = A2
        B = B2

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(M)]
 
ans = 0
t = 0
 
for i in range(N): 
    t = 1
    for j in range(M):
        if not li[j][0] <= i+1:
            t = 0
            break
        elif not li[j][0] <= li[j][1]:
            t = 0
            break
    ans += t
 
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
Lm=0
Rm=N
ans=0

for i in range(M):
    Lt,Rt=map(int,input().split())
    if Lm < Lt:
        Lm = Lt
    if Rm > Rt:
        Rm = Rt
print(Rm-Lm+1)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

li = [[0, 99999999999]]

for i in range(M):
    li.append(list(map(int, input().split())))
    if (li[i][0] < li[i+1][0] ):
        L_max = li[i+1][0]
        if (li[i][1] > li[i+1][1]):
            R_min = li[i+1][1]
    elif (li[i ][1] > li[i+1][1]):
            R_min = li[i+1][1]

if (li[M-1][0] < li[M][0] ):
       L_max = li[M][0]
       if (li[M-1][1] > li[M][1]):
          R_min = li[M][1]
elif (li[M-1 ][1] > li[M][1]):
            R_min = li[M][1]

if (L_max > N) and (L_max > R_min):
    print(0)
elif(R_min < N):
    print(R_min - L_max + 1)
else:
    print(N + 1 - L_max)

Submission status: Wrong Answer

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

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

    if j == 0:
      unq=set(d)
    else:
      unq=unq&set(d)

  print(len(list(unq)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l,r = [],[]
for i in range(m):
    x,y = map(int,input().split())
    l.append(x)
    r.append(y)
cnt = 0
for i in range(1,n):
    for j in range(m):
        if l[j]>i or r[j]<i:
            break
    else:
        cnt+=1
if cnt>m:
    cnt=m
print(cnt)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
lLst = [0] * M
rLst = [0] * M
ans = 1
for i in range(M):
    lLst[i], rLst[i] = map(int, input().split())

if lLst[M - 1] < rLst[0]:
    ans += rLst[0] - lLst[M - 1]

print(ans)

Submission status: Wrong Answer

Solution code:
# -- coding:utf-8 --
n,m = map(int,input().split())
lr = [list(map(int,input().split())) for ii in range(m)]
L = [lr[jj][0] for jj in range(m) ]
R = [lr[jj][1] for jj in range(m) ]

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

Submission status: Wrong Answer

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

if L[-1] == R[0]:
    count = 1
elif L[-1] < R[0]:
    count = R[0] - L[M - 1] + 1
else:
    count = 0

print(count)

Submission status: Wrong Answer

Solution code:
l = raw_input().split()
N = int(l[0])
M = int(l[1])

L = []
R = []

for i in range(M):
    l = raw_input().split()
    L.append(int(l[0]))
    R.append(int(l[1]))

l = 0
r = 0
L.sort()
R.sort()
print(R[0] - L[M - 1] + 1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
N, M= map(int, raw_input().split())
i = 1
a, b = map(int, raw_input().split()) 
while 1:
  if i >= M:
    result = a - b + 1
    print result
    sys.exit()
  c, d = map(int, raw_input().split())  
  a = d
  i = i + 1

Submission status: Wrong Answer

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

def merge(P1, P2):
    L1, R1 = P1
    L2, R2 = P2
    return (P2[0], min(R1, R2))

data = []
for i in range(0, M):
    l, r = input().split()
    data.append((int(l), int(r)))
data = sorted(data, key=lambda x: x[0])

import math
logM = math.floor(math.log2(M))

totyuu = data
# 2つずつくみでしょりしたい
for i in range(0, logM):
    half = []
    if len(totyuu)%2==1:
        half.append(totyuu[-1])
    for j in range(0, int(math.floor(len(totyuu)/2))):
        # じっさいににこずつ
        half.append(merge(totyuu[2*j], totyuu[2*j+1]))
    # halfに半分入っている
    # totyuuをおきかえる
    totyuu = half
data = totyuu

for i in range(0, len(data)):
  tmpL, tmpR = data[i]
  L = tmpL
  R = min(tmpR, R)
  if L>R:
    print(0)
    exit()
print(max(0, R-L+1))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
lLst = [0] * M
rLst = [0] * M
ans = 1
for i in range(M):
    lLst[i], rLst[i] = map(int, input().split())

if lLst[M - 1] < rLst[0]:
    ans += rLst[0] - lLst[M - 1]
else:
    if lLst[M-1] == rLst[M - 1]:
        ans = len(lLst)

print(ans)

Submission status: Wrong Answer

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

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)
min_list = []

r_l, r_r = l_list[0], r_list[0]
for l, r in zip(l_list[1:], r_list[1:]):
    if r_r >= l:
        r_l = l
    elif r_l >= r:
        r_r = r
    else:
        r_l, r_r = 0, -1
        break

print(r_r - r_l + 1)

Submission status: Wrong Answer

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

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

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

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

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


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

Submission status: Wrong Answer

Solution code:
N,M=(int(x) for x in input().split())
L_R = list()

A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
mini = 1000000
maxi = 0
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,B])
    if A < mini:
        mini = A
    if B > maxi:
        maxi = B

#mini = 1000000
#for i in range(len(L_R)):
#    if L_R[i][0] < mini:
#        mini = L_R[i][0]
#maxi = 0
#for i in range(len(L_R)):
#    if L_R[i][1] > maxi:
#        maxi = L_R[i][1]

num = 0
for x in range(mini,maxi+1):
    n = 0
    for i in range(1,M+1):
        if L_R[i][0] <= x <= L_R[i][1]:
            n += 1
    if n == M:
        num += 1
    else:
        pass
print(num)

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)
lm=max(l)
rm=min(r)
print(rm-lm+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split(" "))
l_init,r_init=map(int,input().split(" "))
ans_l=l_init
ans_r=r_init
isPossible=True
for i in range(m-1):
	l,r=map(int,input().split(" "))
	isupdated=False
	next_ans_l=ans_l
	next_ans_r=ans_r
	if ans_l <= l and l <= ans_r:
		next_ans_l=l
		isupdated=True
	if ans_l <= r and r <= ans_r:
		next_ans_r=r
		isupdated=True
	if not isupdated:
		isPossible=False
		break
	ans_l=next_ans_l
	ans_r=next_ans_r
ans=ans_r-ans_l
if isPossible:
	print(ans+1)
else:
	print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
lr =[[int(i)for i in input().split()]for _ in range(M)]
ans =[0,N+1]
for LR in lr:
    if ans[0] < LR[0]:
        ans[0]=LR[0]
    if LR[1] < ans[1]:
        ans[1] =LR[1]
print(min(0,ans[1]-ans[0]+1))

Submission status: Wrong Answer

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

def merge(P1, P2):
    L1, R1 = P1
    L2, R2 = P2
    return (max(L1, L2), min(R1, R2))

data = []
for i in range(0, M):
    l, r = input().split()
    data.append((int(l), int(r)))
data = sorted(data, key=lambda x: x[0])

import math
logM = math.floor(math.log2(M))

totyuu = data
# 2つずつくみでしょりしたい
for i in range(0, logM):
    half = []
    if len(totyuu)%2==1:
        half.append(totyuu[-1])
    for j in range(0, int(math.floor(len(totyuu)/2))):
        # じっさいににこずつ
        half.append(merge(totyuu[2*j], totyuu[2*j+1]))
    # halfに半分入っている
    # totyuuをおきかえる
    totyuu = half
data = totyuu

for i in range(0, len(data)):
  tmpL, tmpR = data[i]
  L = max(1, tmpL)
  R = min(tmpR, R)
  if L>R:
    print(0)
    exit()
print(max(0, R-L+1))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
X = []
for i in range(M):
  L,R = map(int,input().split())
  for j in range (R-L+1):
    A = L + j
    X.append(A)
X_duplicate = [x for x in set(X) if X.count(x) > 1]
L=[str(a) for a in X_duplicate]
L=" ".join(L)
print(L)

Submission status: Wrong Answer

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

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

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

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

card_max = R_min - L_max + 1
if card_max > N:
    print(N)
else:
    print(card_max)

Submission status: Wrong Answer

Solution code:
tmp = input().split()
N = int(tmp[0])
M = int(tmp[1])

max = N
min = 0
for i in range(M):
  inputcard = list(map(int, input().split()))
  if min < inputcard[0]:
    min = inputcard[0]
  
  if max > inputcard[1]:
    max = inputcard[1]
  
print(max - min + 1)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
if __name__=='__main__':
  N, M = map(int, input().split())
  L, R = 1, N
  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 = [0] * M
R = [0] * M
for i in range(M):
    L[i], R[i] = map(int, input().split())

G = []
for i in range(M):
    G1 = []
    for j in range(len(L[i:])):
        G1.append(L[i:][j])
    for k in range(len(R[:(i+1)])):
        G1.append(R[:(i+1)][k])
    G.append(G1)

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

print(len(count_list))

Submission status: Wrong Answer

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

lis = []  # [s, e]

for j in range(M):
  L, R = [int(_) for _ in input().split()]
  
  if j==0:
    lis = [L, R]
  else:
    s = lis[0]
    e = lis[1]
    if R <= e and R >= s:
      lis[1] = R
    if L <= e and L >= s:
      lis[0] = L
    
print(lis[1]-lis[0]+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lim = []
res = []
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:
        a = int(res.index(k[0]))
    except ValueError:
        a = res.index(min(res))
    #print(res[:a])
    del res[:a]
    try:
        b = int(res.index(k[1]))
    except ValueError:
        b = res.index(max(res))
    del res[b+1:]
    #print(res)
    #print(a," ",b)
print(len(res))

Submission status: Wrong Answer

Solution code:
N,M=(int(x) for x in input().split())
L_R = list()

A,B = (int(x) for x in input().split())
L_R.append([A,B])
L_R.append([A,B])
for i in range(2,M+1):
    A,B = (int(x) for x in input().split())
    L_R.append([A,B])

num = 0
for x in range(1,N):
    n = 0
    for i in range(1,M+1):
        if L_R[i][0] <= x <= L_R[i][1]:
            n += 1
    if n == M:
        num += 1
    else:
        pass
print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
list_a = list(range(1, a, 1))
for num in range(b):
    c, d = map(int, input().split())
    list_b = list(range(1, c, d))
    list_a = list(set(list_a)-set(list_b))
                
print(len(list_a))

Submission status: Wrong Answer

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


  
  

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

  
out= end - start +1
print(out)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

q = [0] * (N+1)
for l, r in LR:
    print(l, r)
    q[l-1] += 1
    q[r] -= 1

# print(q, list(accumulate(q)))

print(sum(M == m for m in list(accumulate(q))[:-1]))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=0
r=0
list1=[]
for k in range(1,n+1):
  list1.append(k)
for i in range(m):
  l,r=map(int,input().split())
  for h in list1:
    if int(h)<int(l) or int(h)>int(r):
      list1.remove(h)
print(len(list1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))


def merge(a,b):
    l=sorted([a[0],b[0]])
    r=sorted([a[1],b[1]])
    return l[1],r[0]


N,M=spaceinput()
gates=[]
for i in range(M):
    gates.append(spaceinput())

d=gates[0]
for i in range(1,M):
    d=merge(d,gates[i])

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