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 Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use std::cmp::*;
use std::collections::*;
use std::mem::*;
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { let mut $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { let $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }

fn main() {
    let (r, w, e) = (std::io::stdin(), std::io::stdout(), std::io::stderr());
    let mut io = IO::new(r.lock(), w.lock(), e.lock());

    // let mut a: (i64) = io.read();
    // let mut b: (f32) = io.read();
    // let b = (b * 100f32).floor() as i64;

    // let mut ans = 0;
    // let mut bb = b;
    // io.debug(bb);
    // let mut aaa = Vec::new();
    // for i in 0..3 {
    //     let tmp = (bb % 10) * a;
    //     aaa.push(tmp);
    //     bb /= 10;
    // }
    // ans = aaa[0] + aaa[1] * 10 + aaa[2] * 100;
    // ans /= 100;
    // io.writeln(ans);
    //
    // 半分以上現れる最小値

    // a /= 100;
    // io.writeln(a * (b * 100f32) as u128);
    // let b = b * 10000f32;
    // let b = b as u128;
    // let c = (a * b) / 10000 as u128;
    // io.writeln(c);

    let n: usize = io.read();
    let mut rng = Vec::new();
    for i in 0..n {
        let (a, b): (i64, i64) = (io.read(), io.read());
        rng.push((a, b));
    }
    rng.sort();
    // let mut min0 = rng[0].0;
    // let mut min1 = rng.iter().min_by_key(|x| x.1).unwrap().1;
    // let mut max0 = rng.iter().max_by_key(|x| x.0).unwrap().0;
    // let mut max1 = rng.iter().max_by_key(|x| x.1).unwrap().1;
    // println!("{} {} {} {}", min0, min1, max0, max1);

    // binarysera
    if n % 2 == 0 {
        let mut ok = 1e9 as i64;
        let mut ng = 0 as i64;
        while (ok - ng).abs() > 1 {
            let mid = ((ok + ng) / 2);
            let mut l = 0i64;
            let mut r = 0i64;
            let mut m = 0i64;
            for (i, &v) in rng.iter().enumerate() {
                if v.1 <= mid {
                    m += 1;
                } else {
                    r += 1;
                }
            }
            if m >= (r - l).abs() as i64 {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        // io.writeln(ok);
        let mut a1 = ok;

        let mut ok = 0 as i64;
        let mut ng = 1e9 as i64;
        while (ok - ng).abs() > 1 {
            let mid = ((ok + ng) / 2);
            let mut l = 0i64;
            let mut r = 0i64;
            let mut m = 0i64;
            for (i, &v) in rng.iter().enumerate() {
                if v.0 >= mid {
                    m += 1;
                } else {
                    r += 1;
                }
            }
            if m >= (r - l).abs() as i64 {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        // io.writeln(ok);
        let mut a2 = ok;
        io.debug2(a1, a2);

        if n % 2 == 0 {
            io.writeln(a1 - a2 + 3);
        } else {
            io.writeln(a1 - a2 + 1);
        }
    } else {
        // k位数
        let mut ok = 1e9 as i64;
        let mut ng = 0 as i64;
        while (ok - ng).abs() > 1 {
            let mid = ((ok + ng) / 2);
            let mut l = 0i64;
            let mut r = 0i64;
            let mut m = 0i64;
            for (i, &v) in rng.iter().enumerate() {
                if v.1 <= mid {
                    m += 1;
                } else {
                    r += 1;
                }
            }
            if m > (r - l).abs() as i64 {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        // io.writeln(ok);
        let mut a1 = ok;

        let mut ok = 0 as i64;
        let mut ng = 1e9 as i64;
        while (ok - ng).abs() > 1 {
            let mid = ((ok + ng) / 2);
            let mut l = 0i64;
            let mut r = 0i64;
            let mut m = 0i64;
            for (i, &v) in rng.iter().enumerate() {
                if v.0 >= mid {
                    m += 1;
                } else {
                    r += 1;
                }
            }
            if m > (r - l).abs() as i64 {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        // io.writeln(ok);
        let mut a2 = ok;
        io.debug2(a1, a2);

        if n % 2 == 0 {
            io.writeln(a1 - a2 + 1);
        } else {
            io.writeln(a1 - a2 + 1);
        }
    }
    // io.debug(&rng);
}

static MOD: i64 = 1000000007;
static INF: i64 = std::i64::MAX >> 1;
// IO
use std::io::*;
struct IO<R: Read, W: Write, E: Write> {
    Read: R,
    Write: BufWriter<W>,
    Error: BufWriter<E>,
}
impl<R: Read, W: Write, E: Write> IO<R, W, E> {
    fn new(r: R, w: W, e: E) -> IO<R, W, E> {
        IO {
            Read: r,
            Write: BufWriter::new(w),
            Error: BufWriter::new(e),
        }
    }
    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
    {
        let next: String = self
            .Read
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect();
        next.parse().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
    fn readn<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect::<Vec<_>>()
    }
    fn readmat<T: std::str::FromStr>(&mut self, n: usize, m: usize) -> Vec<Vec<T>> {
        (0..n).map(|_| self.readn(m)).collect()
    }
    fn write<S: ToString>(&mut self, val: S) {
        self.Write.write(val.to_string().as_bytes());
    }
    fn writeln<S: ToString>(&mut self, val: S) {
        self.Write.write(val.to_string().as_bytes());
        self.Write.write(b"\n");
    }
    fn debug<S: std::fmt::Debug>(&mut self, val: S) {
        writeln!(self.Error, "(debug) {:?}", &val);
    }
    fn debug2<S: std::fmt::Debug, T: std::fmt::Debug>(&mut self, val1: S, val2: T) {
        writeln!(self.Error, "(debug2) {:?} -> {:?}", val1, val2);
    }
    fn debug3<S: std::fmt::Debug, T: std::fmt::Debug, U: std::fmt::Debug>(
        &mut self,
        val1: S,
        val2: T,
        val3: U,
    ) {
        writeln!(
            self.Error,
            "(debug3) {:?} -> {:?} -> {:?}",
            val1, val2, val3
        );
    }
}