Please subscribe to the official Codeforces channel in Telegram via the link: https://t.me/codeforces_official. ×

### nikizakr's blog

By nikizakr, history, 8 months ago, ,

Hi Codeforces :D I'm looking for some problems about 3D prefix sum can you provide some links thanks a lot

•
• +4
•

 » 8 months ago, # | ← Rev. 4 →   +3 After writing this comment I realized that your post was asking about problems rather than requesting a tutorial :). I hope someone will find this useful anyway.Well, I think I could explain it right there. So, for 1D prefix sums you simply do S[i + 1] = S[i] + a[i], nothing special here. For 2D, on the other side, you have to subtract overlapping region: S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + a[i][j]. For 3D there will be even more overlapping: S[i][j][k] = S[i - 1][j][k] + S[i][j - 1][k] + S[i][j][k - 1] - S[i - 1][j - 1][k] - S[i][j - 1][k - 1] - S[i - 1][j][k - 1] + S[i - 1][j - 1][k - 1] + a[i][j][k]. You can find similarity with inclusion-exclusion formulas. So, with adding new dimensions it becomes more burdensome exponentially. You can check out this code: Code#include using namespace std; int main() { vector>> a = {{{1, 0, 1}, {0, 0, 0}, {1, 1, 1}}, {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}; vector>> S(4, vector>(4, vector(4))); for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) for (int k = 1; k <= 3; k++) S[i][j][k] = S[i - 1][j][k] + S[i][j - 1][k] + S[i][j][k - 1] - S[i - 1][j - 1][k] - S[i][j - 1][k - 1] - S[i - 1][j][k - 1] + S[i - 1][j - 1][k - 1] + a[i - 1][j - 1][k - 1]; cout << S[3][3][3] << endl;//Entire matrix cout << S[2][3][3] - S[1][3][3] << endl;//Middle 2D plane cout << S[2][2][2] - S[1][2][2] - S[2][1][2] - S[2][2][1] + S[1][1][2] + S[2][1][1] + S[1][2][1] - S[1][1][1] << endl;//Middle element (at (2, 2, 2)) return 0; } 
•  » » 8 months ago, # ^ |   +9 The formula for two dimensions is wrong. I think it's a typo.The correct formula is S[i][j] = S[i][j-1] + S[i-1][j] - S[i-1][j-1] + A[i][j]. You wrote S[i][j+1] instead of S[i-1][j].
•  » » » 8 months ago, # ^ |   +3 Thanks, corrected that.
 » 8 months ago, # |   0 Here's a task from UVa online judge 10755 — Garbage Heap.
 » 8 months ago, # |   0 You are given a non-empty, zero-indexed array A of n (1 <= n <= 100 000) integers a[0], a[1], ... , a[n−1] (0 <= a[i] <= 1 000). This array represents number of mushrooms growing on the consecutive spots along a road. You are also given integers k and m (0 <= k, m < n). A mushroom picker is at spot number k on the road and should perform m moves. In one move she moves to an adjacent spot. She collects all the mushrooms growing on spots she visits. The goal is to calculate the maximum number of mushrooms that the mushroom picker can collect in m moves. For example, consider array A such that: 2 3 7 5 1 3 9 0 1 2 3 4 5 6 The mushroom picker starts at spot k = 4 and should perform m = 6 moves. She might move to spots 3, 2, 3, 4, 5, 6 and thereby collect 1 + 5 + 7 + 3 + 9 = 25 mushrooms. This is the maximal number of mushrooms she can collect. Solution (Python)1 def mushrooms(A, k, m): 2 n = len(A) 3 result = 0 4 pref = prefix_sums(A) 5 for p in xrange(min(m, k) + 1): 6 left_pos = k - p 7 right_pos = min(n - 1, max(k, k + m - 2 * p)) 8 result = max(result, count_total(pref, left_pos, right_pos)) 9 for p in xrange(min(m + 1, n - k)): 10 right_pos = k + p 11 left_pos = max(0, min(k, k - (m - 2 * p))) 12 result = max(result, count_total(pref, left_pos, right_pos)) 13 return result