1. 程式人生 > >HDU 3642 - Get The Treasury - [加強版掃描線+線段樹]

HDU 3642 - Get The Treasury - [加強版掃描線+線段樹]

i++ void eas detect ces ras shu sts sca

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3642

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

題意:

給定一些長方體,求這些長方體相交至少3次的體積。

題解:

z坐標的範圍 [-500, 500],比較小,所以可以枚舉 z平面,用類似於掃描線掃描二維圖形的方式,用掃描面掃描整個三維圖形,而這些掃描面,就是所有長方體的上下平面,

那麽,每個我們掃描得到的截面,就可以按照HDU1542裏那樣的普通的二維掃描線+線段樹來做,求出這樣一個截面上,重疊三次及以上的面積有多大,

同時,不難想到,若我們把長方體豎直的四個側面,看成“下閉上開”(下沿取得到,上沿取不到)的話,當我們枚舉到某一個平面 $z_i$ 時,該平面截得的截面,在區間 $\left[ {z_i ,z_{i + 1} } \right)$ 都是不會變動的,這個和普通的二維掃描線+線段樹的道理是一樣的。

故,枚舉到某一個平面 $z_i$ 時,該平面截得的截面積,乘上高度 ${z_{i + 1} - z_i }$ 之後,就是整個“3+次重疊體”在三維區間 $\left[ {z_i ,z_{i + 1} } \right)$ 內的體積。

AC代碼:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

const int maxn=1010;
const int maxz=510;

int n;

vector<int> x;
inline int getidx(int val){return lower_bound(x.begin(),x.end(),val)-x.begin();}

vector<int> z;

int tot;
struct Segment
{
    int x1,x2,y;
    int z1,z2;
    int flag;
    bool operator <(const Segment &oth)const{
        return y<oth.y;
    }
}segment[2*maxn],tmp[2*maxn];

/********************************* Segment Tree - st *********************************/
struct Node{
    int l,r;
    int s;
    int once,twice,more;
    void show()
    {
        printf("l=%d r=%d s=%d once=%d twice=%d more=%d\n",l,r,s,once,twice,more);
    }
}node[8*maxn];
void pushup(int rt)
{
    int ls=rt*2,rs=rt*2+1;
    if(node[rt].s>2)
    {
        node[rt].more=x[node[rt].r+1]-x[node[rt].l];
        node[rt].once=node[rt].twice=0;
    }
    else if(node[rt].s==2)
    {
        if(node[rt].l==node[rt].r)
        {
            node[rt].more=0;
            node[rt].twice=x[node[rt].r+1]-x[node[rt].l];
            node[rt].once=0;
        }
        else
        {
            node[rt].more=node[ls].once+node[ls].twice+node[ls].more+node[rs].once+node[rs].twice+node[rs].more;
            node[rt].twice=x[node[rt].r+1]-x[node[rt].l]-node[rt].more;
            node[rt].once=0;
        }
    }
    else if(node[rt].s==1)
    {
        if(node[rt].l==node[rt].r)
        {
            node[rt].more=0;
            node[rt].twice=0;
            node[rt].once=x[node[rt].r+1]-x[node[rt].l];
        }
        else
        {
            node[rt].more=node[ls].twice+node[ls].more+node[rs].twice+node[rs].more;
            node[rt].twice=node[ls].once+node[rs].once;
            node[rt].once=x[node[rt].r+1]-x[node[rt].l]-node[rt].more-node[rt].twice;
        }
    }
    else
    {
        if(node[rt].l==node[rt].r)
        {
            node[rt].more=0;
            node[rt].twice=0;
            node[rt].once=0;
        }
        else
        {
            node[rt].more=node[ls].more+node[rs].more;
            node[rt].twice=node[ls].twice+node[rs].twice;
            node[rt].once=node[ls].once+node[rs].once;
        }
    }
    //printf("now pushup rt=%d\t",rt); node[rt].show();
}
void build(int rt,int l,int r)
{
    if(l>r) return;
    node[rt].l=l; node[rt].r=r;
    node[rt].s=0;
    node[rt].once=node[rt].twice=node[rt].more=0;
    if(l==r) return;
    else
    {
        int mid=l+(r-l)/2;
        build(rt*2,l,mid);
        build(rt*2+1,mid+1,r);
        pushup(rt);
    }
}
void update(int root,int st,int ed,int val)
{
    if(st>node[root].r || ed<node[root].l) return;
    if(st<=node[root].l && node[root].r<=ed)
    {
        node[root].s+=val;
        pushup(root);
    }
    else
    {
        update(root*2,st,ed,val);
        update(root*2+1,st,ed,val);
        pushup(root);
    }
}
/********************************* Segment Tree - st *********************************/

int main()
{
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%d",&n);

        x.clear();
        z.clear();
        tot=0;
        for(int i=1;i<=n;i++)
        {
            int x1,y1,z1,x2,y2,z2;
            scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);

            segment[tot].x1=x1; segment[tot].x2=x2; segment[tot].y=y1;
            segment[tot].z1=z1; segment[tot].z2=z2;
            segment[tot].flag=1;
            tot++;

            segment[tot].x1=x1; segment[tot].x2=x2; segment[tot].y=y2;
            segment[tot].z1=z1; segment[tot].z2=z2;
            segment[tot].flag=-1;
            tot++;

            x.push_back(x1);
            x.push_back(x2);
            z.push_back(z1);
            z.push_back(z2);
        }

        sort(x.begin(),x.end());
        x.erase(unique(x.begin(),x.end()),x.end());

        sort(z.begin(),z.end());
        z.erase(unique(z.begin(),z.end()),z.end());

        ll ans=0;
        for(int i=0;i<z.size()-1;i++)
        {
            //printf("now z=%d\n",z[i]);
            int cnt=0;
            for(int j=0;j<tot;j++) if(segment[j].z1<=z[i] && segment[j].z2>z[i]) tmp[cnt++]=segment[j];
            sort(tmp,tmp+cnt);

            build(1,0,x.size());
            ll area=0;
            for(int j=0;j<cnt-1;j++)
            {
                int l=getidx(tmp[j].x1);
                int r=getidx(tmp[j].x2);
                //printf("now update y=%d [%d,%d](%d,%d) += %d\n",tmp[j].y,x[l],x[r],l,r-1,tmp[j].flag);
                update(1,l,r-1,tmp[j].flag);
                area+=(ll)node[1].more*(tmp[j+1].y-tmp[j].y);
            }
            ans+=area*(z[i+1]-z[i]);
        }

        printf("Case %d: %I64d\n",kase,ans);
    }
}

HDU 3642 - Get The Treasury - [加強版掃描線+線段樹]