Prefix sums
A prefix sum array stores at index i the sum of the first i elements (or first i+1, depending on convention). Any range sum a[l] + … + a[r] then becomes one subtraction: pref[r+1] - pref[l].
Detailed Explanation
You will be asked for the sum of many subarrays of the same array. Recomputing each sum is O(n) per query. One O(n) pass builds pref, and each query is O(1). That trade — O(n) extra memory, O(1) reads — is prefix sums.
The off-by-one convention that avoids branches: pref[0] = 0, pref[i+1] = pref[i] + a[i]. Then sum(l, r) inclusive is pref[r+1] - pref[l]. pref has n+1 entries. The empty prefix is zero. A range that starts at 0 does not need a special case.
You can overwrite a in place if you do not need the original: a[i] += a[i-1]. Inclusive sum [l, r] is then a[r] - (l ? a[l-1] : 0). The extra-element form is harder to get wrong.
Prefix sums generalise. Prefix XOR answers range XOR. Prefix counts of a predicate answer "how many evens in [l, r]". A 2D prefix table answers rectangle sums in O(1) after O(n²) setup: inclusion-exclusion of four corners.
The difference array is the inverse: to add v to a range [l, r] many times, you increment diff[l] by v and decrement diff[r+1] by v, then rebuild the array with a prefix sum at the end. n range updates become O(n) instead of O(n) each.
When the array has negatives, prefix sums still work; sliding windows often do not. "Count subarrays with sum equal to k" uses a map from prefix value to how many times it appeared: if pref[r] - pref[l] == k, then pref[l] == pref[r] - k. One pass, O(n) time, O(n) extra.
std::partial_sum in <numeric> writes the in-place inclusive form. std::exclusive_scan / inclusive_scan (C++17) are the named algorithms. Building pref by hand is three lines and is fine.
Overflow: int sums of ints wrap (undefined for signed). Use long long for pref when n * max(|a[i]|) can exceed 2³¹.
Code Examples
#include <cstddef>
#include <iostream>
#include <vector>
std::vector<long long> prefix(const std::vector<int>& a) {
std::vector<long long> pref(a.size() + 1);
for (std::size_t i = 0; i < a.size(); ++i) {
pref[i + 1] = pref[i] + a[i];
}
return pref;
}
long long rangeSum(const std::vector<long long>& pref, std::size_t l, std::size_t r) {
return pref[r + 1] - pref[l]; // inclusive [l, r]
}
int main() {
std::vector<int> a{2, 1, 3, 4};
auto pref = prefix(a);
std::cout << rangeSum(pref, 1, 3) << '\n'; // prints: 8 (1+3+4)
}#include <numeric>
#include <vector>
int main() {
std::vector<int> a{2, 1, 3};
std::partial_sum(a.begin(), a.end(), a.begin());
// a is {2, 3, 6}
}Common Gotchas
Mixing inclusive and exclusive prefixes
`pref[r] - pref[l]` is wrong if `pref[i]` means sum of first `i+1` elements and you wanted `[l, r]`. Pick one convention and write it in a comment.
Signed overflow
accumulate into `long long`.
`partial_sum` overwrites with inclusive sums
`a[0]` stays `a[0]`, `a[i]` becomes the sum through `i`. Empty-prefix queries need a branch or an extra cell.
2D inclusion-exclusion misses a corner
the formula is `S(r2,c2) - S(r1-1,c2) - S(r2,c1-1) + S(r1-1,c1-1)` with guards at 0.