CF243D Cubes 题解
首先由于都是平视,因此我们将这个东西分层考虑,每层分别考虑能看到几个。
由于最多有
将这个东西转一下,使得光线永远是从右上方照射过来,并且
考察什么时候会照到一个光线。实际上我们只关心垂直光线方向的投影。因此我们考虑将一个方块转化成经过右上方顶点的一条直线。
因此,我们就把这个问题转化成了一个线段覆盖问题,每次询问能看到几种颜色不同的线段。
不妨考虑没有修改的时候这么做。区间覆盖倒着做,考虑从优先级高到低加入,每次查询区间是否有空的,如果有就能看到,否则不能看到。
但是,带修改的情况就会破坏这个从小到大加入的情况,你发现直接考虑并不是很好维护。
我们换个角度,每次加入的区间持续范围是一个后缀。我们线段树维护的是
实际上,我们要求的就是这样一个问题,每次在
考虑从高到底加入矩形,每次查询
一个方块的优先级是经过右上角定点的,斜率和光线法向量一样的直线的
[!info]- 参考代码
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <vector>
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
namespace data_structure {
template <class info_t, class tag_t, class merge_info_op = std::plus<info_t>,
class apply_op = std::multiplies<void>,
class merge_tag_op = std::plus<tag_t>>
class lazy_segtree {
public:
const merge_info_op merge_info;
const apply_op apply_to_info;
const merge_tag_op merge_tag;
const info_t id_info;
const tag_t id_tag;
const size_t npos = static_cast<size_t>(-1);
struct node {
info_t info;
tag_t tag;
size_t l, r;
size_t mid() const { return (l + r) / 2; }
};
std::vector<node> t;
private:
size_t lc(const size_t &p) const { return 2 * p; }
size_t rc(const size_t &p) const { return 2 * p + 1; }
void update(size_t p) {
t[p].info = merge_info(t[lc(p)].info, t[rc(p)].info);
return;
}
void apply_to_node(size_t p, const tag_t &tag) {
t[p].tag = merge_tag(tag, t[p].tag);
t[p].info = apply_to_info(tag, t[p].info);
}
void spread(size_t p) {
apply_to_node(lc(p), t[p].tag);
apply_to_node(rc(p), t[p].tag);
t[p].tag = id_tag;
return;
}
void build(size_t l, size_t r, const std::vector<info_t> &init_val,
size_t p = 1) {
t[p].l = l;
t[p].r = r;
t[p].info = id_info;
t[p].tag = id_tag;
if (l + 1 == r) {
t[p].info = init_val[l];
return;
}
size_t mid = t[p].mid();
build(l, mid, init_val, lc(p));
build(mid, r, init_val, rc(p));
update(p);
return;
}
public:
lazy_segtree()
: merge_info(), apply_to_info(), merge_tag(), id_info(), id_tag(), t() {
}
lazy_segtree(size_t n, const merge_info_op &_merge_info = merge_info_op(),
const apply_op &_apply = apply_op(),
const merge_tag_op &_merge_tag = merge_tag_op(),
const info_t &_info_id = info_t(),
const tag_t &_tag_id = tag_t())
: merge_info(_merge_info), apply_to_info(_apply), merge_tag(_merge_tag),
id_info(_info_id), id_tag(_tag_id), t(n * 4) {
build(0, n, std::vector<info_t>(n, id_info));
}
template <class iter>
void assign(iter iter_begin, iter iter_end) {
size_t n = std::distance(iter_begin, iter_end);
build(0, n, std::vector<info_t>(iter_begin, iter_end));
}
template <class iter>
lazy_segtree(iter iter_begin, iter iter_end,
const merge_info_op &_merge_info = merge_info_op(),
const apply_op &_apply = apply_op(),
const merge_tag_op &_merge_tag = merge_tag_op(),
const info_t &_info_id = info_t(),
const tag_t &_tag_id = tag_t())
: merge_info(_merge_info), apply_to_info(_apply), merge_tag(_merge_tag),
id_info(_info_id), id_tag(_tag_id),
t(std::distance(iter_begin, iter_end) * 4) {
assign(iter_begin, iter_end);
}
void apply(size_t l, size_t r, const tag_t &tag, size_t p = 1) {
if (l <= t[p].l && t[p].r <= r) {
apply_to_node(p, tag);
return;
}
size_t mid = t[p].mid();
spread(p);
if (l < mid) {
apply(l, r, tag, lc(p));
}
if (mid < r) {
apply(l, r, tag, rc(p));
}
update(p);
return;
}
info_t query(size_t l, size_t r, size_t p = 1) {
if (l <= t[p].l && t[p].r <= r) {
return t[p].info;
}
size_t mid = t[p].mid();
spread(p);
info_t ret = id_info;
if (l < mid) {
ret = merge_info(query(l, r, lc(p)), ret);
}
if (mid < r) {
ret = merge_info(ret, query(l, r, rc(p)));
}
update(p);
return ret;
}
void set(size_t pos, const info_t &info, size_t p = 1) {
if (t[p].l + 1 == t[p].r) {
t[p].info = info;
return;
}
size_t mid = t[p].mid();
spread(p);
if (pos < mid) {
set(pos, info, lc(p));
}
else {
set(pos, info, rc(p));
}
update(p);
return;
}
};
} // namespace data_structure
namespace numbers {
template <typename T>
T inf() {
assert(false);
}
template <>
int inf<int>() {
return 0x3f3f3f3f;
}
template <>
uint inf<uint>() {
return 0x3f3f3f3f;
}
template <>
ll inf<ll>() {
return 0x3f3f3f3f3f3f3f3f;
}
template <>
ull inf<ull>() {
return 0x3f3f3f3f3f3f3f3f;
}
} // namespace numbers
namespace solve {
struct info_t {
uint val;
info_t(uint _val = 0) : val(_val) {}
friend info_t operator+(const info_t &lhs, const info_t &rhs) {
return std::max(lhs.val, rhs.val);
}
};
struct tag_t {
uint val;
tag_t(uint _val = numbers::inf<uint>()) : val(_val) {}
friend tag_t operator+(const tag_t &lhs, const tag_t &rhs) {
return std::min(lhs.val, rhs.val);
}
friend info_t operator*(const tag_t &lhs, const info_t &rhs) {
return std::min(lhs.val, rhs.val);
}
};
/**
* 请注意数组的第一维其实是 y 坐标。
*/
std::vector<std::vector<uint>> height;
uint n;
int x, y;
void process_map() {
x = -x;
y = -y;
height.assign(n, std::vector<uint>(n));
{
bool rhor = false, rvert = false;
if (x < 0) {
x = -x;
rvert = true;
}
if (y < 0) {
y = -y;
rhor = true;
}
bool flip = false;
if (y == 0) {
std::swap(x, y);
flip = true; // 需要保证 y 不是 0 map to 才能正确。
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
uint ri = rvert ? n - i - 1 : i;
uint rj = rhor ? n - j - 1 : j;
if (flip) {
std::swap(ri, rj);
}
std::cin >> height[rj][ri];
}
}
}
{
ull g = std::gcd(x, y);
x /= g, y /= g;
}
x = -x;
y = -y;
}
struct cube {
int l, r;
int h; // 优先级。
};
std::vector<int> pos;
std::vector<std::vector<cube>> cb;
int map_to(int a, int b) { return a * -y + b * x; }
int priority(int a, int b) {
// 请注意此时保证 y 非 0,交换之后就是 x 非零了。
int cx = y, cy = -x;
return b * -cx + a * cy;
}
void process_cube() {
cb.assign(n, std::vector<cube>(n));
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
/**
* 这里的交点是,
*
* (i, j + 1)
*
* 和
*
* (i + 1, j)
*
* 考虑 formalize,映射到 x 轴上,y 坐标为 0。
*
* 需要同时除掉 y。比较难办啊。那你把横坐标乘上 y 即可。
*/
cb[i][j].l = map_to(j, i + 1);
cb[i][j].r = map_to(j + 1, i);
cb[i][j].h = priority(j + 1, i + 1); // 越大越高。
pos.push_back(map_to(j, i + 1));
pos.push_back(map_to(j + 1, i));
}
}
/**
* 优先级,采用直线截距。靠。这傻逼题目真他妈难写。
*/
std::sort(pos.begin(), pos.end());
pos.resize(std::unique(pos.begin(), pos.end()) - pos.begin());
}
std::vector<std::pair<cube, uint>> query;
uint maxn = 0;
void process_queries() {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
maxn = std::max(height[i][j], maxn);
}
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
height[i][j] = maxn - height[i][j];
}
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
query.emplace_back(cb[i][j], height[i][j]);
}
}
std::sort(
query.begin(), query.end(),
[](const std::pair<cube, uint> &lhs, const std::pair<cube, uint> &rhs) {
return lhs.first.h > rhs.first.h;
});
return;
}
void solve() {
std::cin >> n >> x >> y;
process_map();
process_cube();
process_queries();
std::vector<info_t> init_val(pos.size() - 1, info_t(maxn));
data_structure::lazy_segtree<info_t, tag_t> segtree(init_val.begin(),
init_val.end());
ull ans = 0;
auto find_pos = [](int val) {
return std::lower_bound(pos.begin(), pos.end(), val) - pos.begin();
};
for (size_t i = 0; i < n * n; i++) {
auto cur = query[i];
auto rl = find_pos(cur.first.l), rr = find_pos(cur.first.r);
auto cur_maxn = segtree.query(rl, rr);
if (cur_maxn.val <= cur.second) {
continue;
}
ans += cur_maxn.val - cur.second;
segtree.apply(rl, rr, tag_t{cur.second});
}
std::cout << ans << "\n";
}
} // namespace solve
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve::solve();
std::cout << std::flush;
return 0;
}