本文介绍: 【代码】蓝桥OJ3694肖恩的投球游戏plus。

二维差分

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

const int N = 1e3 + 5;
int a[N][N],d[N][N];

int main()
{
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1 ; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
			d[i][j] = a[i][j] + a[i-1][j-1] - a[i-1][j] - a[i][j-1];
		}
	}
	while(q--)
	{
		int x1,y1,x2,y2,c;
		cin >> x1 >> y1 >> x2 >> y2 >> c;
		d[x1][y1] += c;
		d[x2 + 1][y2 + 1] += c;
		d[x1][y2 + 1] -= c;
		d[x2 + 1][y1] -= c;
	}
	for (int i = 1 ; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			a[i][j] = d[i][j] - a[i-1][j-1] + a[i-1][j] + a[i][j-1];
			cout << a[i][j] << ' ';
		}
		cout << 'n';
	}
	return 0;
}

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注