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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    map<int, int> rev_a, rev_b;
    vector<pair<int, int>> ab(2*n);
    for(int i=0; i<n; i++){
        cin >> a[i] >> b[i];
        ab[2*i] = make_pair(a[i], 1);
        ab[2*i+1] = make_pair(b[i], -1);
        rev_a[a[i]] = i;
        rev_b[b[i]] = i;
    }
    sort(ab.begin(), ab.end());

    int ans = 0;
    if(n % 2 == 1){
        int left = 0, right = n;
        int start = 0;
        for(int i=0; i<2*n; i++){
            if(ab[i].second == 1) left++;
            else right--;
            if(start == 0 && left >= (n+1)/2 && right >= (n+1)/2){
                start = ab[i].first;
            }
            else if(start != 0 && (left < (n+1)/2 || right < (n+1)/2)){
                ans = ab[i].first - start + 1;
                break;
            }
        }
    }
    else{
        int left = 0, right = n;
        int l_l = 0, l_u = 0, r_l = 0, r_u = 0;
        for(int i=0; i<2*n; i++){
            if(ab[i].second == 1) left++;
            else right--;
            if(left >= n/2 && right >= n/2){
                if(l_l == 0){
                    l_l = ab[i].first;
                    l_u = b[rev_a[l_l]];
                }
                else{
                    if(ab[i].second == -1){
                        l_u = max(l_u, ab[i].second);
                    }
                    else if(r_l == 0){
                        r_l = ab[i].first;
                    }
                }
            }
            else if(l_l != 0 && (left < n/2 || right < n/2)){
                r_u = ab[i].first;
                if(r_l == 0){
                    r_l = a[rev_b[r_u]];
                }
                ans = (r_u + l_u) - (r_l + l_l) + 1;
                break;
            }
        }
    }

    cout << ans << endl;
    return 0;
}