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: Accepted)

#include <bits/stdc++.h>
 
#define FOR(i, a, b) for(auto i=a; i<=b; ++i)
#define REP(i, a, b) for(auto i=a; i<b; ++i)
#define FORI(i, a, b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))
#define REPI(i, a, b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))
#define ALL(v) v.begin(),v.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a, b) emplace_back(a, b)
#define fir first
#define sec second
#define what_is(x) cout<<#x<<" is "<<x<<endl;
#define type(x) typeid(x).name()
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define min3(a,b,c) min(min(a,b),c)
#define max3(a,b,c) max(max(a,b),c)
#define PI acos(-1)
#define open_read freopen("christmas.in", "r", stdin)
#define open_write freopen("christmas.out", "w", stdout)
 
using namespace std;
 
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
typedef pair<PII, PII> PPIIPII;
typedef pair<LL, LL> PLL;
typedef pair<ULL, ULL> PUU;
typedef pair<LL, PLL> PLPLL;
typedef pair<PLL, PLL> PPLLPLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
 
template<typename T, typename T1>
ostream& operator << (ostream &out, pair<T, T1> obj) {
    out << "(" << obj.first << ", " << obj.second << ")";
    return out;
}
 
template<typename T, typename T1>
ostream& operator << (ostream &out, map<T, T1> cont) {
    typename map<T, T1>::const_iterator itr = cont.begin();
    typename map<T, T1>::const_iterator ends = cont.end();
 
    for(; itr != ends; ++itr) {
        out << *itr << " ";
    }
 
    out<<endl;
    return out;
} 

template<typename T>
ostream& operator << (ostream &out, set<T> cont) {
    typename set<T>::const_iterator itr = cont.begin();
    typename set<T>::const_iterator ends = cont.end();
    
    for(; itr != ends; ++itr) {
        out << *itr << " ";
    }
 
    out << endl;
    return out;
}
  
template<typename T>
ostream& operator << (ostream &out, multiset<T> cont) {
    typename multiset<T>::const_iterator itr = cont.begin();
    typename multiset<T>::const_iterator ends = cont.end();
 
    for (; itr != ends; ++itr) {
        out << *itr << " ";
    }
 
    out << endl;
    return out;
}

template<typename T, template<typename ELEM, typename ALLOC=allocator<ELEM>> class CONT>
ostream& operator << (ostream &out, CONT<T> cont) {
    typename CONT<T>::const_iterator itr = cont.begin();
    typename CONT<T>::const_iterator ends = cont.end();
 
    for (; itr != ends; ++itr) {
        out << *itr << " ";
    }
 
    out << endl;
    return out;
}
 
template<typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator << (basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
    REP (i, 0, N) {
        out << arr[i] << " ";
    }
 
    out << endl;
    return out;
}
  
template<typename T>
T GCD(T a, T b) {
    T min_v = min(a, b);
    T max_v = max(a, b);
 
    while (min_v) {
        T temp = max_v % min_v;
        max_v = min_v;
        min_v = temp;
    }
 
    return max_v;
}
 
template<typename T>
T LCM(T a, T b) {
    return (a * b) / gcd(a, b);
}
 
template<typename T>
T fastExpPow(T base, T exp, T mod) {
    T res = 1;
 
    while (exp) {
        if (exp & 1) {
            res *= base;
            res %= mod;
        }
 
        exp >>= 1;
        base *= base;
        base %= mod;
    }
 
    return res % mod;
}
 
/*#################################################################################################################
###################################################################################################################
###################################################################################################################
#################################################################################################################*/

#define MAXN 1000000000

int N;
vector<PLL> points;


int main() {   
    scanf("%d", &N);

    REP (i, 0, N) {
        LL A, B;
        scanf("%lld%lld", &A, &B);

        points.eb(A, 1);
        points.eb(B, 2);
    }
    
    sort(ALL(points));
    LL open = 0, neca = 0, minLft = 0, maxLft = 0;

    for (int i = 0; i < (int)points.size(); ++i) {
        if (points[i].sec == 1) {
            ++open;
        } else if (points[i].sec == 2) {
            --open;
            ++neca;
        }

        while (i + 1 < (int)points.size() && points[i] == points[i + 1]) {
            if (points[i].sec == 1) {
                ++open;
            } else if (points[i].sec == 2) {
                --open;
                ++neca;
            }

            ++i;
        }

        if (open + neca >= (N + 1) / 2 && minLft == 0) {
            minLft = points[i].fir;
        }
        
        if (neca >= (N + 1) / 2 && maxLft == 0) {
            maxLft = points[i].fir;
        }
    }
    
    open = 0, neca = 0;
    LL maxRgt = 0, minRgt = 0;

    for (int i = (int)points.size() - 1; i >= 0; --i) {
        if (points[i].sec == 2) {
            ++open;
        } else if (points[i].sec == 1) {
            --open;
            ++neca;
        }

        while (i - 1 >= 0 && points[i] == points[i - 1]) {
            if (points[i].sec == 2) {
                ++open;
            } else if (points[i].sec == 1) {
                --open;
                ++neca;
            }

            --i;
        }
    
        if (open + neca >= (N + 1) / 2 && maxRgt == 0) {
            maxRgt = points[i].fir;
        }

        if (neca >= (N + 1) / 2 && minRgt == 0) {
            minRgt = points[i].fir;
        }
    }

    // cout << minLft << " " << maxLft << endl;
    // cout << minRgt << " " << maxRgt << endl;
    LL answer = maxLft + maxRgt - minLft - minRgt + 1;

    if (N & 1) {
        answer = maxRgt - minLft + 1;
    }

    printf("%lld\n", answer);
    return 0;
}