https://blog.csdn.net/weixin_45629285/article/details/111146240
参考博客学习资料(一二维前缀和,差分)连接,里面还有题目
个人二维分差解题代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int a[1005][1005], b[1005][1005];//之前写在main函数中,一运行就结束了,调试了好久呢
//问了GPT,是在 main 函数内定义的局部数组。如果数组太大,超出了栈的可用空间,就会导致栈溢出或其他问题。
int main(void)
{
int m, n, q;
scanf("%d %d %d", &m, &n, &q);
int x1, x2, y1, y2, c;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);//每个方格读值,方便后续求差分
}
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
b[i][j] = a[i][j] + a[i - 1][j - 1] - a[i][j - 1] - a[i - 1][j];//求差分数组
}
}
for (int i = 1; i <= q; i++)
{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c);//对要改变区域,进行改变
b[x1][y1] += c;
b[x2 + 1][y2 + 1] += c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y1] -= c;
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
b[i][j] = b[i][j] - b[i - 1][j - 1] + b[i][j - 1] + b[i - 1][j];//差分求和
printf("%d ", b[i][j]);
}
printf("\n");
}
return 0;
}