2019牛客多校赛第8场 D Distance 三维树状数组

本文介绍了一种使用三维树状数组解决在立方体空间中计算曼哈顿距离的问题。题目要求根据给定的操作(添加标签或查询距离)来找出所有查询的最小曼哈顿距离。通过在立方体的8个角建立三维树状数组并维护前缀最大值,可以高效地回答查询。

链接:https://ac.nowcoder.com/acm/contest/888/D
来源:牛客网
题目:

Gromah and LZR have entered the fourth level. There is a blank cube with size  n×m×h hanging on the wall.

Gromah soon finds a list beside the cube, there are q​ instructions in the list and each instruction is in one of the following two formats:

1. (1,x,y,z)​, meaning to add a tag on position(x,y,z)​ in the cube

2. (2,x,y,z)​, meaning to determine the minimum Manhattan Distance to the given position (x,y,z)​ among all tagged positions

Manhattan Distance between two positions (x1,y1,z1),(x2,y2,z2) is defined as ∣x1−x2∣+∣y1−y2∣+∣z1−z2∣。

LZR also finds a note board saying that the password of this level is the sequence made up of all results of the instructions in format 2.

Please help them get all results of the instructions in format 2.

输入描述:

The first line contains four positive integers n,m,h,q denoting the sizes in three dimensions of the cube and the number of instructions.

Following q​ lines each contains four positive integers op,x,y,z where op=1 means to add a tag on (x,y,z) while op=2 means to make a query on (x,y,z).

1<=n*m*h,q<=1e5.1<=x<=n.1<=y<=m,1<=z<=h;

It is guaranteed that the first instruction is in format 1 and that no position will be tagged more than once.

输出描述:

For each instruction in format 2, output the answer in one line.

示例1

输入

3 3 3 4
1 1 1 1
2 2 3 3
1 3 1 1
2 3 3 2

输出

5
3

说明

For the first query, there is only one tagged position (1,1,1)currently, so the answer is ∣1−2∣+∣1−3∣+∣1−3∣=5

For the second query, (3,1,1) is the nearest tagged position, so the answer is ∣3−3∣+∣1−3∣+∣1−2∣=3.

题解:

题意: 有一个n*m*h的立体空间 1<=n*m*h<=1e5; 现在有两种操作

1。在 (x,y,z) 这个位置添加一个点

2. 求坐标(x,y,z)到空间上其他点最小的曼哈顿距离。

(x,y,z)到(x2,y2,z2)的曼哈顿距离为 |x2-x|+|y2-y|+|z2-z|.

题解:我们可以以这个立体空间的8个角分别为原点坐标建立8个三维树状数组,树状数组维护前缀最大值,

那么每次枚举8个方向就可以了。

这道题可以动态开三维数组或者定义一个一维的,把三维数组压缩成一维。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,h;
struct node{
    int*** bit;
    void init(){
        bit=new int** [n+1];
        for(int i=0;i<=n;i++){
            bit[i]=new int*[m+1];
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                bit[i][j]=new int[h+1];
            }
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                for(int k=0;k<=h;k++) bit[i][j][k]=0;
            }
        }
    }
    int lowbit(int x){ return x&-x;}
    void update(int x,int y,int z,int w){
        for(int i=x;i<=n;i+=lowbit(i)){
            for(int j=y;j<=m;j+=lowbit(j)){
                for(int k=z;k<=h;k+=lowbit(k)){
                    bit[i][j][k]=max(bit[i][j][k],w);
                }
            }
        }
    }
    int query(int x,int y,int z){
        int ans=0;
        for(int i=x;i;i-=lowbit(i)){
            for(int j=y;j;j-=lowbit(j)){
                for(int k=z;k;k-=lowbit(k)){
                   ans=max(ans,bit[i][j][k]);
                }
            }
        }
        return ans;
    }
}a[8];
int main(){
    int q;
    scanf("%d%d%d%d",&n,&m,&h,&q);
    for(int i=0;i<=7;i++) a[i].init();
    while(q--){
        int op,x,y,z;
        scanf("%d%d%d%d",&op,&x,&y,&z);
        if(op==1){
            a[0].update(x,y,z,x+y+z);
            a[1].update(x,y,h+1-z,x+y-z+h+1);
            a[2].update(x,m+1-y,z,x-y+z+m+1);
            a[3].update(x,m+1-y,h+1-z,x-y-z+m+h+2);
            a[4].update(n+1-x,y,z,-x+y+z+n+1);
            a[5].update(n+1-x,y,h+1-z,-x+y-z+n+h+2);
            a[6].update(n+1-x,m+1-y,z,-x-y+z+n+m+2);
            a[7].update(n+1-x,m+1-y,h+1-z,-x-y-z+n+m+h+3);
        }else{
            int ans=1000000;
            int cnt=a[0].query(x,y,z);
            if(cnt!=0) ans=min(ans,x+y+z-cnt);  //cnt!=0 说明这个方向上有点。
            cnt=a[1].query(x,y,h+1-z);
            if(cnt!=0) ans=min(ans,x+y-z+h+1-cnt);
            cnt=a[2].query(x,m+1-y,z);
           if(cnt!=0)  ans=min(ans,x-y+z+m+1-cnt);
            cnt=a[3].query(x,m+1-y,h+1-z);
            if(cnt!=0) ans=min(ans,x-y-z+m+h+2-cnt);
            cnt=a[4].query(n+1-x,y,z);
            if(cnt!=0) ans=min(ans,-x+y+z+n+1-cnt);
           cnt=a[5].query(n+1-x,y,h+1-z);
            if(cnt!=0) ans=min(ans,-x+y-z+n+h+2-cnt);
            cnt=a[6].query(n+1-x,m+1-y,z);
            if(cnt!=0) ans=min(ans,-x-y+z+n+m+2-cnt);
           cnt=a[7].query(n+1-x,m+1-y,h+1-z);
           if(cnt!=0)  ans=min(ans,-x-y-z+n+m+h+3-cnt);
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值