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: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
#define F first
#define S second
vector<int> g[222222];
signed main(){
  int n;
  cin>>n;
  vector<P> xy(n);
  for(int i=0;i<n;i++) cin>>xy[i].F>>xy[i].S;
  for(int i=0;i<n;i++) if(xy[i].F<xy[i].S) swap(xy[i].F,xy[i].S);
  sort(xy.begin(),xy.end());
  xy.erase(unique(xy.begin(),xy.end()),xy.end());
  n=xy.size();
  int x[n],y[n];
  for(int i=0;i<n;i++) x[i]=xy[i].F,y[i]=xy[i].S;
  //exit(0);
  vector<int> v;
  for(int i=0;i<n;i++){
    v.push_back(x[i]);
    v.push_back(y[i]);
  }
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  //exit(0);
  map<int,int> r;
  for(int i=0;i<(int)v.size();i++) r[v[i]]=i;
  int sx=x[0],sy=y[0];
  for(int i=0;i<n;i++){
    //cout<<i<<" "<<x[i]<<" "<<y[i]<<endl;
    sx=min(sx,x[i]);
    sy=min(sy,y[i]);
  }
  //exit(0);
  map<P,int> m1,m2;
  for(int i=0;i<n;i++){
    m1[P(x[i],y[i])]=i;
  }
  //exit(0);
  for(int i=0;i<n;i++){
    if(!r.count(x[i])) exit(0); 
    if(!r.count(y[i])) exit(0);
    g[r[x[i]]].push_back(y[i]);
    if(x[i]!=y[i]) g[r[y[i]]].push_back(x[i]);
  }
  //exit(0);
  int used[n];
  memset(used,0,sizeof(used));
  int cnt=0;
  int ans=1LL<<62LL;
  map<int,int> mi;
  for(int i=r[sx];i<(int)v.size();i++){
    for(int j=0;j<(int)g[i].size();j++){
      if(v[i]>g[i][j]){
	if(!used[m1[P(v[i],g[i][j])]]){
	  mi[g[i][j]]++;
	  cnt++;
	  continue;
	}
	//cout<<v[i]<<"-"<<g[i][j]<<endl;
	mi[g[i][j]]++;
	mi[v[i]]--;
	if(!mi[v[i]]) mi.erase(mi.find(v[i]));
      }else{
	used[m1[P(g[i][j],v[i])]]=1;
	mi[g[i][j]]++;
	cnt++;
      }
    }
    //cout<<i<<":"<<cnt<<endl;
    //for(P p:mi) cout<<p.first<<" "<<p.second<<endl;
    if(cnt==n){
      auto latte=mi.end();
      --latte;
      ans=min(ans,abs(v[i]-sx)*abs(latte->first-sy));
    }
  }
  cout<<ans<<endl;
  return 0;
}