Skip to content

Commit 460c5b0

Browse files
committed
Runtime: 69 ms (Top 97.04%) | Memory: 32.80 MB (Top 50.74%)
1 parent 2036cf9 commit 460c5b0

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
class Solution {
2-
public:
3-
bool isRectangleCover(vector<vector<int>>& rectangles) {
4-
SegmentTree<STMax> st(2e5 + 1);
5-
sort(rectangles.begin(), rectangles.end(), [](auto& lhs, auto& rhs) -> bool {
6-
if (lhs[1] != rhs[1]) return lhs[1] < rhs[1];
7-
return lhs[0] < rhs[0];
8-
});
1+
// Runtime: 69 ms (Top 97.04%) | Memory: 32.80 MB (Top 50.74%)
92

10-
int min_x = rectangles[0][0];
11-
int min_y = rectangles[0][1];
12-
int max_a = rectangles[0][2];
13-
int max_b = rectangles[0][3];
14-
long long sum = 1ll * (max_a - min_x) * (max_b - min_y);
15-
st.update(min_x + 1e5 + 1, max_a + 1e5, max_b);
16-
for (int i = 1; i < rectangles.size(); ++i) {
17-
if (rectangles[i][0] < min_x) return false;
18-
int max_height = st.query(rectangles[i][0] + 1e5 + 1, rectangles[i][2] + 1e5);
19-
if (max_height > rectangles[i][1]) return false;
20-
max_a = max(max_a, rectangles[i][2]);
21-
max_b = max(max_b, rectangles[i][3]);
22-
sum += 1ll * (rectangles[i][2] - rectangles[i][0]) * (rectangles[i][3] - rectangles[i][1]);
23-
st.update(rectangles[i][0] + 1e5 + 1, rectangles[i][2] + 1e5, rectangles[i][3]);
3+
class Solution {
4+
public:
5+
bool isRectangleCover(vector<vector<int>>& rectangles) {
6+
unordered_map<long long, int> mark;
7+
const long long N=1000000;
8+
for(vector<int>& x:rectangles){
9+
int x1=x[0];
10+
int y1=x[1];
11+
int x2=x[2];
12+
int y2=x[3];
13+
mark[x1*N+y1]++;
14+
mark[x1*N+y2]--;
15+
mark[x2*N+y1]--;
16+
mark[x2*N+y2]++;
17+
}
18+
int n_mark=0;
19+
for(auto ptr=mark.begin();ptr!=mark.end();ptr++)
20+
if(ptr->second!=0){
21+
if(abs(ptr->second)!=1) return false;
22+
n_mark++;
23+
}
24+
return n_mark==4;
2425
}
25-
return sum == 1ll * (max_a - min_x) * (max_b - min_y);
26-
}
2726
};

0 commit comments

Comments
 (0)