You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to theOx 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.
The first line contains a single integer n (1 ≤ n ≤ 5). Nextn lines contain four integers each, describing a single rectangle:x1, y1, x2,y2 (0 ≤ x1 < x2 ≤ 31400, 0 ≤ y1 < y2 ≤ 31400) — x1 and x2 are x-coordinates of the left and right edges of the rectangle, andy1 and y2 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).
In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.
5 0 0 2 3 0 3 3 5 2 0 5 2 3 2 5 5 2 2 3 3
YES
4 0 0 2 3 0 3 3 5 2 0 5 2 3 2 5 5
NO
因为不存在重复区域,因此最小的能完全包含所有小矩形的矩形区域,若面积刚好等于所有小矩形的面积和,说明小矩形构成一个矩形。
离散出所有的边,找到最左最右和最上最下的边,以及它们在自己的维度上的起始位置和终止位置。最上和最下的边,它们的起始和终止应该分别相等。最右和最左相似。
但是Square是正方形,不是矩形,被坑了。-_-b
#include <cstdio>
typedef long long ll;
struct node
{
ll f;
ll t;
};
node edge[31410][10];
node edge2[31410][10];
int cedge[31410];
int cedge2[31410];
const ll inf = 0x3f3f3f3f3f3f3f3fll;
ll miny1 = inf;
ll miny2 = inf;
ll minx1 = inf;
ll minx2 = inf;
ll maxy1 = -inf;
ll maxy2 = -inf;
ll maxx1 = -inf;
ll maxx2 = -inf;
ll minx = inf;
ll maxx = -inf;
ll miny = inf;
ll maxy = -inf;
ll min(ll a,ll b)
{
if (a < b)
return a;
return b;
}
ll max(ll a,ll b)
{
if (a > b)
return a;
return b;
}
ll max(ll a,ll b,ll c)
{
if (a > b && a > c)
return a;
if (b > c)
return b;
return c;
}
ll min(ll a,ll b,ll c)
{
if (a < b && a < c)
return a;
if (b < c)
return b;
return c;
}
int main()
{
//freopen("sar.in","r",stdin);
//freopen("sar.out","w",stdout);
int n;
scanf("%d",&n);
ll S = 0;
for (int i=1;i<=n;i++)
{
int xx1,yy1,xx2,yy2;
scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
ll x1,y1,x2,y2;
x1 = xx1;
y1 = yy1;
x2 = xx2;
y2 = yy2;
cedge[x1] ++;
edge[x1][cedge[x1]].f = y1;
edge[x1][cedge[x1]].t = y2;
cedge[x2] ++;
edge[x2][cedge[x2]].f = y1;
edge[x2][cedge[x2]].t = y2;
cedge2[y1] ++;
edge2[y1][cedge2[y1]].f = x1;
edge2[y1][cedge2[y1]].t = x2;
cedge2[y2] ++;
edge2[y2][cedge2[y2]].f = x1;
edge2[y2][cedge2[y2]].t = x2;
maxx = max(maxx,x1,x2);
minx = min(minx,x1,x2);
maxy = max(maxy,y1,y2);
miny = min(miny,y1,y2);
S += (y2-y1)*(x2-x1);
}
for (int i=1;i<=cedge[minx];i++)
{
miny1 = min(miny1,edge[minx][i].f);
maxy1 = max(maxy1,edge[minx][i].t);
}
for (int i=1;i<=cedge[maxx];i++)
{
miny2 = min(miny2,edge[maxx][i].f);
maxy2 = max(maxy2,edge[maxx][i].t);
}
for (int i=1;i<=cedge2[miny];i++)
{
minx1 = min(minx1,edge2[miny][i].f);
maxx1 = max(maxx1,edge2[miny][i].t);
}
for (int i=1;i<=cedge2[maxy];i++)
{
minx2 = min(minx2,edge2[maxy][i].f);
maxx2 = max(maxx2,edge2[maxy][i].t);
}
minx = min(minx,minx1,minx2);
maxx = max(maxx,maxx1,maxx2);
miny = min(miny,miny1,miny2);
maxy = max(maxy,maxy1,maxy2);
if (miny1 != miny2 || maxy1 != maxy2 ||
minx1 != minx2 || maxy1 != maxy2 ||
(maxy-miny)!=(maxx-minx) ||
(maxy-miny)*(maxx-minx) != S)
printf("NO\n");
else
printf("YES\n");
return 0;
}