NeFut Logo NeFut
Admin Login

Efficient Prefix Sum Algorithm and Its Application in 2D Matrices

Published at: 2026-05-27 09:17 Last updated: 2026-06-21 08:25
#algorithm #optimization #Data Structure

Core Logic and Mathematical Principles

Prefix sum is an efficient preprocessing technique whose physical essence is the application of the Inclusion-Exclusion Principle to discrete grid integration. By trading space for time, this method reduces the query complexity of interval or region sums from $O(N)$ or $O(N \times M)$ in brute-force enumeration down to $O(1)$.

Prefix sum is responsible for "discrete integration" (storing global cumulative quantities), while interval queries must utilize the "Inclusion-Exclusion Principle" for spatial geometric addition and subtraction corrections to eliminate irrelevant regions.

In one-dimensional space, prefix sum is defined as the cumulative sum of the first $i$ elements of a sequence. For the original array $A$, its prefix sum array $S$ satisfies:

$$S[i] = \sum_{k=1}^{i} A[k]$$

Using the discretized form of the Fundamental Theorem of Calculus, the contiguous subarray sum over interval $[l, r]$ can be directly computed via the difference of two prefix sums:

$$\sum_{k=l}^{r} A[k] = S[r] - S[l-1]$$

In two-dimensional space, prefix sum extends to matrix region sums. Define $S[i][j]$ as the algebraic sum of all elements within the submatrix with $(1,1)$ as the top-left corner and $(i,j)$ as the bottom-right corner. The element sum over any rectangular region $[x_1, y_1] \sim [x_2, y_2]$ is essentially a definite integral over a two-dimensional plane. Through geometric overlapping and trimming, multi-dimensional spatial boundaries can be partitioned, allowing the extraction of arbitrary submatrices in constant time using the Inclusion-Exclusion Principle.


State Design and Algorithm Derivation

1. State Recurrence for 2D Prefix Sum

Define state $S[i][j]$ as the sum of the submatrix $\sum_{u=1}^{i} \sum_{v=1}^{j} A[u][v]$. When $S[i-1][j]$ (the upper rectangle), $S[i][j-1]$ (the left rectangle), and $S[i-1][j-1]$ (the diagonally overlapping rectangle) are known, the addition of the current cell $A[i][j]$ causes the overlapping region to be counted twice. According to the Inclusion-Exclusion Principle, the recurrence equation for the current state is:

$$S[i][j] = S[i-1][j] + S[i][j-1] - S[i-1][j-1] + A[i][j]$$

2. Derivation of $O(1)$ Matrix Region Sum Query

Given the top-left coordinates $(x_1, y_1)$ and bottom-right coordinates $(x_2, y_2)$ of the query submatrix. The target region sum is equivalent to the total matrix with $(x_2, y_2)$ as the bottom-right corner, minus the upper irrelevant region $[1, x_1-1] \times [1, y_2]$, minus the left irrelevant region $[1, x_2] \times [1, y_1-1]$. At this point, the top-left block $[1, x_1-1] \times [1, y_1-1]$ has been subtracted twice and must be added back once. Therefore, the precise mathematical expression for any submatrix region sum is:

$$\text{Sum}(x_1, y_1, x_2, y_2) = S[x_2][y_2] - S[x_1-1][y_2] - S[x_2][y_1-1] + S[x_1-1][y_1-1]$$


Template

2D Prefix Sum

// 1. Preprocessing phase: Build the 2D prefix sum array, time complexity O(N * M)
for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
        s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    }
}

// 2. Query phase: Use the Inclusion-Exclusion Principle to compute the sum of submatrix (x1, y1) to (x2, y2) in O(1)
long long ans = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];

NOIP Practical Pitfall Guide

Even if the original matrix elements satisfy $A[i][j] \le 10^5$, when the matrix scale reaches $10^3 \times 10^3$, the maximum prefix sum can reach $10^{11}$, far exceeding the $2 \times 10^9$ upper limit of the int type. The prefix sum array s and query result variables must be forcefully declared as long long. When performing large-modulo prefix sums (e.g., modulo $10^9+7$), because the Inclusion-Exclusion formula contains subtraction terms, computing (s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1]) % MOD can easily produce negative results. The reason for producing negative numbers is that once the prefix sum array s is taken modulo during preprocessing, the array no longer stores the true "total sum" but rather the "remainder." You must use (ans % MOD + MOD) % MOD to normalize the result to a positive value.

If you are accustomed to using 0-indexed arrays, the $i-1$ and $j-1$ in the recurrence equation will trigger severe memory out-of-bounds access (Segmentation Fault) when $i=0$ or $j=0$. Adding an if(i > 0) check inside the loop would disrupt the modern CPU's branch prediction and pipelining optimization, significantly reducing execution speed. The standard engineering specification is to forcefully use 1-indexed arrays, setting all elements in the 0th row and 0th column to zero as natural boundary sentinels.


Classic NOIP/Luogu Problems

1. Luogu P2280 [HNOI2003] Laser Bomb

2. Luogu P1387 Largest Square


[h] Back to Home