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:
def mp(): return map(int, input().split())
def lmp(): return list(map(int, input().split()))

n = int(input())
a_ar = []
b_ar = []
for i in range(n):
    a, b = mp()
    a_ar.append(a)
    b_ar.append(b)
a_ar.sort()
b_ar.sort()

if n % 2 == 1:
    print(b_ar[n//2]-a_ar[n//2]+1)
else:
    l = (a_ar[n//2-1] + a_ar[n//2])/2
    r = (b_ar[n//2-1] + b_ar[n//2])/2
    print(int((r-l)*2+1))

Submission status: Accepted

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

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	n = int(sys.stdin.readline())
	a = []
	b = []
	for i in range(n):
		ai, bi = map(int, sys.stdin.readline().split())
		a.append(ai)
		b.append(bi)
	a_sort = sorted(a)
	if n % 2 == 0:
		min_center = (a_sort[int(n/2)-1] + a_sort[int(n/2)])/2
	else:
		min_center = a_sort[int((n+1)/2)-1]
	b_sort = sorted(b)
	if n % 2 == 0:
		max_center = (b_sort[int(n/2)-1] + b_sort[int(n/2)])/2
	else:
		max_center = b_sort[int((n+1)/2)-1]
	log.print(max_center, min_center)
	if n % 2 == 0:
		print(int((max_center - min_center)*2 + 1))
	else:
		print(int(max_center - min_center + 1))


	
	


main()

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,b = map(int,input().split())
    A[i] = a
    B[i] = b
A.sort()
B.sort()
if N%2:
    print(B[N//2]-A[N//2]+1)
else:
    print(B[N//2]+B[(N-1)//2]-A[N//2]-A[(N-1)//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(reverse=True)
#print(A,B)
if N % 2 == 1:
    x = (N+1) // 2
    ans = B[x-1] - A[x-1] + 1
else:
    x = N // 2
    ans = (B[x-1] + B[x]) - (A[x] + A[x-1]) + 1
print(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.sort()
B.sort()

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

print(mid_max-mid_min+1)

Submission status: Accepted

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

if n%2 == 0:
    ta = (listA[n//2]+listA[n//2-1])
    tb = (listB[n//2]+listB[n//2-1])
else:
    ta = listA[n//2]
    tb = listB[n//2]

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): 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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    ab=LLI(n)
    mxs=[]
    mns=[]
    for a,b in ab:
        if a<b:mns.append(a);mxs.append(b)
        else:mns.append(b);mxs.append(a)
    mns.sort()
    mxs.sort()
    #print(mns)
    #print(mxs)

    if n%2:
        ans=mxs[n//2]-mns[n//2]+1
    else:
        l=mns[n//2]+mns[n//2-1]
        r=mxs[n//2]+mxs[n//2-1]
        ans=r-l+1

    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
import random
from decimal import Decimal
import itertools
import re
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(2147483647)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def main():
	n = I()
	A = np.zeros(n)
	B = np.zeros(n)
	for i in range(n):
			A[i], B[i] = LI()
	if n % 2 == 0:
		print(int((np.median(B)-np.median(A))*2+1))
	else:
		print(int(np.median(B)-np.median(A)+1))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(20000000)
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:
    ans = 0
    i = n//2-1
    j = n//2
    tmp = [[a[i], b[i]], [a[j], b[j]]]
    ans = (b[i] + b[j]) - (a[i] + a[j]) + 1
    print(ans)
else:
    mid_l = a[n//2]
    mid_r = b[n//2]
    print(mid_r - mid_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:
    mid_ind = N//2
    lower = A[mid_ind]
    upper = B[mid_ind]
    print(upper-lower+1)

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

Submission status: Accepted

Solution code:
N=int(input())
A=[list(map(int, input().split())) for _ in range(N)]
L,R=zip(*A)
L=sorted(L)
R=sorted(R)[::-1]
if N%2==1:
  #print(R[N//2],L[N//2])
  print(R[N//2]-L[N//2]+1)
else:
  r1,r2=R[N//2-1],R[N//2]
  l1,l2=L[N//2-1],L[N//2]
  #print(r1,r2,l1,l2)
  print((r1+r2)-(l1+l2)+1)

Submission status: Accepted

Solution code:
N = int(input())

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

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

m = int((N + 1) / 2 if N % 2 == 1 else N / 2)

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

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

amid=0
bmid=0
if n%2==0:
    amid=(A[round(n/2-1)]+A[round(n/2)])/2
    bmid=(B[round(n/2-1)]+B[round(n/2)])/2
else:
    amid=A[round((n-1)/2)]
    bmid=B[round((n-1)/2)]

d=bmid-amid

if n%2==0:
    print(round(2*d)+1)
else:
    print(d+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())
import statistics
X = statistics.median(a)
Y = statistics.median(b)
if N%2==1:
    print(Y-X+1)
else:
    print(int((2*(Y-X)+1)))

Submission status: Accepted

Solution code:
n=int(input())
a,b=[0]*n,[0]*n
for i in range(n):
  a[i],b[i]=map(int,input().split())
a.sort()
b.sort()
if n%2==1:
  print(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 i in range(n):
    a,b=map(int, input().split())
    A.append(a)
    B.append(b)

if n%2==1:
    sortA=sorted(A)
    mA=sortA[(n-1)//2]
    sortB=sorted(B)
    mB=sortB[(n-1)//2]
    #print(mA,mB)
    print(abs(mB-mA)+1)
else:
    sortA=sorted(A)
    mA1=sortA[(n-1)//2]
    mA2=sortA[n//2]
    mA=mA1+mA2
    sortB=sorted(B)
    mB1=sortB[(n-1)//2]
    mB2=sortB[n//2]
    mB=mB1+mB2
    #print(mA,mB1,mB2)
    print(abs(mB-mA)+1)

Submission status: Accepted

Solution code:
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)
a.sort()
b.sort()


def odd():
    left = a[n//2]
    right = b[-(1 + n//2)]
    return right - left + 1


def even():
    left_x = a[(n//2) - 1]
    right_x = b[-(n//2 + 1)]
    left_y = a[n//2]
    right_y = b[-n//2]
    x_range = right_x - left_x + 1
    y_range = right_y - left_y + 1
    return x_range + y_range - 1


def solve():
    if n % 2 == 1:
        return odd()
    return even()

print(solve())

Submission status: Accepted

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

As.sort()
Bs.sort()

if N % 2 == 1:
  i = (N+1) // 2
  print(Bs[i-1] - As[i-1] + 1)
else:
  lower = N // 2
  upper = lower + 1
  
  lower_sum = As[lower-1] + As[upper-1]
  upper_sum = Bs[lower-1] + Bs[upper-1]
  
  print(upper_sum - lower_sum + 1)

Submission status: Accepted

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


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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
ab.sort(key=lambda x:x[0])
a=[a for a,b in ab]
b=[b for a,b in ab]
a.sort()
b.sort()
l=0
if n%2==1:
  print(b[n//2]-a[n//2]+1)
  #print(b[n//2],a[n//2])
else:
  am=(a[n//2]+a[n//2-1])
  bm=(b[n//2]+b[n//2-1])
  print(bm-am+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()
ans=0
if(N%2==1):
  print(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:
def main():
    N = int(input())
    A = list()
    B = list()
    for _ 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:
        mid_a_2 = A[N // 2] + A[(N // 2) - 1]
        mid_b_2 = B[N // 2] + B[(N // 2) - 1]
        print(mid_b_2 - mid_a_2 + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())[:-1]
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [list(input())[:-1] for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
N = int(input())
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
AA = sorted(A)
BB = sorted(B)
if N % 2 == 1:
    MIN = AA[N//2]
    MAX = BB[N//2]
    print(MAX-MIN+1)
else:
    MIN1 = AA[N//2 -1]
    MIN2 = AA[N//2]
    MAX1 = BB[N//2-1]
    MAX2 = BB[N//2]
    MIN = MIN1+MIN2
    MAX = MAX1+MAX2
    print(MAX-MIN+1)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    n = II()
    # ab = [MII() for _ in range(n)]
    # ab.sort(key=lambda x: (x[0], x[1]))
    aa = []
    bb = []
    for _ in range(n):
        a, b = MII()
        aa.append(a)
        bb.append(b)
    aa.sort()
    bb.sort()
    if n%2==1:
        l1, r1 = aa[n//2], bb[n//2]
        print(r1-l1+1)
    else:
        l1, l2 = aa[n//2-1], aa[n//2]
        r1, r2 = bb[n//2-1], bb[n//2]
        print((r1+r2)-(l1+l2)+1)
    # if n%2==1:
        # l1, r1 = ab[n//2][0], ab[n//2][-1]
        # print(r1-l1+1)
    # else:
        # l1, r1 = ab[(n//2)-1][0], ab[(n//2)-1][-1]
        # l2, r2 = ab[(n//2)][0], ab[(n//2)][-1]
        # print((r1+r2)-(l1+l2)+1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
AB = [tuple(map(int, input().split())) for i in range(n)]
X = sorted(AB, key=lambda x: x[0])
Y = sorted(AB, key=lambda x: x[1])
if n % 2 == 0:
    print((Y[n // 2 - 1][1] + Y[n // 2][1]) -
          (X[n // 2 - 1][0] + X[n // 2][0]) + 1)
else:
    print(Y[n // 2][1] - X[n // 2][0] + 1)

Submission status: Accepted

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

minis.sort()
maxis.sort()

if(n%2 == 0):
    bottom = (minis[n//2-1] + minis[n//2])
    top = (maxis[n//2-1] + maxis[n//2])
    if(top >= bottom):
        print(top - bottom + 1)
    else:
        print('0')
if(n%2 == 1):
    bottom = minis[(n-1)//2]
    top = maxis[(n-1)//2]
    if(top >= bottom):
        print(top - bottom + 1)
    else:
        print('0')

Submission status: Accepted

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
MOD = 10**9+7

N = int(input())

a = []
for i in range(N):
    ab = list(map(int,(input().split())))
    a.append(ab)

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

Submission status: Accepted

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

    A.sort()
    B.sort()
    mi = 0
    ma = 0
    if N % 2 ==0:
        mi = (A[(N-1)//2]+A[((N-1)//2)+1])/2
        ma = (B[(N-1)//2]+B[((N-1)//2)+1])/2
        print(int(((ma-mi)*2)+1))

    else :
        mi = A[N//2]
        ma = B[N//2]
        print(int(ma-mi+1))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
li_a = []
li_b = []
for _ in range(n):
    a, b = map(int, input().split())
    li_a.append(a)
    li_b.append(b)
li_a.sort()
li_b.sort()
if n%2 == 1:
    min_c = li_a[(n-1)//2]
    max_c = li_b[(n-1)//2]
    print(max_c-min_c+1)
else:
    min_c = (li_a[(n//2)-1] + li_a[(n//2)])/2
    max_c = (li_b[(n//2)-1] + li_b[(n//2)])/2
    print(int((max_c-min_c)*2+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)
    B.append(b)
A = np.array(A)
B = np.array(B)
p = np.median(A)
q = np.median(B)
if N % 2 == 0:
    p = int(p * 2)
    q = int(q * 2)
else:
    p = int(p)
    q = int(q)

ans = q - p + 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
A = []
B = []
for i in range(n):
    a,b = map(int,readline().split())
    A.append(a)
    B.append(b)
A.sort()
B.sort()
#奇数であると前提してまずは実装しきっちゃう
if not even(n):
    lower = A[n//2]
    upper = B[n//2]
    print(upper-lower+1)
else:
    l1 = A[n//2-1]
    l2 = A[n//2]
    u2 = B[n//2]
    u1 = B[n//2-1]

    a = u1-l1+1
    b = abs(u2-l2)+1
    print(a+b-1)

Submission status: Accepted

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

la.sort()
lb.sort(reverse=True)

if n % 2 == 1:
    i = n // 2 + 1
    md = la[i - 1]
    mu = lb[i - 1]
    answer = mu - md + 1
else:
    i = n // 2
    md2 = la[i - 1] + la[i]
    mu2 = lb[i - 1] + lb[i]
    answer = (mu2 - md2) + 1

print(answer)

Submission status: Accepted

Solution code:
import statistics
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)
a_median = statistics.median(a_list)
b_median = statistics.median(b_list)
if n % 2 == 0:
  c = int((b_median - a_median)*2) + 1
else:
  c = int(b_median - a_median) + 1
print(c)

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)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def median2(lst):
    lst.sort()
    l = len(lst)
    if l % 2:
        return lst[(l - 1)//2]
    else:
        return lst[l // 2 - 1] + lst[l // 2]
        

def submit():
    n = int(input())
    a = list()
    b = list()
    
    for _ in range(n):
        ae, be = map(int, input().split())
        a.append(ae)
        b.append(be)

    m1 = median2(a)
    m2 = median2(b)
    print(m2 - m1 + 1)


submit()

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
ranges = []

def medium(nList):
    nList.sort()
    q = deque(nList)
    while len(q) > 2:
        q.pop()
        q.popleft()
    if len(q) == 2:
        return (q[0] + q[1]) / 2
    else:
        return q[0]

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

m_min = medium(list(map(lambda r: r[0], ranges)))
m_max = medium(list(map(lambda r: r[1], ranges)))
d = m_max - m_min
if n % 2 == 0:
    print(int(d * 2) + 1)
else:
    print(d + 1)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    if N % 2 == 1:
        left = statistics.median(A)
        right = statistics.median(B)
        print(right - left + 1)

    else:
        left = statistics.median(A)
        right = statistics.median(B)
        print(int((right - left) * 2 + 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 statistics as st
import math
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
  A[i] , B[i] = map(int,input().split())
mA = st.median(A)
mB = st.median(B)
if N % 2 == 0:
  ans = (mB - mA)*2 + 1
else:
  ans = mB - mA + 1
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np


def main(N, A, B):
    min = np.median(A)
    max = np.median(B)
    if N % 2 == 1:
        return int(max-min+1)
    else:
        return int((max-min)*2+1)
    print(min, max)
    return 0


N = int(input())
A = np.zeros((N), np.int32)
B = np.zeros((N), np.int32)
for i in range(N):
    A[i], B[i] = map(int, input().split())
print(main(N, A, B))

Submission status: Accepted

Solution code:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n=int(input())
    s = [tuple(map(int, input().split())) for i in range(n)]
    a = sorted(i[0] for i in s)
    b = sorted(i[1] for i in s)
    if len(s) % 2 == 1:
        lft = a[n//2]
        rgt = b[n//2]
        print(rgt - lft + 1)
        return
    lft = a[n//2] + a[n//2-1]
    rgt = b[n//2] + b[n//2-1]
    print(rgt - lft + 1)

main()

Submission status: Accepted

Solution code:
def LI():return list(map(int,input().split()))
def yes():return print("Yes")
def no():return print("No")
from collections import deque, defaultdict, Counter
# from heapq import heappop, heappush
# import math
# from decimal import Decimal

# def divisors(x):
#     ret=[]
#     for i in range(1,int(x**.5)+1):
#         if x%i==0:
#             ret.append(i)
#             if x//i==i:continue
#             ret.append(x//i)
#     return sorted(ret)

# print(divisors(64),2**6)            

# def prime_factor(n):
#     ass = []
#     for i in range(2,int(n**0.5)+1):
#         while n % i==0:
#             ass.append(i)
#             n = n//i
#     if n != 1:
#         ass.append(n)
#     return ass

# n=int(input())
# pf=prime_factor(n)
# pf=Counter(pf)
# # print(pf)

# def cng(x):
#     le=2*x
#     ret=-1
#     mi=int(le**0.5)
#     for i in range(mi-1,mi+10000):
#         # print((i+1)*(i+2),le,i*(i+1))
#         if (i+1)*(i+2)>le>=i*(i+1):
#             ret=i
#             # print((i+1)*(i+2),le,i*(i+1))
#             break
        
#     return ret

# mp=[]   
# for i in range(2*10**5):
#     mp.append(cng(i))
# # print(-1 in mp)

# ans=0
# for i in pf.values():
#     ans+=mp[i]
    
# print(ans)
# import numpy as np

n=int(input())
a=[]
b=[]
# ab=[]
for _ in range(n):
    c,d=LI()
    a.append(c)
    b.append(d)
    # ab.append((c,d))
    
a.sort()
b.sort()
# print(a,b)

if n%2==1:
    # 奇数
    t=(n+1)//2
    ans=b[t-1]-a[t-1]+1

else:
    l=n//2
    r=l+1
    
    aa=(a[l-1]+a[r-1])
    bb=(b[l-1]+b[r-1])
    ans=bb-aa+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.sort()
B.sort()
if n%2==1:
    u=(n+1)//2-1 
    print(B[u]-A[u]+1)
else:
    u=n//2-1
    w=u+1 
    print(B[u]+B[w]-A[u]-A[w]+1)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for _ in range(n):
  _a, _b = map(int, input().split())
  a.append(_a)
  b.append(_b)
  
if n % 2 != 0:
  import numpy as np
  a_med = int(np.median(a))
  b_med = int(np.median(b))
  print(b_med - a_med + 1)
elif n == 2:
  print(sum(b) - sum(a) + 1)
else:
  a = sorted(a)[n//2-1:n//2+1]
  b = sorted(b)[n//2-1:n//2+1]
  print(sum(b) - sum(a) + 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 // 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())
left = []
right = []

for i in range(n):
    array = list(map(int, input().split()))
    left.append(array[0])
    right.append(array[1])

left.sort()
right.sort()

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

Submission status: Accepted

Solution code:
n = int(input())
l1 = []
l2 = []
for i in range(n):
    a, b = map(int, input().split())
    l1.append(a)
    l2.append(b)
l1.sort()
l2.sort()
if n % 2:
    t0 = l1[n // 2]
    t1 = l2[n // 2]
    print(t1 - t0 + 1)
else:
    x1 = l1[n // 2]
    x0 = l1[(n - 1) // 2]
    y1 = l2[n // 2]
    y0 = l2[(n - 1) // 2]
    if x0 > x1: x0, x1 = x1, x0
    if y0 > y1: y0, y1 = y1, y0
    print(y1 - x1 + 1 + y0 - x0)
    # tmp1 = l[(n - 1) // 2]
    # tmp2 = l[n // 2]
    # print(tmp2[1] - tmp2[0] + 1 + tmp1[1] - tmp1[0])

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:
  M = B[n//2 - 1] + B[n//2]
  m = A[n//2 - 1] + A[n//2]
  ans = M-m+1
  print(ans)

Submission status: Accepted

Solution code:
n=int(input())

a=[]
b=[]

for i in range(n):
    aa,bb=list(map(int,input().split()))
    a.append(aa*2)
    b.append(bb*2)

a.sort()
b.sort()

if n%2==1:
    amed=a[(n-1)//2]
    bmed=b[(n-1)//2]

    ans=(bmed-amed)//2+1
else:
    amed=(a[n//2-1]+a[n//2])/2
    bmed = (b[n // 2 - 1] + b[n // 2]) / 2

    ans=bmed-amed+1

print(int(ans))

Submission status: Accepted

Solution code:
import statistics

N = int(input())
A = []
B = []
for i in range(N):
    ab = list(map(int, input().split()))
    A.append(ab[0])
    B.append(ab[1])
if N % 2 == 0:
    o = int((statistics.median(B) - statistics.median(A)) * 2 + 1)
else:
    o = int(statistics.median(B) - statistics.median(A) + 1)
print(o)

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:
  al=A[(N-1)//2]
  ar=A[N//2]
  bl=B[(N-1)//2]
  br=B[N//2]
  print(int(((br+bl)/2-(ar+al)/2)*2+1))
else:
  mini=A[(N-1)//2]
  maxi=B[(N-1)//2]
  print(maxi-mini+1)

Submission status: Accepted

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

if n%2 == 1:
  lst.sort()
  m = lst[n//2][0]
  lst.sort(key=lambda x: x[1])
  M = lst[n//2][1]
  print(M-m+1)
  
else:
  lst.sort()
  p, q  = lst[n//2-1][0], lst[n//2][0]
  m = p+q
  lst.sort(key=lambda x: x[1])
  P, Q  = lst[n//2-1][1], lst[n//2][1]
  M = P+Q
  print(M-m+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=sorted(a)
b=sorted(b,reverse=True)
if n%2==1:
    x=1+n//2
    print(b[x-1]-a[x-1]+1)
else:
    x=n//2
    print(b[x-1]+b[x]-a[x-1]-a[x]+1)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.buffer.readline

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:
    saisho = A[(n - 1) // 2]
    saidai = B[(n - 1) // 2]
    print(saidai - saisho + 1)
else:
    saisho1 = A[n // 2 - 1]
    saisho2 = A[n // 2]
    saidai1 = B[n // 2 - 1]
    saidai2 = B[n // 2]
    saidai = saidai2 + saidai1
    saisho = saisho2 + saisho1
    delta = saidai - saisho
    print(delta + 1)

Submission status: Accepted

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

sop = sorted(p)
soq = sorted(q)
ans = 0
if n % 2 == 1:
    ans = soq[(n+1)//2-1] - sop[(n+1)//2-1] + 1
elif n % 2 == 0:
    s = soq[n//2-1]+soq[n//2]
    t = sop[n//2-1]+sop[n//2]
    ans = (s*0.5-t*0.5+1)*2 - 1
print(int(ans))

Submission status: Accepted

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

def main():
    N = int(input())
    a = [];b = []
    for _ in range(N):
        x,y = map(int,input().split())
        a.append(x)
        b.append(y)
    
    a.sort()
    b.sort()
    if N%2 == 0:
        l = (a[N//2]+a[N//2-1])*2
        r = (b[N//2]+b[N//2-1])*2
        print((r-l)//2+1)
    else:
        l = a[N//2]
        r = b[N//2]
        print(r-l+1)

if __name__ == "__main__":
    main()