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

Solution in Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a, b = {}, {}
for i = 1, n do
  a[i], b[i] = io.read("*n", "*n")
end

local function solvelow(x)
  local c = 0
  for i = 1, n do
    if a[i] <= x then
      c = c + 1
    end
  end
  return n + 1 <= c * 2
end

local function solvehigh(x)
  local c = 0
  for i = 1, n do
    if x <= b[i] then
      c = c + 1
    end
  end
  return n + 1 <= c * 2
end

local function solvelow_even(x)
  local c = 0
  local lowmax, highmin = -1, 2000000007
  for i = 1, n do
    if a[i] <= x then
      c = c + 1
      if lowmax < a[i] then
        lowmax = a[i]
      end
    else
      if a[i] < highmin then
        highmin = a[i]
      end
    end
  end
  if n < c * 2 then return true
  elseif n == c * 2 then
    local diff = x - lowmax
    return highmin - x <= diff
  else
    return false
  end
end

local function solvehigh_even(x)
  local c = 0
  local lowmax, highmin = -1, 2000000007
  for i = 1, n do
    if x <= b[i] then
      c = c + 1
      if b[i] < highmin then
        highmin = b[i]
      end
    else
      if lowmax < b[i] then
        lowmax = b[i]
      end
    end
  end
  if n < c * 2 then return true
  elseif n == c * 2 then
    local diff = highmin - x
    return x - lowmax <= diff
  else
    return false
  end
end

if n % 2 == 1 then
  local min, max = 0, 1000000000
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if solvelow(mid) then
      max = mid
    else
      min = mid
    end
  end
  local lowlim = max
  min, max = 1, 1000000001
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if solvehigh(mid) then
      min = mid
    else
      max = mid
    end
  end
  local highlim = min
  print(highlim - lowlim + 1)
else
  for i = 1, n do
    a[i], b[i] = a[i] * 2, b[i] * 2
  end
  local min, max = 0, 2000000100
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if solvelow_even(mid) then
      max = mid
    else
      min = mid
    end
  end
  local lowlim = max
  min, max = 1, 2000000101
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if solvehigh_even(mid) then
      min = mid
    else
      max = mid
    end
  end
  local highlim = min
  print(highlim - lowlim + 1)
end