HDU-3642 Get The Treasury(扫描线 + 离散化 + 线段树)

Posted seast90

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-3642 Get The Treasury(扫描线 + 离散化 + 线段树)相关的知识,希望对你有一定的参考价值。

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3342 Accepted Submission(s): 1074

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的范围比较小,所以可以对z暴力枚举跑扫描线

代码

#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#define Mod 1000000
#define ll long long 
#define se second
#define fi first
#define pb push_back
#define INF 0x3f3f3f3f
#define de(x) cout << #x << " = "<< x << endl;
#define eps 1e-6
#define db double
#define lson l, mid, t<<1
#define rson mid+1, r, t<<1|1
#define rep(i,a,b) for (int i=(a);i<(b);++i)
#define per(i,a,b) for (int i=(a);i>(b);--i)
#define sz(a) (a).size()
#define vi vector<int > 
using namespace std;
int T, n;
ll ans, tot;
ll cnt[10005], sum1[10005], sum2[10005], sum3[10005];
ll all[10005], zz[10005], po;
struct node
{
    ll x, y, z, X, Y, Z;
    node(ll a,ll b,ll c,ll d,ll e,ll f)
    {
        x = a;      y = b;      z = c;
        X = d;      Y = e;      Z = f;
    }
    node() {};
};
struct seg
{
    ll l, r, h, v;
    seg(ll a, ll b, ll c, ll d)
    {
        l = a; r = b; h = c; v = d;
    }
    seg(){}
    
};
bool cmp(seg a, seg b){ return a.h < b.h;}

void push_up(ll l, ll r, ll t)
{
    if (cnt[t] > 2) sum3[t] = sum2[t] = sum1[t] = all[r + 1] - all[l];
    else if (cnt[t] == 2)
    {
        sum2[t] = sum1[t] = all[r + 1] - all[l];
        sum3[t] = sum1[t << 1] + sum1[t << 1 | 1];
    }
    else if (cnt[t] == 1)
    {
        sum1[t] = all[r + 1] - all[l];
        sum2[t] = sum1[t << 1] + sum1[t << 1 | 1];
        sum3[t] = sum2[t << 1] + sum2[t << 1 | 1];
    }
    else if (l == r)
    {
        sum1[t] = sum2[t] = sum3[t] = 0;
    }
    else
    {
        sum1[t] = sum1[t << 1] + sum1[t << 1 | 1];
        sum2[t] = sum2[t << 1] + sum2[t << 1 | 1];
        sum3[t] = sum3[t << 1] + sum3[t << 1 | 1];
    }
}

void update(ll L, ll R, ll v, ll l, ll r, ll t)
{
    if (L <= l && r <= R)
    {
        cnt[t] += v;
        push_up(l, r, t);
        return;
    }
    ll mid = (l + r) >> 1;
    if (L <= mid) update(L, R, v, lson);
    if (R > mid)update(L, R, v, rson);
    push_up(l, r, t);
}

void inti()
{
    memset(cnt, 0, sizeof(cnt));
    memset(sum1, 0, sizeof(sum1));
    memset(sum2, 0, sizeof(sum2));
    memset(sum3, 0, sizeof(sum3));
    memset(all, 0, sizeof(all));
    tot = 0;
    po = 0;
}
int main()
{
    cin >> T;
    for (int o = 1; o <= T; o++)
    {
        ans = 0;
        node a[5005];
        memset(zz, 0, sizeof(zz));
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            ll x, y, z, X, Y, Z;
            scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &x, &y, &z, &X, &Y, &Z);
            a[i] = node(x, y, z, X, Y, Z);
            zz[i] = z;
            zz[i + n] = Z;
        }
        sort(zz + 1, zz + 2 * n + 1);
        int m = unique(zz + 1, zz + 2 * n + 1) - zz - 1;
        for (int i = 1; i < m; i++)
        {
            inti();
            
            ll number = 0, lp = 0;
            seg b[5005];

            for (int j = 1; j <= n; j++)
                if (a[j].z <= zz[i] && a[j].Z > zz[i])
                {
                    b[++number] = seg(a[j].x, a[j].X, a[j].y, 1);
                    b[++number] = seg(a[j].x, a[j].X, a[j].Y, -1);
                    all[++lp] = a[j].x;
                    all[++lp] = a[j].X;
                }
            sort(all + 1, all + lp + 1);
            ll k = unique(all + 1, all + lp + 1) - all - 1;
            sort(b + 1, b + number + 1, cmp);
            for (int j = 1; j < number; j++)
            {
                ll l = lower_bound(all + 1, all + k + 1, b[j].l) - all;
                ll r = lower_bound(all + 1, all + k + 1, b[j].r) - all;
                if (l < r) update(l, r - 1, b[j].v, 1, k, 1);
                tot += sum3[1] * (b[j + 1].h - b[j].h);
            //  po += sum1[1] * (b[j + 1].h - b[j].h);
            }
            ans += tot * (zz[i + 1] - zz[i]);
        }
        printf("Case %d: %I64d
", o, ans);
    }
    //system("pause");
    return 0;
}

以上是关于HDU-3642 Get The Treasury(扫描线 + 离散化 + 线段树)的主要内容,如果未能解决你的问题,请参考以下文章

HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化

HDU-3642 Get The Treasury(扫描线 + 离散化 + 线段树)

Get The Treasury HDU - 3642(体积扫描线)

HDU 3642 Get The Treasury (线段树扫描线)

hdu 3642 Get The Treasure

HDU 3642 求体积交集