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())
a = []
b = []
for i in range(n):
	a_1, b_1 = input().split()
	a.append(int(a_1))
	b.append(int(b_1))
a.sort()
b.sort()
is_odds = n % 2 == 0
a_med = a[(n + 1) // 2 - 1] if not is_odds else a[n // 2 - 1] + a[n // 2]
b_med = b[(n + 1) // 2 - 1] if not is_odds else b[n // 2 - 1] + b[n // 2]
print(b_med - a_med + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
	a_1, b_1 = input().split()
	a.append(int(a_1))
	b.append(int(b_1))
a.sort()
b.sort()
is_odds = n % 2 == 0
a_med = a[(n + 1) // 2 - 1] if not is_odds else (a[n // 2 - 1] + a[n // 2]) / 2
b_med = b[(n + 1) // 2 - 1] if not is_odds else (b[n // 2 - 1] + b[n // 2]) / 2
from_a = a_med if not is_odds else int(a_med * 2)
to_b = b_med if not is_odds else int(b_med * 2)
print(to_b - from_a + 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()

I=~-N//2
print(B[I]-A[I]+1 + (B[I+1]-A[I+1] if N%2==0 else 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!=0:
  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
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')


def main():
	n=II()
	A=[]
	B=[]
	for _ in range(n):
		a,b=MI()
		A.append(a)
		B.append(b)
	A.sort()
	B.sort()
	#print(A,B)

	r=n//2
	l=(n-1)//2
	#print(l,r)

	mi=A[l]+A[r]
	ma=B[l]+B[r]

	ans=(ma-mi)-(ma-mi)*(n%2)//2+1
	print(ans)







if __name__ == "__main__":
	main()

Submission status: Accepted

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

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())
AB = []
A = [0]*n
B = [0]*n
for i in range(n):
  A[i],B[i] = map(int,input().split())  

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

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])/2
  r = (B[n//2-1]+B[n//2])/2
  print(int((r-l)*2+1))

Submission status: Accepted

Solution code:
import statistics


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

    amed = statistics.median(a)
    bmed = statistics.median(b)

    if n % 2:
        print(int(bmed - amed + 1))
    else:
        print(int((bmed - amed) * 2 + 1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A,B = [],[]
    for i in range(n):
        a,b = map(int,input().split())
        A.append(a)
        B.append(b)
    if n%2==1:
        a_ = sorted(A)[(n-1)//2]
        b_ = sorted(B)[(n-1)//2]
        print(b_-a_+1)
    else:
        A,B = sorted(A),sorted(B)
        a_ = (A[n//2]+A[n//2-1])/2
        b_ = (B[n//2]+B[n//2-1])/2
        print(int(2*(b_-a_))+1)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue
import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = []
    b = []
    for _ in range(n):
        ai, bi = ns()
        a.append(ai)
        b.append(bi)

    a.sort()
    b.sort()

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ba = []
for i, j in ab:
    ba.append([j, i])

ab.sort()
ba.sort(reverse = True)

if N % 2 == 1:
    n = (N+1) // 2 - 1
    ans = ba[n][0] - ab[n][0] + 1
    
else:
    n = N // 2 - 1
    ans = (ba[n][0] + ba[n+1][0]) - (ab[n][0] + ab[n+1][0]) + 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()

youso = int(n/2)

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

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

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 :
    l = a[int(n/2)]
    u = b[int(n/2)]
    ans = u - l + 1
else:
    l = a[int(n/2-1)] + a[int(n/2)]
    u = b[int(n/2-1)] + b[int(n/2)]
    ans = u - l +1

print(ans)

Submission status: Accepted

Solution code:
# 平均ではなく中央値だから
# それぞれの集合の中央値が
# 第一四分位数と第三四分位数になり中央値はその間全てありうる
def median(q):
    # 例題1だと2種
    l=len(q)
    q=sorted(q,reverse=True)
    # qの出力結果[2, 1][3, 2]
    if l%2==0:
        # q0とq1を足して2でわる　4の場合q2とq3
        return (q[int(l/2)]+q[int(l/2)-1])/2
    else:
        # 3の場合q1、5の場合q２真ん中を出す
        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)
# 集合bの中央値
med_a=median(a)
med_b=median(b)
# もし偶数なら
if n%2==0:
    print(int(2*(med_b-med_a)+1))

else:
    print((med_b-med_a)+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import math
import numpy as np

N = int(input())
A = []
B = []
for i in range(N):
    t = list(map(int, input().split()))
    A.append(t[0])
    B.append(t[1])
A.sort()
B.sort()

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

    print(a)

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:
    a_m = a[n // 2]
    b_m = b[n // 2]
    print(b_m - a_m + 1)
else:
    a_m = (a[n // 2 - 1] + a[n // 2]) #/ 2
    b_m = (b[n // 2 - 1] + b[n // 2]) #/ 2
    print(int(b_m - a_m) + 1)

Submission status: Accepted

Solution code:
import math
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)
res = 0
if n % 2 == 1:
    m = n // 2
    m1 = a[m]
    m2 = b[m]
    res = m2 - m1 + 1
else:
    ml = n // 2 - 1
    m1 = a[ml] + a[ml + 1]
    m2 = b[ml] + b[ml + 1]
    res = m2 - m1 + 1

print(res)

Submission status: Accepted

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

n = int(input())

# n行の複数列ある数値をそれぞれの配列へ
a, b = [0]*n, [0]*n
for i in range(n): a[i], b[i] = map(int, input().split())

#print(a,b)

a.sort()
b.sort()

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

Submission status: Accepted

Solution code:
import statistics
import math

N = int(input())
A_B = [[int(i) for i in input().split(' ')] for n in range(N)]

t = list(zip(*A_B))

A_sta = statistics.median(t[0])
B_sta = statistics.median(t[1])

if N % 2 == 0:
    count = int(2 * (B_sta - A_sta) + 1)
else:
    count = B_sta - A_sta + 1

print(count)

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:
    i=int(n/2)
    ans_a=(a[i]+a[i-1])/2
    ans_b=(b[i]+b[i-1])/2
    ans=(ans_b-ans_a+1)*2-1
    print(int(ans))
else:
    i=int((n-1)/2)
    ans_a=a[i]
    ans_b=b[i]
    ans=ans_b-ans_a+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==0:
    A_med=(A[int(n/2)-1]+A[int(n/2)])/2
    B_med=(B[int(n/2)-1]+B[int(n/2)])/2
    print(int((B_med-A_med)*2+1))
else:
    A_med=A[int((n+1)/2)-1]
    B_med=B[int((n+1)/2)-1]
    print(int(B_med-A_med+1))

Submission status: Accepted

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

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

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

    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]

        ans = b1 + b2 - (a1 + a2) + 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 = [0] * (N + 1)
B = [0] * (N + 1)

count = 0

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

A.sort()
B.sort()
if N % 2 == 1:
  centerA = A[(N + 1) // 2]
  centerB = B[(N + 1) // 2]
else:
  centerA = (A[N // 2] + A[N // 2 + 1]) / 2
  centerB = (B[N // 2] + B[N // 2 + 1]) / 2

#print(centerA, centerB)  

if N % 2 == 1:
  count = centerB - centerA + 1
else:
  count = (centerB - centerA) * 2 + 1

print(int(count))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

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)

if N % 2== 0:
    A.sort()
    med_A = (A[N//2] + A[N//2 - 1])/2
    B.sort()
    med_B = (B[N//2] + B[N//2 - 1])/2
    print(int((med_B-med_A) * 2 + 1))
else:
    A.sort()
    med_A = A[(N-1)//2]
    B.sort()
    med_B = B[(N-1)//2]
    print(int(round(med_B) - round(med_A) + 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:
    l1 = A[int(n/2)]
    r1 = B[int(n/2)]
    ans = r1 - l1 + 1
else:
    l2 = A[int(n/2)-1] + A[int((n/2))]
    r2 = B[int(n/2)-1] + B[int((n/2))]
    ans = r2 - l2 + 1

print(ans)

Submission status: Accepted

Solution code:
import statistics

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

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

if n % 2 == 1:
    ans = mb - ma + 1
else:
    ma = int(ma * 2)
    mb = int(mb * 2)
    ans = mb - ma + 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:
  c=(n+1)//2-1
  print(B[c]-A[c]+1)
else:
  c=n//2
  print(B[c]+B[c-1]-A[c]-A[c-1]+1)

Submission status: Accepted

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

if N%2==1:
  print(B[int((N-1)/2+0.5)]-A[int((N-1)/2+0.5)]+1)
else:
  print((B[int(N/2+0.5)-1]+B[int(N/2+0.5)])-(A[int(N/2+0.5)-1]+A[int(N/2+0.5)])+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 = sorted(a)
b = sorted(b)
 
if n%2!=0:
    ans = b[(n-1)//2] - a[(n-1)//2] + 1
else:
    ans = (b[n//2-1] + b[n//2]) - (a[n//2-1] + a[n//2]) + 1
 
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
itl = lambda: list(map(float, input().strip().split()))


def median(N, C):
    if N % 2:
        return C[N // 2]
    else:
        return C[N // 2 - 1] + C[N // 2]


def solve():
    N = int(input())

    A = []
    B = []
    for _ in range(N):
        a, b = itl()
        A.append(int(a))
        B.append(int(b))
    A.sort()
    B.sort()

    ma = median(N, A)
    mb = median(N, B)
    return mb - ma + 1


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
from collections import defaultdict
from fractions import Fraction
N = int(input())
A = [0 for  _ in range(N)]
B = [0 for  _ in range(N)]
for i in range(N):
    A[i], B[i] = [int(i) for i in input().split()]
A.sort()
B.sort()

if N%2 == 1:
    target = ((N+1)//2)-1
    print(B[target]-A[target]+1)
else:
    target1 = ((N)//2)-1
    target2 = (N)//2
    num1 = (A[target1]+A[target2])/2
    num2 = (B[target1]+B[target2])/2
    print(int(((num2-num1)/0.5)+1))

Submission status: Accepted

Solution code:
n=int(input())
low=[]
high=[]
for i in range(n):
    a,b=map(float,input().split())
    low.append(a)
    high.append(b)
low.sort()
high.sort()
if n%2==0:
    low_avg=(low[n//2-1]+low[n//2])/2
    high_avg=(high[n//2-1]+high[n//2])/2
    ans=(high_avg-low_avg)*2+1
else:
    ans=high[(n-1)//2]-low[(n-1)//2]+1
print(int(ans))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

        
n = int(input())

a = np.zeros(n,dtype=int)
b = np.zeros(n,dtype=int)

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

#listをsortする
a.sort()
b.sort()
                    
if n%2 == 1:
    a0cen = a[(n-1)//2]
    b0cen = b[(n-1)//2]
    nn = b0cen - a0cen + 1
else:
    a0cen1 = a[(n-2)//2]+a[n//2]
    b0cen1 = b[(n-2)//2]+b[n//2]
    nn = int(b0cen1 -a0cen1 +1)

print(nn)

Submission status: Accepted

Solution code:
n = int(input())

low = [0]*n
high = [0]*n

for i in range(n):
    a, b = [int(x) for x in input().split(" ")]
    low[i] = a
    high[i] = b

low.sort()
high.sort()

if n%2 == 1:
    print(high[n//2]-low[n//2]+1)
else:
    x = (low[(n-1)//2] + low[n//2])/2
    y = (high[(n-1)//2] + high[n//2])/2
    print(int(2*y - 2*x + 1))

Submission status: Accepted

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

Submission status: Accepted

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


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

    A.sort()
    B.sort()

    if n % 2 == 1:
        left = A[n // 2]
        right = B[n // 2]
    else:
        m = n // 2 - 1
        left = A[m] + A[m + 1]
        right = B[m] + B[m + 1]

    ans = right - left + 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

A.sort()
B.sort()

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

Submission status: Accepted

Solution code:
import statistics
import math

N = int(input())
listA = [0] * N
listB = [0] * N
idx = 0
for i in range(N):
  listA[idx],listB[idx] = map(int, input().split())
  idx += 1

medianA = 0
medianB = 0
ans = 0
if N%2 == 0:
  medianA = statistics.median(listA)
  medianB = statistics.median(listB)
  ans = (medianB - medianA) * 2 + 1
else:
  medianA = statistics.median(listA)
  medianB = statistics.median(listB)
  ans = medianB - medianA + 1

print(int(ans))

Submission status: Accepted

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

A = [a for a, b in AB]
B = [b for a, b in AB]

A.sort()
B.sort()

if N % 2:
    ma = B[N // 2]
    mi = A[N // 2]
else:
    ma = B[N // 2 - 1] + B[N // 2]
    mi = A[N // 2 - 1] + A[N // 2]

print(ma - mi + 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 == 0:
    mi = a[n//2-1] + a[n//2]
    ma = b[n//2-1] + b[n//2]
    print(ma - mi + 1)
else:
    mi = a[n//2]
    ma = b[n//2]
    print(ma - mi + 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 != 0 :
    print(b[n//2] - a[n//2] + 1)
    exit(0)

def m_r(c):
    return (c[n//2] + c[n//2 - 1])

z = m_r(b) - m_r(a) + 1
print(z)

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

res = 0

if n % 2 == 1:
    midA = A[n // 2]
    midB = B[n // 2]
    if midB >= midA:
        res = midB - midA + 1
    else:
        res = 0
else:
    midA = A[n // 2 - 1] + A[n // 2] 
    midB = B[n // 2 - 1] + B[n // 2]
    if midB >= midA:
        res = midB - midA + 1
    else:
        res = 0
print(res)

Submission status: Accepted

Solution code:
def resolve():
	N = int(input())
	# set A and B arrays.
	A = []
	B = []
	for _ in range(N):
		A_i, B_i = map(int, input().split())
		A.append(A_i)
		B.append(B_i)
	# count median
	if N%2==1: # odd
		i = int((N-1)/2)
		median_min = sorted(A)[i]
		median_max = sorted(B)[i]
	else:
		i_0 = int((N/2)-1)
		i_1 = int(N/2)
		ordered_A = sorted(A)
		ordered_B = sorted(B)
		median_min = ordered_A[i_0] + ordered_A[i_1] # 2 times of median of A
		median_max = ordered_B[i_0] + ordered_B[i_1]
	print(median_max - median_min +1)
    
resolve()

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

Submission status: Accepted

Solution code:

import sys, io, os, re
import bisect
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce

def array1(n): return [0] * n
def array2(n, m): return [[0] * m for x in range(n)]
def array3(n, m, l): return [[[0] * l for y in xrange(m)] for x in xrange(n)]

n = int(sys.stdin.readline().strip())
a = array1(n)
b = array1(n)

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

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

if n % 2 == 1:
    print(int(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))

Submission status: Accepted

Solution code:
from statistics import median
n=int(input())
A,B=[],[]
for _ in range(n):
  a,b=map(int,input().split())
  A.append(a*2)
  B.append(b*2)
ma,mb = int(median(A)),int(median(B))
#print(ma,mb)
if n%2 == 0:
  print(mb-ma+1)
else:
  print((mb-ma)//2+1)

Submission status: Accepted

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


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

sorted_A = A
sorted_B = B

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

Submission status: Accepted

Solution code:
def resolve():
	from statistics import median
	import sys
	input = sys.stdin.readline
	n = int(input())
	ab = [list(map(int, input().split())) for i in range(n)]
	mina = median(i[0] for i in ab)
	maxb = median(i[1] for i in ab)
	abrange = maxb - mina
	if n%2 == 0:
		print(int(abrange*2 + 1))
	else:
		print(int(abrange + 1))
resolve()

Submission status: Accepted

Solution code:
def resolve():
	from statistics import median
	import sys
	input = sys.stdin.readline
	n = int(input())
	ab = [list(map(int, input().split())) for i in range(n)]
	mina = median(i[0] for i in ab)
	maxb = median(i[1] for i in ab)
	abrange = maxb - mina
	if n%2 == 0:
		print(int(abrange/0.5 + 1))
	else:
		print(int(abrange/1 + 1))
resolve()

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    N = int(input())
    A = []
    B = []
    for _ in [0]*N:
        a, b = map(int ,input().split())
        A.append(a)
        B.append(b)
    A.sort()
    B.sort()

    if N % 2 == 0:
        median_min = (A[N//2 - 1] + A[N//2])
        median_max = (B[N//2 - 1] + B[N//2])
    else:
        median_min = A[N//2]
        median_max = B[N//2]
    print(median_max - median_min + 1)
    
main()

Submission status: Accepted

Solution code:
n = int(input())

al = []
bl = []
for i in range(n):
    a,b = [int(i) for i in input().split()]
    al.append(a)
    bl.append(b)
    
al = sorted(al)
bl = sorted(bl)

median_a = al[n//2]if n % 2 else (al[n // 2] + al[n//2 - 1]) / 2 
median_b = bl[n//2]if n % 2 else (bl[n // 2] + bl[n//2 - 1]) / 2 

if n % 2:
    print(int((median_b - median_a + 1)))
else:
    print(int((median_b - median_a) * 2 + 1))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = []
    B = []
    for _ in range(n):
        a, b = map(int, input().split())
        A += [a]
        B += [b]
    A.sort()
    B.sort()

    if n % 2 == 0:
        mid_pos = n // 2
        mid_A = A[mid_pos - 1] + A[mid_pos]
        mid_B = B[mid_pos - 1] + B[mid_pos]
        cnt = mid_B - mid_A + 1
    else:
        mid_pos = (n + 1) // 2 - 1
        mid_A = A[mid_pos]
        mid_B = B[mid_pos]
        cnt = mid_B - mid_A + 1
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import  math
import fractions
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))


N=int(input())
min_l=[]
max_l=[]
for k in range(N):
    A,B=nm()
    min_l.append(A)
    max_l.append(B)

min_l.sort()
max_l.sort()

if(N%2==0):
    print(int(((max_l[N//2-1]+max_l[N//2])/2-(min_l[N//2-1]+min_l[N//2])/2)*2+1))    
else:
    print(((max_l[(N-1)//2])-(min_l[(N-1)//2]))+1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    al = []
    bl = []
    for _ in range(n):
        a, b = map(int, input().split())
        al.append(a)
        bl.append(b)
    als = list(sorted(al))
    bls = list(sorted(bl))
    if n%2:
        print(bls[n//2]-als[n//2]+1)
    else:
        print(bls[n//2-1]+bls[n//2]-als[n//2]-als[n//2-1]+1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import glob

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\169\\E'

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'


class Common:
    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index - 1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        N = int(self.input_data())
        A = [[0, 0] for i in range(N)]
        B = [[0, 0] for i in range(N)]

        for i in range(N):
            tmp = self.input_data().split()
            A[i][0] = i
            A[i][1] = int(tmp[0])
            B[i][0] = i
            B[i][1] = int(tmp[1])

        # 最小値群と最大値群を個別に並び替える
        A_sorted = sorted(A, key=lambda x: x[1])
        B_sorted = sorted(B, key=lambda x: x[1])
        '''
        if N % 2 == 0:
            for i in range(N):
                for j in range(N):
                    if A_sorted[j][0] == i:
                        min = A_sorted[j]
                        break
                for j in range(N):
                    if B_sorted[j][0] == i:
                        max = B_sorted[j]
                        break
                

            pass
        else:
            pass
        '''
        if N % 2 == 0:
            median_min = A_sorted[int(N/2)][1] + A_sorted[int(N/2)-1][1]
            median_max = B_sorted[int(N/2)][1] + B_sorted[int(N/2)-1][1]
        else:
            median_min = A_sorted[int(N/2)][1]
            median_max = B_sorted[int(N/2)][1]
        result = median_max - median_min + 1

        print(str(result))



solver = Solver(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

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

l = sorted(ab, key=lambda x: x[0])
r = sorted(ab, key=lambda x: x[1])

if N % 2 == 0:
    maxab = ab[(N // 2) - 1][1]
    minab = ab[N // 2][0]
    x1 = (l[(N // 2) - 1][0] + l[N // 2][0])
    x2 = (r[(N // 2) - 1][1] + r[N // 2][1])
    print(x2 - x1 + 1)

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

s, t = 0, 0
if n % 2 == 0:
    s = (A[n//2-1] + A[n//2])
    t = (B[n//2-1] + B[n//2])
else:
    s = A[n//2]
    t = B[n//2]

print(t-s+1)

Submission status: Accepted

Solution code:
def median(array):
    tmp = sorted(array)
    if len(array) % 2:
        return tmp[len(array)//2]
    else:
        return (tmp[len(array)//2] + tmp[len(array)//2 - 1]) / 2


[n], *x = [[*map(int, t.split())] for t in open(0)]
a, b = zip(*x)
if n % 2 == 0:
    print(int(2*(median(b) - median(a)) + 1))
else:
    print(median(b) - median(a) + 1)

Submission status: Accepted

Solution code:
n = int(input())
A = [[int(i) for i in input().split(" ")] for j in range(n)]
a = sorted([i[0] for i in A])
b = sorted([i[1] for i in A], reverse=True)
if n%2 == 1:
    print(b[n//2]-a[n//2]+1)
else:
    l = (a[n//2-1] + a[n//2]) / 2
    r = (b[n//2-1] + b[n//2]) / 2
    print(int((r-l)*2)+1)

Submission status: Accepted

Solution code:
how_many = int(input())
least, biggest = [],[]
for i in range(how_many):
    nums = input().split()
    nums = [int(num) for num in nums]
    least.append(nums[0])
    biggest.append(nums[1])
least.sort()
biggest.sort()
if how_many % 2 == 1:
    print(biggest[int((how_many-1) / 2)] - least[int((how_many-1) / 2)] + 1)
else:
    l = (least[int(how_many / 2)] + least[int(how_many / 2 - 1)]) / 2
    b = (biggest[int(how_many / 2)] + biggest[int(how_many / 2 - 1)]) / 2
    print(int((b-l) / 0.5 + 1))

Submission status: Accepted

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