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 PyPy2 (7.3.0) (judge verdict: Wrong Answer)

input = raw_input

n = int(input())

pairs = []
kugiri = []
for i in range(n):
    pair = map(int, input().split(" "))
    a, b = pair
    pairs.append(pair)
    kugiri += [a-1, a, b, b+1]

kugiri = set(kugiri)
status_dict = {}
for key in kugiri:
    status_dict[key] = [0, 0]

for a, b in pairs:
    status_dict[a][0] += 1
    status_dict[b][1] += 1

n_kugiri = len(kugiri)
kugiri = sorted(kugiri)
nums = [0] * n_kugiri

count = 0
for i, key in enumerate(kugiri):
    na, nb = status_dict[key]
    count += na
    j = kugiri.index(key)
    nums[j] = count 

    count -= nb


#for a, b in pairs:
#    ai = kugiri.index(a)
#    bi = kugiri.index(b)
#    for j in range(ai, bi+1):
#        nums[j] += 1

# odd
#print(kugiri)
#print(nums)
now_ok = False
out = 0
if n % 2 == 1:
    ok_num = (n+1) // 2 
    for i in range(n_kugiri):
        cur_val = kugiri[i]
        cur_num = nums[i]

        if cur_num >= ok_num:
            if now_ok:
                max_kugiri = cur_val
            else:
                now_ok = True
                min_kugiri = cur_val
                max_kugiri = cur_val
        else:
            if now_ok:
                now_ok = False
                out += max_kugiri - min_kugiri + 1
else:
    ok_num = 1 + (n // 2) 
    for i in range(n_kugiri):
        cur_val = kugiri[i]
        cur_num = nums[i]

        if cur_num >= ok_num:
            if now_ok:
                max_kugiri = cur_val
                max_i = i
            else:
                now_ok = True
                min_kugiri = cur_val
                max_kugiri = cur_val
                min_i = i
                max_i = i
        else:
            if now_ok:
                now_ok = False
                cur_out = max_kugiri - min_kugiri + 1
                cur_out += max_kugiri - min_kugiri

                if min_i-1 >= 0 and nums[min_i-1] >= ok_num - 1:
                    cur_out += 1
                if max_i+1 <= n_kugiri-1  and nums[max_i+1] >= ok_num - 1:
                    cur_out += 1

                out += cur_out

print(out)