Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int optimize = []() {ios::sync_with_stdio(false); cin.tie(nullptr); return 0;} ();
- using ipair = pair<int,int>;
- namespace std{
- template<>
- struct hash<ipair> {
- size_t operator()(const ipair &p) const {
- return p.first*31 ^ (p.second*127+8989);
- }
- };
- }
- class Solution {
- public:
- bool isRectangleCover(vector<vector<int>>& rec) {
- int ymin = INT_MAX;
- int xmin = INT_MAX;
- int ymax = 0;
- int xmax = 0;
- int64_t sq = 0;
- unordered_map<ipair, int> points;
- for (int i = 0; i < rec.size(); ++i) {
- ymin = min(min(ymin, rec[i][0]), rec[i][2]);
- xmin = min(min(xmin, rec[i][1]), rec[i][3]);
- ymax = max(max(ymax, rec[i][0]), rec[i][2]);
- xmax = max(max(xmax, rec[i][1]), rec[i][3]);
- vector<int> py = {rec[i][0], rec[i][0], rec[i][2], rec[i][2]};
- vector<int> px = {rec[i][1], rec[i][3], rec[i][1], rec[i][3]};
- for (int j = 0; j < 4; ++j) {
- points[{py[j], px[j]}]++;
- }
- sq += (rec[i][2]-rec[i][0])*(rec[i][3]-rec[i][1]);
- }
- if (sq != (ymax-ymin)*(xmax-xmin)) return false;
- int angle_count = 0;
- for (auto x: points) {
- int count = x.second;
- ipair point = x.first;
- switch (count) {
- case 1:
- angle_count++;
- if ((point.first != ymin && point.first != ymax) || (point.second != xmin && point.second != xmax)) {
- return false;
- }
- break;
- case 2:
- break;
- case 4:
- if (point.first == ymin || point.first == ymax || point.second == ymin || point.second == ymax) {
- return false;
- }
- break;
- default:
- return false;
- }
- }
- return angle_count == 4;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement