Problem:


Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991



Sample input:
2
1 2
2 3


Sample output:
3

Submission status: Accepted

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    left = []
    right = []
    for _ in range(n):
        a, b = mi()
        left.append(a)
        right.append(b)
    left.sort()
    right.sort()

    if n % 2 == 0:
        x = (left[n//2-1] + left[n//2]) / 2
        y = (right[n//2-1] + right[n//2]) / 2
        print(int((y - x) * 2) + 1)
    else:
        x = left[n//2]
        y = right[n//2]
        print(y - x + 1)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
la = []
lb = []

for i in range(n):
    a, b = map(int, input().split())
    la.append(a)
    lb.append(b)

la.sort()
lb.sort()

if n % 2 == 1:
    lr = (n + 1) // 2
    ma = la[lr-1]
    mb = lb[lr-1]
    print(mb-ma+1)
else:
    l = n // 2
    r = n // 2 + 1
    ma = (la[l-1] + la[r-1]) / 2
    mb = (lb[l - 1] + lb[r - 1]) / 2
    print(int((mb-ma)*2+1))

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*(n+1)
b = [0]*(n+1)
for i in range(1,n+1):
	a[i],b[i] = map(int,input().split())
a.sort()
b.sort()

if n%2 == 1:
	print(b[(n+1)//2]-a[(n+1)//2]+1)
else:
	print(b[n//2]-a[n//2]+b[n//2+1]-a[n//2+1]+1)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())

maxs = []
mins = []

for _ in range(N):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    maxs.append(b)
    mins.append(a)

maxs.sort()
mins.sort()

if N % 2 == 1:
    mid = (N + 1) // 2 - 1
    ans = maxs[mid] - mins[mid] + 1
else:
    mid = N // 2
    ans = (maxs[mid - 1] + maxs[mid] - mins[mid - 1] - mins[mid] + 1)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list()
B = list()
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
A.sort()
B.sort(reverse=True)
if N%2 == 1:
    print(B[(N+1)//2 - 1] - A[(N+1)//2 - 1] + 1)
else:
    print((B[(N+1)//2 - 1] - A[(N+1)//2 - 1]) + (B[(N+1)//2] - A[(N+1)//2]) + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b
A = sorted(A)
B = sorted(B)
res = 0
if N % 2 == 1:
    l = A[int(N/2)]
    r = B[int(N/2)]
    res = r - l + 1
else:
    l = A[int(N/2) - 1] + A[int(N/2)]
    r = B[int(N/2) - 1] + B[int(N/2)]
    res = r - l + 1
print(res)

Submission status: Accepted

Solution code:
N, *AB = map(int, open(0).read().split())

A = sorted(AB[::2])
B = sorted(AB[1::2])

if N % 2 == 1:
    L = A[N // 2]
    R = B[N // 2]
else:
    L = A[N // 2 - 1] + A[N // 2]
    R = B[N // 2 - 1] + B[N // 2]

print(R - L + 1)

Submission status: Accepted

Solution code:
import statistics
N = int(input())
 
a = []
b = []
 
for i in range(N):
    m, n = map(int, input().split())
    a.append(m)
    b.append(n)
 
if N % 2 == 1:
    print(int(statistics.median(b)-statistics.median(a)+1))
else:
    print(int(statistics.median(b)*2-statistics.median(a)*2+1))

Submission status: Accepted

Solution code:
def e_count_median():
    N = int(input())
    Min, Max = [], []
    for _ in range(N):
        a, b = [int(i) for i in input().split()]
        Min.append(a)
        Max.append(b)
    Min.sort()
    Max.sort()

    mid = N // 2
    median_min, median_max = ((Min[mid], Max[mid]) if N % 2 == 1
                              else (Min[mid] + Min[mid - 1], Max[mid] + Max[mid - 1]))
    return median_max - median_min + 1

print(e_count_median())

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = []
b = []

for i in range(n):
    m, p = map(int, input().split())
    a.append(m)
    b.append(p)
a.sort()
b.sort()
middle = n // 2
if n % 2:
    print(b[middle] - a[middle] + 1)
else: 
    a_m = ( a[middle] + a[middle - 1] ) / 2
    b_m = ( b[middle] + b[middle - 1] ) / 2
    print(int((b_m - a_m) * 2 + 1))

Submission status: Accepted

Solution code:
N=int(input())
a=[]
b=[]
for _ in range(N):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
a=sorted(a)
b=sorted(b)
if N%2==1:
    print(b[N//2]-a[N//2]+1)
else:
    print(int(((b[N//2-1]+b[N//2])/2-(a[N//2-1]+a[N//2])/2)*2+1))

Submission status: Accepted

Solution code:
import statistics
f = statistics.median
N = int(input())

a = [0]*N
b = [0]*N

for i in range(N):
    m, n = map(int, input().split())
    a[i] = m
    b[i] = n

if N % 2 == 1:
    print(int(f(b)-f(a)+1))
else:
    print(int(f(b)*2-f(a)*2+1))

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    a_,b_ = map(int,input().split())
    A.append(a_)
    B.append(b_)
A.sort()
B.sort()
if N % 2 == 1:
    print(B[(N - 1) // 2] - A[(N - 1) // 2] + 1)
else:
    print(((B[N // 2] + B[N // 2 - 1]) - (A[N // 2] + A[N // 2 - 1])) + 1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A, B = ZIP(N)
A = sorted(A)
B = sorted(B)
if N%2 == 0:
	l = (A[N//2]+A[N//2-1])
	r = (B[N//2]+B[N//2-1])
	print(r-l+1)
else:
	print(B[N//2]-A[N//2]+1)

Submission status: Accepted

Solution code:
N = int(input())
ab = []
for i in range(N):
    ab.append(tuple(map(int,input().split())))
 
up = sorted(ab,key=lambda a:a[1],reverse=False)
down = sorted(ab,key=lambda a:a[0],reverse=False)
 
if N%2 == 1:
    print(up[N//2][1]-down[N//2][0]+1)
else:
    r = (up[N//2][1]+up[N//2-1][1])
    l = (down[N//2][0]+down[N//2-1][0])
    print(r-l+1)

Submission status: Accepted

Solution code:
#!python3

import numpy as np

LI = lambda: list(map(int, input().split()))

# input
N = int(input())
A, B = [None] * N, [None] * N
for i in range(N):
    A[i], B[i] = LI()


def main():
    a, b = np.array(A, dtype=int), np.array(B, dtype=int)
    a.sort()
    b.sort()
    i = N // 2
    if N % 2 == 0:
        a *= 2
        b *= 2
        l = (a[i - 1] + a[i]) // 2
        r = (b[i - 1] + b[i]) // 2
        ans = r - l + 1
    else:
        ans = b[i] - a[i] + 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=[0]*n
B=[0]*n
for i in range(n):
  a,b=map(int,input().split())
  A[i]=a
  B[i]=b
A.sort()
B.sort()
if n%2:
  c=(n+1)//2-1
  print(B[c]-A[c]+1)
else:
  c=n//2
  print(B[c]+B[c-1]-A[c]-A[c-1]+1)

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    N = int(input())
    A = [0] * N
    B = [0] * N
    for i in range(N):
        A[i],B[i] = map(int,input().split())

    As = sorted(A)
    Bs = sorted(B)

    if N % 2 == 1:
        Am = As[N//2]
        Bm = Bs[N//2]
    else:
        Am = As[N//2-1] + As[N//2]
        Bm = Bs[N//2-1] + Bs[N//2]

    res = Bm - Am + 1

    print(res)



main()

Submission status: Accepted

Solution code:
N = int(input())
ab = []
for i in range(N):
    ab.append(tuple(map(int,input().split())))
 
up = sorted(ab,key=lambda a:a[1],reverse=True)
down = sorted(ab,key=lambda a:a[0],reverse=True)
 
if N%2 == 1:
    print(up[N//2][1]-down[N//2][0]+1)
else:
    r = (up[N//2][1]+up[N//2-1][1])
    l = (down[N//2][0]+down[N//2-1][0])
    print(r-l+1)

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()

def main():
    """
    中央値の最小値から最大値まで、全ての値を取れるというだけのオチ。。。
    確かに中央値じゃなくて和の場合の問題やったことあるわ。。。
    """
    N = int(input())
    lower = []
    upper = []
    for i in range(N):
        a, b = map(int, input().split())
        heappush(lower, (a, i))
        heappush(upper, (-b, i))
    
    if N % 2 != 0:
        for _ in range(N // 2):
            a, i = heappop(lower)
        a, _ = heappop(lower)
        for _ in range(N // 2):
            b, i = heappop(upper)
        b, _ = heappop(upper)
        print(-b - a + 1)
    else:
        for _ in range(N // 2 - 1):
            a, i = heappop(lower)
        a0, i0 = heappop(lower)
        a1, i1 = heappop(lower)
        for _ in range(N // 2 - 1):
            b, i = heappop(upper)
        b0, i0 = heappop(upper)
        b1, i1 = heappop(upper)
        print((-b1 - b0) - (a1 + a0) + 1)


    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
ab = []
for i in range(N):
    ab.append(tuple(map(int,input().split())))
 
up = sorted(ab,key=lambda a:a[1],reverse=True)
down = sorted(ab,key=lambda a:a[0],reverse=True)
 
if N%2 == 1:
    print(up[N//2][1]-down[N//2][0]+1)
else:
    up2 = sorted(ab,key=lambda a:a[1],reverse=False)
    down2 = sorted(ab,key=lambda a:a[0],reverse=False)
    r = (up[N//2][1]+up2[N//2][1])
    l = (down[N//2][0]+down2[N//2][0])
    print(r-l+1)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
  a, b = map(int,input().split())
  A.append(a)
  B.append(b)
  
A.sort()
B.sort()
  
if N % 2:
  A_median = A[N // 2]
  B_median = B[N // 2]
  print(abs(B_median - A_median) + 1)
else:
  A_median = (A[N // 2 - 1] + A[N // 2]) / 2
  B_median = (B[N // 2 - 1] + B[N // 2]) / 2
  print(int(abs(B_median - A_median) * 2) + 1)

Submission status: Accepted

Solution code:
n = int(input())
min_s = []
max_s = []
for i in range(n):
    a, b = map(int, input().split())
    min_s.append(a)
    max_s.append(b)

min_s.sort()
max_s.sort()
if n % 2 == 0:
    min_med = (min_s[n // 2 - 1] + min_s[n // 2]) / 2
    max_med = (max_s[n // 2 - 1] + max_s[n // 2]) / 2
    print(int((max_med - min_med) * 2 + 1))

else:
    min_med = min_s[(n + 1) // 2 - 1]
    max_med = max_s[(n + 1) // 2 - 1]
    print(int((max_med - min_med)+1))

Submission status: Accepted

Solution code:
N = int(input())
ab = []
for i in range(N):
    ab.append(tuple(map(int,input().split())))
 
up = sorted(ab,key=lambda a:a[1])
down = sorted(ab,key=lambda a:a[0])
 
if N%2 == 1:
    print(up[N//2][1]-down[N//2][0]+1)
else:
    up2 = sorted(ab,key=lambda a:a[1],reverse=True)
    down2 = sorted(ab,key=lambda a:a[0],reverse=True)
    r = (up[N//2][1]+up2[N//2][1])
    l = (down[N//2][0]+down2[N//2][0])
    print(r-l+1)

Submission status: Accepted

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

A = [i[0] for i in AB]
B = [i[1] for i in AB]
from statistics import median

a_med = median(A)
b_med = median(B)

if N%2==1:
  print(b_med - a_med + 1)
  
else:
  print(int((b_med - a_med) * 2) + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
for i in range(n):
  c,d=map(int,input().split())
  a.append(c)
  b.append(d)
a=sorted(a)
b=sorted(b)
if n%2==0:
  x=(a[n//2-1]+a[n//2])
  y=(b[n//2-1]+b[n//2])
  print(y-x+1)
else:
  print(b[n//2]-a[n//2]+1)

Submission status: Accepted

Solution code:
import math

N = int(input())
A = []
B = []
for i in range(N):
    in1, in2 = list(map(int,input().split()))
    A.append(in1)
    B.append(in2)
AA = sorted(A)
BB = sorted(B)

if N%2==1:
    low = AA[N//2]
    high = BB[N//2]
    print(high - low +1)

else:
    low = AA[N//2-1] + AA[N//2]
    high = BB[N//2-1] + BB[N//2]
    #print(low,high)
    print(high - low +1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())
a.sort()
b.sort()
if n % 2 == 0:
    ans = 0
    ans += b[n // 2 - 1] + b[n // 2]
    ans -= a[n // 2 - 1] + a[n // 2]
    print(ans + 1)
else:
    print(b[n // 2] - a[n // 2] + 1)

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

a = [ab[i][0] for i in range(n)]
b = [ab[i][1] for i in range(n)]
ma, mb = median(a), median(b)

if n % 2 == 0:
    print(int(2 * mb - 2 * ma + 1))
if n % 2 == 1:
    print(int(mb - ma + 1))

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

a = [ab[i][0] for i in range(n)]
b = [ab[i][1] for i in range(n)]

if n % 2 == 0:
    ma, mb = median(a), median(b)
    print(int(2 * mb - 2 * ma + 1))
if n % 2 == 1:
    ma, mb = median(a), median(b)
    print(int(mb - ma + 1))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def main():
    N = I()
    A = []
    B = []
    ans = 0

    for i in range(N):
        a, b = MI()
        A.append(a)
        B.append(b)

    A.sort()
    B.sort()

    if N % 2 == 1:
        mid = N // 2
        print(B[mid] - A[mid] + 1)
    else:
        mid1 = N // 2
        mid2 = mid1 - 1
        r = B[mid1] + B[mid2]
        l = A[mid1] + A[mid2]
        print(r - l + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
A.sort()
B.sort()

i = N//2
if N%2 == 0:
    print(B[i]+B[i-1]-A[i]-A[i-1]+1)
else:
    print(B[i]-A[i]+1)

Submission status: Accepted

Solution code:
N = int(input())
A, B = [0]*N, [0]*N
for i in range(N):
    A[i], B[i] = map(int, input().split())
A.sort()
B.sort()
if N % 2 == 1:
    print(abs(A[N//2]-B[N//2])+1)
else:
    a_median = (A[N//2] + A[(N//2)-1])/2
    b_median = (B[N//2] + B[(N//2)-1])/2
    print(int(abs(a_median-b_median)*2+1))

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)

A.sort()
B.sort()

if N%2 == 1:
    print(B[N//2]-A[N//2]+1)
else:
    r = (B[N//2-1]+B[N//2])
    l = (A[N//2-1]+A[N//2])
    print(r-l+1)

Submission status: Accepted

Solution code:
n = int(input())
A = []
B = []
for _ in range(n):
    a, b = tuple(map(int, input().split()))
    A.append(a)
    B.append(b)

A = sorted(A)
B = sorted(B)

if n%2==1:
    sa = A[n//2]
    lb = B[n//2]
    print(lb - sa + 1)
else:
    sal = A[n//2-1]
    sar = A[n//2]

    lbl = B[n//2-1]
    lbr = B[n//2]
    print((lbl + lbr) - (sal + sar) +1)

Submission status: Accepted

Solution code:
import numpy as np
def LI():
    return list(map(int, input().split()))

n = int(input())
ab = [LI() for _ in range(n)]
ab = np.array(ab)

def main():
    if n % 2 == 0:
        mi = np.median(ab[:, 0])
        ma = np.median(ab[:, 1]) + 0.5
        ans = (ma - mi) // 0.5
    else:
        mi = np.median(ab[:, 0])
        ma = np.median(ab[:, 1]) + 1
        ans = ma - mi
    print(int(ans))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int( input() )
A = []
B = []

for i in range( n ):
  a, b = map( int, input().split() )
  A.append( a )
  B.append( b )

A = sorted( A )
B = sorted( B )

if n % 2 == 0:
  i = n // 2
  print(( B[ i ] + B[ i - 1 ] ) - ( A[ i ] + A [ i - 1 ] ) + 1 )
else:
  i = ( n + 1 ) // 2 - 1
  print( B[ i ] - A[ i ] + 1 )

Submission status: Accepted

Solution code:
N = int(input())
left = []
right = []
for _ in range(N):
    A, B = list(map(int, input().split()))
    left.append(A)
    right.append(B)
left.sort()
right.sort(reverse=True)
if N%2 == 0:
    n = (N+1)//2-1
    A_med2 = (left[n]+left[n+1])
    B_med2 = (right[n]+right[n+1])
    print(B_med2-A_med2+1)
else:
    n = (N+1)//2-1
    print(right[n]-left[n]+1)

Submission status: Accepted

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
A = sorted([ab[0]*(2-N%2) for ab in AB])
B = sorted([ab[1]*(2-N%2) for ab in AB])
if N%2 == 1:
  print(B[N//2]-A[N//2]+1)
else:
  print((B[N//2]+B[N//2-1])//2-(A[N//2]+A[N//2-1])//2+1)

Submission status: Accepted

Solution code:
from statistics import median

N=int(input())
A=[]
B=[]
for _ in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
median_a=median(A)
median_b=median(B)
if N%2==0:
    print(int((median_b-median_a)*2)+1)
else:
    print(int(median_b-median_a)+1)

Submission status: Accepted

Solution code:
N=int(input())
A=[0 for _ in range(N)]
B=[0 for _ in range(N)]
for i in range(N):
  a,b=[int(s) for s in input().split()]
  A[i]=a
  B[i]=b
A.sort()
B.sort()
def doublemed(ls,num):
  if num%2==0:
    return(ls[num//2-1]+ls[num//2])
  else:
    return(ls[num//2])
meda=doublemed(A,N)
medb=doublemed(B,N)
print(medb-meda+1)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

A.sort()
B.sort()

if N % 2 == 1:
    ans = B[N//2] - A[N//2]+1
    print(ans)
else:
    vmin = A[N//2]+A[N//2-1]
    vmax = B[N//2]+B[N//2-1]
    ans = vmax-vmin+1
    print(ans)

Submission status: Accepted

Solution code:
import statistics

N = int(input())
AList = []
BList = []
for i in range(N):
    A, B = map(int, input().split())
    AList.append(A)
    BList.append(B)
    
Amid = statistics.median(AList)
Bmid = statistics.median(BList)

if N % 2 != 0:
    result = int(Bmid - Amid + 1)
else:
    result = int((Bmid - Amid) * 2 + 1)

print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    a, b = map(int,input().split())
    A.append(a)
    B.append(b)

A.sort()
B.sort()
#print(A)
#print(B)
if N % 2 == 1:
    AA = A[(N+1)//2-1]
    BB = B[(N+1)//2-1] 
    print(BB-AA+1)
else:
    AA = (A[N//2-1]+A[N//2])/2
    BB = (B[N//2-1]+B[N//2])/2
    print(int((BB-AA)*2+1))

Submission status: Accepted

Solution code:
n = int(input())
a = [0 for _ in range(n)]
b = a[:]
for i in range(n):
    a[i],b[i] = map(int,input().split())
a.sort()
b.sort()

ans = 0
if n % 2 == 0:
    l = a[n//2-1] + a[n//2]
    h = b[n//2-1] + b[n//2]

    ans += h - l + 1
else:
    l = a[n//2]
    h = b[n//2]

    ans += h - l + 1



print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    A = [0] * N
    B = [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    A.sort()
    B.sort()

    if N % 2 == 0:
        ma = A[N//2 - 1] + A[N//2]
        mb = B[N//2 - 1] + B[N//2]
        print(mb - ma + 1)
    else:
        ma = A[(N+1)//2 - 1]
        mb = B[(N+1)//2 - 1]
        print(mb - ma + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

aList = []
bList = []
for i in range(n):
    a, b = map(int, input().split())
    aList.append(a)
    bList.append(b)

aList.sort()
bList.sort()


if not n % 2 == 0:
    half = (n+1) // 2 - 1
    print(bList[half] - aList[half] + 1)
else:
    half = (n) // 2 - 1
    print(bList[half] + bList[half+1] - (aList[half] + aList[half+1]) + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
B = [0] * N

for i in range(N):
    A[i], B[i] = map(int, input().split())

A.sort()
B.sort()

if N % 2 == 1:
    mid_a = A[(N + 1) // 2 - 1]
    mid_b = B[(N + 1) // 2 - 1]
    print(mid_b - mid_a + 1)
else:
    mid_a = (A[N // 2 - 1] + A[N // 2]) / 2
    mid_b = (B[N // 2 - 1] + B[N // 2]) / 2
    print(int((mid_b - mid_a) * 2) + 1)

Submission status: Accepted

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    A = [0] * n
    B = [0] * n
    for i in range(n):
        A[i], B[i] = map(int, input().split())

    A.sort()
    B.sort()
    k = n // 2

    if n & 1:
        print(B[k] - A[k] + 1)
    else:
        print((B[k] + B[k - 1]) - (A[k] + A[k - 1]) + 1)
resolve()

Submission status: Accepted

Solution code:
import math

N = int(input())
A = [0] * N
B = [0] * N

for i in range(N):
    A[i], B[i] = map(int, input().split())

A.sort()
B.sort()

mid_a = A[(N + 1) // 2 - 1] if N % 2 == 1 else (A[N // 2 - 1] + A[N // 2]) / 2
mid_b = B[(N + 1) // 2 - 1] if N % 2 == 1 else (B[N // 2 - 1] + B[N // 2]) / 2

# ans = 0
# if isinstance(mid_a, float):
#     if not mid_a.is_integer():
#         ans += 1
#         mid_a = math.ceil(mid_a)
# if isinstance(mid_b, float):
#     if not mid_b.is_integer():
#         ans += 1
#         mid_b = math.floor(mid_b)

ans = (mid_b - mid_a + 1) if N % 2 == 1 else int((mid_b - mid_a) * 2) + 1
print(ans)

Submission status: Accepted

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

AB.sort()
if N % 2 == 0:
  l1 = AB[N//2][0] 
  l2 = AB[N//2 - 1][0]
else:
  l = AB[N//2][0]
AB.sort(key = lambda x: x[1])
if N % 2 == 0:
  r1 = AB[N//2][1] 
  r2 = AB[N//2 - 1][1]
else:
  r = AB[N//2][1]
  
if N % 2 == 0:
  ans = (r1 + r2) - (l1 + l2)
  ans += 1
  print(ans)
else:
  print(r - l + 1)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
B=[]
for i in range(N):
  a,b=map(int,input().split())
  A+=[a]
  B+=[b]
A.sort()
B.sort()
if N%2==1:
  ans=B[N//2]-A[N//2]+1
else:
  Am=A[N//2]+A[N//2-1]
  Bm=B[N//2]+B[N//2-1]
  ans=(Bm-Am)+1
print(ans)

Submission status: Accepted

Solution code:
from statistics import median
N=int(input())
A,B=[],[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a);B.append(b)
mA=median(A);mB=median(B)
if N%2:
    ans=int(mB-mA)+1
else:
    if mA//1==mA and mB//1==mB:
        ans=int(mB-mA)*2+1
    elif mA//1!=mA and mB//1==mB:
        ans=(mB-(mA//1))*2
    elif mA//1==mA and mB//1!=mB:
        ans=((mB//1+1)-mA)*2
    else:
        ans=int(mB-mA)*2+1
print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
for i in range(n):
    c,d=map(int,input().split())
    a.append(c)
    b.append(d)
a.sort()
b.sort()

if n%2==1:
    ans=b[n//2]-a[n//2]+1
else:
    ans=(b[(n-1)//2]+b[n//2])-(a[(n-1)//2]+a[n//2])+1
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[]
r=[]
ans=0
for i in range(n):
    a,b=map(int,input().split())
    l.append(a)
    r.append(b)
l.sort()
r.sort()
if n%2==0:
    ans=int(((r[n//2]+r[n//2-1])/2-(l[n//2]+l[n//2-1])/2)*2+1)
else:
    ans=r[n//2]-l[n//2]+1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from decimal import Decimal
N = int(stdin.readline().rstrip())
A = []
B = []
for i in range(N):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    A.append(a)
    B.append(b)
    
A.sort()
B.sort()

if N % 2 == 0:
    n = N // 2
    med1 = A[n-1] + A[n]
    med2 = B[n-1] + B[n]
    print(med2-med1+1)
else:
    ans = B[N//2] - A[N//2] + 1
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

nums = [[0, 0] for _ in range(n)]

for i in range(n):
  nums[i][0], nums[i][1] = map(int, input().split())
  
if n % 2 == 0:
  # even
  nums.sort(key=lambda x: x[0])
  a = (nums[(n+1)//2 - 1][0] + nums[(n+1)//2][0]) / 2
  nums.sort(key=lambda x: x[1])
  b = (nums[(n+1)//2 - 1][1] + nums[(n+1)//2][1]) / 2
  print(int((b-a) * 2 + 1))
  
else:
  # odd
  nums.sort(key=lambda x: x[0])
  a = nums[(n+1)//2 - 1][0]
  nums.sort(key=lambda x: x[1])
  b = nums[(n+1)//2 - 1][1]
  print(b-a + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=[];b=[]
for _ in range(n):
    ai,bi=map(int, input().split())
    a.append(ai);b.append(bi)
a.sort()
b.sort()
ma=0;mb=0
if n%2:
    ma=a[n//2]
    mb=b[n//2]
    
else:
    ma=a[n//2]+a[n//2-1]
    mb=b[n//2]+b[n//2-1]
print(mb-ma+1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, rl().split())
    
    A.sort()
    B.sort()
    
    if N % 2 == 0:
        med_a = (A[N // 2 - 1] + A[N // 2]) / 2
        med_b = (B[N // 2 - 1] + B[N // 2]) / 2
        ans = int((med_b - med_a) // 0.5 + 1)
    else:
        med_a = A[N // 2]
        med_b = B[N // 2]
        ans = med_b - med_a + 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    a, b = [], []
    for _ in range(N):
        A, B = map(int, input().split())
        a.append(A)
        b.append(B)

    a.sort()
    b.sort()

    if N % 2 == 0:
        u = a[N//2-1] + a[N//2]
        v = b[N//2-1] + b[N//2]
        return max((v-u)+1, 0)
    else:
        u = a[(N)//2]
        v = b[(N)//2]
        return max(v-u+1, 0)

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

import numpy as np

n = int(readline())
ab = np.array(read().split(), np.int64)
a = ab[::2]
b = ab[1::2]

a.sort()
b.sort()

if n % 2:
    idx = (n + 1) // 2 - 1
    mn = a[idx]
    mx = b[idx]
    ans = mx - mn + 1

else:
    i1, i2 = n // 2 - 1, n // 2
    a *= 2
    b *= 2
    mn = (a[i1] + a[i2]) // 2
    mx = (b[i1] + b[i2]) // 2
    ans = mx - mn + 1

print(ans)