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



Sample input:
3
1 2
3 4
5 6


Sample output:
15

Submission status: Wrong Answer

Solution code:
from bisect import insort

n=int(input())
x,y,p=[],[],[]
for i in range(n):
    a,b=map(int,input().split())
    if a > b: a,b=b,a
    x.append(a)
    y.append(b)
    p.append((a,i,True))
    p.append((b,i,False))
p.sort()
ans=(max(x)-min(x))*(max(y)-min(y))
if p[0][1] != p[-1][1]:
    a=p[-1][0] - p[0][0]
    bs=[]
    bs.append((y[p[0][1]] if p[0][2] else x[p[0][1]], p[0][1]))
    bs.append((y[p[-1][1]] if p[-1][2] else x[p[-1][1]], p[-1][1]))
    for i in range(n):
        if i in (p[0][1], p[-1][1]):
            continue
        bs.append((x[i],i))
    bs.sort()
    b=bs[-1][0]-bs[0][0]
    for i in range(n):
        if bs[i][1] in (p[0][1], p[-1][1]):
            continue
        if x[bs[0][1]] == bs[0][0]:
            break
        v,j=bs.pop(0)
        insort(bs,(y[j],j))
        b=min(b, bs[-1][0]-bs[0][0])
    ans=min(ans, a*b)
print(ans)

Submission status: Wrong Answer

Solution code:
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,n//2,-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())

Submission status: Wrong Answer

Solution code:
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()
    if n == 1:
        return 0
    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
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm

        tr = am * (bm-bt)
        if r > tr:
            r = tr

        for j in range(kl):
            ki,km = k[j]
            tr = am * (max(km,bm)-min(ki,bt))
            if r > tr:
                r = tr
            if km >= bm:
                break

    return r


print(main())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import array, bisect, collections, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
 
 
def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}
 
 
def solve(N, x, y):
    state = [x[0], x[0], y[0], y[0]]
    for i in range(1, N):
        state_1 = [max(state[0], x[i]), min(state[1], x[i]), max(state[2], y[i]), min(state[3], y[i])]
        state_2 = [max(state[0], y[i]), min(state[1], y[i]), max(state[2], x[i]), min(state[3], x[i])]

        dif_1 = (state_1[0] - state_1[1]) * (state_1[2] - state_1[3])
        dif_2 = (state_2[0] - state_2[1]) * (state_2[2] - state_2[3])

        if dif_1 > dif_2:
            state = state_2
        else:
            state = state_1

    ans = (state[0] - state[1]) * (state[2] - state[3])
    return ans

 
def main():
    N = II()
    x, y = [], []
    for __ in range(N):
        a, b = ILI()
        x.append(a)
        y.append(b)
    print(solve(N, x, y))
 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm

        tr = am * (bm-bt)
        if r > tr:
            r = tr

        for j in range(kl):
            ki,km = k[j]
            tr = am * (max(km,bm)-min(ki,bt))
            if r > tr:
                r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
balls = []
for i in range(N):
    x,y = map(int, input().split())
    balls.append((x,y))
if all([balls[0][0] in balls[i] for i in range(N)]) or all([balls[0][1] in balls[i] for i in range(N)]) or N ==1:
    print(0)
    exit()
balls.sort(reverse=True)
# print(balls)
if abs(balls[0][0] - balls[1][0])*abs(balls[0][1] - balls[1][1]) < abs(balls[0][1] - balls[1][0])*abs(balls[0][0] - balls[1][1]):
    xmax = max(balls[0][0], balls[1][0])
    xmin = min(balls[0][0], balls[1][0])
    ymax = max(balls[0][1], balls[1][1])
    ymin = min(balls[0][1], balls[1][1])
else:
    xmax = max(balls[0][1], balls[1][0])
    xmin = min(balls[0][1], balls[1][0])
    ymax = max(balls[0][0], balls[1][1])
    ymin = min(balls[0][0], balls[1][1])
# print((ymax-ymin) * (xmax-xmin), ymax,ymin,xmax,xmin)
for i in range(2, N):
    x,y = balls[i]
    if (max(ymax, x)-min(ymin, x)) * (max(xmax, y)-min(xmin, y)) < (max(ymax, y)-min(ymin, y)) * (max(xmax, x)-min(xmin, x)):
        x,y= y,x
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y

# print((ymax-ymin) * (xmax-xmin), ymax,ymin,xmax,xmin)
print((ymax-ymin) * (xmax-xmin))

Submission status: Wrong Answer

Solution code:
N = int(input())
Mx = 0
My = 0
mx = 10**9
my = 10**9
for i in range(N):
	x, y = sorted([int(i) for i in input().split()])
	Mx = max(Mx, x)
	mx = min(mx, x)
	My = max(My, y)
	my = min(my, y)
ans = (Mx-mx)*(My-my)
if (Mx-my)*(My-mx) >= 0:
	ans = min(ans, (Mx-my)*(My-mx))
print(ans)

Submission status: Wrong Answer

