Problem:


Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991



Sample input:
2
1 2
2 3


Sample output:
3

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    T = read_lists(N)
    Q = []
    for a, b in T:
        heapq.heappush(Q, (a, -1))
        heapq.heappush(Q, (b, 1))

    K1 = (N - 1) // 2 + 1
    K2 = N // 2 + 1

    s = 0
    t = 0
    S = [0, 0]
    T = [0, 0]
    while s < K2:
        a, k = heapq.heappop(Q)
        if k < 0:
            t += 1
        else:
            t -= 1
            s += 1
        
        if S[0] == 0 and s + t >= K1:
            S[0] = a
        
        if S[1] == 0 and s >= K1:
            S[1] = a
        
        if T[0] == 0 and s + t >= K2:
            T[0] = a
        
        if T[1] == 0 and s >= K2:
            T[1] = a

    if N % 2 == 1:
        print(S[1] - S[0] + 1)
    else:
        print((S[1] + T[1]) - (S[0] + T[0]) + 1)


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 == 0:
  middle = n // 2
  print(bList[middle] + bList[middle-1] - aList[middle] - aList[middle-1] + 1)
else:
  middle = (n-1) // 2
  print(bList[middle] - aList[middle] + 1)

Submission status: Accepted

Solution code:
N = int(input())
low = [0] * N;  up = [0] * N;
for i in range(N):
    low[i], up[i] = [int(x) for x in input().split()]

low.sort()
up.sort(reverse = True)

if N % 2 == 0:
    k = N // 2
    ans = 0
    l1 = low[k - 1]
    l2 = low[k]
    u1 = up[k - 1]
    u2 = up[k]
    ans = u1 + u2 - l1 - l2 + 1
else:
    k = (N - 1) // 2
    l = low[k]
    u = up[k]
    ans = u - l + 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()
