Problem:
Score : 700 points

Problem Statement

There are N bags, each containing two white balls. The i-th box contains two balls with integers x_i and y_i written on them, respectively.

For each of these bags, you will paint one of the balls red, and paint the other blue.

Afterwards, the 2N balls will be classified according to color.

Then, we will define the following:

R_{max}: the maximum integer written on a ball painted in red

R_{min}: the minimum integer written on a ball painted in red

B_{max}: the maximum integer written on a ball painted in blue

B_{min}: the minimum integer written on a ball painted in blue

Find the minimum possible value of (R_{max} - R_{min}) \times (B_{max} - B_{min}).

Constraints

1 ≤ N ≤ 200,000

1 ≤ x_i, y_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum possible value.

Sample Input 1

3
1 2
3 4
5 6

Sample Output 1

15

The optimal solution is to paint the balls with x_1, x_2, y_3 red, and paint the balls with y_1, y_2, x_3 blue.

Sample Input 2

3
1010 10
1000 1
20 1020

Sample Output 2

380

Sample Input 3

2
1 1
1000000000 1000000000

Sample Output 3

999999998000000001

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use io::*;
use std::cmp;
use std::collections::BTreeMap;

fn main() {
    let input = read_string();
    let mut sc = Scanner::new(&input);
    let n: usize = sc.next();
    let mut v: Vec<Vec<i64>> = sc.next_mat(2, n);
    for i in 0..n {
        if v[i][0] > v[i][1] {
            let t = v[i][0];
            v[i][0] = v[i][1];
            v[i][1] = t;
        }
    }
    let mut mini = 0;
    let mut maxi = 0;
    for i in 1..n {
        if v[mini][0] > v[i][0] {
            mini = i;
        }
        if v[maxi][1] < v[i][1] {
            maxi = i;
        }
    }
    let min2 = v.iter().min_by_key(|x| x[1]).unwrap()[1];
    let max1 = v.iter().max_by_key(|x| x[0]).unwrap()[0];
    let mut ans = (max1 - v[mini][0]) * (v[maxi][1] - min2);
    if maxi != mini {
        let r = v[maxi][1] - v[mini][0];
        let mut map: BTreeMap<i64,usize> = BTreeMap::new();
        for i in 0..n {
            let c = map.entry(v[i][0]).or_insert(0);
            *c += 1;
        }
        v.sort_by_key(|x| x[0]);
        let mut ans_t = max1 - v[0][0];
        for i in 0..n {
            {
                let &x = map.get(&v[i][0]).unwrap();
                if x == 1 {
                    map.remove(&v[i][0]);
                } else {
                    map.insert(v[i][0],x-1);
                }
            }
            {
                let mut x = map.entry(v[i][1]).or_insert(0);
                *x += 1;
            }
            ans_t = cmp::min(ans_t, map.keys().last().unwrap() - *map.keys().nth(0).unwrap());
        }

        ans = cmp::min(ans, r * ans_t);
    }
    println!("{}",ans);
}

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }

        pub fn next_mat<T: FromStr>(&mut self, w: usize, h: usize) -> Vec<Vec<T>> {
            (0..h).map(|_| self.next_vec(w)).collect()
        }

        pub fn next_vec_char(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_mat_char(&mut self, n: usize) -> Vec<Vec<char>> {
            (0..n).map(|_| self.next_vec_char()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}