More on Prefix Sums
Authors: Darren Yao, Eric Wei, Neo Wang
Prerequisites
Max subarray sum, prefix sums in two dimensions, and a more complicated example.
Max Subarray Sum
Focus Problem – read through this problem before continuing!
This problem has a solution known as Kadane's Algorithm. Please don't use that solution; try to solve it with prefix sums.
Why are the two methods equivalent?
2D Prefix Sums
Focus Problem – read through this problem before continuing!
Now, what if we wanted to process queries for the sum over a subrectangle of a 2D matrix with rows and columns? Let's assume both rows and columns are 1-indexed, and we use the following matrix as an example:
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 5 | 6 | 11 | 8 |
0 | 1 | 7 | 11 | 9 | 4 |
0 | 4 | 6 | 1 | 3 | 2 |
0 | 7 | 5 | 4 | 2 | 3 |
Naively, each sum query would then take time, for a total of . This is too slow.
Let's take the following example region, which we want to sum:
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 5 | 6 | 11 | 8 |
0 | 1 | 7 | 11 | 9 | 4 |
0 | 4 | 6 | 1 | 3 | 2 |
0 | 7 | 5 | 4 | 2 | 3 |
Manually summing all the cells, we have a submatrix sum of .
The first logical optimization would be to do one-dimensional prefix sums of each row. Then, we'd have the following row-prefix sum matrix. The desired subarray sum of each row in our desired region is simply the green cell minus the red cell in that respective row. We do this for each row to get .
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 6 | 12 | 23 | 31 |
0 | 1 | 8 | 19 | 28 | 32 |
0 | 4 | 10 | 11 | 14 | 16 |
0 | 7 | 12 | 16 | 18 | 21 |
Now, if we wanted to find a submatrix sum, we could break up the submatrix into a subarray for each row, and then add their sums, which would be calculated using the prefix sums method described earlier. Since the matrix has rows, the time complexity of this is . This might be fast enough for and , but we can do better.
In fact, we can do two-dimensional prefix sums. In our two dimensional prefix sum array, we have
This can be calculated as follows for row index and column index :
Let's calculate . Try playing with the interactive widget below by clicking the buttons to see which numbers are added in each step. Notice how we overcount a subrectangle, and how we fix this by subtracting .
The submatrix sum between rows and and columns and , can thus be expressed as follows:
Summing the blue region from above using the 2D prefix sums method, we add the value of the green square, subtract the values of the red squares, and then add the value of the gray square. In this example, we have
as expected.
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 6 | 12 | 23 | 31 |
0 | 2 | 14 | 31 | 51 | 63 |
0 | 6 | 24 | 42 | 65 | 79 |
0 | 13 | 36 | 58 | 83 | 100 |
Try playing with the interactive widget below by clicking the buttons to see which numbers are added in each step.
Since no matter the size of the submatrix we are summing, we only need to access four values of the 2D prefix sum array, this runs in per query after an preprocessing.
Solution - Forest Queries
Warning!
We need to be cautious of off-by-one errors, as intervals can be inclusive, exclusive, 1-indexed, etc.
C++
#include <bits/stdc++.h>using namespace std;#define vt vector#define FOR(i, a, b) for(int i = (a); i < (b); i++)#define FORE(i, a, b) for(int i = (a); i <= (b); i++)#define F0R(i, a) for(int i = 0; i < (a); i++)#define trav(a, x) for (auto& a : x)
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
Silver | Easy | Show Tags2D Prefix Sums | ||||
Silver | Hard | Show Tags2D Prefix Sums | External Sol | |||
Gold | Hard | Show Tags2D Prefix Sums, Max Subarray Sum | External Sol | |||
Plat | Very Hard | Show Tags2D Prefix Sums | External Sol |
A More Complicated Example
What if we want to quickly answer the following type of query?
Find .
In addition to taking prefix sums over , we'll also need to take prefix sums over .
First, define the following:
Then, we have:
And so,
Which is what we were looking for!
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
KS | Easy | Show TagsPrefix Sums | ||||
AC | Hard | Show TagsPrefix Sums | Check AC | |||
Plat | Insane | Show TagsPrefix Sums | External Sol |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!