-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangleArea.cpp
More file actions
41 lines (41 loc) · 1.23 KB
/
rectangleArea.cpp
File metadata and controls
41 lines (41 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
const int mod = static_cast<int>(1e9) + 7;
int rectangleArea(vector<vector<int>>& rectangles) {
vector<int> hor;
for (auto &v: rectangles) {
hor.push_back(v[0]);
hor.push_back(v[2]);
}
sort(hor.begin(), hor.end());
long long ans = 0;
for (int i = 1; i < hor.size(); ++i) {
int a = hor[i - 1], b = hor[i], len = b - a;
if (!len) {
continue;
}
vector<pair<int, int>> lines;
for (auto &v: rectangles) {
if (v[0] <= a && b <= v[2]) {
lines.push_back({v[1], v[3]});
}
}
sort(lines.begin(), lines.end());
long long total = 0, l = -1, r = -1;
for (auto &cur: lines) {
if (cur.first > r) {
total += r - l;
l = cur.first;
r = cur.second;
}
else if (cur.second > r) {
r = cur.second;
}
}
total += r - l;
ans += (total * len) % mod;
ans %= mod;
}
return static_cast<int>(ans);
}
};