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): 3635    Accepted Submission(s): 1167


 

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

2010 Asia Regional Hangzhou Site —— Online Contest


题解:三维线段扫描 先离散化z轴在离散化x轴然后扫描x轴计算出长度覆盖了三次以上的区域就对了


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define eb(a)    emplace_back(a)
#define pb(a)    push_back(a)
#define reg      register
#define rep(a,b,c) for(reg int a=b;a ve;
set     sez,sex;
int t,n,lz,lx;
int z[maxn<<1],x[maxn<<1],tree[4][maxn<<2],cnt[maxn<<2];
void pushup(int l, int r, int k) {
    if(cnt[k] >= 3)
    {
            rep(i,1,4){tree[i][k]=x[r+1] - x[l];}
    } else if(cnt[k] == 2) {
        tree[2][k] = tree[1][k] = x[r+1] - x[l];
        tree[3][k] = tree[1][k << 1] + tree[1][k << 1 | 1];
    } else if(cnt[k] == 1) {
        tree[1][k] = x[r+1] - x[l];
        tree[3][k] = tree[2][k << 1] + tree[2][k << 1 | 1];
        tree[2][k] = tree[1][k << 1] + tree[1][k << 1 | 1];
    } else
    {
          rep(i,1,4){tree[i][k]=tree[i][k<<1]+tree[i][k<<1|1];}
    }
}
void update(int l,int r,int lt,int rt,int k,int w)
{
    if(lt<=l&&r<=rt)
    {
        cnt[k]+=w;
        pushup(l,r,k);
        return ;
    }
    int mind=(l+r)>>1;
    if(lt<=mind){update(l,mind,lt,rt,k<<1,w);}
    if(rt>mind) {update(mind+1,r,lt,rt,k<<1|1,w);}
     pushup(l,r,k);
    return ;
}
void init()
{
    ve.clear();
    sex.clear();
    clr(tree,0);
    clr(cnt,0);
    return ;
}
int main()
{
    ///freopen("data.txt","r",stdin);
    scanf("%d",&t);
    ll res;
    rep(ti,1,t+1)
    {
        sez.clear();
        res=0;
        scanf("%d",&n);
        rep(i,0,n)
        {
            scanf("%d%d%d",&spot[i].x1,&spot[i].y1,&spot[i].z1);
            scanf("%d%d%d",&spot[i].x2,&spot[i].y2,&spot[i].z2);
            sez.insert(spot[i].z1);
            sez.insert(spot[i].z2);
        }
        lz=0;
        for(reg auto i:sez){z[++lz]=i;}
        rep(i,1,lz)
        {
            init();
            rep(j,0,n)
            {
                if(spot[j].z1<=z[i]&&z[i+1]<=spot[j].z2)
                {
                    ve.pb(Node(spot[j].x1,spot[j].x2,spot[j].y1,1));
                    ve.pb(Node(spot[j].x1,spot[j].x2,spot[j].y2,-1));
                    sex.insert(spot[j].x1);
                    sex.insert(spot[j].x2);
                }
            }
            lx=0;
            for(reg auto j:sex){x[++lx]=j;}
            sort(ve.begin(),ve.end());
            rep(j,0,ve.size()-1)
            {
                int lt=lower_bound(x+1,x+1+lx,ve[j].l)-x;
                int rt=lower_bound(x+1,x+1+lx,ve[j].r)-x-1;
                update(1,lx,lt,rt,1,ve[j].w);
                res+=1ll*tree[3][1]*(ve[j+1].h-ve[j].h)*(z[i+1]-z[i]);
            }
        }
        printf("Case %d: %lld\n", ti,res);
    }
    return 0;
}

 

你可能感兴趣的:(数据结构)