二维前缀和

原理

容斥原理。

模板题

AcWing-796-子矩阵的和open in new window

#include <iostream>
using namespace std;

const int N = 1010;
int a[N][N], sum[N][N];

int query (int x1, int y1, int x2, int y2) { // 容斥原理
    return sum[x2][y2]
        - sum[x2][y1 - 1]
        - sum[x1 - 1][y2]
        + sum[x1 - 1][y1 - 1];
}

int main () {
    int n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= m; ++ j)
            scanf("%d", &a[i][j]);

    for (int i = 1; i <= n; ++ i)
        for (int j = 1; j <= m; ++ j)
            sum[i][j] = sum[i][j - 1] // 容斥原理
                + sum[i - 1][j]
                - sum[i - 1][j - 1]
                + a[i][j];

    while (q --) {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        printf("%d\n", query(x1, y1, x2, y2));
    }
    return 0;
}
最后修改于: