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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

class Program
{
    const string Yes = "Yes";
    const string No = "No";
    const char White = '.';
    const char Black = '#';
    const long Mod = 1000000007;

    static void Main(string[] args) {
#if DEBUG
        MyDebugger.Test(process, "../TextFile1.txt");
#else
        checked {
            object ret = process(new StreamReader(Console.OpenStandardInput()));
            Console.WriteLine(ret);
        }
#endif
    }

    static object process(TextReader input) {
        var n = int.Parse(input.ReadLine());
        //var ab = new int[n][];
        //for(int i=0; i<n; i++) {
        //    ab[i] = ToIntArray(input.ReadLine(), 2);
        //}
        var aa = new int[n];
        var bb = new int[n];
        for (int i = 0; i < n; i++) {
            var spl = input.ReadLine().Split(' ');
            aa[i] = int.Parse(spl[0]);
            bb[i] = int.Parse(spl[1]);
        }

        Array.Sort(aa);
        Array.Sort(bb);

        var mins = new int[n];
        var maxs = new int[n];

        for(int i=0; i<n; i++) {
            mins[i] = aa[i];
            maxs[i] = bb[i];
        }

        if(n % 2 == 1) {
            var ce = (n+1)/2 -1;
            return maxs[ce] - mins[ce] + 1;
        } else {
            var ce1 = n/2-1;
            var ce2 = n/2+1-1;

            var hash = new HashSet<RationalNumber>();

            for(int i=mins[ce1]; i<=maxs[ce1]; i++) {
                for(int j=Math.Max(mins[ce2], i); j<=maxs[ce2]; j++) {
                    var y = new RationalNumber(j + i, 2);

                    if(!hash.Contains(y)) hash.Add(y);
                    //Debug.WriteLine(y);
                }
            }

            return hash.Count();
        }
    }

    class RationalNumber : IEquatable<RationalNumber>, IEquatable<long>, IComparable<RationalNumber>
    {
        private bool sign = true;
        private long a = 0;
        private long b = 1;

        private static Regex Pattern
            = new Regex("\\s*(-?[0-9]+)\\s*(/\\s*([0-9]+)\\s*)?");

        //--------------------

        /// <summary>
        /// 分子の絶対値を取得します。
        /// </summary>
        public long Numerator {
            get { return a; }
        }

        /// <summary>
        /// 分母の絶対値を取得します。
        /// </summary>
        public long Denominator {
            get { return b; }
        }

        /// <summary>
        /// 小数値を取得します。
        /// </summary>
        public double DoubleValue {
            get {
                var v = (double)a / (double)b;
                return SignValue * v;
            }
        }
        /// <summary>
        /// 値が0であるかを取得します。
        /// </summary>
        public bool IsZero {
            get { return a == 0; }
        }

        /// <summary>
        /// 正の数の場合1、負の数の場合-1を取得します。
        /// </summary>
        public int SignValue {
            get { return this.sign ? 1 : -1; }
        }

        //--------------------

        /// <summary>
        /// 0を生成します。
        /// </summary>
        public RationalNumber() {
        }

        /// <summary>
        /// 分子と分母を指定して数を生成します。
        /// </summary>
        /// <param name="a">分子</param>
        /// <param name="b">分母</param>
        public RationalNumber(long a, long b = 1) {
            if (b == 0) {
                throw new DivideByZeroException("分母が0に指定されました。");
            }
            if (a == 0) {
                sign = true;
                this.a = 0;
                this.b = 1;
            } else {
                sign = IsSign(a, b);
                this.a = Math.Abs(a);
                this.b = Math.Abs(b);
                Reduction();
            }
        }

        //--------------------

        /// <summary>
        /// 分数形式で表記されたの文字列から数を生成します。
        /// </summary>
        /// <param name="s">分数または整数式の文字列</param>
        public static RationalNumber Parse(string s) {
            var mc = Pattern.Match(s);
            if (!mc.Success) {
                throw new FormatException("文字列が分数の形式ではありません。");
            }
            var a = long.Parse(mc.Groups[1].Value);
            var b = mc.Groups[3].Success ? long.Parse(mc.Groups[3].Value) : 1;
            return new RationalNumber(a, b);
        }

        /// <summary>
        /// 符号を検証します。
        /// </summary>
        private bool IsSign(long a, long b) {
            if (a >= 0 && b >= 0) return true;
            if (a < 0 && b < 0) return true;
            return false;
        }

        /// <summary>
        /// 約分します。
        /// </summary>
        private void Reduction() {
            var c = Gcd(a, b);
            a /= c;
            b /= c;
        }

        /// <summary>
        /// 逆数を取得します。
        /// </summary>
        /// <returns>この有理数の逆数を表すオブジェクト</returns>
        public RationalNumber Inverse() {
            return new RationalNumber(b * SignValue, a);
        }

        /// <summary>
        /// 約分された分数の文字列表現を取得します。
        /// </summary>
        public override string ToString() {
            return $"{(sign ? "" : "-")}{a}{(b > 1 ? $"/{b}" : "")}";
        }

        /// <summary>
        /// 数値の一致を評価します。
        /// </summary>
        public bool Equals(RationalNumber other) {
            return sign == other.sign
                && a == other.a && b == other.b;
        }

        /// <summary>
        /// オブジェクトがこのクラスのインスタンスかつ数値が一致するかを評価します。
        /// </summary>
        public override bool Equals(object obj) {
            var x = obj as RationalNumber;
            return x != null && Equals(x);
        }

        /// <summary>
        /// 数値の一致を評価します。
        /// </summary>
        public bool Equals(long v) {
            if (b != 1) return false;
            var aa = SignValue * a;
            return aa == v;
        }

        /// <summary>
        /// 指定した値との大小を評価します。
        /// </summary>
        public int CompareTo(RationalNumber other) {
            if (this > other) return 1;
            if (this < other) return -1;
            return 0;
        }

        public override int GetHashCode() {
            //return sign.GetHashCode()
            //    ^ a.GetHashCode() 
            //    ^ b.GetHashCode();
            //return ToString().GetHashCode();
            return ((0x5555555555555555 & a) ^ (0x2AAAAAAAAAAAAAAA & b)).GetHashCode();
        }

        //--------------------

        //演算
        public static RationalNumber operator +(RationalNumber x1, RationalNumber x2) {
            var c = Lcm(x1.b, x2.b);
            var x1a = x1.a * (c / x1.b) * x1.SignValue;
            var x2a = x2.a * (c / x2.b) * x2.SignValue;
            return new RationalNumber(x1a + x2a, c);
        }
        public static RationalNumber operator -(RationalNumber x1, RationalNumber x2) {
            var c = Lcm(x1.b, x2.b);
            var x1a = x1.a * (c / x1.b) * x1.SignValue;
            var x2a = x2.a * (c / x2.b) * x2.SignValue;
            return new RationalNumber(x1a - x2a, c);
        }
        public static RationalNumber operator *(RationalNumber x1, RationalNumber x2) {
            var x1a = x1.a * x1.SignValue;
            var x2a = x2.a * x2.SignValue;
            return new RationalNumber(x1a * x2a, x1.b * x2.b);
        }
        public static RationalNumber operator /(RationalNumber x1, RationalNumber x2) {
            var x1a = x1.a * x1.SignValue;
            var x2a = x2.a * x2.SignValue;
            return new RationalNumber(x1a * x2.b, x2a * x1.b);
        }
        public static bool operator ==(RationalNumber x1, RationalNumber x2) {
            return x1.Equals(x2);
        }
        public static bool operator !=(RationalNumber x1, RationalNumber x2) {
            return !x1.Equals(x2);
        }
        public static bool operator >(RationalNumber x1, RationalNumber x2) {
            var c = Lcm(x1.b, x2.b);
            var x1a = x1.a * (c / x1.b) * x1.SignValue;
            var x2a = x2.a * (c / x2.b) * x2.SignValue;
            return x1a > x2a;
        }
        public static bool operator >=(RationalNumber x1, RationalNumber x2) {
            return x1 == x2 || x1 > x2;
        }
        public static bool operator <(RationalNumber x1, RationalNumber x2) {
            return !(x1 >= x2);
        }
        public static bool operator <=(RationalNumber x1, RationalNumber x2) {
            return !(x1 > x2);
        }

        //整数との演算
        public static RationalNumber operator +(RationalNumber x1, long i) {
            var x1a = x1.a * x1.SignValue;
            return new RationalNumber(x1a + i * x1.b, x1.b);
        }
        public static RationalNumber operator -(RationalNumber x1, long i) {
            var x1a = x1.a * x1.SignValue;
            return new RationalNumber(x1a - i * x1.b, x1.b);
        }
        public static RationalNumber operator *(RationalNumber x1, long i) {
            var x1a = x1.a * x1.SignValue;
            return new RationalNumber(x1a * i, x1.b);
        }
        public static RationalNumber operator /(RationalNumber x1, long i) {
            if (i == 0) throw new DivideByZeroException("0で除算を行いました。");
            var x1a = x1.a * x1.SignValue;
            return new RationalNumber(x1a, x1.b * i);
        }
        public static bool operator ==(RationalNumber x1, long i) {
            return x1.Equals(i);
        }
        public static bool operator !=(RationalNumber x1, long i) {
            return !x1.Equals(i);
        }
        public static bool operator >(RationalNumber x1, long i) {
            return x1.a * x1.SignValue > i * x1.b;
        }
        public static bool operator >=(RationalNumber x1, long i) {
            return x1 == i || x1 > i;
        }
        public static bool operator <(RationalNumber x1, long i) {
            return !(x1 >= i);
        }
        public static bool operator <=(RationalNumber x1, long i) {
            return !(x1 > i);
        }

        /// <summary>
        /// 最大公約数
        /// </summary>
        private static long Gcd(long m, long n) {
            var x1 = Math.Max(m, n);
            var x2 = Math.Min(m, n);

            while (true) {
                var mod = x1 % x2;
                if (mod == 0) return x2;
                x1 = x2;
                x2 = mod;
            }
        }

        /// <summary>
        /// 最小公倍数
        /// </summary>
        private static long Lcm(long m, long n) {
            var x1 = Math.Max(m, n);
            var x2 = Math.Min(m, n);

            var gcd = Gcd(x1, x2);
            return x1 / gcd * x2;
        }
    }

    static int[] ToIntArray(string strs, int n) {
        var ret = new int[n];
        var spl = strs.Split(' ');
        for (int i = 0; i < n; i++)
            ret[i] = int.Parse(spl[i]);
        return ret;
    }
}