Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For an axis-parallel rectangle, every side is either perfectly horizontal or perfectly vertical. More importantly: how many different -coordinates and -coordinates can appear among all endpoints?
If the four segments really are the four sides of a rectangle, then the endpoint -values must be exactly and , and the endpoint -values must be exactly and . So both distinct-counts must be .
Once you know and , the rectangle is completely determined. There are only four possible side segments.
Normalize each segment so its endpoints are stored in a consistent order. Then compare the multiset of given segments with the multiset of the four expected rectangle sides.
Build the four required sides:
If and only if the normalized input segments match this multiset exactly, answer YES. Otherwise it is NO.
If four segments form a rectangle with sides parallel to the axes, then all rectangle corners are determined by just two -values and two -values:
So among all endpoints, there can be only:
If either count is not , then it is impossible. End of story.
Also, having two distinct 's and two distinct 's already guarantees positive width and height, so the area is positive.
Once are known, the rectangle is uniquely determined. The only valid sides are:
So the whole problem becomes:
Do the given four segments match these four segments exactly, ignoring input order and endpoint order?
That is all. No fancy geometry. No cross products. No sacrificing goats to the implementation gods.
A segment may be given in either direction, so first normalize it:
Now the same undirected segment always has the same representation.
Then:
NOIf they are identical, print YES, otherwise NO.
Degenerate input segments are automatically rejected, because they simply will not match any of the four positive-length sides.
YESThen the normalized input multiset is exactly equal to the multiset of the four segments listed above. Those four segments are precisely the sides of the axis-parallel rectangle with corners:
Since and , this rectangle has positive area. So the given segments do form the required rectangle.
Then all endpoints must lie on the rectangle corners, so there are exactly two distinct -values and exactly two distinct -values.
Moreover, the four given segments must be exactly the four sides of that rectangle. After normalizing endpoint order, the multiset of input segments is identical to the multiset of expected sides. Therefore the algorithm prints YES.
So the condition is both necessary and sufficient. Nice, tight, no bullshit.
There are only segments, so the running time is effectively constant:
If you want to sound extra ceremonial, you can say the set operations are , which is the same thing wearing a fake mustache.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
array<ll, 4> normSeg(ll x1, ll y1, ll x2, ll y2) {
if (make_pair(x1, y1) > make_pair(x2, y2)) {
swap(x1, x2);
swap(y1, y2);
}
return {x1, y1, x2, y2};
}
int main() {
setIO();
multiset<array<ll, 4>> got, need;
set<ll> xs, ys;
for (int i = 0; i < 4; ++i) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
got.insert(normSeg(x1, y1, x2, y2));
xs.insert(x1);
xs.insert(x2);
ys.insert(y1);
ys.insert(y2);
}
if (xs.size() != 2 || ys.size() != 2) {
cout << "NO\n";
return 0;
}
vector<ll> vx(xs.begin(), xs.end());
vector<ll> vy(ys.begin(), ys.end());
ll xL = vx[0], xR = vx[1];
ll yB = vy[0], yT = vy[1];
need.insert(normSeg(xL, yB, xR, yB));
need.insert(normSeg(xL, yT, xR, yT));
need.insert(normSeg(xL, yB, xL, yT));
need.insert(normSeg(xR, yB, xR, yT));
cout << (got == need ? "YES\n" : "NO\n");
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
array<ll, 4> normSeg(ll x1, ll y1, ll x2, ll y2) {
if (make_pair(x1, y1) > make_pair(x2, y2)) {
swap(x1, x2);
swap(y1, y2);
}
return {x1, y1, x2, y2};
}
int main() {
setIO();
multiset<array<ll, 4>> got, need;
set<ll> xs, ys;
for (int i = 0; i < 4; ++i) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
got.insert(normSeg(x1, y1, x2, y2));
xs.insert(x1);
xs.insert(x2);
ys.insert(y1);
ys.insert(y2);
}
if (xs.size() != 2 || ys.size() != 2) {
cout << "NO\n";
return 0;
}
vector<ll> vx(xs.begin(), xs.end());
vector<ll> vy(ys.begin(), ys.end());
ll xL = vx[0], xR = vx[1];
ll yB = vy[0], yT = vy[1];
need.insert(normSeg(xL, yB, xR, yB));
need.insert(normSeg(xL, yT, xR, yT));
need.insert(normSeg(xL, yB, xL, yT));
need.insert(normSeg(xR, yB, xR, yT));
cout << (got == need ? "YES\n" : "NO\n");
}