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())
ab=[list(map(int,input().split())) for i in range(n)]

all_min=[]
all_max=[]
for itm in ab:
    a,b=itm
    all_min.append(a)
    all_max.append(b)

import statistics
med_min=statistics.median(all_min)
med_max=statistics.median(all_max)

diff = med_max - med_min

if n%2==0:
    print(int(diff*2+1))
else:
    print(diff+1)

Submission status: Accepted

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n = int(input())
seg = [list(map(int, input().split())) for _ in range(n)]
a = [x[0] for x in seg]
b = [x[1] for x in seg]
a.sort()
b.sort()

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

Submission status: Accepted

Solution code:
from statistics import median

N = int(input())
As = [0] * N
Bs = [0] * N

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

med_A = median(As)
med_B = median(Bs)

if N%2:
    print(med_B-med_A+1)
else:
    print(int((med_B-med_A)*2)+1)

Submission status: Accepted

Solution code:
n = int(input())
a, b = [], []
for _ in range(n):
    u, v = map(int, input().split())
    a.append(u)
    b.append(v)
a.sort()
b.sort(reverse = True)
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:
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, B = sorted(A), sorted(B)
m1 = A[N//2] + A[(N-1)//2]
m2 = B[N//2] + B[(N-1)//2]
print((m2 - m1) // (N % 2 + 1) + 1)

Submission status: Accepted

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

minlst=[0]*N
maxlst=[0]*N

for i in range(N):
    minlst[i]=part[i][0]
    maxlst[i]=part[i][1]
minlst.sort()
maxlst.sort()

if N%2==1:
    left=minlst[(N+1)//2-1]
    right=maxlst[(N+1)//2-1]
else:
    left=minlst[N//2-1]+minlst[N//2]
    right=maxlst[N//2-1]+maxlst[N//2]

print(right-left+1)

Submission status: Accepted

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

X_l = sorted([Ai for Ai, Bi in AB])
X_r = sorted([Bi for Ai, Bi in AB])

if N % 2 == 0:
    ans_min = X_l[N // 2 - 1] + X_l[N // 2]
    ans_max = X_r[N // 2 - 1] + X_r[N // 2]
else:
    ans_min = X_l[N // 2]
    ans_max = X_r[N // 2]

ans = ans_max - ans_min + 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    info = [tuple(map(int, input().split())) for _ in range(N)]

    A = [a for a, _ in info]
    B = [b for _, b in info]

    A.sort()
    B.sort()
    if N%2 == 0:
        min_median2 = A[N//2-1] + A[N//2]
        max_median2 = B[N//2-1] + B[N//2]
        ans = max_median2 - min_median2 + 1
    else:
        min_median2 = A[N//2]
        max_median2 = B[N//2]
        ans = max_median2 - min_median2 + 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 = list(map(int, input().split()))
  A[i] = a
  B[i] = b
  
A = sorted(A)
B = sorted(B)

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

MOD = 10 ** 9 + 7
# MOD = 998244353

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

A = [e[0] for e in P]
B = [e[1] for e in P]
A.sort()
B.sort()

if n%2 == 1:
    piv = (n+1)//2
    start = A[piv-1]
    end = B[-piv]
    # print(start, end)
    print(end - start + 1)
else:
    piv = n//2
    start = (A[piv-1] + A[piv])/2
    end = (B[-piv] + B[-piv-1])/2
    # print(start, end)
    print(int((end - start)*2 + 1))

Submission status: Accepted

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

A = sorted(ab[0] for ab in AB)
B = sorted(ab[1] for ab in AB)

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

Submission status: Accepted

Solution code:
N = int(input())
L = [tuple( map(int, input().split() ) ) for i in range(N) ]
#print(L)

A = [L[i][0] for i in range(N)]
A.sort()
B = [L[i][1] for i in range(N)]
B.sort()

if N % 2 == 1:
  m = A[(N-1)//2]
else: 
  m_2 = A[N//2 - 1] + A[N//2]
  
if N % 2 == 1:
  M = B[(N-1)//2]
else: 
  M_2 = B[N//2 - 1] + B[N//2]
  
  
# Conjecture
if N % 2 == 1:
  print(M-m+1)
else:
  print(M_2 - m_2 + 1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

A,B = zip(*AB)
A = list(A)
B = list(B)
A.sort()
B.sort()
medianA = statistics.median(A)
medianB = statistics.median(B)
if N%2 == 0:
    ans = (medianB-medianA)/0.5+1
else:
    ans = medianB-medianA+1
print(int(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.sort()
B.sort()
if N % 2 == 0:
    b = (B[N//2-1]+B[N//2])/2
    a = (A[N//2-1]+A[N//2])/2
    ans = (b-a)//0.5 +1
else:
    b = B[(N+1)//2-1]
    a = A[(N+1)//2-1]
    ans = b-a+1
print(int(ans))

Submission status: Accepted

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


MIN = [0] * N
MAX = [0] * N

for i in range(N):
  A, B = map(int, input().split())
  
  MIN[i] = A
  MAX[i] = B
  
min_ = median(MIN)
max_ = median(MAX)
d = max_ - min_

if N % 2 == 0:
  print(int(d * 2) + 1)
else:
  print(int(d)+1)

Submission status: Accepted

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

if n % 2 == 1:
    aa = adat[n//2]
    bb = bdat[n//2]
    print(bb-aa + 1)

else:
    aa1 = adat[n//2-1]
    aa2 = adat[n//2]
    bb1 = bdat[n//2-1]
    bb2 = bdat[n//2]
    #print(aa1,aa2,bb1,bb2)
    aaa = (aa1+aa2) / 2
    bbb = (bb1+bb2) / 2
    cnt = bbb - aaa
    cnt = cnt // 0.5
    print(int(cnt + 1))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
AB = np.array([lr() for _ in range(N)])
A = AB[:, 0]
B = AB[:, 1]
me_A = np.median(A)
me_B = np.median(B)
if N&1:
    answer = int(me_B - me_A) + 1
else:
    answer = int((me_B - me_A) / 0.5) + 1

print(answer)

Submission status: Accepted

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

if n % 2 == 1:
    a.sort()
    b.sort()
    idx = n//2
    print(b[idx]-a[idx]+1)
    exit()

a.sort()
b.sort()
mini = (a[n//2]+a[n//2-1])/2
maxi = (b[n//2]+b[n//2-1])/2
print(int((maxi-mini)*2+1))

Submission status: Accepted

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

if N % 2 == 0:
    n0 = N // 2 - 1
    n1 = n0 + 1
else:
    n0 = (N + 1) // 2 - 1
    n1 = n0

# Aの順
X.sort()
l0 = X[n0][0]
l1 = X[n1][0]
Ls, Lt = divmod(l0 + l1, 2)

# Bの順
X.sort(key=lambda x: x[1])
r0 = X[n0][1]
r1 = X[n1][1]
Rs, Rt = divmod(r0 + r1, 2)

e = 2 if N % 2 == 0 else 1
d = (Rs * e) - (Ls * e) + 1
cnt = d
cnt -= (Lt == 1)
cnt += (Rt == 1)
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]
if N % 2 == 1:
    p = sorted(a)[N // 2][0]
    q = sorted(a, key = lambda x: x[1])[N // 2][1]
    print(q - p + 1)
else:
    a = sorted(a)
    p1 = a[N // 2 - 1][0]
    p2 = a[N // 2][0]
    a = sorted(a, key = lambda x: x[1])
    q1 = a[N // 2 - 1][1]
    q2 = a[N // 2][1]
    print(q1 + q2 - p1 - p2 + 1)

Submission status: Accepted

Solution code:
import sys
import heapq
import random
import collections
import math

# available on Google, not available on Codeforces
# import numpy as np
# import scipy

def solve(grid):  # fix inputs here

    a,b = zip(*grid)
    a = sorted(a)
    b = sorted(b)
    
    if len(a)%2 == 1:
        return b[len(a)//2] - a[len(a)//2] + 1

    lb = (a[len(a)//2] + a[len(a)//2-1])/2
    ub = (b[len(a)//2] + b[len(a)//2-1])/2
    # print(lb,ub)
    return int(2 * (ub - lb) + 1)


def console(*args):  # the judge will not read these print statement
    print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return


# for case_num in range(int(input())):
    # read line as a string
    # strr = input()

    # read line as an integer
nrows = int(input())

# read one line and parse each word as a string
# lst = input().split()

# read one line and parse each word as an integer
# lst = list(map(int,input().split()))

# read matrix and parse as integers (after reading read nrows)
# lst = list(map(int,input().split()))
# nrows = lst[0]  # index containing information, please change
grid = []
for _ in range(nrows):
    grid.append(list(map(int,input().split())))

res = solve(grid)  # please change

# Google - case number required
# print("Case #{}: {}".format(case_num+1, res))

# Codeforces - no case number required
print(res)

Submission status: Accepted

Solution code:
n = int(input())

AB = []
for _ in range(n):
    ab = tuple(map(int, input().split()))
    AB.append(ab)

AB_A = sorted(AB, key=lambda x: x[0])
AB_B = sorted(AB, key=lambda x: x[1])

if n%2==1:
    sa, sb = AB_A[n//2]
    la, lb = AB_B[n//2]
    print(lb - sa + 1)
else:
    sal, sbl = AB_A[n//2-1]
    sar, sbr = AB_A[n//2]

    lal, lbl = AB_B[n//2-1]
    lar, lbr = AB_B[n//2]
    print((lbl + lbr) - (sal + sar) +1)

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

Submission status: Accepted

Solution code:
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:
    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:
N = int(input())
A, B = [], []
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)
A.sort()
B.sort()
if N%2 != 0:
    k = int(N/2)
    ans = B[k] - A[k] + 1
else:
    k = int(N/2)
    a, b = A[k]+A[k-1], B[k]+B[k-1]
    ans = b - a + 1
print(ans)

Submission status: Accepted

Solution code:
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()
B.sort()
medmin=0
medmax=0
if N%2==0:
    medmin=(A[-1+N//2]+A[N//2])
    medmax=(B[-1+N//2]+B[N//2])
else:
    medmin=A[(N-1)//2]
    medmax=B[(N-1)//2]
print(medmax-medmin+1)
#print(medmax,medmin)

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(input())
    al = []
    ar = []
    bl = []
    br = []
    for i in range(n):
        a,b = map(int,ipt().split())
        ali = heapq.heappushpop(al,-a)
        ari = heapq.heappushpop(ar,a)
        heapq.heappush(ar,-ali)
        heapq.heappush(al,-ari)
        bli = heapq.heappushpop(bl,-b)
        bri = heapq.heappushpop(br,b)
        heapq.heappush(br,-bli)
        heapq.heappush(bl,-bri)
    ali = -heapq.heappop(al)
    ari = heapq.heappop(ar)
    bli = -heapq.heappop(bl)
    bri = heapq.heappop(br)
    amed2 = ali+ari
    bmed2 = bri+bli
    if n%2 == 0:
        print(bmed2-amed2+1)
    else:
        print((bmed2-amed2)//2+1)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

    minimum.append(a)
    maximum.append(b)

# 奇数の場合は？

minimum.sort()
maximum.sort()

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

Submission status: Accepted

Solution code:
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N)]
mnl = [ab[i][0] for i in range(N)]
mxl = [ab[i][1] for i in range(N)]
mnl.sort()
mxl.sort()

if N % 2:
  mn = mnl[N//2]
  mx = mxl[N//2]
  print(mx-mn+1)
else:
  mn = (mnl[N//2-1]+mnl[N//2])/2
  mx = (mxl[N//2-1]+mxl[N//2])/2
  d = mx-mn
  print(int(mx-mn+1+d))

Submission status: Accepted

Solution code:
from operator import itemgetter, attrgetter
N, *A = map(int, open(0).read().split())
*A, = zip(*[iter(A)]*2)

if N % 2 == 0:
    A.sort()
    mid_l = A[N//2-1][0]+A[N//2][0]
    A.sort(key=itemgetter(1))
    mid_r = A[N//2-1][1]+A[N//2][1]
    print(mid_r - mid_l+1)
else:
    A.sort()
    mid_l = A[N//2][0]
    A.sort(key=itemgetter(1))

    mid_r = A[N//2][1]
    print(mid_r - mid_l+1)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for _ in range(n):
    atmp, btmp = map(int, input().split())
    a.append(atmp)
    b.append(btmp)
a.sort()
b.sort()
if n % 2 == 1:
    pos = n // 2
    print(b[pos] - a[pos] + 1)
else:
    pos = n // 2 - 1
    a1 = a[pos]
    a2 = a[pos + 1]
    b1 = b[pos]
    b2 = b[pos + 1]
    medmin = a1 + a2
    medmax = b1 + b2
    print(medmax - medmin + 1)

Submission status: Accepted

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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

#処理内容
def main():
	N = int(input())
	A = []
	B = []
	for i in range(N):
		a, b = getlist()
		A.append(a)
		B.append(b)

	A.sort()
	B.sort()

	if N % 2 == 0:
		M = int(N // 2)
		L = A[M - 1] + A[M]
		R = B[M - 1] + B[M]
		ans = R - L + 1
		print(ans)

	else:
		M = int(N // 2)
		L = A[M]
		R = B[M]
		ans = R - L + 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[i], B[i] = map(int, input().split())

def median(X):
  n = len(X)
  X = sorted(X)
  if len(X)%2==0:
    return (X[n//2-1]+X[n//2])*.5
  else:
    return X[n//2]

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,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==0:
    x=[a[n//2-1],a[n//2]]
    y=[b[n//2-1],b[n//2]]
    print(sum(y)-sum(x)+1)
else:
    x=a[n//2]
    y=b[n//2]
    print(y-x+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

def solve():
    input = sys.stdin.readline
    N = int(input())
    X = [[int(a) for a in input().split()] for _ in range(N)]
    X.sort()
    lower = 0
    if N % 2 == 1: lower = X[N//2][0]
    else: lower = X[(N-1)//2][0] + X[N//2][0]

    X.sort(key = itemgetter(1))
    higher = 0
    if N % 2 == 1: higher = X[N//2][1]
    else: higher = X[(N-1)//2][1] + X[N//2][1]

    print(higher - lower + 1)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from operator import itemgetter
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
l.sort()
if n%2==1:
  x=l[n//2][0]
  l=sorted(l,key=itemgetter(1))
  y=l[n//2][1]
  print(y-x+1)
else:
  x=(l[n//2][0]+l[(n//2)-1][0])/2
  l=sorted(l,key=itemgetter(1))
  y=(l[n//2][1]+l[(n//2)-1][1])/2
  print(int((y-x)*2)+1)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

from statistics import median

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

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def rangeI(it, l, r):
    for i, e in enumerate(it):
        if l <= i < r:
            yield e
        elif l >= r:
            break

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

INF = 999999999999999999999999
MOD = 10**9+7

n = int(input())
AB = []
for _ in range(n):
    AB.append(get_nums_l())
A = [ ab[0] for ab in AB ]
B = [ ab[1] for ab in AB ]

if n%2 == 1:
    min_of_med = median(A)
    max_of_med = median(B)
    print(max_of_med - min_of_med + 1)

else:
    A.sort()
    B.sort()
    min_of_med = A[n//2 - 1] + A[n//2]
    max_of_med = B[n//2 - 1] + B[n//2]
    print(max_of_med - min_of_med + 1)

Submission status: Accepted

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

A,B = zip(*AB)
A = sorted(list(A))
B = sorted(list(B))


if N % 2 == 1:
    mn = A[N//2]
    mx = B[N//2]
    print(mx-mn+1)
else:
    mn1 = 2*A[N//2-1]
    mn2 = 2*A[N//2]
    mn = (mn2+mn1)//2
    mx1 = 2*B[N//2-1]
    mx2 = 2*B[N//2]
    mx = (mx2+mx1)//2
    print(mx-mn+1)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
import itertools
# import math
# import networkx as nx
from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
AB=n3(n)
A=[x[0] for x in AB]
B=[x[1] for x in AB]

A.sort()
B.sort()

if n%2==0:
    a1,a2=A[n//2-1],A[n//2]
    b1,b2=B[n//2-1],B[n//2]
    a=(a1+a2)/2
    b=(b1+b2)/2
    ans=int((b-a+0.5)*2)
else:
    a=A[n//2]
    b=B[n//2]
    ans=b-a+1
print(ans)

Submission status: Accepted

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

N = int(input())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
As, Bs = zip(*pairs)

if N % 2 == 0:
    sA = sorted(As)
    medA2 = sA[N//2-1] + sA[N//2]
    sB = sorted(Bs)
    medB2 = sB[N//2-1] + sB[N//2]
    print(medB2 - medA2 + 1)
else:
    sA = sorted(As)
    medA = sA[N//2]
    sB = sorted(Bs)
    medB = sB[N//2]
    print(medB - medA + 1)

Submission status: Accepted

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

Submission status: Accepted

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


import numpy as np

if n%2 == 0:
    print(int((np.median(b) - np.median(a)) / 0.5) + 1)
else:
    print(int((np.median(b) - np.median(a))) + 1)

Submission status: Accepted

Solution code:
n = int(input())

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

ab.sort()

if n % 2 == 1:
    m = (n-1) // 2
    l = ab[m][0]

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

    m = (n-1) // 2
    r = ab[m][1]
    print(r - l + 1)
else:

    m = n // 2
    l1 = ab[m-1][0]
    l2 = ab[m][0]

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

    m = n // 2
    r1 = ab[m][1]
    r2 = ab[m-1][1]
    print((r1+r2)-(l1+l2)+1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*ab = map(int,read().split())


a = []
b = []
M = iter(ab)
for ai,bi in zip(M,M):
    a.append(ai)
    b.append(bi)

a.sort()
b.sort()

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


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

Submission status: Accepted

Solution code:
n = int(input())
bounds = [list(map(int, input().split())) for _ in range(n)]
upper = sorted([b[1] for b in bounds])
lower = sorted([b[0] for b in bounds])

if n % 2 == 0:
	u_mid2 = upper[n // 2 - 1] + upper[n // 2]
	l_mid2 = lower[n // 2 - 1] + lower[n // 2]
	print(u_mid2 - l_mid2 + 1)
else:
	print(upper[n // 2] - lower[n // 2] + 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.sort()
B.sort()

if N%2==1:
    print(B[(N-1)//2] - A[(N-1)//2]+1)
else:
    b1 = B[(N-2)//2]
    b2 = B[N//2]
    a1 = A[(N-2)//2]
    a2 = A[N//2]
    print(b1 + b2 - (a1 + a2) + 1)
    #print(a1,a2,b1,b2)

Submission status: Accepted

Solution code:
from __future__ import division, print_function

''' Hey stalker :) '''
INF = 10 ** 10
TEST_CASES = False

def main():
    n = get_int()
    starts = []
    ends = []
    for i in range(n):
        x, y = get_list()
        starts.append(x)
        ends.append(y)
    starts.sort()
    ends.sort()
    flo = n//2
    if n%2==0:
        a = starts[flo] + starts[flo-1]
        b = ends[flo] + ends[flo-1]
    else: a,b = starts[flo], ends[flo]
    print(b-a+1)


''' FastIO Footer: PyRival Library, Thanks @c1729 and contributors '''
import os
import sys
from bisect import bisect_left, bisect_right
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
# else:
#     sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))

if __name__ == "__main__":
    if TEST_CASES:
        [main() for _ in range(int(input()))]
    else:
        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(reverse=True)
if n%2 == 1:
    ans = B[(n+1)//2-1]-A[(n+1)//2-1]+1
    print(ans)
else:
    y = A[n//2-1] + A[n//2]
    x = B[n//2-1] + B[n//2]
    ans = x-y+1
    print(ans)

Submission status: Accepted

Solution code:
import sys

input=sys.stdin.readline

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:
    k=N//2
    A.sort()
    B.sort()
    small=A[k]
    large=B[k]
    print(large-small+1)

else:
    k=N//2
    A.sort()
    B.sort()
    small=A[k-1]+A[k]
    large=B[k-1]+B[k]
    print(large-small+1)

Submission status: Accepted

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
left = sorted([i for i,j in ab])
right = sorted([j for i,j in ab])
if n%2 == 0:
    x = left[n//2] + left[n//2-1]
    y = right[n//2] + right[n//2-1]
    ans = y-x+1
else:
    x = left[n//2]
    y = right[n//2]
    ans = y-x+1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    ab.sort(key=lambda x: x[0])
    if n % 2 == 0:
        median_min1 = ab[n//2-1][0]
        median_min2 = ab[n//2][0]
    else:
        median_min1 = ab[n//2][0]
        median_min2 = ab[n//2][0]

    ab.sort(key=lambda x: x[1])
    if n % 2 == 0:
        median_max1 = ab[n//2-1][1]
        median_max2 = ab[n//2][1]
    else:
        median_max1 = ab[n//2][1]
        median_max2 = ab[n//2][1]

    if n % 2 == 0:
        ans = (median_max1 + median_max2) - (median_min1 + median_min2) + 1
    else:
        ans = median_max1 - median_min1 + 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import statistics
n=int(input())
a=list()
b=list()
for i in range(n):
    x,y=map(int,input().split())
    a.append(x)
    b.append(y)

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