HDU - 3642 Get The Treasury —— 扫描线求体积

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lngxling/article/details/81326497

Get The Treasury

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


 

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坐标储存下来,在每个z坐标处遍历所有的立方体,找到被这个z坐标穿过的立方体,在这个面上用扫描线求面积的方法求出这些立方体在底面投影的面积,面积乘上z坐标的差就是这一段的体积

可能会爆int,要用LL

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <bitset>
#define max_ 1010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 364875103
using namespace std;
struct node
{
    int l,r;
    int s;
    ll len1,len2,len3;
};
struct node tree[max_*8];
struct edge
{
    int l,r;
    int h;
    int f;
    bool operator < (const edge &a)const
    {
        return h<a.h;
    }
};
struct edge e[max_*2];
struct cube
{
    int x1,x2,y1,y2,z1,z2;
};
struct cube c[max_];
int n;
int casnum=1;
int z[max_*2],x[max_*2];
void built(int i,int l,int r)
{
    tree[i].l=l;
    tree[i].r=r;
    tree[i].s=0;
    tree[i].len1=tree[i].len2=tree[i].len3=0;
    if(l+1==r)
    return;
    int mid=(l+r)>>1;
    built(i<<1,l,mid);
    built(i<<1|1,mid,r);
}
void up(int i)
{
    if(tree[i].s>=3)
    tree[i].len1=tree[i].len2=tree[i].len3=x[tree[i].r]-x[tree[i].l];
    else if(tree[i].s==2)
    {
        tree[i].len2=tree[i].len1=x[tree[i].r]-x[tree[i].l];
        if(tree[i].l+1==tree[i].r)
        tree[i].len3=0;
        else
        tree[i].len3=tree[i<<1].len1+tree[i<<1|1].len1;
    }
    else if(tree[i].s==1)
    {
        tree[i].len1=x[tree[i].r]-x[tree[i].l];
        if(tree[i].l+1==tree[i].r)
        tree[i].len2=tree[i].len3=0;
        else
        {
            tree[i].len2=tree[i<<1].len1+tree[i<<1|1].len1;
            tree[i].len3=tree[i<<1].len2+tree[i<<1|1].len2;
        }
    }
    else
    {
        if(tree[i].l+1==tree[i].r)
        tree[i].len1=tree[i].len2=tree[i].len3=0;
        else
        {
            tree[i].len1=tree[i<<1].len1+tree[i<<1|1].len1;
            tree[i].len2=tree[i<<1].len2+tree[i<<1|1].len2;
            tree[i].len3=tree[i<<1].len3+tree[i<<1|1].len3;
        }
    }
}
void updata(int i,int l,int r,int v)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        tree[i].s+=v;
        up(i);
        return;
    }
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
    updata(i<<1,l,r,v);
    else if(l>=mid)
    updata(i<<1|1,l,r,v);
    else
    {
        updata(i<<1,l,mid,v);
        updata(i<<1|1,mid,r,v);
    }
    up(i);
}
int main(int argc, char const *argv[]) {
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d%d%d",&c[i].x1,&c[i].y1,&c[i].z1,&c[i].x2,&c[i].y2,&c[i].z2);
            z[i*2-1]=c[i].z1;
            z[i*2]=c[i].z2;
        }
        sort(z+1,z+1+2*n);
        int tot=unique(z+1,z+1+2*n)-z-1;
        ll ans=0;
        for(int i=1;i<tot;i++)
        {
            int cnt=0;
            for(int j=1;j<=n;j++)
            {
                if(z[i]>=c[j].z1&&z[i]<c[j].z2)
                {
                    int p=cnt+1;
                    int q=cnt+2;
                    cnt+=2;
                    e[p].l=e[q].l=c[j].x1;
                    e[p].r=e[q].r=c[j].x2;
                    e[p].h=c[j].y1;
                    e[p].f=1;
                    e[q].h=c[j].y2;
                    e[q].f=-1;
                    x[p]=c[j].x1;
                    x[q]=c[j].x2;
                }
            }
            sort(x+1,x+1+cnt);
            sort(e+1,e+1+cnt);
            built(1,1,cnt);
            ll sum=0;
            for(int k=1;k<cnt;k++)
            {
                int l=lower_bound(x+1,x+1+cnt,e[k].l)-x;
                int r=lower_bound(x+1,x+1+cnt,e[k].r)-x;
                updata(1,l,r,e[k].f);
                sum+=tree[1].len3*(e[k+1].h-e[k].h);
            }
            ans+=sum*(z[i+1]-z[i]);
        }
        printf("Case %d: %lld\n",casnum++,ans );
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Lngxling/article/details/81326497