Solution code:
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
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        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:
                tr = am * (km-ki)
            else:
                tr = am * (bm-ki)
            if r > tr:
                r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
Rmax = 0
Rmin = 1000000000
Bmax = 0
Bmin = 1000000000

N = int(input())
xs = []
ys = []
arr = []
for i in range(N):
    x, y = map(int, input().split())
    xs.append(x)
    ys.append(y)
    arr.append((max(x, y) / min(x, y), min(x, y), max(x, y)))

arr = list(sorted(arr))
for (_, x, y) in arr:
    Rmin0 = min(Rmin, x)
    Rmax0 = max(Rmax, x)
    Bmin0 = min(Bmin, y)
    Bmax0 = max(Bmax, y)
    p0 = (Rmax0 - Rmin0) * (Bmax0 - Bmin0)
    Rmin1 = min(Rmin, y)
    Rmax1 = max(Rmax, y)
    Bmin1 = min(Bmin, x)
    Bmax1 = max(Bmax, x)
    p1 = (Rmax1 - Rmin1) * (Bmax1 - Bmin1)
    if p0 <= p1:
        Rmin = Rmin0
        Rmax = Rmax0
        Bmin = Bmin0
        Bmax = Bmax0
    else:
        Rmin = Rmin1
        Rmax = Rmax1
        Bmin = Bmin1
        Bmax = Bmax1

ans = (Rmax - Rmin) * (Bmax - Bmin)
print(ans)

Submission status: Wrong Answer

Solution code:
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
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        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-ki)
            if r > tr:
                r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
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[i][0], b[i][0], b[i+1][1]))
        kk.append(max(k[i][1], b[i][0]))
        k.append(kk)

    k = k[1:]

    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm

        tr = am * (bm-bt)
        if r > tr:
            r = tr

        for j in range(n-1):
            ki,km = k[j]
            if km > bm:
                break
            tr = am * (bm-ki)
            if r > tr:
                r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
balls = []
for i in range(N):
    x,y = map(int, input().split())
    balls.append((x,y))
if all([balls[0][0] in balls[i] for i in range(N)]) or all([balls[0][1] in balls[i] for i in range(N)]):
    print(0)
    exit()
balls.sort(reverse=True)

for i in range(N):
    if i == 0:
        xmin = xmax = x
        ymin = ymax = y
        continue
    if (max(ymax, x)-min(ymin, x)) * (max(xmax, y)-min(xmin, y)) < (max(ymax, y)-min(ymin, y)) * (max(xmax, x)-min(xmin, x)):
        x,y= y,x
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y

print((ymax-ymin) * (xmax-xmin))

Submission status: Wrong Answer

Solution code:
N = int(input())
for i in range(N):
    x,y = map(int, input().split())
    if i == 0:
        xmin = xmax = x
        ymin = ymax = y
        continue
    if (max(ymax, x)-min(ymin, x)) * (max(xmax, y)-min(xmin, y)) < (max(ymax, y)-min(ymin, y)) * (max(xmax, x)-min(xmin, x)):
        x,y= y,x
    if x < xmin:
        xmin = x
    elif x > xmax:
        xmax = x
    if y < ymin:
        ymin = y
    elif y > ymax:
        ymax = y

print((ymax-ymin) * (xmax-xmin))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N=int(raw_input())
x=[0]*N
y=[0]*N
maxx=[0]*N
minn=[0]*N

for i in range(N):
	x[i],y[i]=map(int,raw_input().split())
	if x[i]>y[i]:maxx[i]=x[i];minn[i]=y[i]
	else:maxx[i]=y[i];minn[i]=x[i]

a1=(max(maxx)-min(maxx))*(max(minn)-min(minn))
a2=(max(maxx)-min(minn))*(max(minn)-min(maxx))

if a1>0 and a2>0:print min([a1,a2])
else:print max([a1,a2])

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm
        tr = am * (bm-bt)
        if r > tr:
            r = tr

    bm = b[-1][1] - a[0][0]
    bmi = b[0][2]
    am = a[-1][1]
    at = 0

    for i in range(0,n-1):
        tm = a[i][1]
        if bmi == a[i][2]:
            break
        if tm > at:
            at = tm
        if tm > a[i+1][0]:
            tm = a[i+1][0]
        if tm < am:
            am = tm
        tr = bm * (at-am)
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm
        tr = am * (bm-bt)
        if r > tr:
            r = tr

    bm = b[-1][1] - a[0][0]
    bmi = b[0][2]
    am = 0
    at = a[-1][1]

    for i in range(0,n-1):
        tm = a[i][1]
        if bmi == a[i][2]:
            break
        if tm > at:
            at = tm
        if tm > a[i+1][0]:
            tm = a[i+1][0]
        if tm < am:
            am = tm
        tr = bm * (at-am)
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm
        tr = am * (bm-bt)
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm < bt:
            bt = tm
        if tm < b[i-1][1]:
            tm = b[i-1][1]
        if tm > bm:
            bm = tm
        else:
            break
        tr = am * (bm-bt)
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
xys = [[int(_) for _ in raw_input().split()] for i in xrange(n)]

