hdu3642——矩形体积并(覆盖三次以上)

题目链接:https://vjudge.net/problem/HDU-3642

Description

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 x 1, y 1, z 1, x 2, y 2 and z 2 (x 1<x 2, y 1<y 2, z 1<z 2). 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 x 1 to x 2. 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.
 

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 x 1, y 1, z 1, x 2, y 2 and z 2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, and that of z coordinate is no more than 500.
 

Output

For each test case, you should output “Case a: b” in a single line. a is the case number, and b is the minimum volume of treasury. The case number is counted from one.

Sample Input

2
1
0 0 0 5 6 4
3
0 0 0 5 5 5
3 3 3 9 10 11
3 3 3 13 20 45

Sample Output

Case 1: 0
Case 2: 8

 题意:

给你n个六面体的坐标范围,求被这些六面体覆盖三次以上的体积。

题解:

矩形面积并的进阶版本。

因为z轴的范围不大,所以我们可以直接枚举z轴,当然离散化后更好,我的解法是离散化后处理。

然后枚举z轴上的每个点,再在二维上求出被覆盖三次以上的面积,最后乘于高度差,得到答案,

求被覆盖三次以上的矩形面积并的方法类比求被覆盖两次以上的矩形面积并(https://blog.csdn.net/qq_43472263/article/details/103600645

代码实现

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#define PI atan(1.0)*4
#define E 2.718281828
#define rp(rt,cnt,t) for (rt = (cnt); rt <= (t); rt++)
#define RP(rt,cnt,t) for (rt = (t); rt >= (cnt); rt--)
#define ll long long
#define ull unsigned long long
#define mst(p,b) memset(p,b,sizeof(p))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
inline int read()
{
    int p=0,b=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            b=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        p=(p<<3)+(p<<1)+c-'0';
        c=getchar();
    }
    return p*b;
}
/*  
    cnt 表示当前节点控制的x轴区域被覆盖的次数
    len1 表示被完全覆盖1次的长度
    len2 表示被完全覆盖2次的长度
    len3 表示被完全覆盖3次及以上的长度 
*/
const int N = 2222;
struct node
{
    int l,r,h,f;
    node(){}
    node(int lx, int rx, int hx, int fx):l(lx),r(rx),h(hx),f(fx){}
    bool operator <(const node &others){
        return h<others.h;
    }
}P[N<<1];
struct Node
{
    int l, r;
    int cnt; 
    int len1,len2,len3;
} tree[N*8];
int x[N << 1], z[N << 1];
void pushup(int rt,int l,int r)
{
    if (tree[rt].cnt >= 3)//被完全覆盖了三次以上
    {
        tree[rt].len3 = x[r + 1] - x[l];
        tree[rt].len1 = tree[rt].len2 = 0;
    }
    else if (tree[rt].cnt == 2)//至少被完全覆盖了两次,是否被完全覆盖两次以上不确定
    {
        tree[rt].len3 = tree[rt<<1].len3 + tree[rt<<1|1].len3 + tree[rt<<1].len2 + tree[rt<<1|1].len2 + tree[rt<<1].len1 + tree[rt<<1|1].len1;
        tree[rt].len2 = x[r+1]-x[l]-tree[rt].len3;
        tree[rt].len1 = 0;
    }
    else if (tree[rt].cnt == 1)//至少被完全覆盖了一次,是否被完全覆盖一次以上不确定
    {
        tree[rt].len3 = tree[rt<<1].len3 + tree[rt<<1|1].len3 + tree[rt<<1].len2 + tree[rt<<1|1].len2;
        tree[rt].len2 = tree[rt<<1].len1 + tree[rt<<1|1].len1;
        tree[rt].len1 = x[r + 1] - x[l] - tree[rt].len3 - tree[rt].len2;
    }
    else
    {
        tree[rt].len3 = tree[rt<<1].len3 + tree[rt<<1|1].len3;
        tree[rt].len2 = tree[rt<<1].len2 + tree[rt<<1|1].len2;
        tree[rt].len1 = tree[rt<<1].len1 + tree[rt<<1|1].len1;
    }
}
void build(int l, int r,int rt)
{
    tree[rt].l = l, tree[rt].r = r;
    if (l == r) return;
    int m = (tree[rt].l+tree[rt].r)>>1;
    build(lson);
    build(rson);
}
void update(int rt, int L, int R, int val)
{
    if (L <= tree[rt].l && tree[rt].r <= R)
    {
        tree[rt].cnt+=val;
        pushup(rt,tree[rt].l,tree[rt].r);
        return;
    }
    int m = (tree[rt].l+tree[rt].r)>>1;
    if(L<=m) update(rt<<1,L,R,val);
    if(m<R) update(rt<<1|1,L,R,val);
    pushup(rt,tree[rt].l,tree[rt].r);
}
struct Point //需要把输入保存下来
{
    int x, y, z;
    Point(){}
    Point(int xx,int yy,int zz):x(xx),y(yy),z(zz){}
}p[N << 1];
int main()
{
    int T=read();
    int kase = 0;
    while (T--)
    {
        int n=read();
        int totx = 0, totz = 0;
        int tot = 0;
        for (int i = 1; i <= n; ++i)
        {
            int x1, y1, z1, x2, y2, z2;
            scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
            x[totx++]=x1;x[totx++]=x2;
            z[totz++]=z1;z[totz++]=z2;
            p[tot++]=Point(x1,y1,z1);
            p[tot++]=Point(x2, y2, z2);
        }
        printf("Case %d: ", ++kase);
        sort(x, x + totx);
        sort(z, z + totz);
        totx = unique(x, x + totx) - x;
        totz = unique(z, z + totz) - z;
        ll res = 0;
        for (int i = 0; i < totz - 1; ++i) //对于每一层z都求一次二维面积并
        {
            int k = 0;
            for (int j = 0; j < tot; j += 2)
            {
                if (p[j].z <= z[i] && p[j + 1].z > z[i])
                {
                    P[k++]=node(p[j].x, p[j + 1].x, p[j].y, 1); //上边
                    P[k++]=node(p[j].x, p[j + 1].x, p[j + 1].y, -1); //下边
                }
            }
            sort(P,P+k);
            mst(tree, 0);
            build(0,totx-1,1);
            ll ans = 0;
            for (int j = 0; j < k - 1; ++j)
            {
                int l = lower_bound(x, x + totx, P[j].l) - x;
                int r = lower_bound(x, x + totx, P[j].r) - x - 1;
                update(1, l, r, P[j].f);
                ans += (ll)tree[1].len3 * (ll)(P[j + 1].h - P[j].h);
            }
            res += (ll)ans*(ll)(z[i+1]-z[i]);
        }
        printf("%lld\n", res);
    }
    return 0;
}
发布了342 篇原创文章 · 获赞 220 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43472263/article/details/103602986