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 fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
    enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;

bool comp(pair<int,int> a, pair<int, int> b)
{
	return a.second<b.second;
}

void solve()
{
	int n;
	cin>>n;
	vector<pair<int,int>> a(n), b(n);
	for (int i=0; i<n; i++) {
		cin>>a[i].first >> a[i].second;
		b[i].first = a[i].first;
		b[i].second = a[i].second;
	}	
	sort(a.begin(), a.end());
	sort(b.begin(), b.end(), comp);
	if (n&1) {
		if (n==1) {
			cout << a[0].second-a[0].first+1 << "\n";
			return;
		}
		int l1,r1,l2,r2;
		l1 = a[n/2].first;
		r1 = min(a[n/2].second, a[n/2+1].first);
		l2 = max(b[n/2].first, b[n/2-1].second);
		r2 = b[n/2].second;
		debug() << imie(l1) imie(r1) imie(l2) imie(r2);
		ll ans = 0;
		if (r2<l1 || r1<l2) {
			ans = (r1-l1+1) + (r2-l2+1);
		}
		else {
			l1 = min(l1,l2);
			r1 = max(r1, r2);
			ans = r1-l1+1;
		}
		cout << ans << "\n";
		return;
	}
	else {
		if (n==2) {
			int l = a[0].first+a[1].first;
			int r = a[0].second+a[1].second;
			cout << r-l+1 << "\n";
			return;
		}
		int l1,r1,l2,r2;
		l1 = (a[n/2-1].first+a[n/2].first);
		r1 = min(2*a[n/2+1].first, (a[n/2-1].second+a[n/2].second));
		r2 = b[n/2-1].second+b[n/2].second;
		l2 = max(2*b[n/2-2].second, (b[n/2-1].first+b[n/2].first));
		ll ans = 0;
		debug() << imie(l1) imie(r1) imie(l2) imie(r2);
		if (r2<l1 || r1<l2) {
			ans = (r1-l1+1) + (r2-l2+1);
		}
		else {
			l1 = min(l1,l2);
			r1 = max(r1, r2);
			ans = r1-l1+1;
		}
		cout << ans << "\n";
		return;
	}
}

int main()
{
	int t=1;
	// cin>>t;
	while (t--) {
		solve();
	}
	return 0;
}