m_l = A[N//2] if N % 2 == 1 else A[N//2-1] + A[N//2]
m_r = B[N//2] if N % 2 == 1 else B[N//2-1] + B[N//2]
print(m_r - m_l + 1)

Submission status: Accepted

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

if n % 2 == 0:
    low = lows[n // 2 - 1] + lows[n // 2]
    high = highs[n // 2 - 1] + highs[n // 2]
    print(high - low + 1)
else:
    low = lows[(n - 1) // 2]
    high = highs[(n - 1) // 2]
    print(high - low + 1)

Submission status: Accepted

Solution code:
n = int(input())
l = [list(map(int,input().split())) for _ in range(n)]
l1 = sorted([i for i,k in l])
l2 = sorted([k for i,k in l])[::-1]

if n%2!=0:
  a = l1[int(n//2)]
  b = l2[int(n//2)]
  print(b-a+1)
  
else:
  a1 = l1[int(n//2)-1]
  a2 = l1[int(n//2)]
  b1 = l2[int(n//2)-1]
  b2 = l2[int(n//2)]
  print((b1-a1+1)+(b2-a2+1)-1)

Submission status: Accepted

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = list(map(list, (zip(*AB))))
A = sorted(AB[0])
B = sorted(AB[1])
if N % 2 == 0:
    m = N // 2
    left = (A[m] + A[m-1])
    right = (B[m] + B[m-1])
    print(right - left + 1)
else :
    m = N // 2
    print((B[m] - A[m]) + 1)

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(reverse=False) 
blist.sort(reverse=False) 

if n%2 == 0:
  amedian = (alist[int(n/2-1)]+alist[int(n/2)])/2
  bmedian = (blist[int(n/2-1)]+blist[int(n/2)])/2
  print(int((bmedian-amedian)*2+1))
if n%2 == 1:
  amedian = alist[int((n-1)/2)]
  bmedian = blist[int((n-1)/2)]
  print(int((bmedian-amedian)+1))

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
abl = [list(map(int,input().split())) for nesya in range(n)]
al = []
bl = []
for ab in abl:
  al.append(ab[0])
  bl.append(ab[1])
if n%2 == 1:
  print(median(bl)-median(al)+1)
else:
  print(int(median(bl)*2-median(al)*2+1))

Submission status: Accepted

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

n = int(input())

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

a_median = statistics.median(a_list)
b_median = statistics.median(b_list)

# print(a_median)
# print(b_median)
if n % 2 != 0:
    print(int(b_median-a_median+1))
else:
    print(int(b_median*2-a_median*2+1))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right


def solve(n, ab_list):
    a_list = []
    b_list = []
    for a, b in ab_list:
        a_list.append(a)
        b_list.append(b)
    a_list = sorted(a_list)
    b_list = sorted(b_list)
    v_list = list(sorted(list(set(a_list + b_list))))
    m = len(v_list)

    if n % 2 == 1:
        ok_min = 10 ** 9 + 7
        ok_max = -1
        for i in range(m):
            v = v_list[i]
            lower_count = bisect_left(b_list, v)
            higher_count = n - bisect_right(a_list, v)
            if lower_count <= n // 2 and higher_count <= n // 2:
                ok_min = min(ok_min, v)
                ok_max = max(ok_max, v)
        return ok_max - ok_min + 1
    else:
        ok_min_left = 10 ** 9 + 7
        ok_max_left = -1
        ok_min_right = 10 ** 9 + 7
        ok_max_right = -1
        for i in range(m):
            v = v_list[i]
            lower_count = bisect_left(b_list, v)
            higher_count = n - bisect_right(a_list, v)
            if lower_count <= n // 2 - 1 and higher_count <= n // 2:
                ok_min_left = min(ok_min_left, v)
                ok_max_left = max(ok_max_left, v)
            if lower_count <= n // 2 and higher_count <= n // 2 - 1:
                ok_min_right = min(ok_min_right, v)
                ok_max_right = max(ok_max_right, v)
        return (ok_max_right - ok_min_right + ok_max_left - ok_min_left) + 1


def main():
    n = int(input())
    ab_list = [list(map(int, input().split())) for _ in range(n)]
    res = solve(n, ab_list)
    print(res)


def test():
    assert solve(2, [[1, 2], [2, 3]]) == 3
    assert solve(3, [[100, 100], [10, 10000], [1, 1000000000]]) == 9991


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

Solution code:
N = int(input())
List = []

A = []
B = []

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

if N%2 == 1:
  A.sort()
  B.sort()
  ans = B[N//2] - A[N//2] + 1
  print(int(ans))
else:
  A.sort()
  B.sort()
  c = B[N//2] - A[N//2] + 1
  d = B[N//2-1] - A[N//2-1] + 1
  print(c+d-1)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
AB = [list(map(int, readline().split())) for _ in range(N)]

LINE = defaultdict(lambda: [0, 0])
for a, b in AB:
    LINE[a][0] += 1
    LINE[b + 1][1] += 1

LINE = list(LINE.items())
LINE.sort()

ans = 0

small = 0
small_able = 0
prevx = -1


if N % 2 == 1:

    mn = 10 ** 10
    mx = 10 ** 10

    flg = False

    for x, p in LINE:
        small_able += p[0]
        small += p[1]

        if small < (N + 1) // 2 and ((N + 1) // 2) <= small_able:
            mn = min(x, mn)

        if (N + 1) // 2 <= small:
            mx = min(mx, x - 1)
            break


else:
    mn_0 = 10 ** 10
    mn_1 = 10 ** 10
    mx_0 = 10 ** 10
    mx_1 = 10 ** 10

    flg_0 = False
    flg_1 = False

    for x, p in LINE:
        small_able += p[0]
        small += p[1]

        if small < N // 2 and (N // 2) <= small_able:
            mn_0 = min(x, mn_0)

        if N // 2 <= small:
            mx_0 = min(mx_0, x - 1)

        if small < (N // 2) + 1 and (N // 2) + 1 <= small_able:
            mn_1 = min(x, mn_1)

        if (N // 2) + 1 <= small:
            mx_1 = min(mx_1, x - 1)
            break

    mn = mn_0 + mn_1
    mx = mx_0 + mx_1


print(mx - mn + 1)

Submission status: Accepted

Solution code:
import math
import fractions
import collections
import itertools
import pprint
N=int(input())
l=[list(map(int,input().split())) for _ in range(N)]
l1=[]
l2=[]
for i in range(N):
    l1.append(l[i][0])
    l2.append(l[i][1])
l1.sort()
l2.sort()
if N%2==0:
    minmed=(l1[N//2]+l1[N//2-1])/2
    maxmed=(l2[N//2]+l2[N//2-1])/2
    print(int(2*(maxmed-minmed)+1))
else:
    minmed=l1[N//2]
    maxmed=l2[N//2]
    print(maxmed-minmed+1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000000)

import bisect
import math
import collections
import heapq


def solve(N, ABs):
    # print(N, ", ", ABs)
    starts = []
    ends = []
    for a, b in ABs:
        starts.append(a)
        ends.append(b)
    starts.sort()
    ends.sort()

    if N % 2 == 1:
        k = (N - 1) // 2
        left = starts[k]
        right = ends[-(k + 1)]
        return right - left + 1
    else:
        k = N // 2 - 1
        left1 = starts[k]
        left2 = starts[k+1]
        left_mid = abs(left2 - left1) / 2 + left1

        right1 = ends[-(k+2)]
        right2 = ends[-(k+1)]
        right_mid = abs(right2 - right1) / 2 + right1
        # print(f"left1, left2 = {left1}, {left2}")
        # print(f"right1, right2 = {right1}, {right2}")
        # print(f"left_mid, right_mid = {left_mid}, {right_mid}")
        d = (right_mid - left_mid) * 2 + 1
        return int(d)


# assert (solve(3, [(100, 100), (10, 10000), (1, 1000000000)]) == 9991)
# assert (solve(2, [(1, 2), (2, 3)]) == 3)
#
# def createrange():
#     s = random.randint(1, 20)
#     k = random.randint(1, 10)
#     return (s, s + k)
#
#
# def slow_solve1(N, As):
#     import itertools
#     ans = []
#     for k1 in range(As[0][0], As[0][1] + 1):
#         for k2 in range(As[1][0], As[1][1] + 1):
#             for k3 in range(As[2][0], As[2][1] + 1):
#                 for k4 in range(As[3][0], As[3][1] + 1):
#                     r = [k1, k2, k3, k4]
#                     r.sort()
#                     ans.append((r[1] + r[2]) / 2)
#                     # print((r[1] + r[2]) / 2)
#     print(sorted(list(set(ans))))
#     return len(set(ans))
#
# def slow_solve2(N, As):
#     ans = []
#     for k1 in range(As[0][0], As[0][1] + 1):
#         for k2 in range(As[1][0], As[1][1] + 1):
#             for k3 in range(As[2][0], As[2][1] + 1):
#                 r = [k1, k2, k3]
#                 r.sort()
#                 ans.append(r[1])
#     print(sorted(list(set(ans))))
#     return len(set(ans))
#
# import random
#
# N = 3
# for _ in range(10000):
#     print("=========")
#     ranges = [createrange() for _ in range(N)]
#     print(N, ranges)
#
#     print(slow_solve2(N, ranges))
#     print(solve(N, ranges))
#     if slow_solve2(N, ranges) != solve(N, ranges):
#         print("OUT")
#         input()

if __name__ == "__main__":
    N = int(input())
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, ABs))

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)
n-=1

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

Submission status: Accepted

Solution code:
import math

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

A=[]
B=[]
for i in range(x):
    A.append(ab[i][0])
    B.append(ab[i][1])

A.sort()
B.sort()

if x%2==1:
    min=A[x//2]
    max=B[x//2]
    ans=max-min+1
else:
    min=(A[x//2-1]+A[x//2])/2
    max=(B[x//2-1]+B[x//2])/2
    ans=(max-min)*2+1

print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
ma = []
mi = []
for i in range(N):
    x1, x2 = map(int, input().split())
    mi.append(x1)
    ma.append(x2)

mi = sorted(mi)
ma = sorted(ma)

L = 0
R = 0

if N % 2 == 0:
    L = (mi[N//2-1]+mi[N//2]) / 2
    R = (ma[N//2-1]+ma[N//2]) / 2
    ans = int((R-L)/0.5)+1
else:
    L = mi[N//2]
    R = ma[N//2]
    ans = R - L + 1


print(ans)

Submission status: Accepted

Solution code:
def median_cal(data):
    num = len(data)
    data.sort()
    if num % 2 == 0:
        median1 = int(num / 2)
        median2 = int(num / 2 - 1)
        median = (data[median1] + data[median2]) / 2.0
        return median
    else:
        median = int((num + 1) / 2 - 1)
        median = data[median]
        return median

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
if n % 2 == 0:
    print(int(median_cal(B)*2 - median_cal(A)*2 + 1))
else:
    print(median_cal(B) - median_cal(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()
if N % 2 == 0:
    n = N // 2
    m2 = A[n - 1] + A[n]
    M2 = B[n - 1] + B[n]
    ans = M2 - m2 + 1
    print(ans)
else:
    n = N // 2
    m2 = A[n]
    M2 = B[n]
    ans = M2 - m2 + 1
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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


s=sorted(s)
e=sorted(e,reverse=True)

if n%2==1:
  x=n//2
  ans=(e[x]-s[x]+1)

else:
  x=n//2
  ts=(s[x]+s[x-1])/2
  te=(e[x]+e[x-1])/2
#  print(ts,te)
  ans=(te-ts)//0.5+1


print(int(ans))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    t = [[int(t)for t in input().split()]for _ in [0]*n]

    

    if n%2==1:
        t_1 = sorted(t,key=lambda a: a[0])
        t_2 = sorted(t,key=lambda b:b[1],reverse=True)
        ans = t_2[n//2][1]-t_1[n//2][0]+1
        print(ans)
    else:
        t_1 = sorted(t,key=lambda a: a[0])
        t_2 = sorted(t,key=lambda b:b[1])

        a0,a1 = t_1[n//2][0],t_2[n//2][1]

        t_1 = sorted(t,key=lambda a: a[0],reverse=True)
        t_2 = sorted(t,key=lambda b:b[1],reverse=True)
        
        b0,b1 = t_1[n//2][0],t_2[n//2][1]
        ans = a1-a0+b1-b0 + 1
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
a_list = [0] * n
b_list = [0] * n

for i in range(n):
     a, b = map(int, input().split())
     a_list[i] = a
     b_list[i] = b
    
min_median = median(a_list)
max_median = median(b_list)

median_range = max_median - min_median

if n%2 == 0:
    print(int(median_range/0.5 + 1))
else:
    print(int(median_range/1 + 1))

Submission status: Accepted

Solution code:
N, *AB = map(int, open(0).read().split())
A = sorted(AB[::2])
B = sorted(AB[1::2])
if N % 2:
    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())
a = []
b = []
ab = []
for i in range(n):
    ai,bi = map(int,input().split())
    a.append(ai)
    b.append(bi)
    ab.append((ai,bi))
a.sort()
b.sort()
x1 = a[(n-1)//2]
x2 = a[(n-1)//2+1]
y1 = b[n-1-(n-1)//2]
y2 = b[n-2-(n-1)//2]
if n % 2 == 1:
    print(y1-x1+1)
else:
    print(y2+y1-x2-x1+1)

Submission status: Accepted

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import bisect
from queue import PriorityQueue
import itertools
from copy import deepcopy
import random
import time
import os
import sys
import datetime
from functools import lru_cache
from functools import reduce
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
alphabet="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):
    if b:print("yes")
    else:print("no")
def Yn(b):
    if b:print("Yes")
    else:print("No")
def YN(b):
    if b:print("YES")
    else:print("NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(bbool,ret1,ret2):
    if bbool:return ret1
    else:return  ret2
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def pm(x):
    return x//abs(x)
def inputintlist(): 
    return list(map(int,input().split()))
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def ruisekiwa(A):
    ret=[0]*(len(A)+1)
    for a in range(len(A)):
        ret[a+1]=ret[a]+A[a]
    return ret
def getmd(x1,y1,x2,y2):
    return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):
    return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow



######################################################################################################
n=int(input())
AB=[inputintlist() for a in range(n)]
A=[0]*n
B=[0]*n
for a in range(n):
    A[a]=AB[a][0]
    B[a]=AB[a][1]
A.sort()
B.sort()
Am=A[n//2] if n%2==1 else A[n//2]+A[n//2-1]
Bm=B[n//2] if n%2==1 else 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(reverse=True)

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

# print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect


if __name__ == '__main__':
    n = int(input())
    a_s = [0] * n
    b_s = [0] * n
    for i in range(n):
        a, b = list(map(int, input().split()))
        a_s[i] = a
        b_s[i] = b
    a_s.sort()
    b_s.sort()
    #print('a_s') # debug
    #print(a_s) # debug
    #print('b_s') # debug
    #print(b_s) # debug
    if n % 2 == 0:
        mb2 = (b_s[n // 2 - 1] + b_s[n // 2])
        ma2 = (a_s[n // 2 - 1] + a_s[n // 2])
        #print('ma2, mb2') # debug
        #print(ma2, mb2) # debug
        ans = mb2 - ma2 + 1
    else:
        ans = b_s[n // 2] - a_s[n // 2] + 1
    #print('ans') # debug
    print(ans)

    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

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

# 奇数の時
if n%2==1:
    # 下限
    a.sort()
    l=a[n//2][0]

    a.sort(key=lambda x:x[1])

    r=a[n//2][1]
    print(r-l+1)

else:
    a.sort()
    l=a[n//2-1][0]+a[n//2][0]
    a.sort(key=lambda x:x[1])
    r=a[n//2-1][1]+a[n//2][1]
    print(r-l+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()
m=int(n//2)
if n%2==0:
  a_av=(a[m]+a[m-1])/2
  b_av=(b[m]+b[m-1])/2
  ans=int(b_av*2-a_av*2+1)
else:
  ans=int(b[m]-a[m]+1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ABlist = [list(map(int, input().split())) for _ in range(N)]
Alist = []
Blist = []
for A, B in ABlist:
    Alist.append(A)
    Blist.append(B)
Alist.sort()
Blist.sort()

if N % 2 == 1:
    print(Blist[(N+1)//2-1]-Alist[(N+1)//2-1]+1)
else:
    print(Blist[N//2-1]+Blist[N//2] - Alist[N//2-1]-Alist[N//2]+1)

Submission status: Accepted

Solution code:
n,*ab=map(int,open(0).read().split())
a=ab[::2]
b=ab[1::2]
m=n//2
a.sort()
b.sort()
ans=0
if n&1==1:
  ans=b[m]-a[m]+1
else:
  ans=int(2*((b[m]+b[m-1])/2-(a[m]+a[m-1])/2))
  ans+=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=sorted(a)
b=sorted(b)
if (n%2==1):
    target=(n+1)//2
    print(b[target-1]-a[target-1]+1)
if (n%2==0):
    target=(n)//2
    left=a[target]+a[target-1]
    right=b[target]+b[target-1]
    ans=right-left+1
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
	x,y = map(int,input().split())
	a.append(x)
	b.append(y)
a.sort()
b.sort()
if(n&1):
	l = a[n//2]
	r = b[n//2]
else:
	l = a[n//2]+a[n//2-1]
	r = b[n//2]+b[n//2-1]
print(r-l+1)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    low = []
    high = []
    for i in range(n):
        a,b = map(int, input().split())
        low.append(a)
        high.append(b)

    low.sort()
    high.sort()
    if n % 2:
        print(high[(n+1)//2-1] - low[(n+1)//2-1] + 1)
    else:
        hh = (high[(n)//2-1] + high[(n)//2]) / 2
        ll = (low[(n)//2-1] + low[(n)//2]) / 2
        print(int((hh - ll)*2) + 1)


if __name__ == "__main__":
    resolve()

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

n=n_in()

from collections import defaultdict

memo=defaultdict(list)
for i in range(n):
    a,b=l_in()
    memo[a].append((i,True))
    memo[b].append((i,False))
    


left=set()
mid=set()
right=set(range(n))


lazy=set()

res_left=-1
res_right=-1

res2_left=-1
res2_right=-1

for x in sorted(memo.keys()):
    for i in lazy:
        mid.remove(i)
        left.add(i)
    lazy.clear()
    
    for i, is_a in memo[x]:
        if is_a:
            right.remove(i)
            mid.add(i)
        else:
            lazy.add(i)
            
    if n%2 == 0:
        m = n//2-1

        # n//2
        if len(mid) >= 1 and len(left) <= m and len(right) <= m+1:
            if res_left == -1:
                res_left = x
            res_right = x
            
        # n//2 + 1
        if len(mid) >= 1 and len(left) <= m+1 and len(right) <= m:
            if res2_left == -1:
                res2_left = x
            res2_right = x
    else:
        m = (n-1)//2
        if len(mid) >= 1 and len(left) <= m and len(right) <= m:
            if res_left == -1:
                res_left = x
            res_right = x

if n%2 == 0:
    print(res_right-res_left+res2_right-res2_left+1)
else:
    print(res_right-res_left+1)

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 == 0:
    leftA, leftB = As[N // 2 - 1], Bs[N // 2 - 1]
    rightA, rightB = As[N // 2], Bs[N // 2]
    minM = leftA + rightA
    maxM = leftB + rightB
    print(maxM-minM+1)
else:
    midA, midB = As[N // 2], Bs[N // 2]
    print(midB-midA+1)

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
A = []
B = []
for i in range(n):
    A.append(a[i][0])
    B.append(a[i][1])
A = sorted(A)
B = sorted(B)
if n%2 == 0:
    ca = (A[int((n/2)-1)] + A[int(n/2)]) / 2
    cb = (B[int((n/2)-1)] + B[int(n/2)]) / 2
    ans = (cb - ca) * 2 + 1
else:
    ca = A[int(((n+1)//2)-1)]
    cb = B[int(((n+1)//2)-1)]
    ans = (cb - ca) + 1
print(int(ans))

Submission status: Accepted

Solution code:
import statistics as st

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

for i in range(n):
  ab.append(list(map(int, input().split())))

temp = []
temp2 = []
for i in range(len(ab)):
  temp.append(ab[i][0])
  temp2.append(ab[i][1])
  
min = st.median(temp)
max = st.median(temp2)

if n % 2 == 0:
  print(int((max-min)/0.5)+1)
else:
  print(int(max-min)+1)