-
Notifications
You must be signed in to change notification settings - Fork 114
/
850. Rectangle Area II.cpp
304 lines (265 loc) · 9.46 KB
/
850. 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//Approach #1: Principle of Inclusion-Exclusion
//TLE
//46 / 76 test cases passed.
//time: O(N*2^N), space: O(N)
class Solution {
public:
vector<int> intersect(vector<int>& rec1, vector<int>& rec2){
//the returned rectangle will have negative side if the two input rectangles have no intersection
return {max(rec1[0], rec2[0]),
max(rec1[1], rec2[1]),
min(rec1[2], rec2[2]),
min(rec1[3], rec2[3])};
};
long area(vector<int>& rec){
long dx = max(rec[2] - rec[0], 0);
long dy = max(rec[3] - rec[1], 0);
return dx*dy;
}
int rectangleArea(vector<vector<int>>& rectangles) {
int n = rectangles.size();
long ans = 0;
for(int subset = 1; subset < (1<<n); subset++){
//subset from 0001 to 1111
//maximum possible rectangle
vector<int> rec = {0, 0, (int)1e9, (int)1e9};
int parity = -1;
//inspect the subset, start from least significant bit
for(int bit = 0; bit < n; bit++){
if((subset>>bit) & 1){
rec = intersect(rec, rectangles[bit]);
parity *= -1;
}
}
/*
if there are odd set bits, parity is 1
otherwise parity is -1
*/
ans += parity * area(rec);
}
long mod = 1e9+7;
ans %= mod;
//?
if(ans < 0) ans += mod;
return ans;
}
};
//Approach #2: Coordinate Compression
//Runtime: 196 ms, faster than 6.10% of C++ online submissions for Rectangle Area II.
//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Rectangle Area II.
//time: O(N^3), space: O(N^2)
class Solution {
public:
int rectangleArea(vector<vector<int>>& rectangles) {
int n = rectangles.size();
set<int> xvals, yvals;
for(vector<int>& rec : rectangles){
xvals.insert(rec[0]);
yvals.insert(rec[1]);
xvals.insert(rec[2]);
yvals.insert(rec[3]);
}
vector<int> imapx(xvals.begin(), xvals.end());
vector<int> imapy(yvals.begin(), yvals.end());
sort(imapx.begin(), imapx.end());
sort(imapy.begin(), imapy.end());
map<int, int> mapx, mapy;
// cout << "imapx: " << endl;
for(int i = 0; i < imapx.size(); i++){
mapx[imapx[i]] = i;
// cout << i << " " << imapx[i] << endl;
}
// cout << "imapy: " << endl;
for(int i = 0; i < imapy.size(); i++){
mapy[imapy[i]] = i;
// cout << i << " " << imapy[i] << endl;
}
vector<vector<bool>> grid = vector(imapx.size(), vector(imapy.size(), false));
for(vector<int>& rec : rectangles){
//fill the corresponding grids
for(int x = mapx[rec[0]]; x < mapx[rec[2]]; x++){
for(int y = mapy[rec[1]]; y < mapy[rec[3]]; y++){
grid[x][y] = true;
}
}
}
long ans = 0;
for(int x = 0; x < grid.size(); x++){
for(int y = 0; y < grid[0].size(); y++){
if(grid[x][y]){
ans += (long)(imapx[x+1] - imapx[x]) * (imapy[y+1] - imapy[y]) % ((int)1e9+7);
ans %= ((int)1e9+7);
}
}
}
ans %= ((int)1e9+7);
return ans;
}
};
//Approach #3: Line Sweep
//Runtime: 16 ms, faster than 43.29% of C++ online submissions for Rectangle Area II.
//Memory Usage: 7.1 MB, less than 100.00% of C++ online submissions for Rectangle Area II.
//time: O(N^2* logN), space: O(N^2)
class Solution {
public:
int rectangleArea(vector<vector<int>>& rectangles) {
int OPEN = 0, CLOSE = 1;
int n = rectangles.size();
//every rectangle form two events
vector<vector<int>> events(n*2);
int i = 0;
for(vector<int>& rec : rectangles){
//horizontal line(y-coordinate), open or close, x1, x2
events[i++] = {rec[1], OPEN, rec[0], rec[2]};
events[i++] = {rec[3], CLOSE, rec[0], rec[2]};
}
//sort by y
sort(events.begin(), events.end(),
[](vector<int>& a, vector<int>& b){
return a[0] < b[0];
});
//actives: vector of (x1,x2), currently opening events
vector<vector<int>> actives;
int last_y = events[0][0];
long ans = 0;
for(vector<int>& event : events){
int y = event[0];
bool isClose = event[1];
int x1 = event[2], x2 = event[3];
/*
query: on current horizontal line,
the length of intervals formed by active events
*/
long query = 0;
int cur = INT_MIN;
// cout << "y: [" << last_y << ", " << y << "]" << endl;
for(vector<int>& active : actives){
cur = max(cur, active[0]);
// cout << "x: [" << active[0] << ", " << active[1] << "], cur: " << cur << ", width: " << max(active[1] - cur, 0) << endl;
/*
cur: last right boundary,
since rectangles may overlap,
so we need the max()
*/
query += max(active[1] - cur, 0);
cur = max(cur, active[1]);
}
// cout << "width: " << query << ", height: " << y - last_y << endl;
/*
query: the total length in horizontal direction
y - last_y: the height of current rectangle
*/
ans += query * (y - last_y);
//update actives(current open events)
if(!isClose){
actives.push_back({x1, x2});
sort(actives.begin(), actives.end(),
[](vector<int>& a, vector<int>& b){
return a[0] < b[0];
});
}else{
//find out the corresponding open active event and erase it
for(auto it = actives.begin(); it != actives.end(); it++){
if((*it)[0] == x1 && (*it)[1] == x2){
actives.erase(it);
break;
}
}
}
// cout << "actives: ";
// for(auto it = actives.begin(); it != actives.end(); it++){
// cout << (*it)[0] << ", " << (*it)[1] << " | ";
// }
// cout << endl;
last_y = y;
}
ans %= (int)1e9+7;
return ans;
}
};
//Approach #4: Segment Tree
//Runtime: 24 ms, faster than 26.22% of C++ online submissions for Rectangle Area II.
//Memory Usage: 20.8 MB, less than 25.00% of C++ online submissions for Rectangle Area II.
//time: O(N* logN), space: O(N)
class Node {
public:
int start, end;
vector<int> X;
Node *left, *right;
int count;
long total;
Node(int start, int end, vector<int> X){
this->start = start;
this->end = end;
this->X = X;
left = nullptr;
right = nullptr;
count = 0;
total = 0;
}
int getRangeMid(){
return start + (end-start)/2;
}
Node* getLeft(){
if(left == nullptr) left = new Node(start, getRangeMid(), X);
return left;
}
Node* getRight(){
if(right == nullptr) right = new Node(getRangeMid(), end, X);
return right;
}
long update(int i, int j, int val){
if(i >= j) return 0;
if(start == i && end == j){
count += val;
}else{
getLeft()->update(i, min(getRangeMid(), j), val);
getRight()->update(max(getRangeMid(), i), j, val);
}
if(count > 0) total = X[end] - X[start];
else total = getLeft()->total + getRight()->total;
return total;
}
};
class Solution {
public:
int rectangleArea(vector<vector<int>>& rectangles) {
int OPEN = 1, CLOSE = -1;
int n = rectangles.size();
vector<vector<int>> events(n*2);
set<int> xvals_set;
int i = 0;
for(vector<int>& rec : rectangles){
//horizontal line, open or close, x1, x2
events[i++] = {rec[1], OPEN, rec[0], rec[2]};
events[i++] = {rec[3], CLOSE, rec[0], rec[2]};
xvals_set.insert(rec[0]);
xvals_set.insert(rec[2]);
}
//sort by y
sort(events.begin(), events.end(),
[](vector<int>& a, vector<int>& b){
return a[0] < b[0];
});
vector<int> xvals(xvals_set.begin(), xvals_set.end());
sort(xvals.begin(), xvals.end());
map<int, int> rxvals;
for(int i = 0; i < xvals.size(); i++){
rxvals[xvals[i]] = i;
}
int last_y = events[0][0];
long ans = 0;
long cur_x_sum = 0;
Node active(0, xvals.size()-1, xvals);
for(vector<int>& event : events){
int y = event[0];
int type = event[1];
int x1 = event[2], x2 = event[3];
ans += cur_x_sum * (y - last_y);
cur_x_sum = active.update(rxvals[x1], rxvals[x2], type);
last_y = y;
}
ans %= (int)1e9+7;
return ans;
}
};