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:
n = int(input())
x = [list(map(int, input().split())) for _ in range(n)]
y = sorted(x)
z = sorted(x, key=lambda x: x[1])

if n % 2 == 1:
    print(z[n//2][1] - y[n//2][0] + 1)
else:
    a = y[n//2-1][0]
    b = y[n//2][0]
    c = z[n//2-1][1]
    d = z[n//2][1]
    e = (c + d) / 2
    f = (a + b) / 2
    print(int((e - f) * 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:
    x = a[N//2]+a[N//2-1]
    y = b[N//2]+b[N//2-1]
    print(y-x+1)

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

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = I()
L = LI2(N)
A = [i[0] for i in L]
B = [i[1] for i in L]
A.sort()
B.sort()
if N%2==0:
    medA = (A[N//2-1]+A[N//2])
    medB = (B[N//2-1]+B[N//2])
    print((medB-medA)+1)
else:
    medA = A[(N-1)//2]
    medB = B[(N-1)//2]
    print(medB-medA+1)

Submission status: Accepted

Solution code:
n = int(input())

ls = []
rs = []
for _ in range(n):
    x, y = map(int, input().split())
    ls.append(x)
    rs.append(y)

ls = sorted(ls)
rs = sorted(rs)

if n % 2 == 1:
    print(rs[len(rs)//2] - ls[len(ls)//2] + 1)
else:
    a = (rs[len(rs)//2-1] * 10 + rs[len(rs)//2] * 10) // 2
    b = (ls[len(ls)//2-1] * 10 + ls[len(ls)//2] * 10) // 2
    print((a - b) // 5 + 1)

Submission status: Accepted

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

mid_min = 0
mid_max = 0

ab.sort(key=lambda x: x[0])
if n & 1:
    mid_min = ab[n // 2][0]
else:
    mid_min = (ab[(n - 1) // 2][0] + ab[n // 2][0])

ab.sort(key=lambda x: x[1])
if n & 1:
    mid_max = ab[n // 2][1]
else:
    mid_max = (ab[(n - 1) // 2][1] + ab[n // 2][1])

print(mid_max - mid_min + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from sys import stdin, stdout

def solve():
    n = int(stdin.readline().strip())

    seqA = []
    seqB = []
    for i in range(n):
        a,b = map(int, stdin.readline().split())
        seqA.append(a)
        seqB.append(b)

    seqA = sorted(seqA)
    seqB = sorted(seqB)
    mA = seqA[n//2]
    mB = seqB[n//2]
    if n%2==0:
        mA += seqA[n//2-1]
        mB += seqB[n//2-1]

    print(mB-mA+1)

solve()

Submission status: Accepted

Solution code:
# coding: utf-8

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

print(ans)

Submission status: Accepted

Solution code:
# https://codeforces.com/blog/entry/78195: Geothermal editorial

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:
    medA, medB = A[n//2], B[n//2]
    
else:
    medA = A[n//2] + A[n//2 - 1]
    medB = B[n//2] + B[n//2 - 1]

print(medB - medA + 1)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache
 
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())
n=read()
a,b=[0]*n,[0]*n
for i in range(n):
  a[i],b[i]=reads()
a=sorted(a)
b=sorted(b)
if n%2==1:
  print(b[(n-1)//2]-a[(n-1)//2]+1)
else:
  mi=(a[n//2-1],a[n//2])
  ma=(b[n//2-1],b[n//2])
  res=(ma[0]+ma[1])-(mi[0]+mi[1])+1
  print(res)

Submission status: Accepted

Solution code:
from sys import stdin

def main():
  s = []
  read = stdin.readline
  N =int(read())
  data = [list(map(int,read().split(" "))) for _ in range(N)]
  A = sorted([i[0] for i in data])
  B = sorted([i[1] for i in data])
  if N%2 == 1:
    a1 = A[((N+1)//2)-1]
    b1 = B[((N+1)//2)-1]
    print(b1 - a1 + 1)
  else:
    l = []
    a1 = A[(N//2)-1]
    b1 = B[(N//2)-1]
    a2 = A[(N//2)]
    b2 = B[(N//2)]
    print(b1 + b2 + 1 - a1 - a2)
    
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
#E
import sys
input=sys.stdin.readline

N=int(input())
A=[0 for i in range(N)]
B=[0 for i in range(N)]

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

if N%2==0:
    f=(A_sort[int(N/2)]+A_sort[int(N/2)-1])/2
    t=(B_sort[int(N/2)]+B_sort[int(N/2)-1])/2
    ans=int((t-f)*2+1)
else:
    f=A_sort[int((N-1)/2)]
    t=B_sort[int((N-1)/2)]
    ans=int((t-f)+1)
    
print(ans)

Submission status: Accepted

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


def main(input, print):
    n = int(input())
    li = [0] * n
    ri = [0] * n
    for i in range(n):
        li[i], ri[i] = map(int, input().split())
    li.sort()
    ri.sort()
    ans = 0
    if n % 2 == 1:
        l = li[n//2]
        r = ri[n//2]
        ans = r-l+1
    else:
        l = li[n//2-1]+li[n//2]
        r = ri[n//2-1]+ri[n//2]
        ans = r-l+1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
AB = [list(map(int, input().split())) for n in range(N)]
sortA = sorted(AB, key= lambda x:x[0])
sortB = sorted(AB, key= lambda x:x[1])
if N % 2 == 0:
  k1 = int(N/2-1)
  k2 = int(N/2)
  minM = sortA[k1][0] + sortA[k2][0]
  maxM = sortB[k1][1] + sortB[k2][1]
  print(maxM-minM+1)
else:
  k = int((N+1)/2)-1
  A = sortA[k][0]
  B = sortB[k][1]
  print(B-A+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(reverse=True)
if N%2==1:
  mid = (N+1)//2-1
  ans = B[mid]-A[mid]+1
else:
  mid1 = N//2-1
  mid2 = N//2
  ans = (B[mid1]+B[mid2])-(A[mid1]+A[mid2])+1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def solve():
    N = int(input())
    AB = np.array([list(map(int, input().split() )) for _ in range(N)], dtype=np.int64)
    A = AB[:,0]
    B = AB[:,1]
    Amed = np.median(A)
    Bmed = np.median(B)
    if N % 2 ==1:
        print(int(Bmed - Amed+1))
    else:
        print(int((Bmed - Amed )*2+1))


if __name__ == '__main__':
    solve()

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)

def median(arr):
    arr.sort()
    n = len(arr)
    if n % 2 == 1:
        return arr[(n + 1) // 2 - 1]
    else:
        return (arr[n//2 - 1] + arr[n//2]) / 2

med_a = median(A)
med_b = median(B)
if N % 2 == 1:
    ans = int(med_b) - int(med_a) + 1
else:
    ans = med_b * 2 - med_a * 2 + 1
    ans = int(ans)

print(ans)

Submission status: Accepted

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

if n % 2:

    ab.sort()
    l = ab[(n+1)//2-1][0]

    ab.sort(key=lambda x:x[1], reverse=True)
    r = ab[(n+1)//2-1][1]

    print(r-l+1)

else:

    a = [ai for ai, bi in ab]
    b = [bi for ai, bi in ab]

    a.sort()
    b.sort()

    ll = a[n//2-1]
    lr = b[n//2-1]

    rl = a[n//2]
    rr = b[n//2]


    print((lr+rr) - (ll+rl) + 1)

Submission status: Accepted

Solution code:
n=int(input())
alis=[]
blis=[]
for i in range(n):
  a,b=map(int,input().split())
  alis.append(a)
  blis.append(b)
  
  
alis=sorted(alis)
blis=sorted(blis)
if n%2!=0:
  print(blis[int((n-1)/2)]-alis[int((n-1)/2)]+1)
else:
  bb=(blis[int(n/2)-1]+blis[int(n/2)])
  aa=(alis[int(n/2)-1]+alis[int(n/2)])
  print(bb-aa+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:
  med_minimum = A[(N-1)//2]
  med_maximum = B[(N-1)//2]
else:
  med_minimum = (A[(N-2)//2] + A[(N-2)//2+1])
  med_maximum = (B[(N-2)//2] + B[(N-2)//2+1])
print(med_maximum - med_minimum + 1)

Submission status: Accepted

Solution code:
from decimal import *

n = int(input())

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

if n % 2 == 1:
    med = (n + 1) // 2
    ans = (b[med - 1] - a[med - 1]) + 1
    print(int(ans))
else:
    med0 = n // 2
    med1 = (n // 2) + 1
    amed = (Decimal(a[med0-1])+Decimal(a[med1-1]))/Decimal(2)
    bmed = (Decimal(b[med0-1])+Decimal(b[med1-1]))/Decimal(2)
    ans = int(((bmed - amed) * Decimal(2) + Decimal(1)))
    print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.readline

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

A.sort()
B.sort()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    ab = [map(int, input().split()) for _ in range(n)]
    a, b = [list(i) for i in zip(*ab)]
    new_a = sorted(a)
    new_b = sorted(b)
    if n % 2 == 1:
        min_m = new_a[(n + 1) // 2 - 1]
        max_m = new_b[(n + 1) // 2 - 1]
        print((max_m - min_m) + 1)
    else:
        min_m = (new_a[n // 2 - 1] + new_a[n // 2])
        max_m = (new_b[n // 2 - 1] + new_b[n // 2])
        print((max_m - min_m) + 1)


if __name__ == '__main__':
    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()
ans = 0
if n % 2 == 1:
    ans = B[n // 2] - A[n // 2] + 1
else:
    large = B[n // 2] + B[n // 2 - 1]
    small = A[n // 2] + A[n // 2 - 1]
    ans = large - small + 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = []
    b = []
    for _ in range(n):
        ta, tb = [int(i) for i in input().split()]
        a.append(ta)
        b.append(tb)
    a = sorted(a)
    b = sorted(b)

    if n % 2:
        mid_idx = (n + 1)//2 -1
        print(b[mid_idx] -a[mid_idx] + 1)
    else:
        mid_idx = [n//2 - 1, n//2]
        mid_a = (a[mid_idx[1]] + a[mid_idx[0]])
        mid_b = (b[mid_idx[1]] + b[mid_idx[0]])
        print(mid_b - mid_a + 1)

if __name__ == '__main__':
    main()

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 = map(int,input().split())
    A[i] = a
    B[i] = b

A_sort = sorted(A)
B_sort = sorted(B)

if N % 2 == 1:
    print(B_sort[N//2] - A_sort[N//2] + 1)
else:
    med_A = (A_sort[N//2] + A_sort[N//2 - 1])
    med_B = (B_sort[N//2] + B_sort[N//2 - 1])
    print(med_B - med_A + 1)

Submission status: Accepted

Solution code:
n = int(input())

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

if n & 1:
    m = n // 2
    print(v2[m] - v1[m] + 1)
else:
    m = n // 2
    print(v2[m-1]-v1[m-1] + v2[m]-v1[m] + 1)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

import statistics as st

n = ii()
a = []
b = []
for i in range(n):
    aa,bb = mi()
    a.append(aa)
    b.append(bb)

a.sort()
b.sort()

minmed = st.median(a)
maxmed = st.median(b)

if len(a) %2 == 0:
    ans = maxmed*2 - minmed*2 + 1
else:
    ans = maxmed-minmed+1
print(int(ans) )

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    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(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)

if __name__=='__main__':
    main()

Submission status: Accepted

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

A_list.sort()
B_list.sort()

if N%2 == 1:
    print(B_list[(N-1)//2] - A_list[(N-1)//2] + 1)
else:
    big = (B_list[N//2] + B_list[N//2-1])/2
    small = (A_list[N//2] + A_list[N//2-1])/2
    print(int((big-small)*2 + 1))

Submission status: Accepted

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

lb = sorted(v for v, _ in X)
ub = sorted(v for _, v in X)

if N % 2 == 0:
    l = lb[N // 2 - 1] + lb[N // 2]
    u = ub[N // 2 - 1] + ub[N // 2]
    print(u - l + 1)
else:
    l = lb[N // 2]
    u = ub[N // 2]
    print(u - l + 1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

a = sorted([x[0] for x in ab])
b = sorted([y[1] for y 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 - 1) // 2] - a[(n - 1) // 2] + 1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

def main():
    N=I()
    L=[]
    R=[]

    
    for i in range(N):
        a,b=MI()
        L.append(a)
        R.append(b)
    L.sort()
    R.sort()
        
    #中央値の最小値は，Lの中央値．中央値の最大値はRの中央値
    #間は全部埋められそう?
        
    if N%2==0:
        Lm=(L[N//2]+L[N//2-1])
        Rm=(R[N//2]+R[N//2-1])
    else:
        Lm=L[N//2]
        Rm=R[N//2]
    ans=Rm-Lm+1
    print(ans)
        
        
        
        
        
    
            

main()

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

a = []
b = []


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

a_med = np.median(a)
b_med = np.median(b)

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

Submission status: Accepted

Solution code:
N = int(input())

a_array = []
b_array = []

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

a_array.sort()
b_array.sort()

if N % 2 != 0:
    mid_min = a_array[(N - 1) // 2]
    mid_max = b_array[(N - 1) // 2]
    print(mid_max - mid_min + 1)

else:
    mid_min = (a_array[N // 2 - 1] + a_array[N // 2]) / 2
    mid_max = (b_array[N // 2 - 1] + b_array[N // 2]) / 2
    print(int((mid_max - mid_min) * 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()
ans = 0
if n%2 == 1:
  ans = b[n//2]-a[n//2]+1
else:
  ans = (b[n//2]-a[n//2])+(b[n//2-1]-a[n//2-1])+1
print (ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
AX = [A[i][0] for i in range(N)]
BX = [A[i][1] for i in range(N)]
AX.sort()
BX.sort()
if N % 2 == 1:
  L = AX[(N-1)//2]
  R = BX[(N-1)//2]
  print(R-L+1)
else:
  L = AX[N//2-1] + AX[N//2]
  R = BX[N//2-1] + BX[N//2]
  print(R-L+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:
    m = n//2
    ans = b[m] - a[m] + 1
else:
    ml = n//2-1
    mr = n//2
    ma = (a[ml] + a[mr])/2 
    mb = (b[ml] + b[mr])/2
    ans = int((mb - ma)*2 + 1)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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:
        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:
# E - Count Median
def main():
    N, *AB = map(int, open(0).read().split())
    A, B = AB[::2], AB[1::2]
    A.sort(), B.sort()
    mid = N // 2
    if N % 2:
        print(B[mid] - A[mid] + 1)
    else:
        print((B[mid] + B[mid - 1]) - (A[mid] + A[mid - 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[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:
    print(abs(A[N//2]+A[N//2-1]-B[N//2]-B[N//2-1])+1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
AB = [[int(i) for i in input().split()] for _ in range(N)]
AB.sort()
ans = 0
if N & 1:
    l = AB[N//2][0]
    AB.sort(key=lambda x: x[1])
    ans = AB[N // 2][1] - l + 1
else:
    l = (AB[N // 2][0], AB[N // 2 - 1][0])
    AB.sort(key=lambda x: x[1])
    r = (AB[N // 2][1], AB[N // 2 - 1][1])
    ans = sum(r) - sum(l) + 1
print(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:
    print(b[n//2] - a[n//2] + 1)
else:
    front = n//2
    back = (n-1)//2
    print((b[front] + b[back] - (a[front] + a[back]) + 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:
    min_m, max_m = a[n // 2], b[n // 2]
    ans = max_m - (min_m - 1)
else:
    min_m2 = a[n // 2] + a[n // 2 - 1]
    max_m2 = b[n // 2] + b[n // 2 - 1]
    ans = max_m2 - min_m2 + 1
print(ans)

Submission status: Accepted

Solution code:
#E
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:
    n = int(N/2)
    xmin = (A[n-1]+A[n])
    xmax = (B[n-1]+B[n])
    count = int(xmax)-int(xmin+0.5)+1
else:
    n = int((N+1)/2)
    xmin = A[n-1]
    xmax = B[n-1]
    count = xmax-xmin+1

print(count)

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)  # aの中央値を計算
med_b = median(b)  # bの中央値を計算

if n%2 == 0:  # nが偶数
    print(int(2*(med_b-med_a)+1))
else:  # nが奇数
    print((med_b-med_a)+1)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def calc_median(X):
    #中央値計算
    Xl = len(X)
    if Xl % 2:
        #ret = X[(Xl + 1) // 2 - 1]
        ret = X[Xl // 2]
    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())

mins = []
maxs = []

for i in range(n):
  a, b = map(int, input().split())
  mins.append(a)
  maxs.append(b)
  
mins.sort()
maxs.sort()

if n % 2 == 1:
  mid = n // 2
  print(maxs[mid] - mins[mid] + 1)
else:
  mid1 = n//2
  mid2 = mid1 - 1
  smallest = mins[mid1] + mins[mid2]
  largest = maxs[mid1] + maxs[mid2]
  print(largest - smallest + 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:
    medi = N // 2
    ans = B[medi] - A[medi] + 1
else:
    medi1 = N // 2 - 1
    medi2 = medi1 + 1
    A_medi = (A[medi1] + A[medi2]) / 2
    B_medi = (B[medi1] + B[medi2]) / 2
    ans = (B_medi - A_medi) * 2 + 1
print(int(ans))

Submission status: Accepted

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

ab.sort()
a = []
b =[]
for x in ab:
  a.append(x[0])
  b.append(x[1])
a.sort()
b.sort()

ca,cb =0,0
if n % 2 == 0:
  ca = (a[n//2-1] + a[n//2])/2
  cb = (b[n//2-1] + b[n//2])/2
  ans = int((cb-ca)//0.5+1)
else:
  ca = a[(n+1)//2-1]
  cb = b[(n+1)//2-1]
  ans = (cb-ca) +1

print(int(ans))

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    N = int(input())
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())

    #output
    import statistics as st
    import math

    p = st.median(A)
    q = st.median(B)

    # %%
    if N % 2 == 0:
        print(int((q-p)/0.5)+1)
    else:
        print(int(q-p)+1)

    #N = 1のときなどcorner caseを確認！
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)

l=[]
r=[]
A.sort()
B.sort()

if N%2==1:
    n=N//2
    ans=B[n]-A[n]+1
else:
    n1=N//2-1
    n2=N//2
    ans=B[n1]-A[n1]+B[n2]-A[n2]+1

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

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

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
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:
  l=A[n//2]
  r=B[n//2]
  print(r-l+1)
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:
mi = []
ma = []
n = int(input())

for _ in range(n):
    a, b = [int(a) for a in input().split()]
    mi.append(a)
    ma.append(b)

mi.sort()
ma.sort()
mi = (mi[int(n/2)] + mi[int((n-1)/2)])/2
ma = (ma[int(n/2)] + ma[int((n-1)/2)])/2

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

Submission status: Accepted

Solution code:
def main():
    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 == 0:
        m1 = (A[n//2-1]+A[n//2])/2
        m2 = (B[n//2-1]+B[n//2])/2
        print(int(2*(m2-m1))+1)
    else:
        print(B[(n+1)//2-1]-A[(n+1)//2-1]+1)    
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()

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:
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(median(b) - median(a) + 1)
else:
    print(int((median(b) - median(a)) * 2) + 1)

Submission status: Accepted

Solution code:
import sys                                                
n = int(sys.stdin.readline())                             
                                                          
xs = []                                                   
for _ in range(n):                                        
  v = [int(x) for x in sys.stdin.readline().split(" ")]   
  xs.append(v)                                            
                                                          
xs.sort(key=lambda x: x[0])                               
if n % 2 == 0:                                            
  m1 = n // 2 - 1                                         
  m2 = m1 + 1                                             
  a1 = xs[m1][0]                                          
  a2 = xs[m2][0]                                          
  xs.sort(key=lambda x: x[1])                             
  b1 = xs[m1][1]                                          
  b2 = xs[m2][1]                                          
  a = (a1 + a2)                                           
  b = (b1 + b2)                                           
  c = (b - a) + 1                                         
  #print(m1, m2, a1, a2, b1, b2, "*", a, b, c)            
  print(c)                                                
else:                                                     
  m = (n + 1) // 2 - 1                                    
  a = xs[m][0]                                            
  xs.sort(key=lambda x: x[1])                             
  b = xs[m][1]                                            
  c = b - a + 1                                           
  #print(m, a, b, c)                                      
  print(c)

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 len(A)%2 != 0:
  ans = B[N//2]-A[N//2]+1
else:
  ans = (B[N//2]+B[N//2-1]) - (A[N//2]+A[N//2-1]) + 1

print(ans)

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)
    B.append(b)
    
Aarray = np.array(A)
Barray = np.array(B)

medA = np.median(Aarray)
medB = np.median(Barray)

if N%2 == 1:
    ans = medB-medA+1
else:
    ans = 2*(medB-medA)+1
    

print(str(int(ans)))

Submission status: Accepted

Solution code:
import sys
N=int(sys.stdin.readline().strip())
A=[]
B=[]
for _ in range(N):
    a,b=map(int, sys.stdin.readline().split())
    A.append(a)
    B.append(b)
A.sort()
B.sort()
if N%2==0:
    A_med=float(A[N/2]+A[N/2-1])/2
    B_med=float(B[N/2]+B[N/2-1])/2
    print int((B_med-A_med)*2+1)
else:
    A_med=A[N/2]
    B_med=B[N/2]
    print B_med-A_med+1

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A,B = [],[]
    for _ in range(N):
        a,b = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
    A.sort()
    B.sort()
    if N % 2 == 1:
        minMed = A[N//2]
        maxMed = B[N//2]
        print(int(maxMed-minMed)+1)
    else:
        minMed = A[N//2-1] + A[N//2]
        maxMed = B[N//2-1] + B[N//2]
        print(int(maxMed-minMed)+1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    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_sorted = list(sorted(a_list))
    b_sorted = list(sorted(b_list))
    if n%2:
        print(b_sorted[n//2]-a_sorted[n//2]+1)
    else:
        print((b_sorted[n//2-1]+b_sorted[n//2]-a_sorted[n//2-1]-a_sorted[n//2])+1)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 解説を参考に作成


def solve(N, As, Bs):
    As.sort()
    Bs.sort()
    ans = 0
    # N が奇数の場合
    if N % 2 == 1:
        x = (N + 1) // 2 - 1
        ans = Bs[x] - As[x] + 1
    # N が偶数の場合
    else:
        lowx = N // 2 - 1
        highx = N // 2
        ans = (Bs[lowx] + Bs[highx]) - (As[lowx] + As[highx]) + 1
    print(ans)
    pass


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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
N = int(readline())
A = []
B = []
for _ in range(N):
    a, b = map(int, readline().split())
    A.append(a)
    B.append(b)
A.sort()
B.sort()
if N % 2 == 0:
    mi = A[N//2-1]+A[N//2]
    ma = B[N//2-1]+B[N//2]
else:
    mi = A[N//2]
    ma = B[N//2]
print(ma-mi+1)