• 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): 1408    Accepted Submission(s): 440


    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
     
    Source
     
    Recommend
    lcy

    求立方体相交至少3次的体积。

    枚举z

    然后求矩形面积交。

    常规的线段树题目。

    较麻烦

    /*
    *HDU 3642
    给定一些长方体,求这些长方体相交至少3次的体积
    z坐标的绝对值不超过500,所以枚举z坐标,然后求矩形面积并
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    const int MAXN=2010;
    struct Node
    {
        int l,r;
        int lf,rf;//实际的左右端点
        int c;
        int once,twice,more;
    }segTree[MAXN*3];
    int y[MAXN];
    int z[MAXN];
    struct Line
    {
        int x;
        int y1,y2;
        int z1,z2;//这两个是枚举的时候判断使用的
        int f;
    }line[MAXN];
    bool cmp(Line a,Line b)
    {
        return a.x<b.x;
    }
    void Build(int i,int l,int r)
    {
        segTree[i].l=l;
        segTree[i].r=r;
        segTree[i].lf=y[l];
        segTree[i].rf=y[r];
        segTree[i].c=0;
        segTree[i].once=segTree[i].twice=segTree[i].more=0;
        if(r==l+1)return;
        int mid=(l+r)>>1;
        Build(i<<1,l,mid);
        Build((i<<1)|1,mid,r);
    }
    void push_up(int i)
    {
        if(segTree[i].c>2)
        {
            segTree[i].more=segTree[i].rf-segTree[i].lf;
            segTree[i].once=segTree[i].twice=0;
        }
        else if(segTree[i].c==2)
        {
            if(segTree[i].l+1==segTree[i].r)//叶子结点
            {
                segTree[i].more=0;
                segTree[i].twice=segTree[i].rf-segTree[i].lf;
                segTree[i].once=0;
                return;
            }
            segTree[i].more=segTree[i<<1].once+segTree[i<<1].twice+segTree[i<<1].more
                           +segTree[(i<<1)|1].once+segTree[(i<<1)|1].twice+segTree[(i<<1)|1].more;
            segTree[i].twice=segTree[i].rf-segTree[i].lf-segTree[i].more;
            segTree[i].once=0;
        }
        else if(segTree[i].c==1)
        {
            if(segTree[i].l+1==segTree[i].r)
            {
                segTree[i].more=0;
                segTree[i].twice=0;
                segTree[i].once=segTree[i].rf-segTree[i].lf;
                return;
            }
            segTree[i].more=segTree[i<<1].more+segTree[i<<1].twice
                        +segTree[(i<<1)|1].more+segTree[(i<<1)|1].twice;
            segTree[i].twice=segTree[i<<1].once+segTree[(i<<1)|1].once;
            segTree[i].once=segTree[i].rf-segTree[i].lf-segTree[i].more-segTree[i].twice;
        }
        else
        {
            if(segTree[i].l+1==segTree[i].r)
            {
                segTree[i].more=segTree[i].once=segTree[i].twice=0;
                return;
            }
            segTree[i].more=segTree[i<<1].more+segTree[(i<<1)|1].more;
            segTree[i].twice=segTree[i<<1].twice+segTree[(i<<1)|1].twice;
            segTree[i].once=segTree[i<<1].once+segTree[(i<<1)|1].once;
        }
    }
    void update(int i,Line e)
    {
        if(segTree[i].lf>=e.y1 && segTree[i].rf<=e.y2)
        {
            segTree[i].c+=e.f;
            push_up(i);
            return;
        }
        if(e.y2<=segTree[i<<1].rf) update(i<<1,e);
        else if(e.y1>=segTree[(i<<1)|1].lf) update((i<<1)|1,e);
        else
        {
            Line temp=e;
            temp.y2=segTree[i<<1].rf;
            update(i<<1,temp);
            temp=e;
            temp.y1=segTree[(i<<1)|1].lf;
            update((i<<1)|1,temp);
        }
        push_up(i);
    }
    Line temp[MAXN];
    int main()
    {
        int T;
        int n;
        int x1,y1,z1,x2,y2,z2;
        scanf("%d",&T);
        int iCase=0;
        while(T--)
        {
            iCase++;
            scanf("%d",&n);
            int t=0;
            for(int i=0;i<n;i++)
            {
                scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
                line[t].x=x1;
                line[t].y1=y1;
                line[t].y2=y2;
                line[t].z1=z1;
                line[t].z2=z2;
                line[t].f=1;
                y[t]=y1;
                z[t++]=z1;
    
                line[t].x=x2;
                line[t].y1=y1;
                line[t].y2=y2;
                line[t].z1=z1;
                line[t].z2=z2;
                line[t].f=-1;
                y[t]=y2;
                z[t++]=z2;
            }
            sort(line,line+t,cmp);
            sort(y,y+t);
            int t1=unique(y,y+t)-y;
            Build(1,0,t1-1);
            sort(z,z+t);
            int t2=unique(z,z+t)-z;
            long long ans=0;
            long long area=0;
            for(int i=0;i<t2-1;i++)
            {
                int m=0;
                for(int j=0;j<t;j++)
                  if(line[j].z1<=z[i]&&line[j].z2>z[i])
                     temp[m++]=line[j];
                area=0;
                update(1,temp[0]);
                for(int j=1;j<m;j++)
                {
                    area+=(long long)segTree[1].more*(temp[j].x-temp[j-1].x);
                    update(1,temp[j]);
                }
                ans+=area*(z[i+1]-z[i]);
            }
            printf("Case %d: %I64d\n",iCase,ans);
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    window.clipboardData(转载)
    动态添加样式(转载)
    IE6 IE7 FF的CSS Hack总结(转载)
    [轉貼] linux解壓 tar 命令
    [轉]用 snprintf / asprintf 取代不安全的 sprintf
    寫一個函數計算當參數為 n(n很大) 時的值 12+34+56+7……+n
    [轉]vi 與 vim 的指令整理
    MySQL和php採用UTF8的方法
    [轉]printf 引數說明
    [C] warning: ISO C90 forbids mixed declarations and code
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3014842.html
Copyright © 2020-2023  润新知