-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathunion_of_rectangles.cpp
More file actions
347 lines (322 loc) · 11.1 KB
/
Copy pathunion_of_rectangles.cpp
File metadata and controls
347 lines (322 loc) · 11.1 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// 長方形の和集合
//
// verified:
// Yosupo Library Checker - Area of Union of Rectangles
// https://judge.yosupo.jp/problem/area_of_union_of_rectangles
//
// AtCoder ABC 449 F - Grid Clipping
// https://atcoder.jp/contests/abc449/tasks/abc449_f
//
#include <bits/stdc++.h>
using namespace std;
// Lazy Segment Tree
template<class Monoid, class Action> struct LazySegmentTree {
// various function types
using FuncMonoid = function<Monoid(Monoid, Monoid)>;
using FuncAction = function<Monoid(Action, Monoid)>;
using FuncComposition = function<Action(Action, Action)>;
// core member
int N;
FuncMonoid OP;
FuncAction ACT;
FuncComposition COMP;
Monoid IDENTITY_MONOID;
Action IDENTITY_ACTION;
// inner data
int log, offset;
vector<Monoid> dat;
vector<Action> lazy;
// constructor
LazySegmentTree() {}
LazySegmentTree(const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action)
: OP(op), ACT(act), COMP(comp),
IDENTITY_MONOID(identity_monoid), IDENTITY_ACTION(identity_action) {}
LazySegmentTree(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action) {
init(n, op, act, comp, identity_monoid, identity_action);
}
LazySegmentTree(const vector<Monoid> &v,
const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action) {
init(v, op, act, comp, identity_monoid, identity_action);
}
void init(const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action) {
OP = op, ACT = act, COMP = comp;
IDENTITY_MONOID = identity_monoid, IDENTITY_ACTION = identity_action;
}
void init(int n) {
N = n,
log = 0, offset = 1;
while (offset < N) ++log, offset <<= 1;
dat.assign(offset * 2, IDENTITY_MONOID);
lazy.assign(offset * 2, IDENTITY_ACTION);
}
void init(const vector<Monoid> &v) {
init((int)v.size());
build(v);
}
void init(int n, const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action) {
init(op, act, comp, identity_monoid, identity_action);
init(n);
}
void init(const vector<Monoid> &v,
const FuncMonoid op, const FuncAction act, const FuncComposition comp,
const Monoid &identity_monoid, const Action &identity_action) {
init((int)v.size(), op, act, comp, identity_monoid, identity_action);
build(v);
}
void build(const vector<Monoid> &v) {
assert(N == (int)v.size());
for (int i = 0; i < N; ++i) dat[i + offset] = v[i];
for (int k = offset - 1; k > 0; --k) pull_dat(k);
}
int size() const {
return N;
}
// basic functions for lazy segment tree
void pull_dat(int k) {
dat[k] = OP(dat[k * 2], dat[k * 2 + 1]);
}
void apply_lazy(int k, const Action &f) {
dat[k] = ACT(f, dat[k]);
if (k < offset) lazy[k] = COMP(f, lazy[k]);
}
void push_lazy(int k) {
apply_lazy(k * 2, lazy[k]);
apply_lazy(k * 2 + 1, lazy[k]);
lazy[k] = IDENTITY_ACTION;
}
void pull_dat_deep(int k) {
for (int h = 1; h <= log; ++h) pull_dat(k >> h);
}
void push_lazy_deep(int k) {
for (int h = log; h >= 1; --h) push_lazy(k >> h);
}
// setter and getter, update A[i], i is 0-indexed, O(log N)
void set(int i, const Monoid &v) {
assert(0 <= i && i < N);
int k = i + offset;
push_lazy_deep(k);
dat[k] = v;
pull_dat_deep(k);
}
Monoid get(int i) {
assert(0 <= i && i < N);
int k = i + offset;
push_lazy_deep(k);
return dat[k];
}
Monoid operator [] (int i) {
return get(i);
}
// apply f for index i
void apply(int i, const Action &f) {
assert(0 <= i && i < N);
int k = i + offset;
push_lazy_deep(k);
dat[k] = ACT(f, dat[k]);
pull_dat_deep(k);
}
// apply f for interval [l, r)
void apply(int l, int r, const Action &f) {
assert(0 <= l && l <= r && r <= N);
if (l == r) return;
l += offset, r += offset;
for (int h = log; h >= 1; --h) {
if (((l >> h) << h) != l) push_lazy(l >> h);
if (((r >> h) << h) != r) push_lazy((r - 1) >> h);
}
int original_l = l, original_r = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) apply_lazy(l++, f);
if (r & 1) apply_lazy(--r, f);
}
l = original_l, r = original_r;
for (int h = 1; h <= log; ++h) {
if (((l >> h) << h) != l) pull_dat(l >> h);
if (((r >> h) << h) != r) pull_dat((r - 1) >> h);
}
}
// get prod of interval [l, r)
Monoid prod(int l, int r) {
assert(0 <= l && l <= r && r <= N);
if (l == r) return IDENTITY_MONOID;
l += offset, r += offset;
for (int h = log; h >= 1; --h) {
if (((l >> h) << h) != l) push_lazy(l >> h);
if (((r >> h) << h) != r) push_lazy(r >> h);
}
Monoid val_left = IDENTITY_MONOID, val_right = IDENTITY_MONOID;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) val_left = OP(val_left, dat[l++]);
if (r & 1) val_right = OP(dat[--r], val_right);
}
return OP(val_left, val_right);
}
Monoid all_prod() {
return dat[1];
}
// get max r that f(get(l, r)) = True (0-indexed), O(log N)
// f(IDENTITY) need to be True
int max_right(const function<bool(Monoid)> f, int l = 0) {
if (l == N) return N;
l += offset;
push_lazy_deep(l);
Monoid sum = IDENTITY_MONOID;
do {
while (l % 2 == 0) l >>= 1;
if (!f(OP(sum, dat[l]))) {
while (l < offset) {
push_lazy(l);
l = l * 2;
if (f(OP(sum, dat[l]))) {
sum = OP(sum, dat[l]);
++l;
}
}
return l - offset;
}
sum = OP(sum, dat[l]);
++l;
} while ((l & -l) != l); // stop if l = 2^e
return N;
}
// get min l that f(get(l, r)) = True (0-indexed), O(log N)
// f(IDENTITY) need to be True
int min_left(const function<bool(Monoid)> f, int r = -1) {
if (r == 0) return 0;
if (r == -1) r = N;
r += offset;
push_lazy_deep(r - 1);
Monoid sum = IDENTITY_MONOID;
do {
--r;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(OP(dat[r], sum))) {
while (r < offset) {
push_lazy(r);
r = r * 2 + 1;
if (f(OP(dat[r], sum))) {
sum = OP(dat[r], sum);
--r;
}
}
return r + 1 - offset;
}
sum = OP(dat[r], sum);
} while ((r & -r) != r);
return 0;
}
// debug stream
friend ostream& operator << (ostream &s, LazySegmentTree seg) {
for (int i = 0; i < (int)seg.size(); ++i) {
s << seg[i];
if (i != (int)seg.size() - 1) s << " ";
}
return s;
}
// dump
void dump() {
for (int i = 0; i <= log; ++i) {
for (int j = (1 << i); j < (1 << (i + 1)); ++j) {
cout << "{" << dat[j] << "," << lazy[j] << "} ";
}
cout << endl;
}
}
};
// Area of Union of Rectangles
template<class VAL> struct RectangleUnion {
using FVAL = array<VAL, 4>;
vector<FVAL> rects;
vector<VAL> ys;
// constructor
RectangleUnion() {}
RectangleUnion(const vector<FVAL> &r) {
for (auto [lx, rx, ly, ry] : r) add_rect(lx, rx, ly, ry);
}
void add_rect(VAL lx, VAL rx, VAL ly, VAL ry) {
assert(lx <= rx && ly <= ry);
rects.emplace_back(FVAL{lx, rx, ly, ry});
ys.emplace_back(ly), ys.emplace_back(ry);
}
// calc
VAL solve() {
if (rects.empty()) return VAL(0);
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int Y = (int)ys.size();
vector<FVAL> events;
const VAL IN = 1, OUT = -1, INF = numeric_limits<VAL>::max() / 2;
for (auto [lx, rx, ly, ry] : rects) {
int lyi = lower_bound(ys.begin(), ys.end(), ly) - ys.begin();
int ryi = lower_bound(ys.begin(), ys.end(), ry) - ys.begin();
events.emplace_back(FVAL{lx, lyi, ryi, IN});
events.emplace_back(FVAL{rx, lyi, ryi, OUT});
}
sort(events.begin(), events.end());
using Monoid = pair<VAL, VAL>; // (min, num)
auto op = [&](Monoid a, Monoid b) -> Monoid {
if (a.first == b.first) return make_pair(a.first, a.second + b.second);
else return min(a, b);
};
auto mapping = [&](VAL f, Monoid a) -> Monoid {
return make_pair(a.first + f, a.second);
};
auto composition = [&](VAL g, VAL f) -> VAL {
return g + f;
};
VAL res = 0, width = ys.back() - ys[0];
vector<Monoid> ini(Y - 1);
for (int i = 0; i < Y - 1; i++) ini[i] = Monoid(0, ys[i + 1] - ys[i]);
LazySegmentTree<Monoid, VAL> seg(ini, op, mapping, composition, Monoid(INF, 0), 0);
for (int i = 0; i + 1 < events.size(); i++) {
auto [x, ly, ry, inout] = events[i];
seg.apply(ly, ry, inout);
auto [mi, num] = seg.all_prod();
res += (width - (mi == 0 ? num : 0)) * (events[i + 1][0] - x);
}
return res;
}
};
//------------------------------//
// Examples
//------------------------------//
// Yosupo Library Checker - Area of Union of Rectangles
void Yosupo_Area_of_Union_of_Rectangles() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
RectangleUnion<long long> ru;
for (int i = 0; i < N; i++) {
long long l, d, r, u;
cin >> l >> d >> r >> u;
ru.add_rect(l, r, d, u);
}
long long res = ru.solve();
cout << res << endl;
}
// AtCoder ABC 449 F - Grid Clipping
void ABC_449_F() {
long long H, W, h, w, N;
cin >> H >> W >> h >> w >> N;
RectangleUnion<long long> ru;
for (int i = 0; i < N; i++) {
long long R, C;
cin >> R >> C;
R--, C--;
ru.add_rect(max(0LL, R-h+1), min(H-h+1, R+1), max(0LL, C-w+1), min(W-w+1, C+1));
}
long long black = ru.solve();
long long res = (H - h + 1) * (W - w + 1) - black;
cout << res << endl;
}
int main() {
//Yosupo_Area_of_Union_of_Rectangles();
ABC_449_F();
}