Get The Treasury - HDU 3642 扫描线 重复三次的体积

本文介绍了一种解决三维空间中多个长方体区域重叠问题的方法,通过离散化和转换为二维问题来计算至少三次重叠的体积总和。采用线段树和扫描线算法实现,并给出具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Get The Treasury

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


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 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.

 

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.

 

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轴不超过500,所以要离散化z轴以后,转化成二维的来做。这样就和HDU1255差不多了。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Cube
{
    ll x1,y1,z1,x2,y2,z2;
}cubes[10100];
struct node
{
    int l,r,lazy;
    ll sum[4];
}tree[16010];
struct node2
{
    int l,r,f;
    ll h;
}line[40100];
bool cmp(node2 a,node2 b)
{
    return a.h<b.h;
}
int n,mx,mz;
ll pz[40100],px[40100],fx[2000010];
void build(int o,int l,int r)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].lazy=0;
    for(int i=1;i<=3;i++)
       tree[o].sum[i]=0;
    tree[o].sum[0]=px[tree[o].r]-px[tree[o].l-1];
    if(l==r)
      return;
    int mi=(l+r)/2;
    build(o<<1,l,mi);
    build(o<<1|1,mi+1,r);
}
void solve(int o)
{
    int i,j,k;
    if(tree[o].l==tree[o].r)
    {
        for(i=1;i<=min(tree[o].lazy,3);i++)
           tree[o].sum[i]=tree[o].sum[0];
        for(;i<=3;i++)
           tree[o].sum[i]=0;
        return;
    }
    if(tree[o].lazy>=3)
    {
        for(i=1;i<=3;i++)
           tree[o].sum[i]=tree[o].sum[0];
    }
    else if(tree[o].lazy==2)
    {
        for(i=1;i<=2;i++)
           tree[o].sum[i]=tree[o].sum[0];
        tree[o].sum[3]=tree[o<<1].sum[1]+tree[o<<1|1].sum[1];
    }
    else if(tree[o].lazy==1)
    {
        tree[o].sum[1]=tree[o].sum[0];
        tree[o].sum[2]=tree[o<<1].sum[1]+tree[o<<1|1].sum[1];
        tree[o].sum[3]=tree[o<<1].sum[2]+tree[o<<1|1].sum[2];
    }
    else if(tree[o].lazy==0)
    {
        for(i=1;i<=3;i++)
           tree[o].sum[i]=tree[o<<1].sum[i]+tree[o<<1|1].sum[i];
    }
}
void update(int o,int l,int r,int f)
{
    if(tree[o].l==l && tree[o].r==r && tree[o].lazy+f>=0)
    {
        tree[o].lazy+=f;
        solve(o);
        return;
    }
    int mi=(tree[o].l+tree[o].r)/2;
    if(r<=mi)
      update(o<<1,l,r,f);
    else if(l>mi)
      update(o<<1|1,l,r,f);
    else
    {
        update(o<<1,l,mi,f);
        update(o<<1|1,mi+1,r,f);
    }
    solve(o);
}
int main()
{
    int T,t,i,j,k,p,z,l,r;
    ll ans;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&cubes[i].x1,&cubes[i].y1,&cubes[i].z1,
                                                   &cubes[i].x2,&cubes[i].y2,&cubes[i].z2);
            cubes[i].x1+=1000000;cubes[i].x2+=1000000;
            pz[i*2-1]=cubes[i].z1;pz[i*2]=cubes[i].z2;
            px[i*2-1]=cubes[i].x1;px[i*2]=cubes[i].x2;
        }
        sort(pz+1,pz+1+n*2);
        sort(px+1,px+1+n*2);
        mx=-1;mz=-1;px[0]=-100000000;pz[0]=-100000000;
        for(i=1;i<=n*2;i++)
        {
            if(px[i]!=px[i-1])
            {
                px[++mx]=px[i];
                fx[px[i]]=mx;
            }
            if(pz[i]!=pz[i-1])
              pz[++mz]=pz[i];
        }
        ans=0;
        for(i=1;i<=mz;i++)
        {

            p=0;
            for(j=1;j<=n;j++)
               if(cubes[j].z1<pz[i] && cubes[j].z2>=pz[i])
               {
                   p+=2;
                   l=fx[cubes[j].x1]+1;r=fx[cubes[j].x2];
                   line[p-1].l=l;line[p-1].r=r;line[p-1].f=1;line[p-1].h=cubes[j].y1;
                   line[p].l=l;line[p].r=r;line[p].f=-1;line[p].h=cubes[j].y2;
               }
            sort(line+1,line+1+p,cmp);
            build(1,1,mx);
            for(j=1;j<p;j++)
            {
                update(1,line[j].l,line[j].r,line[j].f);
                ans+=(pz[i]-pz[i-1])*tree[1].sum[3]*(line[j+1].h-line[j].h);
            }
        }
        printf("Case %d: %I64d\n",t,ans);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值