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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Codeforces
{
	internal class Template
	{

		private void Solve()
		{
			var n = cin.NextLong();
			var a = new Tuple<int, int>[n];
			var tree1 = new Treap<int>();
			var tree2 = new Treap<int>();
			tree1.Min = tree2.Min = int.MaxValue;
			tree1.Max = tree2.Max = int.MinValue;
			for (var i = 0; i < n; i++)
			{
				var x = cin.NextInt();
				var y = cin.NextInt();
				a[i] = Tuple.Create(x, y);
				tree1.Add(a[i].Item1);
				tree2.Add(a[i].Item2);
			}
			a = a.OrderBy(x => x.Item1.CompareTo(x.Item2)).ToArray();
			var res = 1L * (tree1.Max - tree1.Min)*(tree2.Max - tree2.Min);
			for (var i = 0; i < a.Length; i++)
			{
				tree1.Remove(a[i].Item1);
				tree1.Add(a[i].Item2);
				tree2.Remove(a[i].Item2);
				tree2.Add(a[i].Item1);
				res = Math.Min(res, 1L * (tree1.Max - tree1.Min)*(tree2.Max - tree2.Min));
			}
			Console.WriteLine(res);
		}

		public class Treap<T>
		{
			private static Random rnd = new Random(13);

			public T Max, Min;

			public class Node
			{
				public readonly T Value;
				public readonly int Priority;
				public Node Left, Right;
				public Node(T value)
				{
					Value = value;
					Priority = rnd.Next();
				}
			}

			private class NodePair
			{
				public Node Left, Right;
				public NodePair(Node left, Node right)
				{
					Left = left;
					Right = right;
				}
			}

			private Node root;
			private readonly Comparison<T> compare;

			public Treap()
				: this(Comparer<T>.Default.Compare)
			{
			}

			public Treap(Comparison<T> compare)
			{
				this.compare = compare;
			}

			public void Add(T value)
			{
				NodePair pair = Split(root, value);
				root = Merge(Merge(pair.Left, new Node(value)), pair.Right);
				if (compare(Max, value) < 0)
					Max = value;
				if (compare(Min, value) > 0)
					Min = value;
			}

			public void Remove(T value)
			{
				root = Remove(root, value);
				if (compare(Min, value) == 0)
					Min = pMin(root);
				if (compare(Max, value) == 0)
					Max = pMax(root);
			}

			private NodePair Split(Node node, T value)
			{
				if (node == null)
					return new NodePair(null, null);
				if (compare(node.Value, value) >= 0)
				{
					NodePair leftSplit = Split(node.Left, value);
					node.Left = leftSplit.Right;
					leftSplit.Right = node;
					return leftSplit;
				}
				else
				{
					NodePair rightSplit = Split(node.Right, value);
					node.Right = rightSplit.Left;
					rightSplit.Left = node;
					return rightSplit;
				}
			}

			private Node Remove(Node node, T value)
			{
				if (node == null)
					return null;
				if (compare(value, node.Value) < 0)
				{
					node.Left = Remove(node.Left, value);
					return node;
				}
				if (compare(value, node.Value) > 0)
				{
					node.Right = Remove(node.Right, value);
					return node;
				}
				return Merge(node.Left, node.Right);
			}

			private static Node Merge(Node left, Node right)
			{
				if (left == null)
					return right;
				if (right == null)
					return left;
				if (left.Priority > right.Priority)
				{
					left.Right = Merge(left.Right, right);
					return left;
				}
				else
				{
					right.Left = Merge(left, right.Left);
					return right;
				}
			}

			private T pMin(Node node)
			{
				if (node.Left != null)
					return pMin(node.Left);
				return node.Value;
			}

			private T pMax(Node node)
			{
				if (node.Right != null)
					return pMax(node.Right);
				return node.Value;
			}
		}

		private static readonly Scanner cin = new Scanner();

		private static void Main()
		{
#if DEBUG
			var inputText = File.ReadAllText(@"..\..\input.txt");
			var testCases = inputText.Split(new[] { "input" }, StringSplitOptions.RemoveEmptyEntries);
			var consoleOut = Console.Out;
			for (var i = 0; i < testCases.Length; i++)
			{
				var parts = testCases[i].Split(new[] { "output" }, StringSplitOptions.RemoveEmptyEntries);
				Console.SetIn(new StringReader(parts[0].Trim()));
				var stringWriter = new StringWriter();
				Console.SetOut(stringWriter);
				var sw = Stopwatch.StartNew();
				new Template().Solve();
				sw.Stop();
				var output = stringWriter.ToString();

				Console.SetOut(consoleOut);
				var color = ConsoleColor.Green;
				var status = "Passed";
				if (parts[1].Trim() != output.Trim())
				{
					color = ConsoleColor.Red;
					status = "Failed";
				}
				Console.ForegroundColor = color;
				Console.WriteLine("Test {0} {1} in {2}ms", i + 1, status, sw.ElapsedMilliseconds);
			}
			Console.ReadLine();
			Console.ReadKey();
#else
			new Template().Solve();
			Console.ReadLine();
#endif
		}
	}

	internal class Scanner
	{
		private string[] s = new string[0];
		private int i;
		private readonly char[] cs = { ' ' };

		public string NextString()
		{
			if (i < s.Length) return s[i++];
			var line = Console.ReadLine() ?? string.Empty;
			s = line.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			i = 1;
			return s.First();
		}

		public double NextDouble()
		{
			return double.Parse(NextString());
		}

		public int NextInt()
		{
			return int.Parse(NextString());
		}

		public long NextLong()
		{
			return long.Parse(NextString());
		}
	}
}