-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectangle Area II.cpp
41 lines (40 loc) · 1.3 KB
/
Rectangle Area II.cpp
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
class Solution {
public:
int rectangleArea(vector<vector<int>>& rectangles) {
int mod = 1000000000 + 7;
vector<int> x;
for (auto r : rectangles) {
x.push_back(r[0]);
x.push_back(r[2]);
}
sort(x.begin(), x.end());
vector<int>::iterator end_unique = unique(x.begin(), x.end());
x.erase(end_unique, x.end());
unordered_map<int, int> x_i;
for (int i = 0; i < x.size(); ++i) {
x_i[x[i]] = i;
}
vector<int> count(x.size(), 0);
vector<vector<int>> L;
for (auto r : rectangles) {
int x1 = r[0], y1 = r[1], x2 = r[2], y2 = r[3];
L.push_back({y1, x1, x2, 1});
L.push_back({y2, x1, x2, -1});
}
sort(L.begin(), L.end());
long long cur_y = 0, cur_x_sum = 0, area = 0;
for (auto l : L) {
long long y = l[0], x1 = l[1], x2 = l[2], sig = l[3];
area = (area + (y - cur_y) * cur_x_sum) % mod;
cur_y = y;
for (int i = x_i[x1]; i < x_i[x2]; i++)
count[i] += sig;
cur_x_sum = 0;
for (int i = 0; i < x.size(); ++i) {
if (count[i] > 0)
cur_x_sum += x[i + 1] - x[i];
}
}
return area;
}
};