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 numpy as np
i4 = np.int32

nin = np.fromstring(open(0).read(), i4, sep=' ')
N = nin[0]
ab = nin[1:].reshape(-1, 2).T
a = np.sort(ab[0])
b = np.sort(ab[1])
i = N // 2
if N % 2:
    print(b[i] - a[i] + 1)
else:
    nmin = a[i - 1] + a[i]
    nmax = b[i - 1] + b[i]
    print(nmax - nmin + 1)

Submission status: Accepted

Solution code:
n=int(input())
ab=[list(map(int, input().split())) for _ in range(n)]
a=[]
b=[]
for a_i, b_i in ab:
    a.append(a_i)
    b.append(b_i)
a.sort()
b.sort()

import statistics
a_med=statistics.median(a)
b_med=statistics.median(b)

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

Submission status: Accepted

Solution code:
import statistics
n = int(input())

a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
ans = 0

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

ma = statistics.median(a)
mb = statistics.median(b)

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

print(int(ans))

Submission status: Accepted

Solution code:
import statistics
import math

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

A_m = statistics.median(A)
B_m = statistics.median(B)
if n%2 == 1:
    num = B_m - A_m +1
elif n %2 ==0:
    num = 2*B_m - 2*A_m +1
print(int(num))

Submission status: Accepted

Solution code:
n=int(input())
i=0
x=[]
y=[]
def linked_merge_sort(lst):
    if len(lst) == 1:
        return
    
    #
    # 1. 分割
    #
    q, r = divmod(len(lst), 2)
    left = [lst.pop() for _ in range(q)]
    right = [lst.pop() for _ in range(q + r)]
    
    #
    # 2. ソート
    #
    linked_merge_sort(left)
    linked_merge_sort(right)
    
    #
    # 3. 統合
    #
    left.reverse()  # 補足
    right.reverse()
    while left and right:
        if left[-1] <= right[-1]:
            lst.append(left.pop())
        else:
            lst.append(right.pop())
    while left:
        lst.append(left.pop())
    while right:
        lst.append(right.pop())
while i<n:
    a=list(map(int,input().split()))
    x.append(a[0])
    y.append(a[1])
    i+=1
