HDU 3642 Get The Treasury (线段树 体积交[转成面积交])

本文介绍了一种求解多个长方体在三维空间中至少三次重叠的体积交集的方法。通过将问题转化为二维平面上的面积交集计算,并采用线段树等数据结构优化计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

求n 个长方体的体积交(至少三次以上)。

思路:

因为Z的范围比较小。

直接暴力Z轴, 把所有Z轴转成   二维的平面。

直接求z个 面积交即可。

注意 区间是 一闭一开。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;


const int maxn = 2000 + 10;
int T, ks;

struct node{
    int l,r;
    int len[4];
    int addr;
}nod[maxn<<2];


struct RECT{
    int x,y,x1,y1;
    RECT(int x = 0,int y = 0,int x1 = 0, int y1 = 0):x(x), y(y), x1(x1), y1(y1){}
};

vector<RECT>v[maxn>>1];

struct LINE{
    int l,r;
    int h;
    int cnt;
    LINE(int l = 0,int r = 0,int h = 0,int cnt = 0):l(l), r(r), h(h), cnt(cnt){}
}line[maxn];
bool cmp(const LINE& lhs, const LINE& rhs){
    return lhs.h < rhs.h || (lhs.h == rhs.h && lhs.cnt > rhs.cnt);
}
int a[maxn];
int cnt, id2;

int get(int x){
    int l = 0,r = id2 - 1, m;
    while(l <= r){
        m = l + r >> 1;
        if (a[m] == x) return m;
        else if (a[m] > x){
            r = m - 1;
        }
        else l = m + 1;
    }
}
void pushup(int o){
    int l = nod[o].l;
    int r = nod[o].r;

    if (l == r){
        if (nod[o].addr >= 3){
            nod[o].len[3] = nod[o].len[0];
            nod[o].len[2] = nod[o].len[1] = 0;
        }
        else if (nod[o].addr >= 2){
            nod[o].len[3] = nod[o].len[1] = 0;
            nod[o].len[2] = nod[o].len[0];
        }
        else if (nod[o].addr >= 1){
            nod[o].len[2] = nod[o].len[3] = 0;
            nod[o].len[1] = nod[o].len[0];
        }
        else {
            nod[o].len[1] = nod[o].len[2] = nod[o].len[3] = 0;
        }
        return;
    }

    int lson = o << 1;
    int rson = o << 1 | 1;

    if (nod[o].addr >= 3){
        nod[o].len[3] = nod[o].len[0];
        nod[o].len[1] = nod[o].len[2] = 0;
    }
    else if (nod[o].addr >= 2){
        nod[o].len[3] = 0;
        for (int i = 1; i <= 3; ++i){
            nod[o].len[3] += nod[lson].len[i];
            nod[o].len[3] += nod[rson].len[i];
        }
        nod[o].len[1] = 0;
        nod[o].len[2] = nod[o].len[0] - nod[o].len[3];
    }
    else if (nod[o].addr >= 1){
        nod[o].len[3] = 0;
        for (int i = 2; i <= 3; ++i){
            nod[o].len[3] += nod[lson].len[i];
            nod[o].len[3] += nod[rson].len[i];
        }
        nod[o].len[2] = nod[lson].len[1] + nod[rson].len[1];
        nod[o].len[1] = nod[o].len[0] - nod[o].len[3] - nod[o].len[2];
    }
    else {
        nod[o].len[3] = nod[lson].len[3] + nod[rson].len[3];
        nod[o].len[2] = nod[lson].len[2] + nod[rson].len[2];
        nod[o].len[1] = nod[lson].len[1] + nod[rson].len[1];
    }
}
void build(int l,int r,int o){
    nod[o].l = l;
    nod[o].r = r;
    nod[o].addr = 0;
    nod[o].len[0] = a[r] - a[l-1];
    for (int i = 1; i <= 3; ++i){
        nod[o].len[i] = 0;
    }
    if (l == r) {
        return;
    }
    int m = l + r >> 1;
    int lson = o << 1;
    int rson = o << 1 | 1;
    build(l,m,lson);
    build(m+1,r,rson);
    nod[o].len[0] = nod[lson].len[0] + nod[rson].len[0];
}

void update(int L,int R,int c,int l,int r,int o){
    if (L <= l && r <= R){
        nod[o].addr += c;
        pushup(o);
        return;
    }

    int m = l + r >> 1;
    if (m >= L){
        update(L,R,c,l,m,o<<1);
    }
    if (m < R){
        update(L,R,c,m+1,r,o<<1|1);
    }
    pushup(o);
}


long long solve(int id){
    if ((int)v[id].size() < 3) return 0;
    cnt = id2 = 0;
    for (int i = 0; i < v[id].size(); ++i){
        int x = v[id][i].x;
        int y = v[id][i].y;
        int x1 = v[id][i].x1;
        int y1 = v[id][i].y1;
        line[cnt++] = LINE(x,x1, y, 1);
        line[cnt++] = LINE(x,x1, y1, -1);
        a[id2++] = x;
        a[id2++] = x1;
    }

    sort(a,a+id2);
    id2 = unique(a, a + id2) - a;
    sort(line, line + cnt, cmp);
    build(1, id2-1, 1);
    update(get(line[0].l) + 1, get(line[0].r), line[0].cnt, 1, id2 - 1, 1);

    long long ans = 0;
    for (int i = 1; i < cnt; ++i){
        ans += (long long)nod[1].len[3] * (line[i].h - line[i-1].h);
        update(get(line[i].l) + 1, get(line[i].r), line[i].cnt, 1, id2 - 1, 1);
    }
    return ans;
}

int main(){
    scanf("%d",&T);
    while(T--){
        for (int i = 0; i < (maxn >> 1); ++i ){
            v[i].clear();
        }

        int n;
        scanf("%d",&n);
        for (int i = 0; i < n; ++i){
            int x,y,z,x1,y1,z1;
            scanf("%d %d %d %d %d %d", &x, &y, &z, &x1, &y1, &z1);
            for (int j = z; j < z1; ++j){
                v[j + 500].push_back(RECT(x,y,x1,y1));
            }
        }
        long long ans = 0;
        for (int i = 0; i <= 1000; ++i){
            ans += solve(i);
        }
        printf("Case %d: %I64d\n", ++ks, ans);
    }



    return 0;
}

Get The Treasury

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


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 x 1, y 1, z 1, x 2, y 2 and z 2 (x 1<x 2, y 1<y 2, z 1<z 2). 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 x 1 to x 2. 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 x 1, y 1, z 1, x 2, y 2 and z 2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, 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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值