3D 数组的前缀和
先决条件:前缀和 - 1D ,前缀和 -2D
给定一个由整数A[L][R][C]组成的 3 维数组,其中L、R和C是数组的维数(层、行、列)。找到它的前缀和 3d 数组。设前缀和 3d 数组为pre[L][R][C]。这里pre[k][i][j]给出了pre[0][0][0]和pre[k][i][j]之间所有整数的总和(包括两者)。
例子:
Input: A[L][R][C] = {
{{1, 1, 1, 1}, //Layer 1
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}},
{{1, 1, 1, 1}, //Layer 2
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}},
{{1, 1, 1, 1}, //Layer 3
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}},
{{1, 1, 1, 1}, //Layer 4
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}}
}
Output: pre[L][R][C]
Layer 1:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Layer 2:
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32
Layer 3:
3 6 9 12
6 12 18 24
9 18 27 36
12 24 36 48
Layer 4:
4 8 12 16
8 16 24 32
12 24 36 48
16 32 48 64
方法:考虑上图来理解单元格pre[0][0][0]位于 x,y,z 轴的原点。要填充pre[][][]数组,请执行以下步骤以计算前缀和。
- (0, 0, 0) 处的元素被直接填充。前[0][0][0] = A[0][0][0]
- 使用一维数组上的前缀和填充三个边的单元格(平行于 x、y、z 轴并由单元格组成)。这些边有共同的元素pre[0][0][0]
- 在 for 循环[1, L]中迭代以计算其中一个轴的前缀和。 pre[i][0][0] = pre[i – 1][0][0] + A[i][0][0];
- 同样,在范围[1. R]和[1, C]来计算其他两个轴的前缀和。
- 使用二维数组上的前缀和填充三个边的单元格(平行于 xy、yz、zx 平面并由单元格组成)。这些边有共同的元素pre[0][0][0] 。
- 在 for 循环中迭代[1, L]以计算二维数组的前缀和。
- 在 for 循环中迭代[1, R]以计算二维数组的前缀和。
- 进行以下操作。 pre[k][i][0] = A[k][i][0] + pre[k – 1][i][0] + pre[k][i – 1][0] – pre[ k – 1][i – 1][0]
- 在 for 循环中迭代[1, R]以计算二维数组的前缀和。
- 类似地,计算其他 2 边或其他二维数组的前缀和。
- 在 for 循环中迭代[1, L]以计算三维数组pre[][][] 的前缀和。
- 在 for 循环中迭代[1, R]以计算三维数组pre[][][] 的前缀和。
- 在 for 循环[1, C]中迭代以计算三维数组pre[][][] 的前缀和。
- 进行以下操作。 pre[k][i][j] = A[k][i][j] + pre[k – 1][i][j] + pre[k][i – 1][j] + pre[ k][i][j – 1] – pre[k – 1][i – 1][j] – pre[k][i – 1][j – 1] – pre[k – 1][i] [j – 1] + pre[k – 1][i – 1][j – 1]
- 在 for 循环[1, C]中迭代以计算三维数组pre[][][] 的前缀和。
- 在 for 循环中迭代[1, R]以计算三维数组pre[][][] 的前缀和。
- 最后,打印三维数组pre[][][]。
下面是上述方法的实现。
C++
// C++ program for the above approach.
#include
using namespace std;
// Declaring size of the array
#define L 4 // Layer
#define R 4 // Row
#define C 4 // Column
// Calculating prefix sum array
void prefixSum3d(int A[L][R][C])
{
int pre[L][R][C];
// Step 0:
pre[0][0][0] = A[0][0][0];
// Step 1: Filling the first row,
// column, and pile of ceils.
// Using prefix sum of 1d array
for (int i = 1; i < L; i++)
pre[i][0][0] = pre[i - 1][0][0] + A[i][0][0];
for (int i = 1; i < R; i++)
pre[0][i][0] = pre[0][i - 1][0] + A[0][i][0];
for (int i = 1; i < C; i++)
pre[0][0][i] = pre[0][0][i - 1] + A[0][0][i];
// Step 2: Filling the cells
// of sides(made up using cells)
// which have common element A[0][0][0].
// using prefix sum on 2d array
for (int k = 1; k < L; k++) {
for (int i = 1; i < R; i++) {
pre[k][i][0] = A[k][i][0]
+ pre[k - 1][i][0]
+ pre[k][i - 1][0]
- pre[k - 1][i - 1][0];
}
}
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
pre[0][i][j] = A[0][i][j]
+ pre[0][i - 1][j]
+ pre[0][i][j - 1]
- pre[0][i - 1][j - 1];
}
}
for (int j = 1; j < C; j++) {
for (int k = 1; k < L; k++) {
pre[k][0][j] = A[k][0][j]
+ pre[k - 1][0][j]
+ pre[k][0][j - 1]
- pre[k - 1][0][j - 1];
}
}
// Step 3: Filling value
// in remaining cells using formula
for (int k = 1; k < L; k++) {
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
pre[k][i][j] = A[k][i][j]
+ pre[k - 1][i][j]
+ pre[k][i - 1][j]
+ pre[k][i][j - 1]
- pre[k - 1][i - 1][j]
- pre[k][i - 1][j - 1]
- pre[k - 1][i][j - 1]
+ pre[k - 1][i - 1][j - 1];
}
}
}
// Displaying final prefix sum of array
for (int k = 0; k < L; k++) {
cout << "Layer " << k + 1 << ':' << endl;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << pre[k][i][j] << " ";
}
cout << endl;
}
cout << endl;
}
}
// Driver Code
int main()
{
int A[L][R][C] = {
{ { 1, 1, 1, 1 }, // Layer 1
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 2
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 3
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 4
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } }
};
prefixSum3d(A);
return 0;
}
Java
// java program to calculate prefix sum of 3d array
import java.util.*;
class GFG
{
// Declaring size of the array
public static int L = 4;// Layer
public static int R = 4;// Row
public static int C = 4;// Column
// Calculating prefix
public static void prefixSum3d(int A[][][]) {
int pre[][][] = new int[L][R][C];
// Step 0:
pre[0][0][0] = A[0][0][0];
// Step 1: Filling the first row,column, and pile of ceils.
// Using prefix sum of 1d array
for (int i = 1; i < L; i++)
pre[i][0][0] = pre[i - 1][0][0] + A[i][0][0];
for (int i = 1; i < R; i++)
pre[0][i][0] = pre[0][i - 1][0] + A[0][i][0];
for (int i = 1; i < C; i++)
pre[0][0][i] = pre[0][0][i - 1] + A[0][0][i];
// Step 2: Filling the cells of sides(made up using cells)
// which have common element A[0][0][0].
// using prefix sum on 2d array
for (int k = 1; k < L; k++) {
for (int i = 1; i < R; i++) {
pre[k][i][0] = A[k][i][0] + pre[k - 1][i][0] + pre[k][i - 1][0]
- pre[k - 1][i - 1][0];
}
}
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
pre[0][i][j] = A[0][i][j] + pre[0][i - 1][j] + pre[0][i][j - 1]
- pre[0][i - 1][j - 1];
}
}
for (int j = 1; j < C; j++) {
for (int k = 1; k < L; k++) {
pre[k][0][j] = A[k][0][j] + pre[k - 1][0][j] + pre[k][0][j - 1]
- pre[k - 1][0][j - 1];
}
}
// Step 3: Filling value in remaining cells using formula
for (int k = 1; k < L; k++) {
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
pre[k][i][j] = A[k][i][j]
+ pre[k - 1][i][j] + pre[k][i - 1][j] + pre[k][i][j - 1]
- pre[k - 1][i - 1][j] - pre[k][i - 1][j - 1] - pre[k - 1][i][j - 1]
+ pre[k - 1][i - 1][j - 1];
}
}
}
// Displaying final prefix sum of array
for (int k = 0; k < L; k++) {
System.out.println("Layer " + (k + 1) + ":");
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
System.out.print(pre[k][i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}
// Driver program to test above function
public static void main(String[] args) {
int A[][][] = {
{{1, 1, 1, 1}, // Layer 1
{1, 1, 1, 1 },
{1, 1, 1, 1 },
{1, 1, 1, 1 }},
{{1, 1, 1, 1}, // Layer 2
{1, 1, 1, 1 },
{1, 1, 1, 1 },
{1, 1, 1, 1 }},
{{1, 1, 1, 1}, // Layer 3
{1, 1, 1, 1 },
{1, 1, 1, 1 },
{1, 1, 1, 1 }},
{{1, 1, 1, 1}, // Layer 4
{1, 1, 1, 1 },
{1, 1, 1, 1 },
{1, 1, 1, 1 }}
};
prefixSum3d(A);
}
}
// This code is contributed by gajjardeep50.
Python3
# python program to calculate prefix sum of 3d array
# Declaring size of the array
L = 4 # Layer
R = 4 # Row
C = 4 # Column
# Calculating prefix
def prefixSum3d(A):
pre = [[[0 for a in range(C)]
for b in range(R)]
for d in range(L)]
# Step 0:
pre[0][0][0] = A[0][0][0]
# Step 1: Filling the first row,column, and pile of ceils.
# Using prefix sum of 1d array
for i in range(1, L):
pre[i][0][0] = pre[i - 1][0][0] + A[i][0][0]
for i in range(1, R):
pre[0][i][0] = pre[0][i - 1][0] + A[0][i][0]
for i in range(1, C):
pre[0][0][i] = pre[0][0][i - 1] + A[0][0][i]
# Step 2: Filling the cells of sides(made up using cells)
# which have common element A[0][0][0].
# using prefix sum on 2d array
for k in range(1, L):
for i in range(1, R):
pre[k][i][0] = (A[k][i][0] + pre[k - 1][i][0] + pre[k][i - 1][0]
- pre[k - 1][i - 1][0])
for i in range(1, R):
for j in range(1, C):
pre[0][i][j] = (A[0][i][j] + pre[0][i - 1][j] + pre[0][i][j - 1]
- pre[0][i - 1][j - 1])
for j in range(1, C):
for k in range(1, L):
pre[k][0][j] = (A[k][0][j] + pre[k - 1][0][j] + pre[k][0][j - 1]
- pre[k - 1][0][j - 1])
# Step 3: Filling value in remaining cells using formula
for k in range(1, L):
for i in range(1, R):
for j in range(1, C):
pre[k][i][j] = (A[k][i][j]
+ pre[k - 1][i][j]
+ pre[k][i - 1][j]
+ pre[k][i][j - 1]
- pre[k - 1][i - 1][j]
- pre[k][i - 1][j - 1]
- pre[k - 1][i][j - 1]
+ pre[k - 1][i - 1][j - 1])
# Displaying final prefix sum of array
for k in range(L):
print("Layer", k+1, ":")
for i in range(R):
for j in range(C):
print(pre[k][i][j], end=' ')
print()
print()
# Driver program to test above function
A = [
[[1, 1, 1, 1], # Layer 1
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1], # Layer 2
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1], # Layer 3
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1], # Layer 4
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
]
prefixSum3d(A)
# This code is contributed by gajjardeep50.
C#
// C# program for the above approach.
using System;
using System.Collections.Generic;
class GFG{
// Declaring size of the array
static int L = 4; // Layer
static int R = 4; // Row
static int C = 4; // Column
// Calculating prefix sum array
static void prefixSum3d(int [,,]A)
{
int [,,]pre = new int[L, R, C];
// Step 0:
pre[0, 0, 0] = A[0, 0, 0];
// Step 1: Filling the first row,
// column, and pile of ceils.
// Using prefix sum of 1d array
for(int i = 1; i < L; i++)
pre[i, 0, 0] = pre[i - 1, 0, 0] + A[i, 0, 0];
for(int i = 1; i < R; i++)
pre[0, i, 0] = pre[0, i - 1, 0] + A[0, i, 0];
for(int i = 1; i < C; i++)
pre[0, 0, i] = pre[0, 0, i - 1] + A[0, 0, i];
// Step 2: Filling the cells
// of sides(made up using cells)
// which have common element A[0][0][0].
// using prefix sum on 2d array
for(int k = 1; k < L; k++)
{
for(int i = 1; i < R; i++)
{
pre[k, i, 0] = A[k, i, 0] + pre[k - 1, i, 0] +
pre[k, i - 1, 0] -
pre[k - 1, i - 1, 0];
}
}
for(int i = 1; i < R; i++)
{
for(int j = 1; j < C; j++)
{
pre[0, i, j] = A[0, i, j] + pre[0, i - 1, j] +
pre[0, i, j - 1] -
pre[0, i - 1, j - 1];
}
}
for(int j = 1; j < C; j++)
{
for(int k = 1; k < L; k++)
{
pre[k, 0, j] = A[k, 0, j] + pre[k - 1, 0, j] +
pre[k, 0, j - 1] -
pre[k - 1, 0, j - 1];
}
}
// Step 3: Filling value
// in remaining cells using formula
for(int k = 1; k < L; k++)
{
for(int i = 1; i < R; i++)
{
for(int j = 1; j < C; j++)
{
pre[k, i, j] = A[k, i, j] + pre[k - 1, i, j] +
pre[k, i - 1, j] +
pre[k, i, j - 1] -
pre[k - 1, i - 1, j] -
pre[k, i - 1, j - 1] -
pre[k - 1, i, j - 1] +
pre[k - 1, i - 1, j - 1];
}
}
}
// Displaying final prefix sum of array
for(int k = 0; k < L; k++)
{
Console.WriteLine("Layer " + k + 1 + ":");
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
Console.Write(pre[k, i, j] +" ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
int [,,]A = {
{ { 1, 1, 1, 1 }, // Layer 1
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 2
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 3
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } },
{ { 1, 1, 1, 1 }, // Layer 4
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } }
};
prefixSum3d(A);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
Layer 1:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Layer 2:
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32
Layer 3:
3 6 9 12
6 12 18 24
9 18 27 36
12 24 36 48
Layer 4:
4 8 12 16
8 16 24 32
12 24 36 48
16 32 48 64
时间复杂度:O(L*R*C)
辅助空间:O(L*R*C)