Prefix sums in two dimensions
Use inclusion-exclusion for rectangles and marked-cell counts.
A prefix is now a corner rectangle
Define P[r][c] as the sum of rows 1 through r and columns 1 through c. Add a zero row and zero column to represent empty rectangles. To construct a cell, add the current value, the prefix above, and the prefix to the left, then subtract their shared upper-left overlap.
The recurrence is P[r][c] = a[r][c] + P[r − 1][c] + P[r][c − 1] − P[r − 1][c − 1]. Row-major order works because all three earlier entries are ready. An R-by-C matrix needs (R + 1)(C + 1) prefix entries; memory depends on the product of dimensions.
Cut off two strips and restore the overlap
For inclusive corners (r1, c1) and (r2, c2), start with P[r2][c2]. Subtract P[r1 − 1][c2] for the rows above and P[r2][c1 − 1] for the columns left. Their upper-left intersection was removed twice, so add P[r1 − 1][c1 − 1] once.
Build time is O(RC), each query is O(1), and all work is O(RC + q). Numeric sums use long long. Zero padding handles top and left boundaries without special cases.
// Complete C++23 program. Input: rows cols q, matrix, then r1 c1 r2 c2.
// Valid inclusive rectangles; rows,cols <= 500; |cell| <= 1e9.
#include <iostream>
#include <vector>
int main() {
int rows, cols, q;
std::cin >> rows >> cols >> q;
std::vector<std::vector<long long>> pref(
rows + 1, std::vector<long long>(cols + 1, 0));
for (int r = 1; r <= rows; ++r) {
for (int c = 1; c <= cols; ++c) {
long long value;
std::cin >> value;
pref[r][c] = value + pref[r - 1][c] + pref[r][c - 1]
- pref[r - 1][c - 1];
}
}
while (q--) {
int r1, c1, r2, c2;
std::cin >> r1 >> c1 >> r2 >> c2;
std::cout << pref[r2][c2] - pref[r1 - 1][c2]
- pref[r2][c1 - 1] + pref[r1 - 1][c1 - 1] << '\n';
}
}A marked grid is another indicator sum
For a grid of # and ., use 1 for a marked cell and 0 otherwise. The same rectangle sum counts marked cells. With at most 500 × 500 cells, a count fits int; the input row string remains zero-based, so one-based column c uses row[c − 1].
Test a single cell, the full grid, top-row and left-column queries, and one-row or one-column matrices. Include nonsquare grids so swapped row and column dimensions cannot hide.
Why is the upper-left overlap added back in a rectangle query?
Turn the idea into code
For rows [2,1,−1,3], [0,4,2,−2], [5,−3,1,2], find the sum of rows 2..3 and columns 2..4. Verify directly and with prefix corners.
Give me a hint
The desired cells are six values. The four needed prefix totals are 14, 5, 7, and 2.
Show the explained solution
Directly: 4 + 2 − 2 − 3 + 1 + 2 = 4. Inclusion-exclusion gives 14 − 5 − 7 + 2 = 4. Agreement verifies both the chosen rectangle and the corner formula.
Your notes
Mark complete when you can explain it yourself.