You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle). 

Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 5). Next n lines contain four integers each, describing a single rectangle: x_1, y_1, x_2, y_2 (0 ≤ x_1 < x_2 ≤ 31400, 0 ≤ y_1 < y_2 ≤ 31400) — x_1 and x_2 are x-coordinates of the left and right edges of the rectangle, and y_1 and y_2 are y-coordinates of the bottom and top edges of the rectangle. 

No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).


-----Output-----

In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.


-----Examples-----
Input
5
0 0 2 3
0 3 3 5
2 0 5 2
3 2 5 5
2 2 3 3

Output
YES

Input
4
0 0 2 3
0 3 3 5
2 0 5 2
3 2 5 5

Output
NO

Difficulty: MEDIUM
Tags: Implementation

Starter code:

Solutions:
n = int(input())
x1 = []
x2 = []
y1 = []
y2 = []
S = 0
for i in range(n):
	(a, b, c, d) = list(map(int, input().split()))
	x1.append(a)
	y1.append(b)
	x2.append(c)
	y2.append(d)
	S += (c - a) * (d - b)
if (max(x2) - min(x1)) * (max(y2) - min(y1)) == S and max(x2) - min(x1) == max(y2) - min(y1):
	print('YES')
else:
	print('NO')


n = int(input())
(a1, b1, a2, b2) = map(int, input().split())
s = (a2 - a1) * (b2 - b1)
for i in range(n - 1):
	(x1, y1, x2, y2) = map(int, input().split())
	(a1, b1, a2, b2) = (min(a1, x1), min(b1, y1), max(a2, x2), max(b2, y2))
	s += (x2 - x1) * (y2 - y1)
print('YES' if a2 - a1 == b2 - b1 and s == (a2 - a1) * (b2 - b1) else 'NO')


import sys
fin = sys.stdin
n = int(fin.readline())
p = []
for i in range(n):
	(a, b, c, d) = map(int, fin.readline().split())
	p += [((a, b), (c, d))]

def width(rect):
	return rect[1][0] - rect[0][0]

def height(rect):
	return rect[1][1] - rect[0][1]

def square(rect):
	return width(rect) * height(rect)

def squareOfRects(rects):
	return sum((square(r) for r in rects))

def left(rects):
	return min((r[0][0] for r in rects))

def right(rects):
	return max((r[1][0] for r in rects))

def bottom(rects):
	return min((r[0][1] for r in rects))

def top(rects):
	return max((r[1][1] for r in rects))

def isSquare(rects):
	(w, h) = (right(rects) - left(rects), top(rects) - bottom(rects))
	return w == h and squareOfRects(rects) == w * h
print('YES' if isSquare(p) else 'NO')


n = int(input())
x = set()
y = set()
p = [list(map(int, input().split())) for i in range(n)]
for d in p:
	x.add(d[0])
	y.add(d[1])
	x.add(d[2])
	y.add(d[3])
xl = sorted(list(x))
yl = sorted(list(y))
if xl[-1] - xl[0] != yl[-1] - yl[0]:
	print('NO')
	return
z = [[0 for i in range(len(xl) - 1)] for j in range(len(yl) - 1)]
for d in p:
	for i in range(xl.index(d[0]), xl.index(d[2])):
		for j in range(yl.index(d[1]), yl.index(d[3])):
			z[j][i] = 1
for d in z:
	if d.count(0):
		print('NO')
		return
print('YES')


n = int(input())
l = [[int(x) for x in input().split()] for i in range(n)]
dx = max([r[2] for r in l]) - min([r[0] for r in l])
dy = max([r[3] for r in l]) - min([r[1] for r in l])
size = dx * dy
if dx == dy and sum([(r[2] - r[0]) * (r[3] - r[1]) for r in l]) == size:
	print('YES')
else:
	print('NO')


n = int(input())
c = [[], [], [], []]
plos = 0
for i in range(n):
	q = list(map(int, input().split()))
	plos += (q[2] - q[0]) * (q[3] - q[1])
	for j in range(4):
		c[j].append(q[j])
