We need to determine if the union of the given axis-aligned rectangles (non-overlapping interior, but can touch edges) is exactly equal to a square (with interior and border). The rectangles have integer coordinates, but the square might have integer coordinates? Not necessarily; the square might be axis-aligned? The problem statement: "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." It doesn't say the square must be axis-aligned? Usually in such problems, the square also has sides parallel to axes, because rectangles are given axis-aligned. It's implied: Since rectangles are axis-aligned, their union is a set of points. If the union is exactly a square, it would have to be axis-aligned as well? Could be rotated? Possibly the square could be rotated; but the union is comprised of axis-aligned rectangles. If they form a rotated square, the union would have axis-aligned edges inside? Actually, if the union is exactly a rotated square, the boundary would be at 45 degrees. But the union is composed of axis-aligned rectangles; could that cover a rotated square? Possibly yes: think of covering a diamond shape by axis-aligned rectangles that together fill it exactly, but the diamond shape is a square rotated 45 degrees. The union of axis-aligned rectangles (with edges parallel to axes) would produce a region that is a union of axis-aligned boxes. The shape's boundary would be staircased? Actually, the union's boundary is composed of segments parallel to axes. To be exactly a rotated square, the boundary would have to be straight lines at 45 degrees, which are not axis-aligned. The only way to get a line at 45 degrees from axis-aligned rectangles is as a "limit" of many small rectangles, but here we have finite number of rectangles (≤5), and they are finite. So the union would be a polygon whose edges are either horizontal or vertical segments. Therefore, the only possible square that can be formed is axis-aligned. Because the boundary of the union is a set of axis-aligned segments. For it to equal a square, the square's boundary must be composed of axis-aligned segments, so the square must be axis-aligned. So we can assume the square is axis-aligned.
Thus we need to check if the union of given rectangles is exactly an axis-aligned square.
We have up to 5 rectangles, coordinates up to 31400. So we can approach by computing the overall bounding box: min x among all rectangles, max x, min y, max y. Let's denote left = min x1, right = max x2, bottom = min y1, top = max y2. For the union to be a square, these must define a square: (right - left) == (top - bottom). Also, the union must fill this entire square without gaps or overhangs. But careful: Could there be internal gaps? Since rectangles are non-overlapping interior, but they might not cover every point within the bounding box. We need to verify that every point inside [left,right] × [bottom,top] is covered by at least one rectangle, and no point outside that box is covered.
But also, the square could be exactly the union, and the square's sides might be defined by the outermost edges of the rectangles. However, it's possible that the outermost edges define a square but there are points inside not covered (holes) or points outside covered (extra protrusions). So we need to check coverage.
Given small n, we can compute the union area and compare to area of bounding square. If they match, and the bounding box is exactly covered, then it's a square? But there is nuance: The union could be exactly a square but with internal boundaries (edges) due to multiple rectangles, but still all points inside the square are covered. So area equality (union area = (right-left)(top-bottom)) ensures that there are no holes or protrusions? Actually, protrusions outside bounding box would increase the bounding box, so if we computed bounding box as min x among rectangles (lowest x1) and max x among rectangles (max x2), any rectangle extending beyond that would affect it. But the bounding box is determined by the extreme coordinates of the rectangles. So if any point lies outside the bounding box, the bounding box would be larger because that point's coordinates would be min or max. So bounding box encloses all points covered. So area equality ensures that the total area covered equals area of bounding box. However, it is possible that there are holes inside the bounding box but the area still matches? Holes would reduce the total area covered. For area to match, the union must have area exactly the area of the bounding box. That implies there is no uncovered region inside the bounding box (holes) and no overlap? Overlap would cause double counting but union area doesn't count overlaps. Overlaps are not allowed (no interior overlap) but rectangles can touch; touching edges don't affect area. Since no interior overlap, total area of rectangles equals union area? Actually if rectangles are non-overlapping interior, their interiors are disjoint. They may share boundaries, but area of union = sum of areas. So union area = sum_i ( (x2_i-x1_i)(y2_i-y1_i) ). So we could compute sum of areas. Then we compute (right-left)*(top-bottom). If they are equal, does that guarantee that the union is exactly the bounding box? Not necessarily: consider two rectangles that together have same area as bounding box but arranged such that they don't cover the whole box, but there are gaps and also some parts of rectangles extend outside the bounding box? Wait, if there is any part outside bounding box, the bounding box would expand to include it, so that's impossible because we defined bounding box as extremes. So rectangles are fully inside [left,right] × [bottom,top] because left is min of x1, right is max of x2, etc. So all rectangles are within that bounding box. Thus union is a subset of bounding box. If sum of areas = area(bbox), then because union ⊆ bbox, the only way for the measure of union to equal measure of bbox is that union = bbox up to measure zero differences (like boundaries). But could there be holes? A hole would be an open region not covered; its area would be >0. Then union area would be less than bbox area. So if they are equal, there is no positive-area hole. However, could there be overlapping? Overlap would cause sum of areas > union area. But we said rectangles don't overlap interior, so union area = sum of areas. So sum of areas = bbox area implies union = bbox essentially (except maybe measure-zero boundaries). However, is it possible that union covers bbox but there are also some uncovered "line" segments? Those have zero area, so area equality wouldn't catch them. But the condition for union being exactly equal to the set of points in the square requires that every point (including boundaries) is covered. Could there be a point on the boundary of the square that is not covered? If a point on the boundary is not covered, that would mean the union does not include that point. However, boundaries have zero area, so area equality still holds. But the union set would not equal the square; there would be missing points. For example, suppose we have a square of side 2 from (0,0) to (2,2). Two rectangles: one covering (0,0)-(2,1) and one covering (0,1)-(2,2) but missing the line y=1? Actually they could be placed such that they are open on the shared edge? But rectangles are defined with inclusive/exclusive? The problem says "set of points inside or on the border of at least one rectangle". Typically rectangles include their borders. The coordinates: x1,y1,x2,y2. Usually it's [x1,x2]×[y1,y2] inclusive? The problem statement: "x1 and x2 are x-coordinates of the left and right edges of the rectangle, and y1 and y2 are y-coordinates of the bottom and top edges of the rectangle." It says "inside or on the border". So each rectangle includes its boundary points. So if two rectangles are adjacent, the common edge is included in both, but that's fine, the union still includes those points.
Could there be missing points on the outer boundary? For the union to equal the square, the square's outer boundary must be fully covered. That means the leftmost x coordinate must have the entire vertical segment from bottom to top covered by some rectangle(s). If there is a gap on the left side, then a point on that edge is not covered. But would that affect area? Possibly not if the gap is a line (zero area). But the condition is set equality, not just measure. So we need to check that the union exactly matches the square (including boundaries). So area equality alone is insufficient: we also need that the union's shape is exactly that axis-aligned square, i.e., the union set = [left,right]×[bottom,top]. So we need to verify that every point in that rectangle is covered.
Since n ≤ 5 and coordinates integer up to 31400, we could in principle "rasterize" the region by considering intervals? But brute force per integer coordinate might be too large (31400^2). However we can use a sweep-line approach to compute the union area and also check coverage, but checking that the union exactly equals the bounding rectangle requires verifying that there are no uncovered regions. That can be done by checking that the union of rectangles equals the bounding rectangle as a set.
One approach: compute the union polygon (or region) and see if it's exactly the bounding rectangle. Since rectangles are axis-aligned and non-overlapping interior, the union is a rectilinear polygon (possibly with holes). We need to ensure it has no holes and its outer boundary is exactly the bounding rectangle. Equivalent: The complement of the union within the bounding rectangle should be empty. That is, no point inside the bounding rectangle is uncovered.
We could compute the union of rectangles (as a set) by plane sweep to get the covered area, and also compute area of uncovered parts? Actually we could compute the total area covered, and if it equals area of bounding rectangle, then the uncovered area is zero. But as argued, that ensures measure zero uncovered, but not necessarily that every point is covered; there could be uncovered lines or points. However, since rectangles are closed (include boundaries) and they are axis-aligned with integer coordinates, any uncovered region of positive measure would be detected by area difference. Uncovered points forming lines have zero area. Could such a line exist without positive area uncovered? Consider two rectangles placed so that they touch at corners but leave a diagonal line uncovered? Actually a line (1D) uncovered would be something like a gap of zero width? But rectangles are closed sets; their union is closed (finite union of closed sets is closed). The complement within the bounding rectangle (also closed) is open relative? Not exactly. If union = bbox minus some set, for the union to be closed, missing points would be boundary points of the union. For a point to be uncovered, it must be not in any rectangle. Could that happen on a line segment without area? Possibly if the rectangles are arranged such that there's a "crack" between them: e.g., two rectangles that are supposed to be adjacent but there is a gap of zero width? That's impossible because if they are adjacent, their boundaries touch; if there is a gap, it would have positive width (if coordinates differ). Since coordinates are integers, but still real numbers. The rectangles are defined by continuous coordinates; if x1 of one rectangle is 2 and x2 of another rectangle is 2, then they share the vertical line x=2. If one rectangle ends at x=2 and the other starts at x=2, then the line x=2 is covered by both (since boundaries are included). So there is no gap. If one rectangle ends at x=2 and the other starts at x=2+epsilon, then there is a gap of width epsilon >0, which has positive area if it's a vertical strip. So area would be less. So any uncovered region of the bounding rectangle must have positive area unless it's of zero width/height (i.e., lines). But can a line segment be uncovered while the area is still full? Consider the case where the bounding rectangle is [0,2]×[0,2]. Suppose we have two rectangles: Rectangle A = [0,2]×[0,1] (covering bottom half including the horizontal line y=1? Actually it includes y=1 as top edge) and Rectangle B = [0,2]×[1,2] (covering top half including y=1 as bottom edge). Their union covers the whole square, including the line y=1. So that's fine.
What about a missing point at a corner? If the rectangles cover all interior but miss a single corner point? That would require that the corner point is not included in any rectangle. For that to happen, the rectangle that would cover that corner would have to not include that point, but rectangles include their boundaries. For the top-right corner (right, top), it would be included if there is a rectangle that has x2 = right and y2 = top. But if no rectangle has both x2=right and y2=top, but perhaps the point is covered by another rectangle's side? The point (right, top) would be covered if there is any rectangle with x2 ≥ right? But right is the max x2, so all rectangles have x2 ≤ right, and the ones achieving right have x2 = right. So the vertical line x=right may be covered by rectangles that have x2 = right; but the point (right, top) requires also y = top. If no rectangle with x2 = right has y2 = top, but maybe a rectangle with y2 ≥ top? No top is max y2. So a rectangle with y2 = top exists. But the point (right, top) is the intersection of the rightmost vertical boundary and topmost horizontal boundary. To be covered, there must be a rectangle that includes that point, meaning there exists a rectangle whose x-range includes right (i.e., x1 ≤ right ≤ x2) and y-range includes top (y1 ≤ top ≤ y2). Since right is max x2, any rectangle with x2 = right includes right (since boundaries included). Similarly any rectangle with y2 = top includes top. But does there need to be a rectangle that has both? Possibly two different rectangles cover the right edge and top edge separately, but the corner point itself may be covered if either rectangle includes it? For the point (right, top), it's on the right edge if some rectangle has x2 = right and y between its y1 and y2 includes top. But top is max y2; a rectangle with x2 = right may have y2 < top, so its top edge is below top. Then (right, top) would be above its top, not covered. Similarly a rectangle with y2 = top may have x2 < right, so its right edge is left of right. So (right, top) would not be covered. Could it be covered by a rectangle that has x1 ≤ right ≤ x2 but x2 > right? Not possible because right is max x2, so no rectangle has x2 > right. Could a rectangle have x1 ≤ right ≤ x2 with x2 = right? That's the same. So the corner point might be uncovered if no rectangle covers it. However, would that affect area? No, it's a point, area zero. So area equality would still hold. But the union would not equal the square because that corner point is missing. Similarly, missing points along edges (like a segment of the edge not covered) would also be missing, but such a missing segment would have positive length but zero area? Actually a segment of the edge has zero 2D area. So area equality would not detect it.
Thus we need to check that the outer boundary is fully covered. Similarly, there could be missing points on the boundary that are not corners, e.g., a small portion of the left edge not covered. That would be a 1D gap; area unchanged. So we need to ensure that every point on the perimeter of the bounding box is covered by at least one rectangle.
Moreover, internal holes? Those would have area >0 if they have interior. So area equality catches positive area holes. But could there be internal "cracks" (lines) not covered? For instance, two rectangles that meet at a corner but leave a line segment uncovered? Example: square 2x2. Rectangle A: [0,1]×[0,2]; Rectangle B: [1,2]×[0,1]; missing the region [1,2]×[1,2]? That's a positive area hole (2x2 missing top right quadrant) => area less. To have a line uncovered, you need a gap of zero width, but because rectangles are closed and continuous, any uncovered line would require that the rectangles' boundaries align exactly but leave a line uncovered. For instance, consider two rectangles: R1 = [0,1]×[0,2], R2 = [1,2]×[0,2] but R2's left edge is at x=1, but does it include x=1? Yes, because boundaries included. So they share the vertical line x=1, covered. So no gap. If R2's left edge is x>1, then there is a strip uncovered, positive area. So zero-width gaps cannot happen because rectangles are closed and the coordinates are real numbers; if two rectangles are adjacent but not overlapping, the only way they don't cover a line between them is if they are separated by a distance >0 (positive area gap) or they touch at a point? Could there be a missing line segment on the interior? Suppose we have three rectangles that together cover the square but leave a diagonal line? Not possible as they are axis-aligned. So likely the only potential missing points are on the outer boundary, specifically points that are not included in any rectangle's interior or boundary because no rectangle extends to that edge at that y (or x). So we need to check coverage of the perimeter.
Thus the condition reduces to:
- Compute left = min(x1), right = max(x2), bottom = min(y1), top = max(y2).
- Check that right - left == top - bottom (square shape).
- Check that the total area of rectangles (sum of (x2-x1)(y2-y1)) equals (right-left)(top-bottom). Since no overlap, union area = sum areas.
- Check that the outer perimeter of the bounding box is fully covered. That is, for each point on the boundary, there exists a rectangle that contains it. Since the boundary is a closed set, it's enough to check that each of the four edges is fully covered by the union of rectangles. However, checking every point is infinite; we need to check that for each edge, the union of intervals (projections of rectangles covering that edge) covers the entire edge length.
Specifically:
- Bottom edge: y = bottom, x from left to right. For each rectangle, if it contains points with y=bottom (i.e., y1 == bottom, because rectangle includes its bottom edge). Actually a rectangle includes its bottom boundary: y = y1 is included. So if a rectangle has y1 <= bottom <= y2? Since bottom is min of y1, bottom equals the smallest y1 among all rectangles. So some rectangles have y1 = bottom, others have y1 > bottom. For a point (x, bottom) to be covered, there must be a rectangle with y1 <= bottom <= y2 and x1 <= x <= x2. Since bottom is the minimum y1, any rectangle with y1 > bottom cannot have bottom in its y-range because bottom < y1. Only rectangles with y1 == bottom can have bottom in their y-range (since y2 > y1 > bottom? Actually if y1 == bottom, then bottom is included. If y1 > bottom, then bottom is less than y1, so not in the closed interval [y1, y2]. So only rectangles with y1 == bottom can cover the bottom edge. Also, could a rectangle with y1 < bottom? That's impossible because bottom is min y1, so none. So the bottom edge is covered exactly by those rectangles whose y1 == bottom. Their projections on x (the intervals [x1, x2]) must cover [left, right] (the entire bottom edge). Also, these rectangles must have y2 >= bottom (which they do) and their x-intervals must collectively cover the whole width.
Similarly:
- Top edge: y = top. Only rectangles with y2 == top cover top edge (since top is max y2). Their x-intervals must cover [left, right].
- Left edge: x = left. Only rectangles with x1 == left cover left edge. Their y-intervals must cover [bottom, top].
- Right edge: x = right. Only rectangles with x2 == right cover right edge. Their y-intervals must cover [bottom, top].
But is that sufficient? What about corner points? They would be covered if both corresponding edges are covered? Actually a corner point could be covered by either edge coverage, but if the bottom edge coverage includes the leftmost point (x=left) and the left edge coverage includes the bottommost point (y=bottom), the corner (left,bottom) is covered if at least one rectangle covers it. But our edge coverage condition ensures that the entire bottom edge including the left endpoint is covered by some rectangle with y1=bottom, and the entire left edge including the bottom endpoint is covered by some rectangle with x1=left. However, could it be that the rectangle covering the bottom edge at x=left doesn't include that corner because its x-interval might be [left, something] but does it include the point (left,bottom)? Yes, if its x1 <= left <= x2 and y1=bottom. Since left is min x1, for a rectangle with y1=bottom to cover x=left, it must have x1 = left (because if x1 > left, then the interval [x1, x2] does not include left because x1 > left). Actually consider left = min x1. Some rectangles might have x1 > left. If a rectangle has y1=bottom and x1 > left, then its projection on x does not include left. So to cover the point (left, bottom), we need a rectangle that has both x1 = left and y1 = bottom (or maybe a rectangle with x1 <= left <= x2 and y1 <= bottom <= y2). Since left is min x1, only rectangles with x1 = left can have left within [x1,x2] (if x1 = left). Could a rectangle have x1 < left? No, left is minimum, so x1 >= left. So for left to be in [x1,x2], we need x1 <= left <= x2. Since x1 >= left, this forces x1 = left and left <= x2 (which is true). So indeed, to cover the leftmost point on bottom edge, there must be a rectangle with x1 = left and y1 = bottom. Similarly, top-right corner needs a rectangle with x2=right and y2=top, etc. But is it necessary to have a single rectangle covering the corner, or could two different rectangles cover it from different edges? For the point (left, bottom), if there is a rectangle with x1=left and y1=bottom, it's covered. Could it be covered by a rectangle with x1=left but y1 > bottom? No, because then bottom < y1, so not in y-range. Could it be covered by a rectangle with y1=bottom but x1 > left? No, because left not in x-range. Could it be covered by a rectangle that is not aligned with either edge? That is, a rectangle that contains the point but is not necessarily having x1=left or y1=bottom? For it to contain (left,bottom), it must satisfy x1 <= left <= x2 and y1 <= bottom <= y2. Since left is min x1, for x1 <= left to hold, we need x1 <= left, but since x1 >= left, this forces x1 = left. Similarly y1 <= bottom forces y1 = bottom. So indeed, any rectangle covering the corner point must have x1=left and y1=bottom. So the corner point requires a rectangle that exactly starts at that corner. Similarly, (right, top) requires a rectangle with x2=right, y2=top. (left, top) requires x1=left, y2=top. (right, bottom) requires x2=right, y1=bottom.
Thus, edge coverage conditions (the intervals covering the whole edge) may not guarantee that the corners are covered if the covering rectangles for the edges do not extend all the way to the corner. For example, bottom edge could be covered by two rectangles: one covering [left, a] and another covering [b, right] with a < b, but they might overlap? Actually to cover the whole bottom edge continuously, the union of intervals must be [left,right]. This could be achieved without any rectangle covering the exact left endpoint if an interval includes left but maybe with closed endpoints? If a rectangle has x1 = left, then it includes left. If no rectangle has x1 = left, can the bottom edge still be covered? Suppose left = 0, right = 10. Rectangles with y1=bottom: R1 has x1=0, x2=5 (covers [0,5]), R2 has x1=5, x2=10 (covers [5,10]). The point x=5 is covered by both (since boundaries included). So that's fine. But if R1 has x1=1, x2=5, R2 has x1=5, x2=10, then the interval from 0 to 1 is uncovered. So left would not be covered. So to cover the whole [left,right], there must be at least one rectangle whose interval starts at left (x1=left) and at least one whose interval ends at right (x2=right) for that edge. But for bottom edge, we need that the leftmost point is covered; thus there must be a rectangle with y1=bottom and x1=left. Similarly, rightmost point needs rectangle with y1=bottom and x2=right. So the edge coverage condition (union of intervals = [left,right]) already implies that there is at least one interval with left endpoint = left (otherwise the union wouldn't include left, because intervals are closed; if all intervals have x1 > left, then left is not included). However, it's possible that an interval covers left but its x1 is not exactly left? Can an interval cover left if x1 > left? No, because the interval is [x1,x2] with x1 > left, so the smallest point in the interval is x1, which is > left, so left is not covered. So to cover left, we need some interval with x1 = left. So indeed, the union of intervals covering [left,right] forces that there is at least one interval whose left endpoint is exactly left, and at least one whose right endpoint is exactly right. So the edge coverage condition (the union of the x-intervals of rectangles with y1=bottom equals [left,right]) already ensures the corner points on that edge are covered? The bottom-left corner is (left, bottom). For it to be covered, we need a rectangle with x1=left and y1=bottom. The edge condition ensures there is a rectangle with y1=bottom and x1=left? Actually it ensures there is some rectangle with y1=bottom that covers the leftmost point. That rectangle must have x1 <= left <= x2. Since left is min over all x1, that rectangle must have x1 = left. So yes, that rectangle has both x1=left and y1=bottom. So corner covered. So if the bottom edge is fully covered, then the bottom-left corner is covered. Similarly bottom-right corner would be covered by rectangle with y1=bottom and x2=right. So edge coverage implies corner coverage for that edge, but we still need to ensure the top edge and left edge coverage also hold, which would then cover all four corners (they might be covered by different rectangles, but that's fine). However, we must also ensure that the left edge is fully covered; that will require a rectangle with x1=left and y1=bottom? Not necessarily; left edge coverage requires rectangles with x1=left and their y-intervals covering [bottom,top]. The bottom-left corner is already covered by a rectangle from bottom edge condition (with y1=bottom and x1=left). That rectangle also contributes to left edge coverage if its y-interval includes bottom? It does: y1=bottom, y2 >= bottom. For it to cover the left edge at bottom point, it's fine. But to cover the entire left edge, we need that the union of y-intervals of rectangles with x1=left covers [bottom,top]. That includes the bottom point. So likely, if both bottom and left edge are fully covered, the corner is covered.
Thus the necessary and sufficient conditions might be:
- Square side length equality.
- Sum of areas equals area of square.
- The four edges are fully covered by rectangles that are flush with those edges (i.e., rectangles that have their boundary at that edge).
But is sum of areas equal to area of square equivalent to no holes? As argued, if there is a hole of positive area, area sum would be less. So area equality ensures no holes. Could there be holes that are measure zero but still the union is not equal to the square? Holes of measure zero (like missing points) would not affect area, but we also check edge coverage, which ensures perimeter covered. Could there be interior missing line segments? Possibly, but as argued, such missing lines would have to be of zero width, meaning they'd be boundaries between rectangles that don't include that line? But if two rectangles are adjacent, they share boundary; the line between them is covered by both boundaries. If they are not adjacent, there is a gap of positive width. So interior missing lines cannot happen without positive area gap. So area equality and edge coverage should be sufficient.
But wait: Could there be a scenario where the union equals the square but the sum of areas is greater than square area? That's impossible because union area <= square area (since union ⊆ square). Sum of areas = union area because no overlap (interiors disjoint). So sum <= square area. So if sum equals square area, union = square up to measure zero. And edge coverage ensures that the boundary is included, so union = square exactly.
Thus algorithm:
- Read n, rectangles.
- Compute left = min(x1), right = max(x2), bottom = min(y1), top = max(y2).
- If right - left != top - bottom: output NO.
- Compute total area sum = sum over i ( (x2_i - x1_i) * (y2_i - y1_i) ). If total != (right-left)*(top-bottom): output NO.
- Check edge coverage:
a. Bottom edge: collect all rectangles with y1 == bottom. For each, add interval [x1, x2]. Merge and check if the merged intervals cover [left, right] entirely (i.e., union = [left,right]).
b. Top edge: rectangles with y2 == top, intervals [x1, x2] cover [left, right].
c. Left edge: rectangles with x1 == left, intervals [y1, y2] cover [bottom, top].
d. Right edge: rectangles with x2 == right, intervals [y1, y2] cover [bottom, top].
If any fails, output NO.
- Otherwise YES.
But is it necessary to check both bottom and top edges? Could the square be covered without a rectangle having y1 exactly bottom? Let's examine: bottom edge consists of points with y = bottom. For any such point to be covered, there must be a rectangle with y1 <= bottom <= y2. Since bottom is the minimum y1, any rectangle with y1 <= bottom implies y1 = bottom (since y1 >= bottom). So indeed, rectangles covering the bottom edge must have y1 = bottom. So if the bottom edge is covered, there must exist rectangles with y1 = bottom. So checking bottom edge coverage using those rectangles is correct.
But what about the possibility that a rectangle covers the bottom edge but its y1 is less than bottom? That's impossible because bottom is the minimum over all y1. So no rectangle has y1 < bottom. So indeed only those with y1 = bottom can cover bottom.
Similarly for top edge: only rectangles with y2 = top cover it.
Left edge: only rectangles with x1 = left cover it.
Right edge: only rectangles with x2 = right cover it.
Thus the edge coverage checks are valid.
Now, is it sufficient to only check these four edges plus area equality? Let's think about potential counterexamples.
Consider a square of side 2, from (0,0) to (2,2). Suppose we have three rectangles:
R1: [0,1]×[0,2] (covers left half)
R2: [1,2]×[0,1] (covers bottom-right quarter)
R3: [1,2]×[1,2] (covers top-right quarter)
But R2 and R3 together cover the right half: [1,2]×[0,2]. Actually that's two rectangles covering the right half, but they are adjacent. The union is the whole square. Check edges: bottom edge: y1=bottom? R1 has y1=0, R2 has y1=0, R3 has y1=1. So bottom edge covered by R1 and R2 intervals: R1: [0,1], R2: [1,2] => covers [0,2]. Good. Top edge: y2=2: R1 y2=2, R3 y2=2. Intervals: R1 [0,1], R3 [1,2] -> covers [0,2]. Left edge: x1=0: only R1. y-interval [0,2] covers [0,2]. Right edge: x2=2: R2 x2=2? Actually R2 has x2=2? R2: [1,2] yes. R3: [1,2] yes. So rectangles with x2=2: R2 and R3. Their y-intervals: R2 [0,1], R3 [1,2] -> union [0,2]. Good. Area: R1:12=2, R2:11=1, R3:1*1=1 total=4, square area=4. So passes. So it's fine.
Now, consider a scenario where there is a missing sliver along the interior vertical line x=1 from y=0.5 to y=1.5, but still area matches? That missing sliver would be a gap of width zero? Actually if rectangles are placed such that they touch at a point but not along the line, there would be a gap of positive width if the x coordinates don't align. For example, R1: [0,1]×[0,2]; R2: [1,2]×[0,1]; R3: [1,2]×[1,2]? That covers everything as before. If we try to create a missing vertical line, we would need R1 to end at x=1 and R2 to start at x=1, which gives the line covered. If we try to have R1 end at x=0.9 and R2 start at x=1.1, then there's a gap of width 0.2, area missing. So area sum would be less than square area. So area catches that.
Now, consider a scenario where the square is 2x2 but we have rectangles that cover the interior fully but the outer boundary is partially missing, but area sum still equals square area? That would require that the missing boundary points are compensated by extra area elsewhere? But rectangles are bounded within the square (by definition of left,right,bottom,top). So they cannot have extra area outside the square to compensate. So if some boundary points are missing, that doesn't affect area; area sum could still equal square area. Example: square from (0,0) to (2,2). Use two rectangles: R1: [0,2]×[0,1] (bottom half), R2: [0,2]×[1,2] (top half). That covers all, edges covered. To have missing top edge? Suppose we use rectangles: R1: [0,2]×[0,1] (bottom half), R2: [0,1]×[1,2] (left top quarter), R3: [1,2]×[1,2] (right top quarter). That covers entire square, and top edge covered? Check top edge y=2: rectangles with y2=2: R2 and R3. Their x-intervals: R2 [0,1], R3 [1,2] => covers [0,2]. So fine.
To have missing top edge, we need that no rectangle has y2=top covering some x. For example, we could have rectangles: R1: [0,2]×[0,1]; R2: [0,1]×[1,2]; R3: [1,2]×[1,2] but R3 might have y2=2? Yes. Actually if we set R2: [0,1]×[1,1.5] and R3: [1,2]×[1,2], then top edge from x=0 to 1 at y=2 would be uncovered because R2's top is 1.5 < 2. But then the topmost y coordinate among rectangles would be max(y2) = 2 (from R3), so top=2. The point (0.5,2) is not covered because no rectangle extends to y=2 at that x. However, does the area sum equal square area? Let's compute: R1 area = 21 = 2; R2 area = 10.5 = 0.5; R3 area = 11 = 1; total = 3.5; square area = (2-0)(2-0)=4. So area sum less, because the missing top edge region is not just a line but actually a region: the area above y=1.5 for x in [0,1] is missing, which is 0.51=0.5 area. So area detects it. So to have area sum equal, the missing boundary must be only a subset of the boundary of zero area. That would require that along the top edge, for each x, there is a rectangle that reaches y=top, except possibly at isolated points? But if the union of intervals from rectangles with y2=top does not cover the full [left,right], there will be an interval of x where no rectangle reaches top, resulting in a missing region of positive area? Actually consider: Suppose we have a rectangle that reaches top only at a single x point? That's not possible; rectangles are contiguous in x. If the set of x for which some rectangle has y2=top is not the whole [left,right], then there is a subinterval (a,b) where no rectangle has y2=top. For x in (a,b), the top edge point (x, top) might still be covered by a rectangle that does not have y2=top but has y2 > top? That's impossible because top is max y2, so no rectangle has y2 > top. So for such x, the point (x, top) cannot be covered by any rectangle because to cover that point, a rectangle must have y2 >= top (and y1 <= top). Since top is max y2, the only rectangles that can have y2 >= top are those with y2 = top (since y2 cannot exceed top). So they must have y2 = top and also y1 <= top. So indeed, if there is a gap in the x-coverage of rectangles with y2=top, then there is an interval of x where the top edge is missing. But those missing points have zero area. So area sum could still equal square area if the missing top edge points are compensated by extra area elsewhere? But rectangles cannot extend beyond top, so no extra area. The union area would be less than square area because the missing top edge points are on the boundary, but does their absence reduce the area? No, area is measure of interior; boundary has zero Lebesgue measure. So the union could still have area equal to square area even if some boundary points are missing. Example: square [0,1]x[0,1]. Suppose we have a single rectangle covering the open square (0,1)x(0,1) but not including the boundary. But rectangles are closed, so they include boundaries. However, we can try to construct a scenario where the top edge is covered only at rational x? But rectangles provide continuous intervals. So if the union of intervals from top-reaching rectangles is not the whole [0,1], there will be a non-empty open interval (a,b) where no rectangle has y2=top. For x in (a,b), the point (x, top) is not covered. However, does that affect area? The area of the union is still 1, because the missing set is a line segment, measure zero. So area sum = 1? Let's test with actual rectangles: We need the union to cover the entire interior, but miss a portion of the top boundary. For the interior to be covered, for each interior point (x,y) with y<1, there must be coverage. For x in (a,b) and y close to 1 but less than 1, those points could be covered by rectangles that have y2 close to 1 but not equal to 1. For example, rectangle that covers x in (a,b) and y up to 0.999? But then y2 would be 0.999, less than top=1. That rectangle would not cover the top edge, but could cover interior points arbitrarily close. However, to cover interior points arbitrarily close to y=1, the rectangle's y2 must be >= those y values. But to cover all interior points, for each (x,y) with y<1, there must be a rectangle containing it. In particular, for a given x in (a,b) and y=0.9999, we need a rectangle with y2 > 0.9999. Could have y2 = 0.9999? That would include y=0.9999 if closed? If y2=0.9999, then point with y=0.9999 is included (since y2 is inclusive). So we could have a rectangle covering that interior. So the top edge could be missing while the interior is fully covered, provided that for every x, the limit of rectangles' y2 as we approach the top is 1? Actually if the topmost y2 among rectangles covering a given x is less than 1, then there is an interval (y2_max, 1) that is uncovered; that would be a positive area region if it's for a range of x. To avoid positive area, we need that for each x, the supremum of y2 of rectangles covering that x equals 1. But since we only have a finite number of rectangles, for each x, the maximum y2 among rectangles covering that x is either some value < 1 or =1. If there exists an interval of x where the maximum y2 < 1, then the region (x, y) for y in (max_y2, 1) would be uncovered, and that region would have positive area if the interval in x has positive length. So to have no positive area uncovered, for all x except possibly a set of measure zero, the maximum y2 must be 1. If there is a set of x of positive length where max y2 < 1, then area uncovered >0. So area equality would fail. Conversely, if for almost every x, max y2 = 1, then the uncovered set is at most the boundary line segment where y=1 for those x where max y2 = 1? Actually if max y2 = 1 for a given x, then there is at least one rectangle with y2 = 1 covering that x (since rectangles have closed intervals; if the supremum is 1 and there is a rectangle achieving 1, then that point is covered; if no rectangle achieves 1 but there are rectangles with y2 arbitrarily close to 1? But we have only finite rectangles, each with a fixed y2. So the maximum y2 among rectangles covering x is a specific value. So if that value equals 1, then there is a rectangle with y2=1 covering that x. So to have max y2 = 1 for almost every x, we must have rectangles with y2=1 covering those x. So the set of x for which some rectangle with y2=1 covers must be a set whose measure is the full interval [left,right], otherwise there will be positive-length interval where max y2 < 1. So essentially, to avoid positive area uncovered, the union of x-projections of rectangles with y2=top must be the entire width [left,right] (up to measure zero). But measure zero could be a finite set of points; area would still be full. However, is it possible to have union of intervals cover all but a set of measure zero but not cover the endpoints? For a closed interval, if the union of closed intervals is missing only a set of measure zero, could it still miss a point like a single point? For example, intervals: [0,0.5] and [0.5,1] cover all; missing only a point would require a gap of zero length, but that can't happen because if a point is missing, there is a gap of zero length? Actually if we have intervals [0,0.5) and (0.5,1] they are not closed, but our intervals are closed. With closed intervals, the union of [0,0.5] and [0.6,1] leaves a gap (0.5,0.6) of positive length. So any missing point would imply an interval gap around it because intervals are closed and finite. More formally, the union of finitely many closed intervals is a closed set. If it is missing a point p, then there exists an open interval around p not in the union (since union is closed, its complement is open). That open interval would have positive length, thus positive measure. So if the union misses a point, it misses an interval around it. Therefore, for the union of x-projections of rectangles with y2=top to have no positive-length gap, it must cover the entire [left,right] interval (since it's a closed subset of [left,right] and if it's equal to [left,right] then it's covered; if it's a proper closed subset, its complement is open, thus contains an interval, so positive measure). Therefore, area equality (no positive area uncovered) implies that the union of x-projections of top-reaching rectangles must be the entire [left,right]. Similarly for bottom, left, right. Actually area equality ensures that there is no uncovered region in the interior. However, could there be an uncovered region that is not detected by the projections? The uncovered region would be of the form: for some x in a set A of positive measure, there exists y-interval of positive length uncovered. This could happen even if the top edge is covered (i.e., union of top-reaching x-projections covers full width) but there is a hole somewhere else. But area equality would detect any positive area hole. So if area equality holds, then there is no positive area uncovered region anywhere. That already implies that the union of x-projections of rectangles covering any horizontal line y must be full width for almost all y? Not exactly: It could be that for some y, the coverage is not full width, but the missing vertical strips are compensated by coverage at other y? No, area is integral over y of the measure of covered x at that y. If total area = (right-left)(top-bottom), then for almost every y in [bottom,top], the measure of covered x at that y must equal (right-left). Because the area can be expressed as integral over y of the length of the union of intervals of rectangles intersecting that y. Since total area = total rectangle area, and the rectangles are disjoint interior, we have that for almost every y, the sum over rectangles of width of intersection at that y = right-left. But because rectangles are disjoint interior, at a given y, the intervals from different rectangles are disjoint (except possibly at boundaries). Actually if rectangles are non-overlapping interior, at a given y (horizontal line), the intersections of the line with the rectangles are intervals that may touch at boundaries but not overlap in interior. So the total length at that y is the sum of lengths of these intervals, which equals the measure of the union (since they only meet at endpoints). So we have for almost every y, the total length of covered x = right-left. That implies that for almost every y, the union of intervals covers the entire width [left,right] (except possibly measure zero in x). As argued, if the union of intervals is closed, then covering full measure of [left,right] implies it actually equals [left,right] (since if it were missing any point, there would be an open interval missing). So for almost every y, the union equals [left,right]. In particular, for y values arbitrarily close to top, this holds. However, does it guarantee that at y = top exactly, the union also equals [left,right]? Possibly the line y=top might be an exception (measure zero). But we need the top edge points to be covered for set equality. So area equality alone does not guarantee that the boundary is covered. So we indeed need to separately check the four edges.
Now consider the possibility that the union is the square but some edge is covered not by rectangles that have that edge as their own boundary, but by rectangles that extend beyond? But they can't extend beyond because the bounding box is defined as extremes. If a rectangle extends beyond, the bounding box would be larger. So the outermost coordinates are exactly the boundaries. For a point on the top edge, it has y=top. To be covered, there must be a rectangle with y2 >= top. Since top is max y2, that rectangle must have y2 = top and also y1 <= top. So it's exactly those rectangles that have y2 = top. So our approach is correct.
Now, could there be a case where the union is a square but the rectangles overlap? The problem says rectangles may touch but do not overlap (no interior points in common). Overlap would cause sum of areas > union area, and union area would be less than sum. Since union ⊆ square, sum area could be > square area? Actually if rectangles overlap, sum area > union area. But union could still equal square, but sum area > square area. For example, two identical rectangles covering the entire square overlapping would have sum area = 2*square area > square area. But they overlap, which is prohibited. So we can rely on non-overlap condition to assert union area = sum area. But the input guarantees non-overlap interior. However, we need to ensure we don't mistakenly accept overlapping cases if we only check area equality? But if they overlap, sum area > union area. Since union could be square, then union area = square area, but sum area > square area. So if we check sum area == square area, we would reject overlapping cases because sum area > square area. So that's good.
But is it possible that due to overlapping, sum area equals square area? That would require overlapping area exactly compensated by missing area elsewhere? Overlap means interior intersection positive area; to have sum of areas equal to union area, the overlap must be zero. So if there is overlap, sum area > union area. So if sum area = square area, then union area = square area, and overlap area = 0, meaning no overlap. So the check ensures non-overlap indirectly? Not necessarily: if there is overlap but also some area outside the square (which can't happen because bounding box is defined by extremes), then sum area could equal square area? Suppose rectangles overlap and also extend beyond bounding box? That would increase bounding box. So no.
Thus algorithm seems sound.
But we must also consider that rectangles may touch, but that doesn't affect area.
One more nuance: The union may be a square but the rectangles could be arranged such that the square's boundary is not entirely covered by rectangles that have that edge as their own boundary; but as argued, if the top edge is fully covered, there must be rectangles with y2=top covering each x. But could the top edge be covered by a rectangle that does not have y2=top but has y2 > top? That's impossible because top is max y2. So no.
Thus algorithm steps are good.
But we need to be careful with floating points? Coordinates are integers, so area and side length are integers. Check equality of side lengths: right-left and top-bottom are integers. So fine.
Now, we must implement interval merging and coverage check. Since n ≤ 5, we can do simple approach.
Implementation details:
-
Read n and rectangles. Store as list of tuples (x1, y1, x2, y2). Note: x1<x2, y1<y2.
-
Compute left = min(x1_i), right = max(x2_i), bottom = min(y1_i), top = max(y2_i).
-
If (right - left) != (top - bottom): print NO and exit.
-
Compute total_area = sum((x2-x1)(y2-y1)). Compute square_area = (right-left)(top-bottom). If total_area != square_area: print NO.
-
Check edges:
Define function check_cover(intervals, target_start, target_end) that given list of [a,b] intervals (closed), checks if their union covers [target_start, target_end].
Implementation: Sort intervals by start. Initialize current = target_start. For each interval [a,b] in sorted order:
if a > current: return False (gap).
else current = max(current, b). After processing, check current >= target_end.
But careful: Intervals are closed, so if a <= current, they overlap or touch. Since touching at endpoints is okay (e.g., [0,1] and [1,2] cover continuously because point 1 is in both). In our merge, we treat a <= current as connecting because current represents the furthest point we've covered continuously. If a == current, it's acceptable because the interval starts exactly at the covered point, and we extend to b. So the condition a <= current works.
After loop, if current >= target_end, covered.
Now, for bottom edge:
Collect intervals from rectangles with y1 == bottom: each interval = [x1, x2].
But we must also consider that rectangles with y1 == bottom might have x1 > left or x2 < right, but union must cover entire [left,right].
But also note: Some rectangles might have y1 == bottom but also have y2 == bottom? Not possible because y2 > y1. So fine.
Check: if no rectangle with y1 == bottom, then bottom edge cannot be covered (unless there are no points? But bottom is min y1, so at least one rectangle has y1=bottom. Actually it's possible that all rectangles have y1 > bottom? No, because bottom is defined as min y1, so at least one rectangle has y1 = bottom. So there will be at least one rectangle for bottom edge.
But wait: bottom is min over all y1. That means there is at least one rectangle with y1 = bottom. So bottom edge coverage check will have at least one interval.
Similarly for top: at least one rectangle with y2 = top.
Left: at least one rectangle with x1 = left.
Right: at least one rectangle with x2 = right.
So that's fine.
Now, we also need to check that the intervals for left edge (y-intervals) cover [bottom, top]. Similarly for right edge.
But there is nuance: The rectangles that have x1 == left might have y1 > bottom or y2 < top. The union of their y-intervals must cover [bottom, top]. Could it be that a point on the left edge is covered by a rectangle that does not have x1 == left but has x1 < left? That can't happen because left is min x1, so no rectangle has x1 < left. But could a rectangle with x1 > left still cover a point on the left edge? No, because for a point (left, y) to be covered by a rectangle, we need x1 <= left <= x2. Since x1 >= left, this forces x1 = left. So indeed only rectangles with x1 == left can cover left edge. So that's correct.
Thus the edge coverage checks are necessary and sufficient for perimeter coverage.
Now, consider scenario where the union is a square but there is a "hole" that is a single point (or a line of zero width) inside. Could that happen while area equality holds? As argued, a hole of positive area is excluded. A point or line has zero area, so area equality holds. But would that violate the condition that the union equals the square? Yes, because the square contains that point, but union does not. So we must ensure no missing points anywhere, not just on boundary. Could there be an isolated missing point in the interior? For that to happen, the point would be uncovered while all surrounding points are covered. Since rectangles are closed, the union is closed. If a point p is not in the union, then there is an open neighborhood around p not in union? Actually if p is not in the union, since the union is closed (finite union of closed sets is closed), the complement is open. Thus if p is not in union, there exists an open ball around p entirely not in union. That ball would contain a small area, thus positive area uncovered. So any missing interior point implies a missing area around it (since complement open). More precisely: The union is a closed set (as finite union of closed rectangles). Its complement in the square is open relative to the square? Actually the union is a subset of the square. The set of points in the square not in the union is the intersection of the square with the complement of the union. Since union is closed in R^2, its complement is open. Intersection of open with square is relatively open in square? It might not be open in R^2 if the square is closed, but it's open in the subspace topology of the square? Actually if union is closed in R^2, then its complement is open in R^2. Then (complement) ∩ square is open in the subspace topology (since intersection of open with square). So it is open in the square. If that set contains a point p, then there is an ε-ball around p (in R^2) such that the whole ball is in complement. Intersect with square, we get an open set (relative) around p within the square that is uncovered. That set will have positive area (since it contains a small disk). Therefore any missing point (even a single point) implies a positive area uncovered region. So if area of uncovered region is zero, then there cannot be any missing point. Therefore area equality (union area = square area) ensures that the union equals the square as sets, because the difference set (square \ union) would have measure zero. If it had any point, by the argument above, it would contain an open neighborhood (since complement of closed is open) and thus positive measure. Wait, careful: The union is closed, but the square is also closed. The set difference D = square \ union. If D is non-empty, is D necessarily open in the square? Not necessarily. Because union is closed, so its complement is open. D = square ∩ (complement of union). The complement of union is open in R^2. Intersection of an open set with square yields a set that is open in the subspace topology of the square. Indeed, for any point p in D, since complement of union is open, there exists ε>0 such that ball B(p,ε) ⊆ complement of union. Then B(p,ε) ∩ square ⊆ D, and this is an open neighborhood of p in the square. Thus D is open relative to the square. Therefore if D is non-empty, it contains some open set (relative to square) and thus has positive area. Therefore, if total area of union equals area of square, then D must have measure zero. The only way for an open set to have measure zero is if it is empty (since non-empty open set in R^2 has positive Lebesgue measure). So D must be empty. Therefore, area equality alone implies that the union exactly equals the square (as sets). This is a powerful argument: Since union is closed (finite union of closed rectangles), and square is the closure of its interior (if square has positive side length), but more importantly, if union area = square area and union ⊆ square, then square \ union is a subset of square with measure zero. But if it were non-empty, as argued, it would contain an open set (relative to square) because union is closed? Let's double-check: The union U is a finite union of closed rectangles, so it is closed. The square S = [left,right]×[bottom,top] is also closed. Consider D = S \ U. Since U is closed, its complement in R^2 is open. D = S ∩ (R^2 \ U). The set R^2 \ U is open. The intersection of an open set with S is open in the subspace topology of S. However, "open in the subspace topology" means that for each point p in D, there exists an open ball B(p, ε) such that B(p, ε) ∩ S ⊆ D. This ball may not be fully contained in S; but the intersection with S is a relatively open set in S. That relatively open set could have positive area? It will contain at least a small "disk" intersected with S. If p is interior point of S (i.e., not on the boundary), then for sufficiently small ε, B(p, ε) ⊆ S? Actually if p is interior to S, then there is some δ such that B(p, δ) ⊆ S (since S is a closed rectangle, its interior is open). So then B(p, ε) with ε < δ is fully inside S, and then B(p, ε) ⊆ D, meaning D contains a full open disk, which has positive area. If p is on the boundary of S, say on the left edge, then B(p, ε) ∩ S is a half-disk (or a region that includes points inside S). This region also has positive area (the area of the half-disk is positive). So any point in D, even on the boundary, yields a set of positive area inside D (since the intersection of an open ball with the square will contain a small area). Therefore, if D is non-empty, it has positive Lebesgue measure. Therefore, area equality (i.e., area(U) = area(S)) forces D to be empty. So we actually do not need to separately check the edges! Because if area matches and U ⊆ S, then U must equal S exactly. Let's confirm: U ⊆ S, both closed? U is closed, S is closed. If there is a point in S not in U, then D is non-empty. Since U is closed, D is relatively open in S as argued, and thus contains a positive area region. This would imply area(U) < area(S). Therefore, area equality implies U = S.
But is it always true that U is closed? Finite union of closed sets is closed. Each rectangle is closed (including its boundary). So U is closed. S is the bounding box defined by min and max coordinates, which is also closed. Good.
But wait: Could it be that U is not a subset of S? By definition, left = min x1, right = max x2, bottom = min y1, top = max y2. Since each rectangle's x1 >= left, x2 <= right? Actually left is min x1, so each rectangle has x1 >= left? No, left is the minimum of x1, so some rectangle has x1 = left, but others could have x1 > left. But all x1 are >= left? Since left = min, yes all x1 >= left. And all x2 are <= right? Since right = max of x2, all x2 <= right? Actually right = max x2, so all x2 <= right? No, max means some have x2 = right, others have x2 <= right. So all x2 <= right. So each rectangle's x2 <= right, and x1 >= left? Wait, check: x1 may be > left? Actually if left = min(x1), then for any rectangle, x1 >= left (since left is minimum). So yes, all x1 >= left. So each rectangle is entirely within x range [left, right]? For a rectangle, its x-coordinates satisfy x1 >= left and x2 <= right, so indeed x1 >= left and x2 <= right. That means the rectangle's x-interval is a subset of [left, right]. Similarly, y1 >= bottom (since bottom = min y1) and y2 <= top (since top = max y2). So indeed, each rectangle is fully contained within the bounding box S = [left,right]×[bottom,top]. So U ⊆ S.
Thus, if area(U) = area(S), and U ⊆ S, then the measure of S\U is zero. Since S\U is open relative to S (as argued) and non-empty would imply positive measure, we must have S\U empty. Therefore U=S. So area equality alone is sufficient to guarantee that the union equals the square exactly, including all boundary points. The earlier concern about missing boundary points without area loss is invalid because missing a boundary point would mean S\U contains that point, which would imply existence of an open set around it within S (since complement of U is open) leading to positive area. Let's test with a potential counterexample: Suppose square S = [0,1]×[0,1]. Suppose we have rectangles that cover all of S except the top edge points (x,1) for x in some set. Could that happen without any area loss? Let's attempt to construct such a union U that is closed and missing some boundary points but still area(U)=1. For U to be closed, the set of missing points must be open in the complement? Actually U is the union of rectangles. If we intentionally leave out the top edge, would U be closed? The union of rectangles that are closed but do not include the top edge: e.g., rectangle covering [0,1]×[0,0.999]. That rectangle is closed and includes its top edge at y=0.999. So U = [0,1]×[0,0.999]. That's a closed set. Its area is 0.999, not 1. To get area 1, we need the region to have vertical extent 1. If we include another rectangle that covers some parts up to y=1 but maybe missing some points. For instance, rectangle A = [0,1]×[0,0.5], rectangle B = [0,0.5]×[0.5,1], rectangle C = [0.5,1]×[0.5,1]. That union is actually the whole square (as earlier). If we try to remove the top edge point (0.6,1), could we adjust B and C so that they don't cover that point but still cover interior arbitrarily close? For point (0.6,1) to be missing, we need no rectangle with y2=1 covering x=0.6. That means for x=0.6, the maximum y among covering rectangles is less than 1. Then for y just below 1, say 0.999, those points must be covered. That would require rectangles with y2 >= 0.999 but <1. But rectangles have fixed y2; they could be, for example, B: [0,0.5]×[0.5,0.999], C: [0.5,1]×[0.5,0.999]. Then U = [0,1]×[0,0.5] ∪ [0,0.5]×[0.5,0.999] ∪ [0.5,1]×[0.5,0.999]. The area of U = 10.5 + 0.50.499 + 0.50.499 = 0.5 + 0.2495 + 0.2495 = 0.999. So area < 1. To get area =1, we would need the topmost rectangles to have total vertical extent summing to 0.5 for each x, but if they stop at 0.999, then the missing strip from 0.999 to 1 for those x has area 0.001width, positive. So area would be less than 1. To have area exactly 1 while missing the top edge, we would need to compensate by adding extra area somewhere else, but all rectangles must stay within S (since bounding box is defined by extremes). If we try to have rectangles that extend above 1? That would increase top beyond 1, redefining bounding box. So impossible. Thus any missing boundary segment of positive length leads to a missing area region of positive area. A missing isolated boundary point: could that happen without area loss? Suppose we miss a single point on the top edge, say (0.5,1). For the union to be closed and missing that point, we need that for x=0.5, the maximum y among rectangles is <1, but for x != 0.5, the max y is 1. However, since rectangles are finite, their x-projections are intervals. The condition that at x=0.5, no rectangle reaches y=1, but for x slightly less or more, they do. This would imply that near x=0.5, there is a small interval where the max y drops below 1? Let's examine: Suppose we have two rectangles: R1 covering left part up to y=1, and R2 covering right part up to y=1, but they both stop just before x=0.5? For instance, R1: [0,0.5)×[0,1]? But rectangles are closed, so if R1's x2=0.5, it includes x=0.5. So if R1 includes x=0.5, then it would cover (0.5,1) if y2=1. To avoid covering (0.5,1), we could have R1 with x2=0.5 but y2 < 1, or R1 with x2<0.5. But if R1's x2<0.5, then there is a gap between R1's right edge and R2's left edge. That gap would be an interval of x where no rectangle reaches y=1, and likely also no rectangle covers interior at high y? That gap would have positive width, leading to missing area. Alternatively, we could have R1 with x2=0.5, y2=1 except that the point (0.5,1) is excluded? But the rectangle is closed, so if its x-interval includes 0.5 and y-interval includes 1, then (0.5,1) is included. So to exclude (0.5,1), either x-interval does not include 0.5 or y-interval does not include 1. If x-interval does not include 0.5, then there is a gap around 0.5 (since no rectangle covers x=0.5 at high y). If y-interval does not include 1, then for all x in that rectangle, max y < 1, leading to a band of missing area. So it seems that missing even a single boundary point forces a missing area region of positive measure. More formally: Suppose p = (x0, top) is not in U. Since U is closed, its complement is open. So there exists ε>0 such that B(p, ε) ∩ S ⊆ S\U. In particular, the intersection of B(p, ε) with S contains a small rectangular region near p (like a half-disk). That region has positive area. Therefore, missing a single point on the boundary implies positive area uncovered. So area equality would be violated. Hence area equality alone guarantees that every point of S is in U. So we do not need explicit edge checks.
But wait: The argument that D = S\U is relatively open in S relies on U being closed. Yes, U is closed. So if p ∈ D, then there is an open ball B(p, ε) contained in complement of U. Then B(p, ε) ∩ S is an open subset of S containing p, and it is contained in D. Since S contains a small disk (if p interior) or a small half-disk (if p on boundary), its area >0. So D has positive area. Therefore, if area(U)=area(S), D must be empty. So indeed, checking area equality and square shape (side lengths equal) is sufficient. Let's verify: The square shape ensures S is a square. But could S be a rectangle and area equality hold, but shape not square? Then the union would be a rectangle, not a square. So we need both side equality and area equality. Actually area equality with S being the bounding box: if side lengths are unequal, it's not a square, so we should output NO.
Thus the algorithm reduces to:
- Compute left, right, bottom, top.
- If (right-left) != (top-bottom): NO.
- Compute sum area.
- If sum area != (right-left)*(top-bottom): NO.
- Else YES.
But is there any other subtlety? Could there be a case where the union is a square but the bounding box is larger than the square because there are rectangles that extend beyond the square? Wait, if the union is exactly a square, then its bounding box is exactly that square's sides. So bounding box computed as min x1, max x2 etc will match the square. So no issue.
But what if the union is a square, but there are multiple rectangles that cover it, and we compute bounding box as described; it will be the square. So area equality holds. So condition seems sufficient.
But let's double-check with the provided examples. Example 1 has 5 rectangles, area sum =? Let's compute quickly: In first example, square of side 5? Actually coordinates: 0 0 2 3, 0 3 3 5, 2 0 5 2, 3 2 5 5, 2 2 3 3. So left = min x1 = 0, right = max x2 = 5, bottom = min y1 = 0, top = max y2 = 5. So side length 5, area 25. Sum of areas: Compute: (2-0)(3-0)=23=6; (3-0)(5-3)=32=6; (5-2)(2-0)=32=6; (5-3)(5-2)=23=6; (3-2)(3-2)=11=1. Total=25. So passes. So answer YES.
Example 2: 4 rectangles (the same as first but missing the central 1x1). left=0,right=5,bottom=0,top=5 side=5 area=25. Sum areas: 6+6+6+6=24. So not equal => NO. That matches.
Thus our simplified condition works for given examples.
But is there any scenario where area equality and side equality hold, but the union is not a square? Possibly if the union is not connected? But if it's a subset of the bounding box with full area, it must be the whole bounding box as argued. So it's a square.
But wait: The argument that D is relatively open in S depends on U being closed. U is closed. So if D is nonempty, then D contains an open set in S. But could D be nonempty yet have measure zero? No, because any nonempty open set in R^2 (relative to S) has positive Lebesgue measure. However, is it possible that D is nonempty but not relatively open in S? That would require that U is not closed? But U is closed. Let's re-express: D = S \ U. Since U is closed, its complement in R^2, call it V, is open. Then D = S ∩ V. Now, for any point p in D, there exists ε>0 such that B(p, ε) ⊆ V (since V is open). Then S ∩ B(p, ε) ⊆ D. This set is a neighborhood of p in the subspace topology of S, and it is non-empty. But is it necessarily open in the subspace topology? Yes, because B(p, ε) is open in R^2, and intersecting with S gives an open set in the subspace topology. So D is open in S. And any non-empty open subset of S (with S having non-empty interior) has positive area. Therefore D empty. So the argument stands.
Thus we don't need explicit edge checks.
But is there a scenario where U is not closed? The union of closed rectangles is closed. So yes.
One more nuance: The rectangles are given with integer coordinates, but they are continuous sets. So closedness holds.
Thus the solution is very simple: compute bounding box and area sum; check side equality and area equality.
But wait: Are we sure that the union being exactly the bounding rectangle implies it's a square? We also need to confirm that the bounding rectangle is indeed a square (side lengths equal). So we check that.
Thus the algorithm:
- read n.
- initialize left = INF, right = -INF, bottom = INF, top = -INF.
- total_area = 0.
- For each rect:
left = min(left, x1)
right = max(right, x2)
bottom = min(bottom, y1)
top = max(top, y2)
total_area += (x2-x1)*(y2-y1)
- side1 = right - left, side2 = top - bottom.
- if side1 != side2: print NO
- else if total_area != side1 * side2: print NO
- else print YES
But is it possible that total_area == side1*side2 even if rectangles overlap? Overlap would cause sum of areas > union area. But total_area is sum of individual areas. Overlap would mean that union area is less than sum. If total_area == square area, then union area = total_area (since no overlap? Actually we don't know if there is overlap; but total_area could still equal square area if there is overlap but also some parts of the square are not covered? Wait, if there is overlap, union area = total_area - overlap_area. So if total_area == square area, then union area = square area - overlap_area. For union to be subset of square, union area <= square area. So if total_area == square area, then union area = square area - overlap_area <= square area, implying overlap_area >= 0. Actually overlap_area could be positive, then union area < square area, which would contradict total_area == square area? Let's denote A = sum areas, U = union area, O = overlap area (total area of intersection counted multiple times). Since interiors are disjoint? Actually overlap means interior intersection positive area. But rectangles are closed and may share boundaries; overlap area is the area of intersection of interiors? The problem says no overlapping interiors. So interior of any two rectangles do not intersect. However, they could still overlap on boundaries? Overlap on a line or point does not contribute to area. So the sum of areas equals union area because any overlap is of measure zero (boundaries). So if there is any overlap of positive area (i.e., interior intersection), that violates the problem condition. But the input guarantees that there are no points that belong to the interior of more than one rectangle. So we can assume no positive area overlap. So sum areas = union area. Therefore area equality ensures union area = square area.
Thus the check is valid.
But wait: The problem statement: "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)." So they guarantee no interior overlap. Thus sum areas = union area.
Hence algorithm is correct.
But before finalizing, let's think of potential pitfalls: Could there be a case where the union is a square, but the bounding box computed as min x1, max x2, etc. is not the square because some rectangles extend outside the square? If the union is exactly a square, then the square's sides are the extremal coordinates of the union. Since rectangles are within the union, they cannot extend beyond the square. So bounding box matches square.
What about if the union is a square but there are multiple rectangles, and the sum of areas equals square area, but the rectangles have gaps that are of zero area? As argued, gaps would be open sets, thus positive area. So impossible.
Thus the solution is straightforward.
But wait, there is a nuance: The bounding box might be a square, and sum of areas might equal square area, but the rectangles could be arranged such that they do not cover the entire square due to "overlap" that is not interior? Overlap on boundaries doesn't affect area. But could there be a configuration where the union misses some line segment but still has full area? As argued, missing a line segment would imply missing an open region around it? Actually missing a line segment (like a portion of a vertical line) would mean that for some x0, and y in [a,b], the point (x0, y) is not covered. Since rectangles are closed, the complement of U is open. If a point (x0,y0) is not in U, there is an open ball around it not in U. That ball includes points with x != x0, so the uncovered set would include a 2D neighborhood, thus positive area. So missing a line segment also implies positive area. So area catches it.
Thus I'm convinced.
Now, I'd like to test some tricky cases.
Case: n=1, rectangle is a square. Then left=x1, right=x2, bottom=y1, top=y2, side lengths equal? If it's a square, then (x2-x1)==(y2-y1). Sum area = (x2-x1)*(y2-y1) equals square area. So YES.
Case: n=2, two rectangles that together form a square but with a gap? For area to equal square area, no gap. So they must exactly partition the square without gap. That will pass.
Case: n=2, rectangles that are placed such that they overlap partially (positive area overlap). But input says no overlap, so not allowed. But if we ignore that and just compute, sum area > square area? Possibly sum area could be > square area, so we'd output NO. But if they overlapped exactly such that the overlapping region compensates for some other missing region? That would still have sum area > union area, but union area might be square area if the union covers the square. For union to cover the square, the rectangles must cover it. If they overlap, sum area > square area, so we'd get NO. So it's fine.
Case: n=2, rectangles that are disjoint, but their bounding box is square, and sum area equals square area, but they are placed such that they don't cover the entire square? Could that happen? Suppose square 2x2. Rect A: [0,1]×[0,2] area=2. Rect B: [1,2]×[0,1] area=1. Sum=3 <4, so fails. To get sum=4, we need total area 4. If they are disjoint, each area positive, to sum to 4 they must fill the square exactly. Could there be a configuration where two rectangles have total area 4 but don't cover the entire square? For example, one rectangle 2x2 area=4, the other rectangle of area 0? That's just one rectangle. With two rectangles, if they are disjoint, the sum of areas cannot exceed the area of their union. Since union is subset of square, sum <= area(square). If sum = area(square), then union must have area = area(square). As argued, union = square. So they must cover it.
Now, consider a potential pathological case where the square side length is 0? But constraints x1<x2 and y1<y2, so rectangles have positive area. So side length >0.
Thus algorithm is sound.
Given the simplicity, we can implement quickly.
But wait, what about the scenario where the rectangles form a square but there is an extra rectangle that is entirely inside the square but overlapping with others? Overlap is prohibited, but maybe they touch. If there is an extra rectangle that is inside and does not overlap, then the sum area would exceed square area because the extra rectangle would add area without expanding bounding box, causing sum area > square area (since union area <= square area and union area = sum area because no overlap, then sum area = union area <= square area, so if there's an extra rectangle, union area stays same? Actually if there is an extra rectangle that is placed entirely within the union of other rectangles, then that would cause overlap (since it's inside other rectangles' interior). That is not allowed. So the input won't have that. If it's placed within gaps? But if the union already covers the square, any additional rectangle must either be outside (extending bounding box) or overlap. So it would cause either bounding box expansion or overlap. So sum area would not equal square area. So it's fine.
Thus final answer: Check bounding box side equality and total area equality.
Let's test with a custom case: n=3, rectangles: [0,2]×[0,1], [0,1]×[1,3], [1,2]×[1,3]. This forms a 2x3 rectangle? Let's compute: left=0, right=2, bottom=0, top=3, sides 2 and 3, not equal => NO. Good.
Another: n=2, [0,2]×[0,1], [0,2]×[1,2]. This forms a 2x2 square. left=0,right=2,bottom=0,top=2, side=2, area sum=21+21=4=4 => YES.
Edge case: n=2, [0,2]×[0,1], [0,1]×[1,2], [1,2]×[1,2] is three rectangles, but we can try a case where there is a missing point at a corner but area still 4? Let's attempt to construct. Square [0,2]×[0,2]. Use rectangles: R1: [0,2]×[0,2] but missing the top-right corner point? Can't miss a point with a single rectangle because it's closed. To miss a corner, we need multiple rectangles that collectively cover everything but the corner point is not included. Is that possible? For the top-right corner (2,2) to be missing, we need that no rectangle includes both x=2 and y=2. So we need rectangles that cover the right edge and top edge but not the intersection. For example, R_A: [0,2]×[0,2) (i.e., y2=2? If y2=2, it includes top edge. But we want to exclude (2,2). If we set R_A: [0,2]×[0,2) would mean y2=2 but open at top? But rectangles are closed, so y2 is the top edge inclusive. So we cannot have a rectangle that covers the right edge up to y=2 but excludes the point (2,2). Because if x2=2 and y2=2, the point (2,2) is included. If we set R_A with x2=2, y2<2, then the top edge of square would be at y=2, and no rectangle reaches that y, so the top edge would be missing a strip, causing area loss. But could we have R_B that covers the top edge with y2=2 but x2<2, and R_C covering right edge with x2=2 but y2<2, such that the interior is fully covered but the corner point (2,2) is uncovered? Then for points near (2,2) but with x<2, y=2 are covered by R_B (if its x-interval includes near 2). But R_B's x-interval likely ends at something less than 2, say x2=1.9, then there is a gap from 1.9 to 2 on the top edge. That gap would have length 0.1, and the region below it (for y close to 2) would also be missing? Not necessarily: R_C covers right edge for y up to maybe 1.9. So there would be a region near the top-right corner that is uncovered: the set of points with x>1.9 and y>1.9 would be uncovered because neither rectangle covers them. That region has positive area. So area sum would be less than 4. So not possible.
What about missing an isolated interior point? Suppose we have a rectangle covering whole square except a single point p. Can we represent that as a union of closed rectangles? Since rectangles are closed, any union of closed sets is closed. If we want to exclude a point p, the resulting set would not be closed (since p would be a limit point of the set? Actually if we take the square minus a point, that set is not closed; its closure includes p. But the union of closed rectangles is closed. So it's impossible to represent an open set like square minus a point as a finite union of closed rectangles. So the union will always be closed. Therefore, the only closed sets that have full measure in the square and are subsets of it is the whole square (since any proper closed subset would have nonempty interior complement? Not necessarily: Could there be a closed subset of the square with measure equal to the square but missing some points? For a closed set, if it has full measure, its complement is open and measure zero. The only open set with measure zero is the empty set. So complement empty. So indeed the set must be the whole square. So the union being closed and having area equal to the square forces it to be the whole square. So again, area equality suffices.
Thus we can skip edge checks.
One more nuance: The bounding box might be a square but the rectangles might not cover the entire bounding box even if total area equals bounding box area, if there is some overlap that cancels out? But no overlap (interior). Overlap on boundaries doesn't affect area.
Alright.
But wait, there is a catch: The bounding box computed from min x1 and max x2, etc., might not be the actual square if the rectangles are not axis-aligned? They are axis-aligned. So fine.
Thus solution is extremely simple.
But before finalizing, let's think about the constraints: n <= 5, coordinates up to 31400. No issues.
Now, what about floating point? All integers, use long long (since area up to (31400)^2 ≈ 985 million, fits in 32-bit signed? 31400^2 = 985,960,000 < 2^31-1 (2,147,483,647). So int (32-bit) is safe for area. But sum of up to 5 areas could be up to 5~1e9 = 5e9, which exceeds 2^31? Actually 5985,960,000 = 4,929,800,000 > 2,147,483,647. That's >2^31. So we need 64-bit (long long) to be safe. In C++, we can use long long. In Python, int is arbitrary precision, fine.
Thus we implement in Python easily.
Now, consider a scenario where the rectangles are placed such that the union is a square but there is a "hole" that is a line of zero area but the union is still closed? For a line to be a hole, the line would be missing, but the union would be missing that line. However, the union of closed rectangles cannot miss an entire line segment without missing an area around it? As argued, the complement would be open near that line? Let's formalize: Suppose L is a line segment (e.g., a vertical line x=c for y in (a,b)) that is not included in U, but all points around it are included. Is U closed? Consider a point p on L. Since p is not in U, we need to check if p is a limit point of U. Points arbitrarily close to p but off the line (e.g., with x = c ± ε) are in U (since they are covered). So p is a limit point of U. If U is closed, it must contain all its limit points. Thus p should be in U. Contradiction. Therefore U cannot be closed if it misses a line segment but contains points arbitrarily close. Therefore the union of closed rectangles (which is closed) cannot have such a hole. Good.
Hence area equality is enough.
Thus answer: compute and compare.
Now, I'll write the solution accordingly.
But before finalizing, check if there is any trick: Could the rectangles be arranged such that the bounding box is a square and the total area matches, but the union is not connected? However, if total area matches bounding box area, the union must be the whole bounding box, which is connected. So fine.
Thus solution.
However, I recall that there is a known problem "Do the rectangles form a square?" from some contest, and typical solution involves checking area and also that the four outer edges are fully covered. Maybe they included edge checks because they considered that area equality might not guarantee coverage of boundaries if the union is not closed? But we argued it's closed. Let's double-check: The union of closed rectangles is closed. Yes. But what if the rectangles are defined as open? The problem says "inside or on the border", so they are closed. So union is closed.
But wait: Could the union of closed rectangles be not closed? In general, finite union of closed sets is closed. So yes.
Thus we are fine.
Nonetheless, some subtlety: The bounding box itself is defined as [left,right]×[bottom,top]. But if left,right,bottom,top are taken from the rectangles, they are the extreme coordinates. The square we compare to is exactly that bounding box. The condition that the union is exactly this closed rectangle is what we need. As argued, area equality + closedness of union + containment implies equality.
But is it possible that the union is equal to the square but the sum of areas is less than square area? No, because sum of areas = union area (no overlap) = square area.
Now, consider a scenario with floating point or integer coordinates? All integers, so no rounding issues.
Thus solution is straightforward.
Now, I'll provide the final answer.
But I'll also discuss possible pitfalls: Overlap detection is not required because input guarantees no overlap. However, if the input violated that, our check might erroneously output YES? For example, two overlapping rectangles that together exactly cover a square but overlap in a region of area A. Then sum of areas = square area + A > square area. So our check would output NO because total_area != square area. So it's safe. If they overlap exactly such that the overlapping area is of zero measure (i.e., boundaries), sum area still equals square area? Overlap on a line or point doesn't add to area overlap because area of line is zero; sum areas = union area (since overlap area is zero). So overlapping boundaries are allowed (touching). That's fine.
Now, is it possible that the union is a square, but there is a rectangle that lies completely within another rectangle? That would be interior overlap, which is disallowed. So input won't contain that. So fine.
Thus final solution.
Let's write the code.
In Python:
def solve():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
it = iter(input_data)
n = int(next(it))
left = 109
right = -109
bottom = 109
top = -109
total_area = 0
rects = []
for _ in range(n):
x1 = int(next(it)); y1 = int(next(it)); x2 = int(next(it)); y2 = int(next(it))
rects.append((x1,y1,x2,y2))
left = min(left, x1)
right = max(right, x2)
bottom = min(bottom, y1)
top = max(top, y2)
total_area += (x2 - x1) * (y2 - y1)
side1 = right - left
side2 = top - bottom
if side1 != side2:
print("NO")
return
square_area = side1 * side2
if total_area != square_area:
print("NO")
return
print("YES")
That's it.
I'll include some comments.
Now, ensure that we treat the input correctly.
One more thought: The condition that rectangles do not overlap is given, but we don't verify it. However, our logic relies on sum of areas = union area. If there is interior overlap, union area < sum, so total_area > square_area? Possibly total_area could be > square_area if they overlap and also cover the square exactly. Then our check would fail (since total_area != square_area). So it's safe.
But what if overlapping rectangles cause total_area to equal square area despite overlap? That would require that the overlap area is exactly compensated by some uncovered area? But uncovered area would reduce union area, making union area < square area. For total_area to equal square area, we need union area + overlap area = square area. Since union area ≤ square area, overlap area = square area - union area ≥ 0. If there is overlap, union area < square area, so overlap area >0. So total_area = union area + overlap area could equal square area if union area = square area - overlap area, i.e., union area < square area. But then the union does not cover the entire square; there are uncovered regions. So total_area = square area, but union != square. Could this happen? Let's try to construct: square of area 4. Suppose we have two rectangles: R1 covering left half [0,2]×[0,2]? Actually left half would be width 1? Let's design: We want total_area = 4, but union area < 4, so overlap area = total_area - union area >0. So total_area = union area + overlap area = 4. If union area < 4, then overlap area = 4 - union area >0. For total_area to be 4, we need sum of rectangle areas = 4. For example, two rectangles each of area 4 (covering the whole square) would have total_area 8 >4. Not good. Need sum =4. Suppose we have R1: [0,2]×[0,1] area=2, R2: [0,2]×[0,2] area=4? That sums 6. Not 4.
We need two rectangles whose areas sum to 4, but they overlap significantly so union area is less. For sum areas to be 4, each must have area <=4. Let's say R1: [0,2]×[0,2] area=4, but then R2 must have area 0, not possible. So need at least two rectangles with areas that sum to 4, but if they overlap, each must be within the square. The maximum total area without overlap is 4. With overlap, total area can exceed 4. To have total area exactly 4, and overlap positive, we need union area = 4 - overlap <4. So we need rectangles that together cover less than the square but their areas sum to 4. That means they must be overlapping heavily such that the sum of their individual areas is 4, but the union area is smaller. For example, square 2x2. R1: [0,1.5]×[0,2] area=3; R2: [0.5,2]×[0,2] area=3? That sums 6 >4. To sum to 4, we could have R1: [0,1]×[0,2] area=2; R2: [0,2]×[0,1] area=2; they overlap in [0,1]×[0,1] area=1, so union area = 2+2-1=3. Total_area=4, union area=3 <4. So total_area=4, square area=4, but union does not equal square; it's missing the top-right quadrant? Actually union = L-shape covering bottom 2 rows and left 2 columns? Wait: R1: left half (x from 0 to 1) full height; R2: full width (0 to 2) bottom half (y from 0 to 1). Union is all points where either x<=1 or y<=1. The missing region is the top-right rectangle [1,2]×[1,2] area=1. So union area=3. So total_area = 4, square area = 4, side lengths? left=0,right=2,bottom=0,top=2, sides equal 2, so side condition passes. But total_area = square area, yet union is not the square. Would this be a valid input? Check overlap: R1 and R2 overlap in [0,1]×[0,1]. That's interior overlap (positive area). But the problem states that rectangles do not overlap (no interior points belong to more than one rectangle). This configuration violates that because the overlapping region [0,1]×[0,1] is interior to both (since both contain that whole area). So it's not allowed. So if the input respects non-overlap, such scenario cannot happen. So our reliance on non-overlap is essential. But the problem guarantees non-overlap. So we are safe.
Thus algorithm is correct under problem constraints.
Now, is there any scenario where non-overlapping rectangles have total_area = square area, bounding box square, but union not equal square? As argued, if non-overlapping, sum areas = union area. So if total_area = square area, union area = square area. Since union is closed and subset of square, as argued, union = square. So it's fine.
Thus solution accepted.
Now, I'll produce final answer with explanation.
But to be thorough, I'll also consider if there is any scenario where union is not closed due to some pathological definition? No.
Thus final solution.
I'll write the solution in Python and include it.
Now, produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
# extreme coordinates
INF = 10**9
left, right = INF, -INF
bottom, top = INF, -INF
total_area = 0
for _ in range(n):
x1 = int(next(it))
y1 = int(next(it))
x2 = int(next(it))
y2 = int(next(it))
left = min(left, x1)
right = max(right, x2)
bottom = min(bottom, y1)
top = max(top, y2)
total_area += (x2 - x1) * (y2 - y1)
side_x = right - left
side_y = top - bottom
square_area = side_x * side_y
if side_x != side_y or total_area != square_area:
print("NO")
else:
print("YES")
if name == "main":
solve()