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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
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

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
data = ILL(N)
data.sort()
data2 = sorted(data,key = lambda x:x[1])

if N%2 == 1:
  print(data2[N//2][1]-data[N//2][0]+1)
  
if N%2 == 0:
  sita = data[N//2-1][0]+data[N//2][0]
  ue = data2[N//2-1][1]+data2[N//2][1]
  print(ue-sita+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*ab = map(int,open(0).read().split())
a = ab[::2]
b = ab[1::2]
a.sort()
b.sort(reverse=True)

if n%2 == 1:
    med_min = a[(n-1)//2]
    med_max = b[(n-1)//2]
    ans = med_max - med_min + 1
else:
    med_min = (a[n//2-1] + a[n//2] ) /2
    med_max = (b[n//2-1] + b[n//2] ) /2
    ans = (med_max - med_min)*2 + 1

print(int(ans))

Submission status: Accepted

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

if n % 2 == 1:
    x.sort()
    l = x[n // 2][0]
    x.sort(key=lambda x: -x[1])
    r = x[n // 2][1]
    print(r - l + 1)

else:
    x.sort()
    l1 = x[n // 2 - 1][0]
    l2 = x[n // 2][0]
    x.sort(key=lambda x: -x[1])
    r1 = x[n // 2 - 1][1]
    r2 = x[n // 2][1]
    print(r1 + r2 - l1 - l2 + 1)

Submission status: Accepted

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

def main():
    N = int(readline())
    m = map(int,read().split())
    AB = list(zip(m,m))

    A = [ab[0] for ab in AB]
    B = [ab[1] for ab in AB]

    A.sort()
    B.sort()

    # print(AB)
    # print(A)
    # print(B)

    if N % 2 == 1:
        Amed = A[N // 2]
        Bmed = B[N // 2]

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

        print(Bmed - Amed + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

al.sort()
bl.sort(reverse=True)

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = [0]*N
    B = [0]*N
    for i in range(N):
        A[i],B[i] = map(int, input().split())
    A.sort()
    B.sort(reverse=True)
    if N%2==1:
        mid = (N+1)//2-1
        ans = B[mid]-A[mid]+1
    else:
        mid1 = N//2-1
        mid2 = N//2
        ans = (B[mid1]+B[mid2])-(A[mid1]+A[mid2])+1
    return ans
print(solve())

Submission status: Accepted

Solution code:
N = int(input())

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

mi = sorted([A[i][0] for i in range(N)])
ma = sorted([A[i][1] for i in range(N)])

if N % 2 == 0:
    mi2 = mi[N//2-1] + mi[N//2]
    ma2 = ma[N//2-1] + ma[N//2]
else:
    mi2 = mi[N//2]
    ma2 = ma[N//2]

print(ma2 - mi2 + 1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = MI()

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

meda = med(A)
medb = med(B)
if N % 2 != 0:
    print(int(max(meda, medb) - min(meda, medb) + 1))
else:
    print(int(2 * medb - 2 * meda + 1))

Submission status: Accepted

Solution code:
N=int(input())
AB=[list(map(int,input().split())) for i in range(N)]
from statistics import median
A,B=zip(*AB)
x,y=median(A),median(B)
print(int(y-x+1) if N%2 else int((y-x)*2)+1)

Submission status: Accepted

Solution code:
n = int(input())

start = []
end = []

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

start.sort()
end.sort()

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

Submission status: Accepted

Solution code:
N = int(input())
L = []
H = []
for _ in range(N):
    a, b = map(int, input().split())
    L.append(a)
    H.append(b)
L.sort()
H.sort()

if N % 2 == 0:
    h = H[N // 2 - 1] + H[N // 2]
    l = L[N // 2 - 1] + L[N // 2]
    ans = h - l + 1
else:
    h = H[N//2]
    l = L[N//2]
    ans = h - l + 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ab = []

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

if n % 2 == 1:
    ab.sort()
    min_val = ab[n//2][0]
    ab.sort(key=lambda x: -x[1])
    max_val = ab[n//2][1]
    print(max_val - min_val + 1)
else:
    ab.sort()
    min_val = ab[n//2][0] + ab[n//2 - 1][0]
    ab.sort(key=lambda x: -x[1])
    max_val = ab[n//2][1] + ab[n//2 - 1][1]
    print(max_val - min_val + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
 
YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
input=lambda: sys.stdin.readline().rstrip()


class Tree:
    def __init__(self,inp_size=None,ls=None,init=True,index=0):
        self.LCA_init_stat=False
        self.ETtable=[]
        if init:
            if ls==None:
                self.stdin(inp_size,index=index)
            else:
                self.size=len(ls)+1
                self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def stdin(self,inp_size=None,index=1):
        if inp_size==None:
            self.size=int(input())
        else:
            self.size=inp_size
        self.edges,_=GI(self.size,self.size-1,index=index)
        return
    
    def listin(self,ls,index=0):
        self.size=len(ls)+1
        self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def __str__(self):
        return  str(self.edges)
 
    def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append(x)
        v=[-1]*self.size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==-1:
                    q.append(nb)
                    v[nb]=func(v[c],nb,d)
        return v
 
    def EulerTour(self,x):
        q=deque()
        q.append(x)
        self.depth=[None]*self.size
        self.depth[x]=0
        self.ETtable=[]
        self.ETdepth=[]
        self.ETin=[-1]*self.size
        self.ETout=[-1]*self.size
        cnt=0
        while q:
            c=q.pop()
            if c<0:
                ce=~c
            else:
                ce=c
                for nb,d in self.edges[ce]:
                    if self.depth[nb]==None:
                        q.append(~ce)
                        q.append(nb)
                        self.depth[nb]=self.depth[ce]+1
            self.ETtable.append(ce)
            self.ETdepth.append(self.depth[ce])
            if self.ETin[ce]==-1:
                self.ETin[ce]=cnt
            else:
                self.ETout[ce]=cnt
            cnt+=1
        return
    
    def LCA_init(self,root):
        self.EulerTour(root)
        self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
        #self.st=SegTree(self.size*2-1,self.ETdepth,function=min,ide=inf)
        self.LCA_init_stat=True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            self.LCA_init(root)
        xin,xout=self.ETin[x],self.ETout[x]
        yin,yout=self.ETin[y],self.ETout[y]
        a=min(xin,yin)
        b=max(xout,yout,xin,yin)
        id_of_min_dep_in_et=self.st.query_id(a,b+1)
        return self.ETtable[id_of_min_dep_in_et]

## Tested by EduFor 88 D
## https://codeforces.com/contest/1359/problem/D

class SparseTable: # O(N log N) for init, O(1) for query(l,r)
    def __init__(self,ls,init_func=min,init_idl=float('inf')):
        self.func=init_func
        self.idl=init_idl
        self.size=len(ls)
        self.N0=self.size.bit_length()
        self.table=[ls[:]]
        self.index=[list(range(self.size))]
        self.lg=[0]*(self.size+1)
        
        for i in range(2,self.size+1):
            self.lg[i]=self.lg[i>>1]+1
 
        for i in range(self.N0):
            tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
            tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
            self.table+=[tmp]
            self.index+=[tmp_id]
    
    # return func of [l,r)
    def query(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
    
    # return index of which val[i] = func of v among [l,r)
    def query_id(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
        if self.table[0][a]==self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))]):
            b=a
        return b
    
    # return boundary index of r such that func({table[i]} =< x , i in [pos,r]
    def right_bound(self,pos,x):
        k=(self.size-pos).bit_length()
        for j in range(k)[::-1]:
            nx=pos+(1<<j)
            if nx<n and self.query(pos,nx+1)<=x:
                pos+=(1<<j)
        return pos
    
    # return boundary index of l such that func({table[i]} =< x , i in [l,pos]
    def left_bound(self,pos,x):
        k=pos.bit_length()
        for j in range(k)[::-1]:
            nx=pos-(1<<j)
            if 0<=nx and self.query(nx,pos+1)<=x:
                pos-=(1<<j)
        return pos

    def __str__(self):
        return str(self.table[0])
 
    def print(self):
        for i in self.table:
            print(*i)
 

## return prime factors of N as dictionary {prime p:power of p}
## within 2 sec for N = 2*10**20+7
def primeFactor(N):
    i,n=2,N
    ret={}
    d,sq=2,99
    while i<=sq:
        k=0
        while n%i==0:
            n,k,ret[i]=n//i,k+1,k+1
        if k>0 or i==97:
            sq=int(n**(1/2)+0.5)
        if i<4:
            i=i*2-1
        else:
            i,d=i+d,d^6
    if n>1:
        ret[n]=1
    return ret


## return the list of prime numbers in [2,N], using eratosthenes sieve
## within 2sec for N = 1.2*10**7
def PrimeNumSet(N):
    Max = int(N**0.5)
    seachList = [i for i in range(2,N+1)]
    primeNum = []
    while seachList:
        if seachList[0] <= Max:
            break
        primeNum.append(seachList[0])
        tmp = seachList[0]
        seachList = [i for i in seachList if i % tmp != 0]
    primeNum.extend(seachList)
    return primeNum

## retrun LCM of numbers in list b
## within 2sec for no of B = 10*5  and  Bi < 10**6
def LCM(b,mo=10**9+7):
    prs=PrimeNumSet(max(b))
    M=dict(zip(prs,[0]*len(prs)))
    for i in b:
        dc=primeFactor(i)
        for j,k in dc.items():
            M[j]=max(M[j],k)
    
    r=1
    for j,k in M.items():
        if k!=0:
            r*=pow(j,k,mo)
            r%=mo
    return r

 
show_flg=False
show_flg=True
ans=0


n=I()
A=[]
B=[]

for i in rep(n):
    a,b=LI()
    A+=[a]
    B+=[b]

A.sort()
B.sort()
if n%2==0:
    kl=n//2-1
    kr=n//2
else:
    kl=n//2
    kr=n//2
lm,lM=A[kl],A[kr]
rm,rM=B[kl],B[kr]
L,R=0,0
if n%2==0:
    #ans=lM+rM-lm-rm+1
    L=lm+lM
    R=rm+rM
    ans=R-L+1
else:
    ans=rm-lm+1


print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

lower.sort()
upper.sort()

if n % 2 == 1:
    l_mid = lower[n // 2]
    u_mid = upper[n // 2]
    print(u_mid - l_mid + 1)
else:
    l_mid = (lower[n // 2] + lower[n // 2 - 1])
    u_mid = (upper[n // 2] + upper[n // 2 - 1])
    ans = u_mid - l_mid + 1
    print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import sys
    import statistics
    input = sys.stdin.readline
    n = int(input())
    a = [None] * n
    b = [None] * n
    for i in range(n):
        ai, bi = map(int, input().split())
        a[i] = ai
        b[i] = bi
    if n % 2 == 0:
        minmed = statistics.median_high(a) + statistics.median_low(a)
        maxmed = statistics.median_high(b) + statistics.median_low(b)
        print(maxmed - minmed + 1)
    else:
        print(statistics.median(b) - statistics.median(a) + 1)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n = int(input())
AB = [[0,0] for _ in range(n)]
A = [0]*n
B = [0]*n
for i in range(n):
    a,b = map(int,input().split())
    AB[i] = [a,b]
    A[i] = a
    B[i] = 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:
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:     # 奇数
    ans = B[N//2] - A[N//2] + 1
else:       # 偶数
    ans = (B[N//2] + B[N//2-1]) - (A[N//2] + A[N//2-1]) + 1
print(ans)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

N, *AB = map(int, sys.stdin.buffer.read().split())

A = sorted(AB[::2])
B = sorted(AB[1::2])

center = N//2

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

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])
# print(ab)
if n%2==0:
    min=ab[n//2-1][0]+ab[n//2][0]
else:
    min=ab[n//2][0]
ab.sort(key=lambda x:-x[1])
if n%2==0:
    max=ab[n//2-1][1]+ab[n//2][1]
else:
    max=ab[n//2][1]
print(max-min+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from statistics import median
sys.setrecursionlimit(10**8)
INF = float("inf")


def main():
    N = int(input())
    A = [0]*N
    B = [0]*N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    mA = median(A)
    mB = median(B)
    if N % 2 == 1:
        print(mB-mA+1)
    else:
        print(int(2*(mB-mA)+1))

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

A = []
B = []
for i in range(N):
    A.append(AB[i][0])
    B.append(AB[i][1])
A.sort()
B.sort()
if N%2==0:
    A1 = A[N//2-1]
    A2 = A[N//2]
    B1 = B[N//2-1]
    B2 = B[N//2]
    print((B1+B2)-(A1+A2)+1)
else:
    A1 = A[N//2]
    B1 = B[N//2]
    print((B1-A1)+1)

Submission status: Accepted

Solution code:
import sys


def solve_even(n, aaa, bbb):
    li = n // 2 - 1
    ri = n // 2
    aaa.sort()
    bbb.sort()
    ll = aaa[li]
    lr = aaa[ri]
    rl = bbb[li]
    rr = bbb[ri]
    return (rl + rr) - (lr + ll) + 1


def solve_odd(n, aaa, bbb):
    k = n // 2
    aaa.sort()
    bbb.sort()
    return bbb[k] - aaa[k] + 1


n, *ab = map(int, sys.stdin.buffer.read().split())
if n % 2 == 0:
    ans = solve_even(n, ab[0::2], ab[1::2])
else:
    ans = solve_odd(n, ab[0::2], ab[1::2])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())
l=[]
r=[]
for i in range(n):
    temp = list(map(int, input().split()))
    l.append(temp[0])
    r.append(temp[1])

l.sort()
r.sort()

if(n%2==1):
    dif = r[n//2] - l[n//2]
    #print(dif)
    print(dif+1)
else:
    dif2 = (r[n//2]+r[n//2-1]) - (l[n//2]+l[n//2-1])
    #print(dif2)
    print(dif2+1)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
mn = []
mx = []
for i in range(n):
    a,b = na()
    mn.append(a)
    mx.append(b)

mn.sort()
mx.sort()

if n%2 == 0:
    p = mn[n//2-1]+mn[n//2]
    q = mx[n//2-1]+mx[n//2]
    print(q-p+1)
else:
    p = mn[n//2]
    q = mx[n//2]
    print(q-p+1)

Submission status: Accepted

Solution code:
N = int(input())
if N % 2 == 1:
    odd = 1
else:
    odd = 0
L = [list(map(int, input().split())) for _ in range(N)]

L.sort()
A = [L[i][0] for i in range(N)]
if odd:
    minres = A[(N - 1) // 2]
else:
    minres = A[(N - 2) // 2] + A[N // 2]

L.sort(key=lambda x: x[1])
B = [L[i][1] for i in range(N)]
if odd:
    maxres = B[(N - 1) // 2]
else:
    maxres = B[(N - 2) // 2] + B[N // 2]

print(maxres - minres + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
    atmp,btmp = (int(num) for num in input().split())
    a[i] = atmp
    b[i] = btmp
    
a.sort()
b.sort()
minval = 0
maxval = 0
ans = 0
if n % 2 == 0:
    minval = (a[n//2-1] + a[n//2]) / 2
    maxval = (b[n//2-1] + b[n//2]) / 2
    ans = int((maxval - minval) * 2 + 0.00001) + 1
else:
    minval = a[n//2]
    maxval = b[n//2]
    ans = maxval - minval + 1
    
print(ans)

Submission status: Accepted

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

AB_on_A = sorted(AB)
AB_on_B = sorted(AB,key=lambda x:x[1])
if N%2==0:
  min_num = AB_on_A[N//2-1][0]+AB_on_A[N//2][0]
  max_num = AB_on_B[N//2-1][1]+AB_on_B[N//2][1]
  print(max_num-min_num+1)
else:
  min_num = AB_on_A[N//2][0]
  max_num = AB_on_B[N//2][1]
  print(max_num-min_num+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:
    ma = A[N//2]
    mb = B[N//2]
    print(max(0, mb - ma + 1))
else:
    ma = A[N//2] + A[N//2-1]
    mb = B[N//2] + B[N//2-1]
    print(max(0, mb - ma + 1))

Submission status: Accepted

Solution code:
def examA():
    A, B = LI()
    ans = A*B
    print(ans)
    return

def examB():
    N = I()
    A = LI()
    for a in A:
        if a==0:
            print(0)
            return
    ans = 1
    for a in A:
        ans *= a
        if ans>10**18:
            print(-1)
            return
    print(ans)
    return

def examC():
    A, B = list(map(float,input().split()))
    ans = (int(A*B*100))//100
    print(ans)
    return

def examD():
    def factorization(n):
        arr = defaultdict(int)
        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[i] = cnt
        if temp != 1:
            arr[temp] = 1
        if arr == []:
            arr[n] = 1
        return arr
    C = [0,1,3,6,10,15,21,28,36,45]
    N = I()
    F = factorization(N)
    ans = 0
    #print(F)
    for f in F.values():
        for i in range(10):
            if C[i]>f:
                ans += i-1
                break
    print(ans)
    return

def examE():
    N = I()
    A = [0]*N
    B = [0]*N
    for i in range(N):
        A[i],B[i] = LI()
    A.sort()
    B.sort()
    if N%2==0:
        L = (A[N//2-1] + A[N//2])/2
        R = (B[N//2-1] + B[N//2])/2
        ans = int((R-L)*2) +1
    else:
        L = A[N//2]
        R = B[N//2]
        ans = (R-L)+1

    print(ans)
    return

def examF():
    N, S = LI()
    A = LI()

    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 1<<60
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examE()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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

A, B = zip(*AB)
a = list(A)
b = list(B)
a.sort()
b.sort()

if N % 2:
    lb = a[(N - 1) // 2]
    ub = b[(N - 1) // 2]
    ans = ub - lb + 1
else:
    lb = (a[(N - 2) // 2] + a[N // 2]) / 2
    ub = (b[(N - 2) // 2] + b[N // 2]) / 2
    ans = int((ub - lb) * 2) + 1

print(ans)

Submission status: Accepted

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

a.sort()
b.sort()

if n%2:
  k1 = a[n//2]
  k2 = b[n//2]
else:
  k1 = a[n//2-1]+a[n//2]
  k2 = b[n//2-1]+b[n//2]

ans = k2-k1+1
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ab=[]
for _ in range(n):
	a,b=map(int,input().split())
	ab.append((a,b))
ab.sort()
if n%2==1:
	m=ab[n//2][0]
else:
	m=(ab[n//2-1][0]+ab[n//2][0])
ab.sort(key=lambda x:x[1])
if n%2==1:
	M=ab[n//2][1]
else:
	M=(ab[n//2-1][1]+ab[n//2][1])
print(M-m+1)