A range query computes a value from a subarray [a, b]: the sum sumq(a,b), the minimum minq or the maximum maxq. For the range [3, 6] below, sum = 14, min = 1 and max = 6. A loop over the range takes O(n), so q queries take O(nq), too slow when both are large.

01234567
13846134

First the array is static: it does not change between the queries.

Sum queries

The prefix sum array stores, at position k, the sum of the elements up to k; it is built in O(n). Then any sum is a difference of two prefix sums: sumq(a,b) = sumq(0,b) − sumq(0,a−1), in O(1). For [1, 3, 4, 8, 6, 1, 4, 2] the prefix sums are [1, 4, 8, 16, 22, 23, 27, 29] and sumq(3,6) = 27 − 8 = 19. The program keeps an extra 0 at the front, so that pre[r+1] − pre[l] also works for l = 0:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    vector<long long> a(n), pre(n + 1, 0);  // pre[k] = a[0] + ... + a[k-1]
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        pre[i + 1] = pre[i] + a[i];
    }
    while (q--) {
        int l, r;
        cin >> l >> r;
        cout << "sum(" << l << "," << r << ") = " << pre[r + 1] - pre[l] << "\n";
    }
}
8 3
1 3 4 8 6 1 4 2
3 6
0 7
2 2
Output
sum(3,6) = 19
sum(0,7) = 29
sum(2,2) = 4
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    vector<long long> pre(n + 1, 0);
    for (int i = 0; i < n; i++) {
        long long x;
        cin >> x;
        pre[i + 1] = pre[i] + x;
    }
    while (q--) {
        int l, r;
        cin >> l >> r;
        cout << pre[r + 1] - pre[l] << "\n";
    }
}
8 3
1 3 4 8 6 1 4 2
3 6
0 7
2 2

Prefix sums

The idea works in two dimensions: S(y, x) is the sum of the rectangle from the upper-left corner to (y, x), and the sum of any rectangle is S(A) − S(B) − S(C) + S(D), where A is its lower-right corner, B and C cut away the parts to the left and above, and D is subtracted twice, so it is added back. The queries are y1 x1 y2 x2, numbered from 1:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    // S[y][x] = sum of the rectangle from (1,1) to (y,x)
    vector<vector<long long>> S(n + 1, vector<long long>(m + 1, 0));
    for (int y = 1; y <= n; y++)
        for (int x = 1; x <= m; x++) {
            long long v;
            cin >> v;
            S[y][x] = v + S[y - 1][x] + S[y][x - 1] - S[y - 1][x - 1];
        }
    while (q--) {
        int y1, x1, y2, x2;
        cin >> y1 >> x1 >> y2 >> x2;
        cout << S[y2][x2] - S[y1 - 1][x2] - S[y2][x1 - 1] + S[y1 - 1][x1 - 1] << "\n";
    }
}
3 4 2
1 2 3 4
5 6 7 8
9 10 11 12
2 2 3 3
1 1 3 4
Output
34
78

Minimum queries

Minimums are harder, but the sparse table answers them in O(1) after O(n log n) preprocessing. Precompute minq(a,b) for all ranges whose length is a power of two: a range of length 2j is two ranges of length 2j−1. A query [a, b] is covered by two such ranges of length k, the largest power of two not larger than b−a+1: minq(a,b) = min(minq(a,a+k−1), minq(b−k+1,b)). The two ranges may overlap, which does not matter for a minimum. For [1, 6]: k = 4, min(minq(1,4), minq(3,6)) = min(3, 1) = 1.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    int LOG = 1;
    while ((1 << LOG) <= n) LOG++;
    // mn[j][a] = minimum of the range [a, a + 2^j - 1]
    vector<vector<int>> mn(LOG, vector<int>(n));
    for (int &x : mn[0]) cin >> x;
    for (int j = 1; j < LOG; j++)
        for (int a = 0; a + (1 << j) <= n; a++)
            mn[j][a] = min(mn[j - 1][a], mn[j - 1][a + (1 << (j - 1))]);
    while (q--) {
        int a, b;
        cin >> a >> b;
        int j = 31 - __builtin_clz(b - a + 1);  // the largest power of two <= length
        cout << "min(" << a << "," << b << ") = " << min(mn[j][a], mn[j][b - (1 << j) + 1]) << "\n";
    }
}
8 3
1 3 4 8 6 1 4 2
1 6
1 4
6 7
Output
min(1,6) = 1
min(1,4) = 3
min(6,7) = 2

Based on: Antti Laaksonen, Competitive Programmer's Handbook, section 9.1 (CC BY-NC-SA 4.0). Changed: the text is shortened and adapted, the code fragments are complete programs, and the diagrams, tasks and quizzes are new. Adaptation: Boban Blagojević, CC BY-NC-SA 4.0.