hdoj-3642 Get The Treasury

考点

  • 扫描线

题解

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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 50;
int n, dy[4 * maxn], dz[4 * maxn];

struct node1 {
int x1_, y1_, z1_, x2_, y2_, z2_;
} arr[maxn];

struct node2 {
int x_, y1_, y2_, flag_;
bool operator<(node2 &b) { return x_ < b.x_; }
} line[2 * maxn];

struct node3 {
ll cnt_, once_, twice_, third_;
} seg[4 * 2 * maxn];
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define cnt(x) seg[x].cnt_
#define once(x) seg[x].once_
#define twice(x) seg[x].twice_
#define third(x) seg[x].third_

void up(int rt, int l, int r) {
// 大于等于1的访问次数
if (cnt(rt)) {
once(rt) = dy[r + 1] - dy[l];
} else {
once(rt) = l == r ? 0 : once(lson) + once(rson);
}
// 大于等于2的访问次数
if (cnt(rt) > 1) {
twice(rt) = dy[r + 1] - dy[l];
} else if (cnt(rt) == 1) {
twice(rt) = l == r ? 0 : once(lson) + once(rson);
} else {
twice(rt) = l == r ? 0 : twice(lson) + twice(rson);
}
// 大于等于3的访问次数
if (cnt(rt) > 2) {
third(rt) = dy[r + 1] - dy[l];
} else if (cnt(rt) == 2) {
third(rt) = l == r ? 0 : once(lson) + once(rson);
} else if (cnt(rt) == 1) {
third(rt) = l == r ? 0 : twice(lson) + twice(rson);
} else {
third(rt) = l == r ? 0 : third(lson) + third(rson);
}
}

void update(int rt, int l, int r, int L, int R, int v) {
if (L <= l && r <= R) {
cnt(rt) += v, up(rt, l, r);
return;
}
int mid = (l + r) / 2;
if (L <= mid) update(lson, l, mid, L, R, v);
if (R > mid) update(rson, mid + 1, r, L, R, v);
up(rt, l, r);
}

ll work() {
cin >> n;
int ltot = 0, ytot = 0, ztot = 0;
int x1, y1, z1, x2, y2, z2;
for (int i = 1; i <= n; ++i) {
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
arr[i] = {x1, y1, z1, x2, y2, z2};
dz[++ztot] = z1, dz[++ztot] = z2;
dy[++ytot] = y1, dy[++ytot] = y2;
}
sort(dz + 1, dz + 1 + ztot), sort(dy + 1, dy + 1 + ytot);
ztot = unique(dz + 1, dz + 1 + ztot) - 1 - dz;
ytot = unique(dy + 1, dy + 1 + ytot) - 1 - dy;
ll ans = 0;
for (int i = 1; i < ztot; ++i) {
ltot = 0, memset(seg, 0, sizeof(seg));
for (int j = 1; j <= n; ++j) {
if (arr[j].z1_ <= dz[i] && arr[j].z2_ > dz[i]) {
line[++ltot] = {arr[j].x1_, arr[j].y1_, arr[j].y2_, 1};
line[++ltot] = {arr[j].x2_, arr[j].y1_, arr[j].y2_, -1};
}
}
sort(line + 1, line + 1 + ltot);
for (int j = 1; j < ltot; ++j) {
y1 = lower_bound(dy + 1, dy + 1 + ytot, line[j].y1_) - dy;
y2 = lower_bound(dy + 1, dy + 1 + ytot, line[j].y2_) - dy;
update(1, 1, ytot - 1, y1, y2 - 1, line[j].flag_);
ans += third(1) * (line[j + 1].x_ - line[j].x_) * (dz[i + 1] - dz[i]);
}
}
return ans;
}

int main() {
int t;
cin >> t;
for (int i = 1; i <= t; ++i) printf("Case %d: %lld\n", i, work());
return 0;
}

思路

题意是说,给你若干个长方体,求至少3个长方体相交部分的体积和。

由于是三维,先在z轴上使用扫描线,固定z坐标,使其转换为二维的面积问题。

两根z轴上扫描线之间的体积 = 两根z轴上扫描线之间的距离(高) * 两扫描线之间的面积

二维上的面积问题则是经典的求覆盖次数大于等于3的面积和,参见hdu-1255,不再赘述。