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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
 
using namespace std;
 
#define x first

typedef long long ll;
const int mod = 1000000007;
const int oo = 1e9;
const int base = 27;
typedef pair<int,int> pii;
 
int n;
pii pnt[200010];

vector< pair<int,int> > mx,mn;

bool check(int mnR,int mnB,int mxR,int mxB){
  for(int i = 0 ; i < n ; ++i){
    bool can = false;
    for(int it = 0 ; it < 2 ; ++it){
      swap(pnt[i].first,pnt[i].second);
      if(pnt[i].first >= mnR && pnt[i].first <= mxR && pnt[i].second >= mnB && pnt[i].second <= mxB)can = true;
    }
    if(!can)return false;
  }
  return true;
}
map<pair<pii,pii> , int> vis;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)scanf("%d%d",&pnt[i].x,&pnt[i].second);
  for (int i = 0 ; i < n ; ++i){
    mx.push_back(make_pair(max(pnt[i].x,pnt[i].second),i));
    mn.push_back(make_pair(min(pnt[i].x,pnt[i].second),i)); 
  }
  sort(mx.begin(),mx.end());
  sort(mn.begin(),mn.end());
  vector<int> all;
  for(int i = 0 ; i < min((int)mn.size(),4);++i){
    all.push_back(mn[i].second);
    all.push_back(mx[i].second);
  }
  for(int i = 0 ; i < min((int)mx.size(),4);++i){
    all.push_back(mn[mn.size()-1-i].second);
    all.push_back(mx[mx.size()-1-i].second);
  }
  sort(all.begin(),all.end());
  all.resize(unique(all.begin(),all.end())-all.begin());
  
  ll ans = 1e18;
  for(int msk = 0 ; msk < (1ll << (int)all.size());msk++){
    ll mnR=mod,mnB=mod,mxR = 0 , mxB = 0;
    for(int j = 0 ; j < all.size();++j){
      pii sw = (msk&(1<<j))?pnt[all[j]]:make_pair(pnt[all[j]].second,pnt[all[j]].x);
      mnR = min(mnR,1ll*sw.second);
      mxR = max(mxR,1ll*sw.second);

      mnB = min(mnB,1ll*sw.first);
      mxB = max(mxB,1ll*sw.first);
    }
    pii f = make_pair(min(mnB,mnR),max(mnB,mnR));
    pii s = make_pair(min(mxB,mxR),max(mxB,mxR));
    if(vis.find(make_pair(f,s)) != vis.end())continue;
    vis[make_pair(f,s)] =1 ;
    if(check(mnR,mnB,mxR,mxB)){
      ans = min(ans,1ll*(mxR-mnR)*(1ll*mxB - mnB));
    }
  }
  cout << ans << endl;
  return 0;
}