#K48842. Perfect Rectangle Formation
Perfect Rectangle Formation
Perfect Rectangle Formation
You are given a set of rectangular blocks. Each block is described by four integers: the x and y coordinates of its bottom‐left corner, its width, and its height. Your task is to determine whether these blocks together form a perfect rectangle, meaning that:
1. The union of all blocks forms a rectangle with no gaps.
2. No two blocks overlap.
In mathematical terms, let each block have area \(w_i \times h_i\), then the total area is \(\sum_{i=1}^{n}\,w_i\,h_i\). Let the bounding rectangle have dimensions \((\max(x+w)-\min(x))\) and \((\max(y+h)-\min(y))\). These blocks form a perfect rectangle if and only if \[ \sum_{i=1}^{n}w_i\,h_i = (\max(x+w)-\min(x))\times(\max(y+h)-\min(y)) \] and there is no overlapping among cells.
inputFormat
The input is given via stdin in the following format:
n x1 y1 w1 h1 x2 y2 w2 h2 ... xn yn wn hn
where n is the number of blocks. Each of the following n lines contains four space-separated integers representing the bottom‐left corner (x, y), the width (w) and the height (h) of a block.
outputFormat
Output a single line to stdout containing "YES" if the blocks form a perfect rectangle, or "NO" otherwise.
## sample3
1 1 2 2
3 1 2 2
1 3 4 2
YES