【hdu3642】Get The Treasury (立方体体积交+线段树+扫描线)

来源:互联网 发布:js then函数 编辑:程序博客网 时间:2024/06/01 15:48

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

210 0 0 5 6 430 0 0 5 5 53 3 3 9 10 113 3 3 13 20 45

Sample Output

Case 1: 0Case 2: 8

I think

    题意:求相叠三次及以上的体积和 (最开始是没有看懂的…)
    思路:基本思路仍同扫描线的基本套路……
    但是——
    添加一维之后,我们需要多离散化一个z轴,想象z轴离散化之后,平行于x轴y轴方向的空间被分为若干层。对于每一层,这一层要么完全包含在一个立方体的高度中,要么完全不包含于一个立方体的高度。所以,对高度与该层有交集的立方体,将其底面积按照矩形面积交的套路记在线段树,最终相叠三次及以上的面积和×层高即得到本层相叠三次及以上的体积和。
    实现:基本思路同【hdu1255】覆盖的面积(矩形面积交+线段树+扫描线)

Code

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int sm = 2e3+500;const int inf = 1e6+5; typedef long long ll;ll ss,ans;int T,n,t,f;int c[sm<<2],y[sm<<2],h[sm];ll on[sm<<2],tw[sm<<2],th[sm<<2];struct line {    int x,y1,y2,z1,z2;    int flag;}a[sm];char ch;void read(int &x) {    x=0;f=1;ch=getchar();    while(ch>'9'||ch<'0') { if(ch=='-')f=-1; ch=getchar(); }    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();    x*=f;}bool cmp(line u,line v) { return u.x<v.x; }void build(int k,int l,int r) {    if(l+1==r) {        c[k]=on[k]=tw[k]=th[k]=0;        return;    }    int m=(l+r)>>1;    build(k<<1,l,m);    build(k<<1|1,m,r);    c[k]=on[k]=tw[k]=th[k]=0;}void calen(int k,int l,int r) {    int ls=k<<1,rs=k<<1|1;    if(c[k]>=3) th[k]=tw[k]=on[k]=y[r]-y[l];    else if(c[k]==2) {        tw[k]=on[k]=y[r]-y[l];        if(l+1==r)th[k]=0;        else th[k]=on[ls]+on[rs];       }    else if(c[k]==1) {        on[k]=y[r]-y[l];        if(l+1==r)th[k]=tw[k]=0;        else {            th[k]=tw[ls]+tw[rs];            tw[k]=on[ls]+on[rs];        }    } else {        on[k]=on[ls]+on[rs];        tw[k]=tw[ls]+tw[rs];        th[k]=th[ls]+th[rs];    }}void update(int k,int l,int r,line x) {    if(x.y1<=y[l]&&y[r]<=x.y2) {        c[k]+=x.flag;calen(k,l,r);        return ;    }    if(l+1==r)return;    int m=(l+r)>>1;    if(x.y1<y[m])update(k<<1,l,m,x);    if(x.y2>y[m])update(k<<1|1,m,r,x);    calen(k,l,r);}int main() {    read(T);    int t1,t2,x1,x2,y1,y2,z1,z2;    for(int i=1;i<=T;++i) {        read(n);        t1=t2=t=ans=0;        while(n--) {            read(x1),read(y1),read(z1);            read(x2),read(y2),read(z2);            y[++t]=y1,h[t]=z1,a[t].x=x1,a[t].flag=1;            a[t].y1=y1,a[t].y2=y2,a[t].z1=z1,a[t].z2=z2;            y[++t]=y2,h[t]=z2,a[t].x=x2,a[t].flag=-1;            a[t].y1=y1,a[t].y2=y2,a[t].z1=z1,a[t].z2=z2;        }        sort(y+1,y+t+1);        t1=unique(y+1,y+t+1)-y-1;        build(1,1,t1);        sort(h+1,h+t+1);        t2=unique(h+1,h+t+1)-h-1;        sort(a+1,a+t+1,cmp);        for(int cur=2,pre=1;cur<=t2;++cur,++pre) {            ss=0,build(1,1,t1);//ss记本层面积和            for(int j=1,last=inf;j<=t;++j)                if(a[j].z1<=h[pre]&&h[cur]<=a[j].z2) {                    if(last!=inf)ss+=(a[j].x-last)*th[1];                    last=a[j].x,update(1,1,t1,a[j]);                }            ans+=ss*(h[cur]-h[pre]);        }        printf("Case %d: %lld\n",i,ans);    }    return 0;}
阅读全文
0 0
原创粉丝点击