hdu3642(线段树+扫描线体积与)
题目描述:
Get The Treasury
ProblemDescription
Jack knows that there is a great underground treasury in a secret region. And he has a special device that can be used to detect treasury under the surface of the earth. One day he got outside with the device to ascertain the treasury. He chose many different locations on the surface of the earth near the secret region. And at each spot he used the device to detect treasury and got some data from it representing a region, which may contain treasury below the surface. The data from the device at each spot is six integers x1, y1, z1, x2, y2 and z2 (x1<x2, y1<y2, z1<z2). According to the instruction of the device they represent the range of x, y and z coordinates of the region. That is to say, the x coordinate of the region, which may contain treasury, ranges from x1 to x2. So do y and z coordinates. The origin of the coordinates is a fixed point under the ground.
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.
Input
The first line of the input file contains a single integer t, the number of test cases, followed by the input data for each test case.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x1, y1, z1, x2, y2 and z2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 106, and that of z coordinate is no more than 500.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x1, y1, z1, x2, y2 and z2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 106, and that of z coordinate is no more than 500.
思路:与面积与不一样的地方是多了一个z的维度,我们需要遍历所有的z的情况,然后根据当前z的大小来选取包含了z的矩形
用这些矩形的面积与来求和,如果不懂扫描线最好先把hdu1542和hdu1255做了再做这道,代码有注释
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 20000; #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define ls(x) x<<1 #define rs(x) x<<1|1 struct { int x1, x2, y1, y2, z1, z2; }rect[maxn]; struct p { int l,r, h, f; p() {}; p(int a, int b, int c, int d) :l(a),r(b),h(c),f(d) {}; bool operator<(const p b)const { if (h == b.h) return f < b.f; else return h < b.h; } }e[maxn],s[maxn]; int num,z[maxn], x[maxn]; struct INTERVALTREE { ll one[maxn << 2], two[maxn << 2], sum[maxn << 2]; int cnt[maxn << 2]; void build() { memset(one, 0, sizeof(one)); memset(two, 0, sizeof(two)); memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); } void push_up(int rt,int l,int r) { if (cnt[rt] >= 3) {//懒标记以及标记了大于两次次 sum[rt] = x[r + 1] - x[l];//覆盖两次以上的就是这个区间的大小 one[rt] = two[rt] = 0; } else if (cnt[rt] == 2) { sum[rt] = one[ls(rt)] + one[rs(rt)] + two[ls(rt)] + two[rs(rt)] + sum[ls(rt)] + sum[rs(rt)]; one[rt] = 0; two[rt] = x[r + 1] - x[l] - sum[rt];//剩下的就是覆盖两次的 } else if (cnt[rt] == 1) { sum[rt] = two[ls(rt)] + two[rs(rt)] + sum[ls(rt)] + sum[rs(rt)]; two[rt] = one[ls(rt)] + one[rs(rt)]; one[rt] = x[r + 1] - x[l] - sum[rt] - two[rt];//剩下的就是覆盖一次的 } else {//这个区间没有懒标记 sum[rt] = sum[ls(rt)] + sum[rs(rt)]; two[rt] = two[ls(rt)] + two[rs(rt)]; one[rt] = one[ls(rt)] + one[rs(rt)]; } } void update(int rt, int l, int r, int L, int R, int f) { if (L <= l && r <= R) { cnt[rt] += f;//懒标记 push_up(rt, l, r);//更新一下 return; } int mid = (l + r) >> 1; if (L <= mid) { update(lson, L, R, f); } if (R > mid) { update(rson, L, R, f); } push_up(rt, l, r); } }; int n; ll ans; void solve() { INTERVALTREE tree; sort(x + 1, x + num + 1); sort(z + 1, z + num + 1); int cntx = unique(x + 1, x + cntx + 1) - x - 1; int cntz = unique(z + 1, z + cntz + 1) - z - 1; //离散化x,z ans = 0; for (int i = 1; i < cntz; i++) {//遍历z轴的每一层 int tot = 0; for (int j = 1; j <=n; j++) {//选取能够覆盖该层的立方体 if (rect[j].z1 <= z[i] && rect[j].z2 > z[i]) { s[++tot] = { rect[j].x1,rect[j].x2,rect[j].y1,1 }; s[++tot] = { rect[j].x1,rect[j].x2,rect[j].y2,-1}; } } sort(s + 1, s + tot + 1);//对y轴每一层排序 ll area = 0; tree.build(); for (int j = 1; j < tot; j++) {//利用扫描线算法求出z轴该层覆盖超过两次的面积 int l = lower_bound(x + 1, x + cntx + 1, s[j].l) - x; int r = lower_bound(x + 1, x + cntx + 1, s[j].r) - x - 1; tree.update(1, 1, cntx, l, r, s[j].f); area += tree.sum[1] * (s[j + 1].h - s[j].h); } ans += area * (z[i + 1] - z[i]);//求出面积乘上高度差就是覆盖超过两次的体积 } printf("%lld\n", ans); } int main() { //freopen("test.txt", "r", stdin); int t; scanf("%d", &t); for (int q = 1; q <= t;q++) { scanf("%d", &n); num = 0; for (int i = 1; i <= n;i++) { scanf("%d%d%d%d%d%d", &rect[i].x1, &rect[i].y1, &rect[i].z1, &rect[i].x2, &rect[i].y2, &rect[i].z2); x[++num] = rect[i].x1; z[num] = rect[i].z1; x[++num] = rect[i].x2, z[num] = rect[i].z2; } printf("Case %d: ",q); if (n < 3) { printf("0\n"); continue; } solve(); } return 0; }