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

2010 Asia Regional Hangzhou Site —— Online Contest


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


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<iostream>
#include<iterator>
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<c;a++)
typedef long long ll;
const int maxn=2050+3;
const int minn=1000+3;
const double ep=1e-10;
struct Spot
{
    int x1,y1,z1;
    int x2,y2,z2;
}spot[maxn];
struct Node
{
    int l,r,h,w;
    Node(){};
    Node(int l,int r,int h,int w):l(l),r(r),h(h),w(w){};
    bool operator < (Node a) const
    {
        return h<a.h;
    }
};
vector<Node> ve;
set<int>     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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值