Q - Get The Treasury HDU - 3642

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

Recommend

lcy   |   We have carefully selected several similar problems for you:  1828 2871 3308 3641 3397 

这题将平时平面的问题转化为了三维,说实话,折磨了我蛮久,但是我也确实在看了网上很多代码后,学到了不少的东西

自认代码算优雅的吧,hdu上时间排名第6

#include<bits/stdc++.h>
#define ls rt << 1
#define rs rt << 1 | 1
#define ll long long
using namespace std;

const int MAXN = 1010;
struct f
{
    int l, r, h, z1, z2;
    int flag;
    f(){};
    f(int _l, int _r, int _z1, int _z2, int _h, int _flag)
    {
        l = _l;
        r = _r;
        h = _h;
        z1 = _z1;
        z2 = _z2;
        flag = _flag;
    }
    bool operator < (f b)
    {
        return h < b.h;
    }
}line[MAXN << 1], temp[MAXN << 1];
struct node
{
    int l, r;
    int one_len, two_len,three_len;
    int lazy;
}tree[MAXN * 8];
int xx[MAXN << 1], zz[MAXN << 1];

void build( int l, int r, int rt )
{
    tree[rt].l = l; tree[rt].r = r;
    tree[rt].one_len = tree[rt].two_len = tree[rt].three_len = 0;
    tree[rt].lazy = 0;
    if( l + 1 == r )
        return;
    int mid = ( l + r ) >> 1;
    build( l, mid, rt << 1 );
    build( mid, r, rt << 1 | 1 );
}
void push(int rt)
{
    if(tree[rt].lazy >= 3)
    {
        tree[rt].one_len = tree[rt].two_len = tree[rt].three_len = xx[tree[rt].r] - xx[tree[rt].l];
    }
    else if(tree[rt].lazy == 2)
    {
        tree[rt].one_len = tree[rt].two_len = xx[tree[rt].r] - xx[tree[rt].l];
        if(tree[rt].l + 1 == tree[rt].r)
            tree[rt].three_len = 0;
        else
            tree[rt].three_len = tree[rt << 1].one_len + tree[rt << 1 | 1].one_len;
    }
    else if(tree[rt].lazy == 1)
    {
        tree[rt].one_len =  xx[tree[rt].r] - xx[tree[rt].l];;
        if(tree[rt].l + 1 == tree[rt].r)
            tree[rt].two_len = tree[rt].three_len = 0;
        else
        {
            tree[rt].three_len = tree[rt << 1].two_len + tree[rt << 1 | 1].two_len;
            tree[rt].two_len = tree[rt << 1].one_len + tree[rt << 1 | 1].one_len;
        }
    }
    else
    {
        if(tree[rt].l + 1 == tree[rt].r)
            tree[rt].one_len = tree[rt].two_len = tree[rt].three_len = 0;
        else
        {
            tree[rt].one_len = tree[rt << 1].one_len + tree[rt << 1 | 1].one_len;
            tree[rt].two_len = tree[rt << 1].two_len + tree[rt << 1 | 1].two_len;
            tree[rt].three_len = tree[rt << 1].three_len + tree[rt << 1 | 1].three_len;
        }
    }
}
void update(int x, int y, int l, int r, int rt, int q)
{
    if(l >= x && r <= y)
    {
        tree[rt].lazy += q;
        push(rt);
        return;
    }
    int mid = (l + r) >> 1;
    if(mid > x)
        update(x, y, l, mid, rt << 1, q);
    if(mid < y)
        update(x, y, mid, r, rt << 1 | 1, q);
    push(rt);
}

int Find(int tmp , int n)
{
    int l = 1, r = n, mid;
    while(l <= r)
    {
        mid = (l + r) >> 1;
        if(xx[mid] == tmp)
            return mid;
        else if(xx[mid] > tmp)
            r = mid - 1;
        else
            l = mid + 1;
    }
}
int main()
{

    int T, n;
    int cas = 0;
    int x1, y1, x2, y2, z1, z2;
    scanf("%d", &T);
    while( T -- )
    {
        scanf("%d", &n);
        cas ++;
        int tot = 0;
        for( int i = 1; i <= n; i ++ )
        {
            scanf("%d %d %d %d %d %d", &x1, &y1, &z1, &x2, &y2,&z2);
            line[++ tot] = f(x1,x2,z1,z2,y1,1);
            xx[tot] = x1; zz[tot] = z1;
            line[++ tot] = f(x1,x2,z1,z2,y2,-1);
            xx[tot] = x2; zz[tot] = z2;
        }
        sort( xx + 1, xx + tot + 1 );
        sort( zz + 1, zz + tot + 1 );
        sort( line + 1, line + tot + 1);
        int cntx = 1;
        for(int i = 2; i <= tot; i ++)
        {
            if(xx[i] != xx[i - 1])
                xx[++ cntx] = xx[i];
        }
        int cntz = 1;
        for(int i = 2; i <= tot; i ++)
        {
            if(zz[i] != zz[i - 1])
                zz[++ cntz] = zz[i];
        }
        ll  ans = 0;
        for(int i = 1; i < cntz; i ++)
        {
            build(1, cntx, 1);
            int sum = 0;
            for(int j = 1; j <= tot; j ++)
            {
                if(line[j].z1 <= zz[i] && line[j].z2 > zz[i])
                    temp[++ sum] = line[j];
            }
            ll disz = zz[i + 1] - zz[i];
            ll t_ = 0;
            for(int j = 1; j < sum; j ++)
            {
                int L = Find(temp[j].l, cntx);
                int R = Find(temp[j].r, cntx);
                update(L, R, 1, cntx, 1, temp[j].flag);
                t_ += (ll) tree[1].three_len * (temp[j + 1].h - temp[j].h);

            }
            ans += disz * t_;
        }
        printf("Case %d: %lld\n", cas, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Ant_e_zz/article/details/81437849
Q A
q