linked_merge_sort(x)
linked_merge_sort(y)
if n%2==0:
    p=(x[n//2]+x[(n//2)-1])/2
    q=(y[n//2]+y[(n//2)-1])/2
    ans=(q-p)*2+1
else:
    p=x[(n-1)//2]
    q=y[(n-1)//2]
    ans=q-p+1
print(int(ans))

Submission status: Accepted

Solution code:
import sys 
import statistics
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = []
B = []
for _ in range(N):
  a,b = map(int,readline().split())
  A.append(a)
  B.append(b)
median_A = statistics.median(A)
median_B = statistics.median(B)
if N % 2 == 1:
  print(median_B-median_A+1)
else:
  print(int((median_B-median_A)*2)+1)

Submission status: Accepted

Solution code:
from numpy import*
(n,),*d=[[*map(int,i.split())]for i in open(0)]
a,b=median(d,0)
print(int((b-a)*(2-n%2))+1)

Submission status: Accepted

Solution code:
from numpy import*
(n,),*d=[[*map(int,i.split())]for i in open(0)]
a,b=median(d,axis=0)
print(int((b-a)*(2-n%2))+1)

Submission status: Accepted

Solution code:
def main():

    import statistics

    n = int(input())
    a = [0] * n
    b = [0] * n

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

    meda = statistics.median(a)
    medb = statistics.median(b)

    if n % 2:
        print(int(medb - meda + 1))
    else:
        print(int((medb - meda) * 2 + 1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from numpy import*
(n,),*d=[[*map(int,i.split())]for i in open(0)]
a,b=median(d,axis=0)
c=[2,1][n%2]
print(int(b*c-a*c+1))

Submission status: Accepted

Solution code:
import numpy as np
(n,), *d = [[*map(int, i.split())] for i in open(0)]
a, b = np.median(d, axis=0)
if n%2:print(int(b - a + 1))
else:print(int(b*2 - a*2 + 1))

Submission status: Accepted

Solution code:
n = int(input().strip())
lower=[0]*n
upper=[0]*n
for i in range(n):
    A, B = map(int, input().strip().split())
    lower[i]=A
    upper[i]=B
lower.sort()
upper.sort()

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

Submission status: Accepted

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

a, b = map(list, zip(*ab))
a.sort()
b.sort()

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

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

ans = mx - mn + 1
print(ans)

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    As = sorted(A)
    Bs = sorted(B)

    if N % 2 == 1:
        min_ = As[(N-1)//2]
        max_ = Bs[(N-1)//2]

        print(max_-min_ + 1)
    else:
        min_1 = As[N//2]
        # if N <= 2: min_2 = min_1
        min_2 = As[N//2-1]
        min_ = (min_1 + min_2)/2
        max_1 = Bs[N//2]
        # if N <= 2: max_2 = max_1
        max_2 = Bs[N//2-1]
        max_ = (max_1 + max_2)/2
        # print(min_, max_)
        print(int((max_ - min_)//0.5 + 1))


    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
 
N = int(readline().strip())
A = []
B = []
for i in range(N):
  ai, bi = map(int, readline().split())
  A.append(ai)
  B.append(bi)

A.sort()
B.sort()

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

Submission status: Accepted

Solution code:
import statistics  # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


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

    M = statistics.median(B)
    m = statistics.median(A)

    if N % 2 == 0:
        print(int((M-m)//0.5+1))
    else:
        print(M-m+1)


resolve()

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_)
am, bm = median(a), median(b)
print(int(max(0, bm - am) * 2) + 1 if n % 2 == 0 else int(max(0, bm - am)) + 1)

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==0:
        print(int(((B[n//2-1]+B[n//2])/2-(A[n//2-1]+A[n//2])/2)*2)+1)
    else:
        print(B[(n+1)//2-1]-A[(n+1)//2-1]+1)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
list_AB = [ list(map(int,input().split(" "))) for _ in range(n)]
mins = []
maxs = []
for a, b in list_AB:
    mins.append(a)
    maxs.append(b)

mins.sort()
maxs.sort()

if n % 2 == 1:
    ans = maxs[n//2] - mins[n//2] + 1
else:
    small = mins[n//2] + mins[n//2-1]
    large = maxs[n//2] + maxs[n//2-1]
    ans = large - small +1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
As=[0]*N
Bs=[0]*N
for i in range(N):
    As[i],Bs[i] = map(int,input().split())
As.sort()
Bs.sort()

if N%2==1:
    Amin=As[(N+1)//2-1]
    Bmin=Bs[(N+1)//2-1]
    ans=Bmin-Amin+1
else: #N%2==0
    Amin=(As[N//2-1]+As[N//2])/2
    Bmin=(Bs[N//2-1]+Bs[N//2])/2
    ans=(Bmin-Amin)/0.5+1
print(int(ans))

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 = N // 2
    print(B[mid] - A[mid] +1)
else:
    mid1 = N // 2
    mid2 = mid1 -1
    smallest = A[mid1] + A[mid2]
    largest = B[mid1] + B[mid2]
    print(largest - smallest + 1)

Submission status: Accepted

Solution code:
def median(A,N):
    A.sort()
    if N % 2 == 1:
        return A[N//2]
    else:
        return A[(N-1)//2] + A[(N-1)//2+1]

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

    ans = median(B,N) - median(A,N) + 1
    print(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.sort()
B.sort()
if N%2==1:
    print(B[N//2]-A[N//2]+1)
else:
    A_chu=(A[N//2-1]+A[N//2])/2
    B_chu=(B[N//2-1]+B[N//2])/2
    ans=(B_chu-A_chu)*2+1
    print(int(ans))

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((b[N//2-1]+b[N//2])-(a[N//2-1]+a[N//2])+1)

Submission status: Accepted

Solution code:
from sys import stdin
from sys import setrecursionlimit
from functools import cmp_to_key
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)
MAX_N = 200000 + 5

def main():
    from builtins import int, map
    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()
    ans = 0

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def median(q):
    l=len(q)
    q=sorted(q,reverse=True)
    if l%2==0:
        return (q[int(l/2)]+q[int(l/2)-1])/2
    else:
        return q[int((l+1)/2-1)]

n=int(input())

a=[]
b=[]

for i in range(n):
   x,y=map(int,input().split())
   a.append(x)
   b.append(y)
   
med_a=median(a)
med_b=median(b)

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

Submission status: Accepted

Solution code:
N,*ABf = map(int, open(0).read().split())
AB = [ABf[i:i+2] for i in range(0, len(ABf), 2)]
A = sorted([x[0] for x in AB])
B = sorted([x[1] for x in AB])
if N % 2 == 1:
    print(B[N//2]-A[N//2]+1)
else:
    print(int(((B[N//2-1]+B[N//2])*(0.5)-(A[N//2-1]+A[N//2])*(0.5))*2+1))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = []
B = []
for i in range(N):
    in1, in2 = list(map(int,input().split()))
    A.append(in1)
    B.append(in2)
temp1 = np.median(A)
temp2 = np.median(B)
if N % 2 == 0:
    ans = (temp2 - temp1) * 2 + 1
else:
    ans = (temp2 - temp1) + 1
print(int(ans))

Submission status: Accepted

Solution code:
I = lambda : list(map(int,input().split()))

n,=I();a=[];b=[]
for i in range(n):
  x,y=I()
  a.append(x);b.append(y)
a.sort();b.sort()
if n%2:
  print(b[n//2]-a[n//2]+1)
else:
  x=(a[n//2-1]+a[n//2]);y=(b[n//2-1]+b[n//2])
  print(y-x+1)

Submission status: Accepted

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

if n & 1:
  min_medium = a_list[n // 2]
  max_medium = b_list[n // 2]
  print(max_medium - min_medium + 1)
else:
  min_medium_x2 = a_list[n // 2 - 1] + a_list[n // 2]
  max_medium_x2 = b_list[n // 2 - 1] + b_list[n // 2]
  print(max_medium_x2 - min_medium_x2 + 1)

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    L = [0]*N
    R = [0] * N
    for i in range(N):
        l, r = map(int, input().split())
        L[i] = l
        R[i] = r

    L.sort()
    R.sort()
    ans = 0
    if N%2 == 1:
        mid_L = L[N//2]
        mid_R = R[N // 2]
        ans = mid_R - mid_L +1
    else:
        mid_L = L[N // 2-1] + L[N // 2]
        mid_R = R[N // 2-1] + R[N // 2]
        ans = mid_R - mid_L + 1

    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from sys import stdin, stdout

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case #{}: {}".format(i, o))


# https://atcoder.jp/contests/abc169/tasks/abc169_e
def solve(n, ranges):
    a = sorted(map(lambda x: x[0], ranges))
    b = sorted(map(lambda x: x[1], ranges))

    if len(ranges) % 2 == 0:
        m_a = a[(len(a)//2)-1] + a[(len(a)//2)]
        m_b = b[(len(b)//2)-1] + b[(len(b)//2)]
    else:
        m_a = a[((len(a)+1)//2)-1]
        m_b = b[((len(b)+1)//2)-1]
    return m_b - m_a + 1



if __name__ == "__main__":
    n = int_in()
    ranges = mat_in(n)
    out(solve(n, ranges))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import statistics

n = int(input())
la = [0]*n
lb = [0]*n
for i in range(n):
  a, b = map(int, input().split())
  la[i] = a
  lb[i] = b
a = statistics.median(la)
b = statistics.median(lb)
if n % 2 == 1:
  print(b - a + 1)
else:
  print(int((b - a) * 2 + 1))

Submission status: Accepted

Solution code:
import statistics

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

Submission status: Accepted

Solution code:
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==0):
    print(bb[n//2-1]+bb[n//2]-aa[n//2]-aa[n//2-1]+1)
else:
    print(bb[n//2]-aa[n//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)

mn = median(A)
mx = median(B)

if N % 2 == 1:
    print(int(mx - mn + 1))        # mn, mn + 1, mn + 2, ..., mx
else:
    print(int((mx - mn) * 2 + 1))    # mn, mn + 0.5, mn + 1, ..., mx

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 = N // 2
  print(b[mid] - a[mid] + 1)
else:
  mid1 = N // 2
  mid2 = mid1 - 1
  print(b[mid1] + b[mid2] - a[mid1] - a[mid2] + 1)

Submission status: Accepted

Solution code:
import sys
readline=sys.stdin.readline
 
N=int(readline())
A = [None] * N
B = [None] * N
for i in range(N):
  A[i],B[i] = map(int,readline().split())

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

if N % 2 == 1:
  # 奇数の場合
  Amid = A[N // 2]
  Bmid = B[N // 2]
  print(Bmid - Amid + 1)
else:
  # 偶数の場合
  Amid = (A[N // 2] + A[(N // 2) - 1])
  Bmid = (B[N // 2] + B[(N // 2) - 1])
  print(int(Bmid - Amid) + 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:
    x = A[n//2]
    y = B[n//2]
else:
    x = A[n//2 - 1]+A[n//2]
    y = B[n//2 - 1]+B[n//2]
print(y-x+1)

Submission status: Accepted

Solution code:
import numpy as np

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

if N % 2 == 1:
    a = np.sort(arr[:, 0])
    b = np.sort(arr[:, 1])
    print(b[N // 2] - a[N // 2] + 1)

else:
    a = np.sort(arr[:, 0])
    b = np.sort(arr[:, 1])
    print((b[N // 2] + b[N // 2 - 1]) - (a[N // 2 - 1] + a[N // 2]) + 1)

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==0:
    p = n//2
    right = (a[p]+a[p-1])/2 - 0.5
    left = (b[p]+b[p-1])/2
    r = left-right
    if int(r)==r:
        print(int(r)*2)
    else:
        print(int(r)*2 +1)
else:
    p = n//2
    right = a[p]
    left = b[p]
    print(int(left-right)+1)

Submission status: Accepted

Solution code:
import numpy as np
(n,),*l = [[*map(int,i.split())]for i in open(0)]
a,b = np.median(l,axis=0)
if n%2>0:
    print(int(b-a+1))
else:
    print(int(b*2-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:
  n = N // 2
  a, b = A[n], B[n]
  print(b - a + 1)
else:
  n = N // 2
  a0, b0 = A[n-1], B[n-1]
  a1, b1 = A[n], B[n]
  left = a0 + a1; right = b0 + b1
  print(right - left + 1)

Submission status: Accepted

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

if N%2==0:
    Min=[]
    for i in range(N):
        Min.append(min(L[i]))
    #print(Min)
    Min.sort()
    MIN=(Min[N//2]+Min[N//2-1])/2
    #print(MIN)
    Max=[]
    for i in range(N):
        Max.append(max(L[i]))
    Max.sort()
    MAX=(Max[N//2]+Max[N//2-1])/2
    print(int((MAX-MIN)*2+1))
else:
    Min=[]
    for i in range(N):
        Min.append(min(L[i]))
    #print(Min)
    Min.sort()
    MIN=Min[N//2]
    Max=[]
    for i in range(N):
        Max.append(max(L[i]))
    Max.sort()
    MAX=Max[N//2]
    print(int((MAX-MIN)+1))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a if N & 1 else 2 * a)
    B.append(b if N & 1 else 2 * b)

a, b = int(np.median(A)), int(np.median(B))
print(b - a + 1)

Submission status: Accepted

Solution code:
n = int(input())

a, b = [*map(list, zip(*[[*map(int, input().split())] for _ in range(n)]))]
a.sort()
b.sort()

l0, r0 = a[(n-1)//2], b[(n-1)//2]
if n % 2 == 1:
    print(r0-l0+1)
else:
    l1, r1 = a[n//2], b[n//2]
    print((r1+r0)-(l1+l0)+1)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    at, bt = map(int, input().split())
    a.append(at)
    b.append(bt)
if(n % 2 == 1):
    a.sort()
    b.sort()
    min_med = a[(n-1)//2]
    max_med = b[(n-1)//2]
    result = max_med - min_med + 1
else:
    a.sort()
    b.sort()
    min_med = (a[n//2-1] + a[n//2])/2
    max_med = (b[n//2-1] + b[n//2])/2
    result = (max_med - min_med)*2 + 1
print(int(result))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a if N & 1 else 2 * a)
    B.append(b if N & 1 else 2 * b)

a, b = int(np.median(A)), int(np.median(B))
if a > b:
    a, b = b, a
print(b - a + 1)

Submission status: Accepted

Solution code:
import sys
import math

N = int(sys.stdin.readline().strip())
ABlist = []
for _ in range(N):
    a, b = map(int, sys.stdin.readline().strip().split())
    ABlist.append((a, b))

Asorted = sorted(ABlist)
Bsorted = sorted(ABlist, key=lambda x: x[1])
if N % 2 == 1:
    left = Asorted[N//2][0]
    right = Bsorted[N//2][1]
    # print(left, right)
    print(right - left + 1)
else:
    left = (Asorted[N//2 - 1][0] + Asorted[N//2][0]) / 2
    right = (Bsorted[N//2 - 1][1] + Bsorted[N//2][1]) / 2
    print(int((right - left) * 2 + 1))

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    a = [0] * n
    b = [0] * n

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

    a.sort()
    b.sort()

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = []
B = []
for _ in range(n):
    v1, v2 = list(map(int, input().split(' ')))
    A.append(v1)
    B.append(v2)

A.sort()
B.sort()

if n % 2:
    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)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()


def solve():
    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 == 0:
        i1 = N // 2
        i2 = N // 2 - 1
        low = As[i1] + As[i2]
        up = Bs[i1] + Bs[i2]
        ans = up - low + 1
    else:
        i = (N - 1) // 2
        low = As[i]
        up = Bs[i]
        ans = up - low + 1

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
import numpy as np
ab=np.array([[int(j)for j in input().split()]for i in range(n)])
a=np.median(ab[:,0])
b=np.median(ab[:,1])
if n%2:
    print(int(b-a+1))
else:
    print(int(2*b-2*a+1))

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

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()
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:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	if len(x) == 0:
		return []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa != x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
	ll = 0
	rr = r
	while True:
		mid = (ll+rr)//2

	if rr == mid:
		return ll
	if (ここに評価入れる):
		rr = mid
	else:
		ll = mid+1
"""
n = onem()

le = []

ri = []

if n % 2 != 0:
	for i in range(n):
		a,b = m()
		le.append(a)
		ri.append(b)
	le.sort()
	ri.sort()
	print(ri[n//2] - le[n//2]+1)



else:
	for i in range(n):
		a,b = m()
		le.append(a*2)
		ri.append(b*2)
	le.sort()
	ri.sort()
	print(((ri[n//2]+ri[n//2-1]) - (le[n//2]+le[n//2-1]))//2+1)

Submission status: Accepted

Solution code:
N = int(input())
AB = [list(map(int,input().split())) for _ in range(N)]
 
A = []
B = []
for i in range(N):
  A.append(AB[i][0])
  B.append(AB[i][1])
A.sort()
B.sort()
if N % 2 == 0:
    n = N // 2
    med1 = A[n-1] + A[n]
    med2 = B[n-1] + B[n]
    x = med2 - med1 + 1
else:
    n = N // 2
    med1 = A[n]
    med2 = B[n]
    x = med2 - med1 + 1
    
print(x)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    a1,b1 = map(int, input().split())
    a.append(a1)
    b.append(b1)
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]-a[n//2-1]+b[n//2]-a[n//2]+1)

Submission status: Accepted

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

def main():
    N,*ab = map(int, read().split())

    AB = []
    for a, b in zip(*[iter(ab)]*2):
        AB.append((a, b))

    if N % 2 == 0:
        AB.sort()
        a1, _ = AB[N // 2 - 1]
        a2, _ = AB[N // 2]
    
        AB.sort(key=lambda x: x[1])
        _, b1 = AB[N // 2 - 1]
        _, b2 = AB[N // 2]

        ans = b1 + b2 - (a1 + a2) + 1
    
    else:
        AB.sort()
        a, _ = AB[N // 2]
    
        AB.sort(key=lambda x: x[1])
        _, b = AB[N // 2]
        
        ans = b - a + 1
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left,bisect_right
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
from sys import stdin
input = stdin.readline
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = []
b = []
for i in range(n):
    aa,bb = inm()
    a.append(aa)
    b.append(bb)
a.sort()
b.sort()
if n%2==1:
    m = n//2
    print(b[m]-a[m]+1)
else:
    m = n//2 - 1
    print(b[m]+b[m+1]-a[m]-a[m+1]+1)

Submission status: Accepted

Solution code:
import statistics
n = int( input() )
a = []
b = []
for _ in range( n ):
    a_i, b_i = map( int, input().split() )
    a.append( a_i )
    b.append( b_i )

m_a = statistics.median( a )
m_b = statistics.median( b )

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