-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrectangle_union.cpp
135 lines (116 loc) · 2.89 KB
/
rectangle_union.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
// rectangle_union.cpp
// Eric K. Zhang; Jul. 6, 2018
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct rect {
int x1, y1, x2, y2;
};
class footprint_segtree {
const int N;
const vector<int>& weights;
vector<int> mi, cnt, lazy;
int total;
void init(int lo, int hi, int node) {
if (lo == hi) {
cnt[node] = weights[lo];
total += cnt[node];
return;
}
int mid = (lo + hi) / 2;
init(lo, mid, 2 * node + 1);
init(mid + 1, hi, 2 * node + 2);
cnt[node] = cnt[2 * node + 1] + cnt[2 * node + 2];
}
void push(int lo, int hi, int node) {
if (lazy[node]) {
mi[node] += lazy[node];
if (lo != hi) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
}
void update_range(int s, int e, int x, int lo, int hi, int node) {
push(lo, hi, node);
if (lo > e || hi < s)
return;
if (s <= lo && hi <= e) {
lazy[node] = x;
push(lo, hi, node);
return;
}
int mid = (lo + hi) / 2;
update_range(s, e, x, lo, mid, 2 * node + 1);
update_range(s, e, x, mid + 1, hi, 2 * node + 2);
mi[node] = min(mi[2 * node + 1], mi[2 * node + 2]);
cnt[node] = 0;
if (mi[2 * node + 1] == mi[node])
cnt[node] += cnt[2 * node + 1];
if (mi[2 * node + 2] == mi[node])
cnt[node] += cnt[2 * node + 2];
}
public:
footprint_segtree(const vector<int>& weights)
: N(weights.size()), weights(weights) {
mi.resize(4 * N);
cnt.resize(4 * N);
lazy.resize(4 * N);
total = 0;
init(0, N - 1, 0);
}
void update_range(int s, int e, int x) {
update_range(s, e, x, 0, N - 1, 0);
}
int query() {
return total - (mi[0] ? 0 : cnt[0]);
}
};
LL rectangle_union(const vector<rect>& rects) {
// Coordinate Compression
vector<int> ys;
for (const rect& r : rects) {
ys.push_back(r.y1);
ys.push_back(r.y2);
}
sort(ys.begin(), ys.end());
ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
vector<int> lengths(ys.size() - 1);
for (int i = 0; i + 1 < ys.size(); i++)
lengths[i] = ys[i + 1] - ys[i];
footprint_segtree st(lengths);
// Sweepline Preparation
vector<pair<int, pair<int, int> > > events;
for (int i = 0; i < rects.size(); i++) {
const rect& r = rects[i];
events.push_back({ r.x1, { i, 1 } });
events.push_back({ r.x2, { i, -1 } });
}
sort(events.begin(), events.end());
// Sweepline
int pre = INT_MIN;
LL ret = 0;
for (auto& e : events) {
ret += (LL) st.query() * (e.first - pre);
pre = e.first;
const rect& r = rects[e.second.first];
int change = e.second.second;
int y1 = lower_bound(ys.begin(), ys.end(), r.y1) - ys.begin();
int y2 = lower_bound(ys.begin(), ys.end(), r.y2) - ys.begin();
st.update_range(y1, y2 - 1, change);
}
return ret;
}
int main() {
int N;
cin >> N;
vector<rect> rects;
for (int i = 0; i < N; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
rects.push_back(rect { x1, y1, x2, y2 });
}
cout << rectangle_union(rects) << endl;
return 0;
}