templates/prefix-sum-2d.cpp
compilable
$cat templates/prefix-sum-2d
Prefix Sum 2D
2D prefix sum for O(1) rectangle sum queries on a static grid.
#prefix-sum#2d#range-query#static-grid#inclusion-exclusion
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1005;
ll psum[MAXN][MAXN];
int N, M;
void build(ll a[][MAXN]) {
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
psum[i][j] = a[i][j] + psum[i-1][j] + psum[i][j-1] - psum[i-1][j-1];
}
ll query(int x1, int y1, int x2, int y2) {
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
return psum[x2][y2] - psum[x1-1][y2] - psum[x2][y1-1] + psum[x1-1][y1-1];
}19 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)
2D Prefix Sum
Preprocess a static grid once, then answer any rectangle sum query in .
Building
Construct prefix matrix where is the sum of all elements in the sub-grid from to :
Query
Sum over rectangle from to using inclusion-exclusion: