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 (Python):

def input_str(row):
    res = []
    for _ in range(row):
        res.append(raw_input().split())
    return res

N = int(raw_input())
balls = input_str(N)
balls = sorted([sorted([int(x), int(y)]) for x, y in balls], key=lambda x: x[0])

xmax = max(balls, key=lambda x: x[0])[0]
xmin = min(balls, key=lambda x: x[0])[0]
ymax = max(balls, key=lambda x: x[1])[1]
ymin = min(balls, key=lambda x: x[1])[1]
res1 = (xmax - xmin) * (ymax - ymin)

maxdiff = max(xmax, ymax) - min(xmin, ymin)

diff = xmax - xmin
mina = xmax

for i in range(N)[:-1]:
    mina = min(balls[i][1], mina)
    xmax = max(balls[i][1], xmax)
    xmin = min(balls[i+1][0], mina)
    diff = min(diff, xmax - xmin)

res2 = maxdiff * diff

print min(res1, res2)

Solution (Python):
import sys
from operator import itemgetter
 
inf = 1 << 30
 
def solve():
    n = int(sys.stdin.readline())
 
    
 
    r_max = b_max = 0
    r_min = b_min = inf
 
    p = []
 
    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())
 
        if xi > yi:
            xi, yi = yi, xi
 
        p.append((xi, yi))
 
        r_max = max(r_max, yi)
        r_min = min(r_min, yi)
        b_max = max(b_max, xi)
        b_min = min(b_min, xi)
 
    ans1 = (r_max - r_min) * (b_max - b_min)

    
 
    
 
    ans2 = (r_max - b_min)
 
    p.sort(key=itemgetter(0))

    
 
    b_min = p[0][0]
    b_max = p[-1][0]
 
    y_min = inf
 
    dif_b = b_max - b_min
 
    for i in range(n - 1):
        if p[i][1] == r_max:
            break
 
        y_min = min(y_min, p[i][1])
 
        b_min = min(p[i + 1][0], y_min)
        b_max = max(b_max, p[i][1])

        
 
        dif_b = min(dif_b, b_max - b_min)
 
        if y_min < p[i + 1][0]:
            break
 
    ans2 *= dif_b

    
 
    ans = min(ans1, ans2)
 
    print(ans)
 
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from operator import itemgetter
 
inf = 1 << 30
 
def solve():
    n = int(sys.stdin.readline())
 
    
 
    r_max = b_max = 0
    r_min = b_min = inf

    p = []
 
    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())

        if xi > yi:
            xi, yi = yi, xi

        p.append((xi, yi))

        r_max = max(r_max, yi)
        r_min = min(r_min, yi)
        b_max = max(b_max, xi)
        b_min = min(b_min, xi)
 
    ans1 = (r_max - r_min) * (b_max - b_min)
 
    
 
    ans2 = (r_max - b_min)

    p.sort(key=itemgetter(0))

    b_min = p[0][0]
    b_max = p[-1][0]

    y_min = inf

    dif_b = b_max - b_min
 
    for i in range(n - 1):
        if p[i][1] == r_max:
            break

        y_min = min(y_min, p[i][1])

        b_min = min(p[i + 1][0], y_min)
        b_max = max(b_max, p[i][1])

        dif_b = min(dif_b, b_max - b_min)

        if p[i][1] < p[i + 1][0]:
            break
 
    ans2 *= dif_b
 
    ans = min(ans1, ans2)
 
    print(ans)
 
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from operator import itemgetter
 
inf = 1 << 30
 
def solve():
    n = int(sys.stdin.readline())
 
    
 
    r_max = b_max = 0
    r_min = b_min = inf

    p = []
 
    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())

        if xi > yi:
            xi, yi = yi, xi

        p.append((xi, yi))

        r_max = max(r_max, yi)
        r_min = min(r_min, yi)
        b_max = max(b_max, xi)
        b_min = min(b_min, xi)
 
    ans1 = (r_max - r_min) * (b_max - b_min)
 
    
 
    ans2 = (r_max - b_min)

    p.sort(key=itemgetter(0))

    b_min = p[0][0]
    b_max = p[-1][0]

    y_min = inf

    dif_b = b_max - b_min
 
    for i in range(n - 1):
        if p[i][1] == r_max:
            break
        y_min = min(y_min, p[i][1])

        b_min = min(p[i + 1][0], y_min)
        b_max = max(b_max, p[i][1])

        dif_b = min(dif_b, b_max - b_min)
 
    ans2 *= dif_b
 
    ans = min(ans1, ans2)
 
    print(ans)
 
if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from operator import itemgetter
 
inf = 1 << 30
 
