2019牛客暑期多校训练营(第八场)D Distance (三维树状数组)

 

题目链接

题解:

求最小曼哈顿距离,可以用给定范围的最大立方体的八个顶点建立八个树状数组,分别维护距离该顶点的曼哈顿距离的最大值。

出题人给的正解不是这种方法,效率更高,但是树状数组常数很小,也能达到令人满意的时间消耗。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll
#define PI acos(-1.0)
#define INF 0x3f3f3f3f3f3f3f3f
#define P pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0)
const int mod = 998244353;
const int M = 1000000 + 10;
const int N = 200000 + 10;

int n, m, h, q;
int tree[10][N];

inline void add(int* a, int x, int y, int z, int val)
{
    for(int i = x; i <= n; i += i & -i) {
        for(int j = y; j <= m; j += j & -j) {
            for(int k = z; k <= h; k += k & -k) {
                a[i*m*h+j*h+k] = max(val, a[i*m*h+j*h+k]);
            }
        }
    }
}

inline int query(int* a, int x, int y, int z)
{
    int ans = -INF;
    for(int i = x; i; i -= i & -i) {
        for(int j = y; j; j -= j & -j) {
            for(int k = z; k; k -= k & -k) {
                ans = max(ans, a[i*m*h+j*h+k]);
            }
        }
    }
    return ans;
}

int op, x, y, z;

signed main()
{
    scanf("%lld %lld %lld %lld", &n, &m, &h, &q);
    while(q --) {
        scanf("%lld %lld %lld %lld", &op, &x, &y, &z);
        if(op == 1) {
            add(tree[1], x, y, z, x + y + z);
            add(tree[2], n + 1 - x, y, z, n + 1 - x + y + z);
            add(tree[3], n - x + 1, m - y + 1, z, n + m + 2 - x - y + z);
            add(tree[4], x, m - y + 1, z, m + 1 + x - y + z);
            add(tree[5], x, y, h - z + 1, h + 1 + x + y - z);
            add(tree[6], n - x + 1, y, h - z + 1, n + h + 2 - x + y - z);
            add(tree[7], n - x + 1, m - y + 1, h - z + 1, n + m + h - 3 - x - y - z);
            add(tree[8], x, m - y + 1, h - z + 1, m + h + 2 + x - y - z);
        } else {
            int ans = INF, temp;
            temp = query(tree[1], x, y, z);
            if(temp) ans = min(ans, x + y + z - temp);
            temp = query(tree[2], n + 1 - x, y, z);
            if(temp) ans = min(ans, n + 1 - x + y + z - temp);
            temp = query(tree[3], n - x + 1, m - y + 1, z);
            if(temp) ans = min(ans, n + m + 2 - x - y + z - temp);
            temp = query(tree[4], x, m - y + 1, z);
            if(temp) ans = min(ans, m + 1 + x - y + z - temp);
            temp = query(tree[5], x, y, h - z + 1);
            if(temp) ans = min(ans, h + 1 + x + y - z - temp);
            temp = query(tree[6], n - x + 1, y, h - z + 1);
            if(temp) ans = min(ans, n + h + 2 - x + y - z - temp);
            temp = query(tree[7], n - x + 1, m - y + 1, h - z + 1);
            if(temp) ans = min(ans, n + m + h - 3 - x - y - z - temp);
            temp = query(tree[8], x, m - y + 1, h - z + 1);
            if(temp) ans = min(ans, m + h + 2 + x - y - z - temp);
            printf("%lld\n", ans);
        }
    }

    return 0;
}

/*

  Rejoicing in hope, patient in tribulation.

*/

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值