hdu 3642 Get The Treasury(线段树求体积并)

Get The Treasury

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


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
 

Source
 

Recommend
lcy
 

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3642

题意:就是给你一些长方体,求这些长方体相交至少3次的体积和。。。

分析:这题一开始没计算好复杂度,写了个二维的线段树。。。然后不停的re,搞了一整天,后来hdoj还挂掉了,所以直接扔了。。。其实不到万不得以还是把二维的转为1维的来做会好点,题目提示z的坐标在500以内,很明显要我们枚举z坐标,然后求面积并,于是就这样搞了。。。至于覆盖三次,直接开三个数组统计就行,统计的时候注意三者的关系就行,详细见代码吧

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m,r,rs
using namespace std;
const int mm=2222;
const int mn=mm<<2;
struct tran
{
    int x1,x2,y1,y2,z1,z2;
}s[mm];
struct seg
{
    int x,y1,y2,c;
}g[mm];
int y[mm],z[mm];
int t[mn],once[mn],twice[mn],more[mn];
int L,R,val;
void build(int n)
{
    while(n--)t[n]=once[n]=twice[n]=more[n]=0;
}
void updata(int l,int r,int rt)
{
    if(L<=y[l]&&R>=y[r])t[rt]+=val;
    else
    {
        int m=(l+r)>>1;
        if(L<y[m])updata(lson);
        if(R>y[m])updata(rson);
    }
    if(t[rt]>2)
    {
        more[rt]=y[r]-y[l];
        twice[rt]=once[rt]=0;
    }
    else if(t[rt]>1)
    {
        more[rt]=more[ls]+more[rs]+twice[ls]+twice[rs]+once[ls]+once[rs];
        twice[rt]=y[r]-y[l]-more[rt];
        once[rt]=0;
    }
    else if(t[rt])
    {
        more[rt]=more[ls]+more[rs]+twice[ls]+twice[rs];
        twice[rt]=once[ls]+once[rs];
        once[rt]=y[r]-y[l]-twice[rt]-more[rt];
    }
    else if(l>=r)once[rt]=twice[rt]=more[rt]=0;
    else
    {
        once[rt]=once[ls]+once[rs];
        twice[rt]=twice[ls]+twice[rs];
        more[rt]=more[ls]+more[rs];
    }
}
bool cmp(seg a,seg b)
{
    return a.x<b.x;
}
int main()
{
    int i,j,k,n,m,w,t,cs=0;
    __int64 ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
        {
            scanf("%d%d%d%d%d%d",&s[i].x1,&s[i].y1,&s[i].z1,&s[i].x2,&s[i].y2,&s[i].z2);
            z[i]=s[i].z1,z[i+n]=s[i].z2;
        }
        sort(z,z+n+n);
        for(m=i=0;i<n+n;++i)
            if(z[m]<z[i])z[++m]=z[i];
        ans=0;
        for(j=1;j<=m;++j)
        {
            for(k=i=0;i<n;++i)
                if(s[i].z1<=z[j-1]&&s[i].z2>=z[j])
                {
                    g[k].x=s[i].x1,g[k].y1=s[i].y1,g[k].y2=s[i].y2,g[k].c=1,y[k]=s[i].y1,++k;
                    g[k].x=s[i].x2,g[k].y1=s[i].y1,g[k].y2=s[i].y2,g[k].c=-1,y[k]=s[i].y2,++k;
                }
            sort(y,y+k);
            sort(g,g+k,cmp);
            for(w=i=0;i<k;++i)
                if(y[w]<y[i])y[++w]=y[i];
            build(w<<2);
            for(i=0;i<k;++i)
            {
                L=g[i].y1,R=g[i].y2,val=g[i].c;
                updata(0,w,1);
                if(g[i].x<g[i+1].x)ans+=(__int64)(g[i+1].x-g[i].x)*(__int64)more[1]*(__int64)(z[j]-z[j-1]);
            }
        }
        printf("Case %d: %I64d\n",++cs,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值