算法题-前缀和与查分

  1. 一维前缀和模板
  2. 二维前缀和模板
  3. 一维差分模板
  4. 二维差分模板

一维前缀和模板

https://www.acwing.com/problem/content/797/

一维前缀和能够在预处理数组的前提下, 用$O(1)$的时间复杂度获取数组子串$[l, r]$的元素之和.

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int s[N];

int main()
{
    cin >> n >> m;
    /* 下标从1开始 */
    for (int i = 1; i <= n; i ++)
    {
        cin >> s[i];
        s[i] += s[i - 1];
    }

    while (m --)
    {
        int l, r;
        cin >> l >> r;
        cout << s[r] - s[l - 1] << endl;
    }
    return 0;
}

二维前缀和模板

https://www.acwing.com/problem/content/798/

二维前缀和能够在预处理数组的前提下, 用$O(1)$的时间复杂度获取子矩阵元素之和.

#include <iostream>

using namespace std;

const int N = 1010;

int n, m, q;
int a[N][N];

int main()
{
    cin >> n >> m >> q;

    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
        {
            cin >> a[i][j];
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        }

    while (q --)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;

        printf("%d\n", a[x2][y2] + a[x1 - 1][y1 - 1] - a[x1 - 1][y2] - a[x2][y1 - 1]);
    }
    return 0;
}

一维差分模板

https://www.acwing.com/problem/content/799/

一维差分能够在预处理数组的前提下, 用$O(1)$的时间复杂度将子数组中的所有元素加上一个$c$.

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int a[N];

void insert(int l, int r, int c)
{
    a[l] += c;
    a[r + 1] -= c;
}

int main()
{
    cin >> n >> m;
    /* 注意下标是1 */
    for (int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        insert(i, i, x);
    }

    while (m --)
    {
        int l, r, c;
        cin >> l >> r >> c;
        insert(l, r, c);
    }

    for (int i = 1; i <= n; i ++) a[i] += a[i - 1];
    for (int i = 1; i <= n; i ++) printf("%d ", a[i]);

    return 0;
}

二维差分模板

https://www.acwing.com/problem/content/800/

#include <iostream>

using namespace std;

const int N = 1010;

int n, m, q;
int a[N][N];

void insert(int x1, int y1, int x2, int y2, int c)
{
    a[x1][y1] += c;
    a[x1][y2 + 1] -= c;
    a[x2 + 1][y1] -= c;
    a[x2 + 1][y2 + 1] += c;
}

int main()
{
    cin >> n >> m >> q;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
        {
            int x;
            cin >> x;
            insert(i, j, i, j, x);
        }

    while (q --)
    {
        int x1, y1, x2, y2, c;
        cin >> x1 >> y1 >> x2 >> y2 >> c;
        insert(x1, y1, x2, y2, c);
    }

    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= m; j ++)
            printf("%d ", a[i][j]);
        puts("");
    }
    return 0;
}