我试图了解如何将我的2D细分树代码应用于更高的维度,尤其是3D。因为在教程中说过,该2D范围树可以轻松转换为更高的维度。我在做什么错了?
我的代码如下:
private void build_Y(int vz, int lz, int rz, int vx, int lx, int rx, int vy, int ly, int ry, int a[][][]){
if (ly == ry){
if (lx == rx && lz == rz){
t[vz][vx][vy] = a[lz][lx][ly];
}
else if (lx == rx && lz != rz){
t[vz][vx][vy] = t[2 * vz][vx][vy] + t[2 * vz + 1][vx][vy];
}
else if (lx != rx && lz == rz){
t[vz][vx][vy] = t[vz][2 * vx][vy] + t[vz][2 * vx + 1][vy];
}
else if (lx != rx && lz != rz){
t[vz][vx][vy] = t[2 * vz][2 * vx][vy] + t[2 * vz + 1][2 * vx + 1][vy];
}
}else{
int vmiddleY = (ly + ry) / 2;
build_Y(vz, lz, rz, vx, lx, rx,2 * vy, ly, vmiddleY, a);
build_Y(vz, lz, rz, vx, lx, rx,2 * vy + 1, vmiddleY + 1, ry, a);
t[vz][vx][vy] = t[vz][vx][2 * vy] + t[vz][vx][2 * vy + 1];
}
}
public void build_X(int vz, int lz, int rz, int vx, int lx, int rx, int a[][][]){
if (lx != rx){
int vmiddleX = (lx + rx) / 2;
build_X(vz, lz, rz, 2 * vx, lx, vmiddleX, a);
build_X(vz, lz, rz, 2 * vx + 1, vmiddleX + 1, rx, a);
}
build_Y(vz, lz, rz, vx, lx, rx, 1, 0, 2, a);
}
public void build_Z(int vz, int lz, int rz, int a[][][]){
if (lz != rz){
int vmiddleZ = (lz + rz) / 2;
build_Z(2 * vz, lz, vmiddleZ, a);
build_Z(2 * vz + 1, vmiddleZ + 1, rz, a);
}
build_X(vz, lz, rz, 1, 0, 2, a);
}
测试数组为:
int[][][] a = {
{ {0, 1, 0}, {0, 0, 1}, {1, 0, 1} },
{ {0, 1, 0}, {0, 0, 1}, {1, 0, 1} },
{ {0, 0, 0}, {0, 0, 1}, {1, 0, 1} },
};
我的代码给出的结果是总和= 6,但应为11。