def solve():
    n = int(sys.stdin.readline())
 
    
 
    r_max = b_max = 0
    r_min = b_min = inf

    p = []
 
    for i in range(n):
        xi, yi = map(int, sys.stdin.readline().split())

        if xi > yi:
            xi, yi = yi, xi

        p.append((xi, yi))

        r_max = max(r_max, yi)
        r_min = min(r_min, yi)
        b_max = max(b_max, xi)
        b_min = min(b_min, xi)
 
    ans1 = (r_max - r_min) * (b_max - b_min)
 
    
 
    ans2 = (r_max - b_min)

    p.sort(key=itemgetter(0))

    b_min = p[0][0]
    b_max = p[-1][0]

    y_min = inf

    dif_b = b_max - b_min
 
    for i in range(n - 1):
        y_min = min(y_min, p[i][1])

        b_min = min(p[i + 1][0], y_min)
        b_max = max(b_max, p[i][1])

        dif_b = min(dif_b, b_max - b_min)
 
    ans2 *= dif_b
 
    ans = min(ans1, ans2)
 
    print(ans)
 
if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y
ret = (ymax-ymin) * (xmax-xmin)


p.sort()
dx = p[-1][0] - p[0][0]
ymin = p[0][0]
tymin = float('inf')
for i in range(N-1):
    
    tymin = min(tymin, p[i][1])
    xmax = max(xmax, p[i][1])
    xmin = min(tymin, p[i+1][0])
    dx = min(dx, xmax - xmin)
    if tymin < p[i+1][0]:
        break
    

print(min(ret, (ymax-ymin) * dx))

Solution (Python):
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y
ret = (ymax-ymin) * (xmax-xmin)


p.sort()
dx = p[-1][0] - p[0][0]
ymin = p[0][0]
tymin = float('inf')
tymax = 0
for i in range(N-1):
    
    tymin = min(tymin, p[i][1])
    xmax = max(xmax, p[i][1])
    xmin = min(tymin, p[i+1][0])
    dx = min(dx, xmax - xmin)
    


print(min(ret, (ymax-ymin) * dx))

Solution (Python):
N = int(input())
xmin = ymin = float('inf')
xmax = ymax = 0
p = []
for i in range(N):
    x,y = map(int, input().split())
    if x > y : 
        x,y= y,x
    p.append((x, y))
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y
ret = (ymax-ymin) * (xmax-xmin)


p.sort()
dx = p[-1][0] - p[0][0]
ymin = p[0][1]
tymin = float('inf')
tymax = 0
for i in range(N-1):
    
    tymin = min(tymin, p[i][1])
    xmax = max(xmax, p[i][1])
    xmin = min(tymin, p[i+1][0])
    ymin = min(ymin, p[i][0])
    dx = min(dx, xmax - xmin)
    


print(min(ret, (ymax-ymin) * dx))

Solution (Python):

import numpy as np

N = int(input())

X = [0]*N
Y = [0]*N

for i in range(N):
    x, y = map(int, input().split())
    X[i] = min(x, y)
    Y[i] = max(x, y)



res1 = (max(Y) - min(Y)) * (max(X) - min(X))


x_arg = np.argsort(X)
max_a = max(X)
min_a = min(X)
min_b = 1000000001
max_b = 0
res2 = max_a - min_a

for i in range(N-1):
    min_b = min(min_b, Y[x_arg[i]])
    max_a = max(max_a, Y[x_arg[i]])
    if min_b <= X[x_arg[i]]:
        break
    else:
        min_a = min(X[x_arg[i+1]], min_b)
        res2 = min(res2, max_a - min_a)
print(min(res1, res2*(max(Y)-min(X))))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
 
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
 
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
 
def main():
    n = I()
    a = sorted([sorted(LI()) + [_] for _ in range(n)])
    b = sorted(a, key=lambda x: x[1])
    r = (a[-1][0]-a[0][0]) * (b[-1][1]-b[0][1])
 
 
    bm = b[-1][1] - a[0][0]
    bmi = b[0][2]
    am = a[-1][1]
    at = 0
    k = [[inf,0]]
    for i in range(n-1):
        kk = []
        kk.append(min(k[-1][0], b[i][0], b[i+1][1]))
        kk.append(max(k[-1][1], b[i][0]))
        if kk[0] == k[-1][0]:
            k[-1][1] = kk[1]
        else:
            k.append(kk)
 
    k = k[1:]
    kl = len(k)
 
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    mtm = 0
    bt = b[0][1]
 
    for i in range(n-1,0,-1):
        tm = b[i][0]
        if mtm < tm:
            mtm = tm
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        bm = mtm
        if tm > bm:
            bm = tm
 
        tr = am * (bm-bt)
        if r > tr:
            r = tr
 
        for j in range(kl):
            ki,km = k[j]
            if km > bm:
                break
            tr = am * (bm-min(ki,bt))
            if r > tr:
                r = tr
 
    return r
 
 
print(main())