题解:
求最小曼哈顿距离,可以用给定范围的最大立方体的八个顶点建立八个树状数组,分别维护距离该顶点的曼哈顿距离的最大值。
出题人给的正解不是这种方法,效率更高,但是树状数组常数很小,也能达到令人满意的时间消耗。
代码:
#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.
*/