Ms = [max(a, b) for a, b in xys]
ms = [min(a, b) for a, b in xys]
MM = max(Ms)
mm = min(ms)
Mm = max(ms)
mM = min(Ms)

print(min(x for x in [(MM - mM) * (Mm - mm), (MM - mm) * (Mm - mM)] if x >= 0))

Submission status: Wrong Answer

Solution code:
Rmax = 0
Rmin = 1000000000
Bmax = 0
Bmin = 1000000000

N = int(input())
xs = []
ys = []
for i in range(N):
    x, y = map(int, input().split())
    xs.append(x)
    ys.append(y)
    r0 = min(x, y)
    b0 = max(x, y)
    Rmin0 = min(Rmin, r0)
    Rmax0 = max(Rmax, r0)
    Bmin0 = min(Bmin, b0)
    Bmax0 = max(Bmax, b0)
    p0 = (Rmax0 - Rmin0) * (Bmax0 - Bmin0)
    r1 = max(x, y)
    b1 = min(x, y)
    Rmin1 = min(Rmin, r1)
    Rmax1 = max(Rmax, r1)
    Bmin1 = min(Bmin, b1)
    Bmax1 = max(Bmax, b1)
    p1 = (Rmax1 - Rmin1) * (Bmax1 - Bmin1)
    if p0 <= p1:
        Rmin = Rmin0
        Rmax = Rmax0
        Bmin = Bmin0
        Bmax = Bmax0
    else:
        Rmin = Rmin1
        Rmax = Rmax1
        Bmin = Bmin1
        Bmax = Bmax1

ans = (Rmax - Rmin) * (Bmax - Bmin)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
Amax, Amin, Bmax, Bmin = 0, 0, 0, 0 
for i in range(N):
    x, y = map(int, raw_input().split())
    if Amax == Amin == 0:
        Amax = Amin = x
        Bmax = Bmin = y
    else:
        if Amin <= x <= Amax:
            Bmin, Bmax = min(Bmin, y), max(Bmax, y)
        elif Bmin <= x <= Bmax:
            Amin, Amax = min(Amin, y), max(Amax, y)
        else:
            Amean = (Amin+Amax)/2.0
            Bmean = (Bmin+Bmax)/2.0
            if (Amean - x) ** 2 + (Bmean - y) ** 2 < (Amean - y) ** 2 + (Bmean - x) ** 2:
                Amin, Amax = min(Amin, x), max(Amax, x)
                Bmin, Bmax = min(Bmin, y), max(Bmax, y)
            else:
                Amin, Amax = min(Amin, y), max(Amax, y)
                Bmin, Bmax = min(Bmin, x), max(Bmax, x)
    #print(i, Amin, Amax, Bmin, Bmax)


print (Amax - Amin) * (Bmax - Bmin)

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    ami = a[0][2]
    bm = 0
    bt = b[0][1]

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if ami == b[i][2]:
            break
        if tm > bt:
            bt = tm
        if tm < b[i-1][0]:
            tm = b[i-1][0]
        if tm > bm:
            bm = tm
        else:
            break
        tr = am * (bm-bt)
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Wrong Answer

Solution code:
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])
    am = b[-1][1] - a[0][0]
    bm = 0

    for i in range(n-1,0,-1):
        tm = b[i][0]
        if tm < b[0][1]:
            # わからん
            break
        if tm < b[i-1][0]:
            tm = b[i-1][0]
        if tm > bm:
            bm = tm
        else:
            break
        tr = am * (bm-b[0][1])
        if r > tr:
            r = tr

    return r


print(main())

Submission status: Time Limit Exceeded

Solution code:
#####segfunc#####
def segfunc(x, y):
    return min(x,y)
#################

#####ide_ele#####
ide_ele = float("inf")
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

def main():
    import sys

    input=sys.stdin.readline

    N=int(input())
    q=[]
    r=[]
    b=[]
    X=0
    q_append=q.append
    for i in range(N):
        x,y=map(int,input().split())
        r.append(max(x,y))
        b.append(min(x,y))
        q_append((max(x,y),-1,i))

    X=max(b)
    for i in range(N):
        x,y=r[i],b[i]
        if x>=X:
            q_append((x,i,i))
        if y>=X:
            q_append((y,i,i))

    init_val=[b[i] for i in range(N)]

    q.sort()
    pos=0
    while len(q)>pos and q[pos][1]==-1:
        init_val[q[pos][2]]=q[pos][0]
        pos+=1

    test1=float("inf")
    rmq=SegTree(init_val,segfunc,ide_ele)

    for i in range(pos,len(q)):
        val,k,index=q[i]
        if k==-1:
            rmq.update(index,val)
        else:
            res=rmq.query(0,N)
            test1=min(val-res,test1)

    test2=(max(r)-min(r))*(max(b)-min(b))
    test1*=(max(max(b),max(r))-min(min(b),min(r)))
    print(min(test1,test2))

if __name__=="__main__":
    main()