fw = max(c[2]) - min(c[0])
fh = max(c[3]) - min(c[1])
if fw == fh and fw * fh == plos:
	print('YES')
else:
	print('NO')


n = int(input())
s = 0
INF = 10 ** 9
minx = miny = INF
maxx = maxy = -INF
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	s += abs(x1 - x2) * abs(y1 - y2)
	minx = min(minx, x1, x2)
	maxx = max(maxx, x1, x2)
	miny = min(miny, y1, y2)
	maxy = max(maxy, y1, y2)
if maxx - minx == maxy - miny and s == (maxx - minx) ** 2:
	print('YES')
else:
	print('NO')


x0 = []
x1 = []
y0 = []
y1 = []
n = int(input())
s = 0
for i in range(n):
	l = list(map(int, input().split()))
	x0.extend([l[0]])
	x1.extend([l[2]])
	y0.extend([l[1]])
	y1.extend([l[3]])
	s += (l[3] - l[1]) * (l[2] - l[0])
lx = max(x1) - min(x0)
ly = max(y1) - min(y0)
if lx == ly and lx * ly == s:
	print('YES')
else:
	print('NO')


n = int(input())
area = 0
(maxx, maxy, minx, miny) = (0, 0, 10 ** 5, 10 ** 5)
for i in range(0, n):
	(a, b, c, d) = list(map(int, input().split()))
	area += (c - a) * (d - b)
	maxx = max(maxx, c)
	minx = min(minx, a)
	maxy = max(maxy, d)
	miny = min(miny, b)
n = maxx - minx
if n == maxy - miny and n * n == area:
	print('YES')
else:
	print('NO')


(xmin, ymin, xmax, ymax, a) = (31400, 31400, 0, 0, 0)
for i in range(int(input())):
	(x1, y1, x2, y2) = map(int, input().split())
	xmin = min(xmin, x1)
	ymin = min(ymin, y1)
	xmax = max(xmax, x2)
	ymax = max(ymax, y2)
	a += (x2 - x1) * (y2 - y1)
print('YES' if xmax - xmin == ymax - ymin and a == (xmax - xmin) ** 2 else 'NO')


n = eval(input())
area = 0
(p1, p2) = ([], [])
for i in range(n):
	in_ = input().split()
	p1.append(tuple(map(int, in_[0:2])))
	p2.append(tuple(map(int, in_[2:4])))
	area += (p2[i][0] - p1[i][0]) * (p2[i][1] - p1[i][1])
pl = min(p1)
pu = max(p2)
ok = True
for p in p1 + p2:
	if pl[1] > p[1]:
		ok = False
	if pu[1] < p[1]:
		ok = False
(w, h) = (pu[0] - pl[0], pu[1] - pl[1])
exp = w * h
print('YES' if ok and w == h and (area == exp) else 'NO')


n = int(input())
sq = [None] * 51
sqsum = 0
for i in range(n):
	sq[i] = list(map(int, input().split()))
	sqsum += (sq[i][3] - sq[i][1]) * (sq[i][2] - sq[i][0])
maxx = max(map(lambda x: x[2], sq[:n]))
minx = min(map(lambda x: x[0], sq[:n]))
maxy = max(map(lambda x: x[3], sq[:n]))
miny = min(map(lambda x: x[1], sq[:n]))
area = (maxy - miny) * (maxx - minx)
if maxx - minx != maxy - miny or area != sqsum:
	print('NO')
else:
	print('YES')


pl = 0
etpl = 0
max_x = 0
min_x = 40000
max_y = 0
min_y = 40000
n = int(input())
for i in range(0, n, 1):
	crd = input().split()
	if int(crd[0]) >= int(crd[2]):
		st1 = int(crd[0]) - int(crd[2])
	else:
		st1 = int(crd[2]) - int(crd[0])
	if int(crd[1]) >= int(crd[3]):
		st2 = int(crd[1]) - int(crd[3])
	else:
		st2 = int(crd[3]) - int(crd[1])
	if int(crd[0]) < min_x:
		min_x = int(crd[0])
	if int(crd[1]) < min_y:
		min_y = int(crd[1])
	if int(crd[2]) > max_x:
		max_x = int(crd[2])
	if int(crd[3]) > max_y:
		max_y = int(crd[3])
	pl += st1 * st2
	etpl = (max_y - min_y) * (max_x - min_x)
