hdu3642-Get The Treasury 线段树+扫描线+离散化 求三维体积并

Get The Treasury

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2808    Accepted Submission(s): 907


Problem 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划分为几个区间,再求平面内的面积交,体积为面积乘高度。这道题感觉还是很难的。

ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=2222;
#define lson i*2,l,m
#define rson i*2+1,m+1,r
int cover[MAXN*4],sum[MAXN*4],len1[MAXN*4],len2[MAXN*4];
int X[MAXN],Z[MAXN];
int cnt_x,cnt_z;
struct seg
{
    int l,r,h,d;
    seg(){}
    seg(int a,int b,int c,int d):l(a),r(b),h(c),d(d){}
    bool operator < (const seg& b)const
    {
        return h<b.h;
    }
}ss[MAXN];
struct point
{
    int x,y,z;
    void read()
    {
        scanf("%d%d%d",&x,&y,&z);
    }
};
struct cube
{
    point a,b;
}cubes[MAXN];
void PushUp(int i,int l,int r)
{
    if(cover[i]>=3)
    {
        sum[i]=X[r+1]-X[l];
        len1[i]=len2[i]=0;
    }
    else if(cover[i]==2)
    {
        sum[i]=sum[i*2]+sum[i*2+1]+len1[i*2]+len1[i*2+1]+len2[i*2]+len2[i*2+1];
        len2[i]=X[r+1]-X[l]-sum[i];
        len1[i]=0;
    }
    else if(cover[i]==1)
    {
        sum[i]=sum[i*2]+sum[i*2+1]+len2[i*2]+len2[i*2+1];
        len2[i]=len1[i*2]+len1[i*2+1];
        len1[i]=X[r+1]-X[l]-sum[i]-len2[i];
    }
    else
    {
        sum[i]=sum[i*2]+sum[i*2+1];
        len1[i]=len1[i*2]+len1[i*2+1];
        len2[i]=len2[i*2]+len2[i*2+1];
    }
}
void update(int ql,int qr,int v,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    {
        cover[i]+=v;
        PushUp(i,l,r);
        return ;
    }
    int m=(l+r)>>1;
    if(ql<=m) update(ql,qr,v,lson);
    if(m<qr) update(ql,qr,v,rson);
    PushUp(i,l,r);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++)
    {
        int n;
        cnt_x=cnt_z=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            cubes[i].a.read();
            cubes[i].b.read();
            X[cnt_x++]=cubes[i].a.x;//对x离散化 
            X[cnt_x++]=cubes[i].b.x;
            Z[cnt_z++]=cubes[i].a.z;//对z离散化 
            Z[cnt_z++]=cubes[i].b.z;
        }
        if(n<3)
        {
            printf("Case %d: 0\n",kase);
            continue;
        }
        sort(X,X+cnt_x);
        sort(Z,Z+cnt_z);
        cnt_x=unique(X,X+cnt_x)-X;//去重 
        cnt_z=unique(Z,Z+cnt_z)-Z;
        long long ans=0;

        for(int i=0;i<cnt_z-1;i++)
        {
            int cnt=0;
            long long res=0;
            for(int j=1;j<=n;j++)//对z进行离散化,然后求平面的面积交 
            {
                if(cubes[j].a.z<=Z[i] && cubes[j].b.z>Z[i])
                {
                    ss[cnt++]=seg(cubes[j].a.x,cubes[j].b.x,cubes[j].a.y,1);
                    ss[cnt++]=seg(cubes[j].a.x,cubes[j].b.x,cubes[j].b.y,-1);
                }
            }
            sort(ss,ss+cnt);
            memset(cover,0,sizeof(cover));
            memset(sum,0,sizeof(sum));
            memset(len1,0,sizeof(len1));
            memset(len2,0,sizeof(len2));
            for(int j=0;j<cnt-1;j++)
            {
                int ql=lower_bound(X,X+cnt_x,ss[j].l)-X;
                int qr=lower_bound(X,X+cnt_x,ss[j].r)-X-1;
                update(ql,qr,ss[j].d,1,0,cnt_x-1);
                res +=(long long)sum[1]*(ss[j+1].h-ss[j].h);
            }
            ans += res*(Z[i+1]-Z[i]);
        }
        printf("Case %d: %I64d\n",kase,ans);
    }
}


题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=3642



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值