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:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

n = int(input())

ls = []; rs = []
for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    ls.append(a)
    rs.append(b)

ls.sort()
rs.sort()

ans = 0

if n%2 == 1:
    l = ls[n//2]
    r = rs[n//2]
    ans = r-l+1
else:
    l2 = ls[n//2-1]+ls[n//2]
    r2 = rs[n//2-1]+rs[n//2]
    ans = r2-l2+1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10
    U = list()
    L = list()
    N = II()
    for i in range(N):
        a,b = LMIIS()
        U.append(b)
        L.append(a)
    U.sort()
    L.sort()
    if N % 2 == 1:
        upper = U[(1+N)//2-1]
        lower = L[(1+N)//2-1]
        print(int(upper-lower)+1)
    else:
        upper = (U[N//2-1]+U[N//2])
        lower = (L[N//2-1]+L[N//2])
        print(upper-lower+1)
    
    
        




    
main()

Submission status: Accepted

Solution code:
N = int(input())
left = []
right = []
for _ in range(N):
    l, r = map(int, input().split())
    left.append(l)
    right.append(r)
left.sort()
right.sort()
if N%2==0:
    for i, (l, r) in enumerate(zip(left, right), 1):
        if i==N//2:
            L = l
            R = r
        if i==N//2+1:
            R +=r
            L += l
            print((R-L)+1)
else:
    for i, (l, r) in enumerate(zip(left, right), 1):
        if i==N//2+1:
            print(r-l+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.sort()
B.sort()

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

Submission status: Accepted

Solution code:
# E - Count Median

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:
    A_median = A[(N+1)//2-1]
    B_median = B[(N+1)//2-1]
    print(B_median - A_median + 1)
else:
    A_median = (A[N//2-1] + A[N//2])
    B_median = (B[N//2-1] + B[N//2])
    print(B_median - A_median + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import statistics
import math

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

b = [[],[]]
for i in range(n):
    b[0].append(a[i][0])
    b[1].append(a[i][1])

x = statistics.median(b[0])
y = statistics.median(b[1])

if n % 2 != 0:
    ans = y - x + 1

else:
    ans = round((y - x) * 2) + 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ab = [list(map(int, input().split())) for _ in range(n)]
ab_a = sorted(ab, key=lambda x:int(x[0]))
ab_b = sorted(ab, key=lambda x:int(x[1]))

if n%2 == 1:
    print(ab_b[n//2][1]-ab_a[n//2][0]+1)
else:
    print(int(((ab_b[n//2][1]+ab_b[n//2-1][1])/2.0-(ab_a[n//2][0]+ab_a[n//2-1][0])/2.0)*2+1))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr = np.array([[int(x) for x in input().split()] for _ in range(n)])
med_a = np.median(arr[:, 0])
med_b = np.median(arr[:, 1])

if n % 2 == 0:
    print(int(med_b * 2 - med_a * 2 + 1))
else:
    print(int(med_b - med_a + 1))

Submission status: Accepted

Solution code:
from statistics import median

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

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(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(reverse=True)
ans=0
if n%2==1:
    s=a[n//2]
    g=b[n//2]
    ans=g-s+1
else:
    s1,s2=a[n//2-1],a[n//2]
    g2,g1=b[n//2-1],b[n//2]
    ans=(g2+g1)-(s2+s1)+1
print(ans)

Submission status: Accepted

Solution code:
from statistics import median

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

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(a)) * 2) + 1)

Submission status: Accepted

Solution code:
import statistics
import math

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

if N%2 == 1:
    ans = statistics.median(b) - statistics.median(a) + 1
else:
    ans = (statistics.median(b) - statistics.median(a))*2 + 1
print(int(ans))

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
a = [None] * n
b = [None] * n
for i in range(n):
    A, B = map(int, input().split())
    a[i], b[i] = A, B

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(a)) * 2) + 1)

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)

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(a)) * 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 = [i for i, j in ab]
b = [j for i, j in ab]

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(a)) * 2) + 1)

Submission status: Accepted

Solution code:
N = int(input())
A_list = []
B_list = []
for i in range(N):
    A, B = map(int, input().split())
    A_list.append(A)
    B_list.append(B)
sort_A = sorted(A_list)
sort_B = sorted(B_list)

if N % 2 == 0:
    center_A = (sort_A[N//2] + sort_A[N//2-1])/2
    center_B = (sort_B[N//2] + sort_B[N//2-1])/2
    print(int((center_B-center_A)*2)+1)
else:
    center_A = sort_A[N//2]
    center_B = sort_B[N//2]
    print(center_B-center_A+1)

Submission status: Accepted

Solution code:
from statistics import median


def main():
    N = int(input())
    A = [None] * N
    B = [None] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    min_median = median(A)
    max_median = median(B)
    if N % 2 == 1:
        print(max_median - min_median + 1)
    else:
        print(round((max_median-min_median)*2)+1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


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()
# 奇数の時は1ごと、偶数の時は1/2ずつ値を取るので場合分け
if n%2 == 1:
    print(B[n//2] - A[n//2] + 1)
    # 奇数の時、中央値は前からn//2番目(0-index)
else:
    print((B[n//2]+B[n//2-1])-(A[n//2]+A[n//2-1])+1)
    # 偶数の時、中央値は前からn//2-1番目とn//2番目の平均(0-index)

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
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 ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

N = INT()
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = MAP()

A.sort()
B.sort()
if N % 2 == 0:
    mn = (A[N//2-1]+A[N//2]) / 2
    mx = (B[N//2-1]+B[N//2]) / 2
    ans = int((mx-mn)*2) + 1
else:
    mn = A[N//2]
    mx = B[N//2]
    ans = (mx-mn) + 1
print(ans)

Submission status: Accepted

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

# min
if n%2==1:
    med1 = ab[n//2]
    amin=med1[0]
else:
    med1 = ab[(n-1)//2]
    med2 = ab[(n-1)//2+1]
    amin = (med1[0] + med2[0])

ab.sort(key=lambda x: x[1])

# max
if n%2==1:
    med1 = ab[n//2]
    amax=med1[1]
else:
    med1 = ab[(n-1)//2]
    med2 = ab[(n-1)//2+1]
    amax = (med1[1] + med2[1])

if n%2==1:
    print(amax-amin+1)
else:
    print(amax - amin + 1)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
minnums = [[] for _ in range(n)]
maxnums = [[] for _ in range(n)]

for i in range(n):
  minnums[i], maxnums[i] = map(int, input().split())
  
minnums.sort()
maxnums.sort()

if n % 2:
  minmedian = minnums[n // 2]
  maxmedian = maxnums[n // 2]
  print(maxmedian - minmedian + 1)
else:
  minmedian = (minnums[n // 2 - 1] + minnums[n // 2]) / 2
  maxmedian = (maxnums[n // 2 - 1] + maxnums[n // 2]) / 2
  print(int((maxmedian - minmedian) * 2 + 1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

if N % 2 == 1:
    i = N // 2
    AB.sort()
    L = AB[i][0]
    AB.sort(key=lambda x: x[1])
    R = AB[i][1]
    ans = R - L + 1
else:
    i = N // 2 - 1
    j = i + 1
    AB.sort()
    L = AB[i][0] + AB[j][0]
    AB.sort(key=lambda x: x[1])
    R = AB[i][1] + AB[j][1]
    ans = R - L + 1

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import median

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input().split())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input().split()) for _ in range(n) ]

n = I()
ab = LI(n)

a = mycol(ab,0)
ma = median(a)

b = mycol(ab,1)
mb = median(b)

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

Submission status: Accepted

Solution code:
def solve():
    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(reverse=True)

    if N % 2 == 0:
        tmp = (B[N//2]+B[N//2-1])/2 - (A[N//2]+A[N//2-1])/2
        print(int(tmp / 0.5)+1)
    else:
        print(B[N//2] - A[N//2] + 1)
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:

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

a.sort()
b.sort()
    
if n%2==0:
    n2=n//2
    ca=(a[n2-1]+a[n2])
    cb=(b[n2-1]+b[n2])
    c=cb-ca+1
else:
    n2=(n-1)//2
    ca=a[n2]
    cb=b[n2]
    c=cb-ca+1
    
print(c)

Submission status: Accepted

Solution code:
n=int(input())
m=[];M=[]
for i in range(n):
    a,s=map(int,input().split())
    m.append(a);M.append(s)
m.sort();M.sort()
if n%2:
    print(M[n//2]-m[n//2]+1)
else:
    print(M[n//2]+M[n//2-1]-m[n//2]-m[n//2-1]+1)

Submission status: Accepted

Solution code:
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
a, b = map(list, zip(*ab))
a.sort()
b.sort(reverse=True)
a1, b1 = a[n//2], b[n//2]
a2, b2 = a[n//2-1], b[n//2-1]
if n % 2: print(b1 - a1 + 1)
else: print((b1+b2) - (a1+a2) + 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:
    M = B[N // 2]
    m = A[N // 2]
    print(M - m + 1)

else:
    M = (B[N // 2 - 1] + B[N // 2]) / 2
    m = (A[N // 2 - 1] + A[N // 2]) / 2
    print(int((M - m + 0.5) * 2))

Submission status: Accepted

Solution code:
N = int(input())

A, B = [], []

a, b = None, None

sum = [0 for i in range(N)]

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:
    print(B[N//2-1]+B[N//2] - A[N//2-1]-A[N//2]+1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    ab = [LI() for _ in range(n)]
    aa = sorted(map(lambda x: x[0], ab))
    ba = sorted(map(lambda x: x[1], ab))

    if n % 2 == 1:
        a = aa[n//2]
        b = ba[n//2]
        return b - a + 1
    a1 = aa[n//2-1]
    a2 = aa[n//2]
    b1 = ba[n//2-1]
    b2 = ba[n//2]

    r = b2 - a2
    r += b1 - a1

    return r + 1

print(main())

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&1:
    print(b[n//2]-a[n//2]+1)
else:
    print(1+(b[n//2]+b[(n-1)//2])-(a[n//2]+a[(n-1)//2]))

Submission status: Accepted

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

x = np.median(A)
y = np.median(B)
if n%2==0:
    print(int((y-x)/0.5+1))
else:
    print(int(y-x+1))

Submission status: Accepted

Solution code:
import statistics
import math

n = int(input())
aa,bb = [],[]

for i in range(n):
    a,b = map(int,input().split())
    
    aa.append(a)
    bb.append(b)
    
aa.sort()
bb.sort()
    
if n % 2 == 1:
    am = aa[n//2]
    bm = bb[n//2]
    
    ans = bm - am + 1
    
else:
    am = (aa[n//2] + aa[n//2-1]) / 2
    bm = (bb[n//2] + bb[n//2-1]) / 2
    
    ans = (bm - am)*2 + 1
    
print(int(ans))

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()

N = int(input())
arr = [tuple(map(int, input().split())) for _ in range(N)]

l = list(map(lambda x: x[0], arr))
h = list(map(lambda x: x[1], arr))
l.sort()
h.sort()

ml = l[N // 2] if N & 1 else (l[N // 2 - 1] + l[N // 2])
mh = h[N // 2] if N & 1 else (h[N // 2 - 1] + h[N // 2])

print(mh - ml + 1)

Submission status: Accepted

Solution code:
n = int(input())
ab = [map(int, input().split()) for _ in range(n)]
a, b = [list(i) for i in zip(*ab)]
c = sorted(a)
d = sorted(b)

ans = 0
if n % 2 == 1:
    ans = d[(n-1)//2] - c[(n-1)//2] + 1
else:
    ans = d[n//2] + d[n//2 - 1] - c[n//2] - c[n//2 - 1] + 1

print(ans)

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 )

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

Submission status: Accepted

Solution code:
from statistics import median
N = int(input())
A, B = zip(*tuple(tuple(map(int, input().split())) for _ in range(N)))
print(int((median(B) - median(A)) * (1 + (not N & 1))) + 1)

Submission status: Accepted

Solution code:
n=int(input())
arr1=[]
arr2=[]
for _ in range(n):
  a,b=map(int,input().split())
  arr1.append(a)
  arr2.append(b)
arr1=sorted(arr1)
arr2=sorted(arr2)
if n%2==1:
  l=arr1[n//2]
  r=arr2[n//2]
else:
  l=arr1[n//2-1]+arr1[n//2]
  r=arr2[n//2-1]+arr2[n//2]
print(r-l+1)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
A, B = [], []  # AにはXがとりうる値の最小値、Bには最大値を入れていく

for _ in range(n):
    a, b = map(int, input().split())
    A.append(a if n % 2 == 1 else a * 2)  # nが偶数だと、medianがとりうる値が0.5刻みになるので最初から2倍にしておく
    B.append(b if n % 2 == 1 else b * 2)

a = np.median(A)
b = np.median(B)

# medianのとりうる値の最小値と最大値の間に、カウントしたいmedianが１刻みで存在する
print(int(max(a, b) - min(a, b) + 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 == 0:
    print(B[n//2] + B[n//2-1] - A[n//2] - A[n//2-1] + 1)
else:
    print(B[(n-1)//2] - A[(n-1)//2] + 1)

Submission status: Accepted

Solution code:
n = int(input())

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


if n % 2 == 0:
    left = a_line[n // 2 - 1] + a_line[n // 2]
    right = b_line[n // 2 - 1] + b_line[n // 2]
    print(right - left + 1)
else:
    print(b_line[n // 2] - a_line[n // 2] + 1)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

a = sorted(a)
b = sorted(b)

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

Submission status: Accepted

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

a = [_ab[0] for _ab in ab]
b = [_ab[1] for _ab in ab]
a.sort()
b.sort()

if n % 2 == 1:
    m = a[n // 2]
    M = b[n // 2]
    print(M - m + 1)
else:
    m = (a[n // 2 - 1] + a[n // 2]) / 2
    M = (b[n // 2 - 1] + b[n // 2]) / 2
    print(int((M - m) * 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.sort()
B.sort()

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

Submission status: Accepted

Solution code:
def calc_median(X):
    #中央値計算
    Xl = len(X)
    if Xl % 2:
        ret = X[(Xl + 1) // 2 - 1]
    else:
        ret = (X[Xl // 2 - 1] + X[Xl // 2]) / 2

    return ret


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

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

AM = calc_median(A)
BM = calc_median(B)

if N % 2:
    ans = BM - AM + 1
else:
    ans = int((BM - AM) * 2 + 1)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
min_val, max_val = zip(*[map(int, input().split()) for _ in range(n)])
min_val = sorted(min_val)
max_val = sorted(max_val)

if n % 2:
    mid = n//2
    min_median = min_val[mid]
    max_median = max_val[mid]
    print(max_median-min_median+1)
else:
    midl = (n-1)//2
    midr = midl+1
    min_median = min_val[midl]+min_val[midr]
    max_median = max_val[midl]+max_val[midr]
    print(max_median-min_median+1)

Submission status: Accepted

Solution code:
def calc_median(X):
    #中央値計算
    Xl = len(X)
    if Xl % 2:
        ret = X[(Xl + 1) // 2 - 1]
    else:
        ret = (X[Xl // 2 - 1] + X[Xl // 2]) / 2

    return ret


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

A.sort()
B.sort()

AM = calc_median(A)
BM = calc_median(B)

if N % 2:
    ans = BM - AM + 1
else:
    ans = int((BM - AM) * 2 + 1)

print(ans)

Submission status: Accepted

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

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

A.sort()
B.sort()
if N % 2 == 1:
    print(B[N//2]-A[N//2]+1)
else:
    Am = (A[N//2]+A[N//2-1])/2
    Bm = (B[N//2]+B[N//2-1])/2
    print(int((Bm-Am)*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)
x = median(A)
y = median(B)

if n % 2 == 0:
    print(int((y - x) * 2 + 1))
else:
    print(y - x + 1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    rg = []

    for i in range(N):
        a,b = map(int,input().split())
        rg.append((a,b))

    def disasm(i):
        rg.sort()
        a,_ = rg[i]
        rg.sort(key=lambda x: x[1])
        _,b = rg[i]
        return a,b


    if N % 2 == 1:
        a,b = disasm(N//2)
        print(b-a+1)
        exit()

    a,b = disasm(N//2-1)
    c,d = disasm(N//2)

    print(b+d-a-c+1)


if __name__ == "__main__":
  main()

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)
m_a = median(A)
m_b = median(B)

if n%2 == 0:
    ans = (m_b - m_a)*2 + 1
else:
    ans = m_b - m_a + 1
print(int(ans))

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    x=[list(map(int,readline().split())) for _ in range(n)]
    
    a=[x[i][0] for i in range(n)]
    b=[x[i][1] for i in range(n)]
    a.sort()
    b.sort()
    if n%2==1:
        a_med=a[n//2]
        b_med=b[n//2]
    else:
        a_med=a[n//2-1]+a[n//2]
        b_med=b[n//2-1]+b[n//2]
    print(b_med-a_med+1)

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  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 == 1:
    print(B[(N-1)//2]-A[(N-1)//2]+1)
  else:
    med_A = (A[N//2-1] + A[N//2])/2
    med_B = (B[N//2-1] + B[N//2])/2
    print(int((med_B-med_A)*2 + 1))
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.sort()
B.sort()
if n % 2 == 1:
    print(B[(n - 1) // 2] - A[(n - 1) // 2] + 1)
else:
    print((B[(n - 1) // 2] + B[n // 2]) - (A[(n - 1) // 2] + A[n // 2]) + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math,os
from io import BytesIO, IOBase
#from bisect import bisect_left as bl, bisect_right as br, insort
#from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(Decimal, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
from decimal import Decimal


def main():

    n=int(data())
    l=[]
    l1=[]
    for i in range(n):
        a,b=mdata()
        l.append(a)
        l1.append(b)
    l.sort()
    l1.sort()
    if n%2!=0:
        a=l[n//2]
        b=l1[n//2]
        cnt=b-a+1
        out(cnt)
    else:
        a1=l[n//2-1]
        b1 = l1[n // 2-1]
        a2=l[n//2]
        b2 = l1[n // 2]
        out(int(((b1+b2)/2-(a1+a2)/2)*2+1))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)


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:
    min_x = (A[N//2] + A[(N//2)-1]) / 2
    max_x = (B[N//2] + B[(N//2)-1]) / 2
    # print(min_x, max_x)
    ans = (max_x - min_x)*2 + 1
else:
    min_x = A[N//2]
    max_x = B[N//2]
    # print(min_x, max_x)
    ans = max_x - min_x + 1

print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
a,b = [],[]
for i in range(n):
    x,y = map(int,input().split())
    a.append(x)
    b.append(y)
a.sort()
b.sort()
if n % 2 == 1:
    print(b[n//2]-a[n//2]+1)
else:
    M = (b[n//2]+b[n//2-1])/2
    m = (a[n//2]+a[n//2-1])/2
    print(int((M-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.sort()
B.sort()
if N%2==1:
    print(B[(N+1)//2-1]-A[(N+1)//2-1]+1)
else:
    print(B[N//2-1]+B[N//2]-A[N//2-1]-A[N//2]+1)

Submission status: Accepted

Solution code:
n = int(input())

mins = [0] * n
maxs = [0] * n
for i in range(n):
    a, b = map(int, input().split())
    mins[i] = a
    maxs[i] = b

mins = sorted(mins)
maxs = sorted(maxs)

if n % 2 != 0:
    print(maxs[((n+1)//2) - 1] - mins[((n+1)//2) - 1] + 1)
else:
    print(int(2 * ((maxs[((n+1)//2) - 1] + maxs[n//2])/2 - (mins[((n+1)//2) - 1] + mins[n//2])/2) + 1))

Submission status: Accepted

Solution code:
n = int(input())

a = []
b = []
for i in range(n):
  ab = input().split()
  a.append(int(ab[0]))
  b.append(int(ab[1]))
import statistics
a = statistics.median(a)
b = statistics.median(b)
if n % 2 != 0:
  print(int(b-a+1))
else:
  print(int(2*(b-a)+1))

Submission status: Accepted

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

def main():
    N = int(input())
    A = [None]*N
    B = [None]*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==0:
        x=A[N//2-1]+A[N//2]
        y=B[N//2-1]+B[N//2]
        print(y-x+1)
    else:
        x=A[N//2]
        y=B[N//2]
        print(y-x+1)

if __name__ == '__main__':
    main()