if max_y - min_y == max_x - min_x and etpl == pl:
	print('YES')
else:
	print('NO')


n = int(input())
area = 0
minx = miny = 999999
maxx = maxy = 0
for _ in range(n):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	minx = min(minx, min(x1, x2))
	miny = min(miny, min(y1, y2))
	maxx = max(maxx, max(x1, x2))
	maxy = max(maxy, max(y1, y2))
	a = abs(x1 - x2) * abs(y1 - y2)
	area += a
h = maxy - miny
w = maxx - minx
if h != w or h * h != area:
	print('NO')
else:
	print('YES')


n = int(input())
(x1, y1, x2, y2) = list(map(int, input().split()))
(l, r, u, d) = (x1, x2, y2, y1)
s = (x2 - x1) * (y2 - y1)
for i in range(n - 1):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	s += (x2 - x1) * (y2 - y1)
	l = min(l, x1)
	r = max(r, x2)
	u = max(u, y2)
	d = min(d, y1)
if r - l == u - d and (r - l) * (u - d) == s:
	print('YES')
else:
	print('NO')


def f(l, i):
	return min(l) if i < 2 else max(l)

class CodeforcesTask325ASolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.rectangles = []

	def read_input(self):
		self.n = int(input())
		for _ in range(self.n):
			self.rectangles.append([int(x) for x in input().split(' ')])

	def process_task(self):
		cords = [f([x[i] for x in self.rectangles], i) for i in range(4)]
		fields = [(x[3] - x[1]) * (x[2] - x[0]) for x in self.rectangles]
		ff = sum(fields)
		if ff == (cords[3] - cords[1]) * (cords[2] - cords[0]) and cords[3] - cords[1] == cords[2] - cords[0]:
			self.result = 'YES'
		else:
			self.result = 'NO'

	def get_result(self):
		return self.result
Solution = CodeforcesTask325ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


import sys
import math
n = int(input())
vmaxx = 0
vmaxy = 0
vminx = 31401
vminy = 31401
s = 0
for i in range(n):
	(x1, y1, x2, y2) = [int(x) for x in sys.stdin.readline().split()]
	vmaxx = max(vmaxx, x2)
	vmaxy = max(vmaxy, y2)
	vminx = min(vminx, x1)
	vminy = min(vminy, y1)
	s += (x2 - x1) * (y2 - y1)
if s == (vmaxx - vminx) * (vmaxy - vminy) and vmaxx - vminx == vmaxy - vminy:
	print('YES')
else:
	print('NO')


n = eval(input())
area = 0
p1 = []
p2 = []
ps = []
(xl, xu, yl, yu) = (1 << 28, 0, 1 << 28, 0)
for i in range(n):
	in_ = input().split()
	p1.append(tuple(map(int, in_[0:2])))
	p2.append(tuple(map(int, in_[2:4])))
	ps.append(tuple(map(int, in_[0:2])))
	ps.append(tuple(map(int, in_[2:4])))
	xl = min(xl, p1[-1][0], p2[-1][0])
	xu = max(xu, p1[-1][0], p2[-1][0])
	yl = min(yl, p1[-1][1], p2[-1][1])
	yu = max(yu, p1[-1][1], p2[-1][1])
ok = True
for p in ps:
	for dx in (-0.5, 0, 0.5):
		for dy in (-0.5, 0, 0.5):
			q = (p[0] + dx, p[1] + dy)
			if not (xl < q[0] < xu and yl < q[1] < yu):
				continue
			c = False
			for i in range(n):
				if p1[i][0] <= q[0] <= p2[i][0] and p1[i][1] <= q[1] <= p2[i][1]:
					c = True
			if not c:
				ok = False
print('YES' if ok and xu - xl == yu - yl else 'NO')