差分-二维数组

#include 

using namespace std;

const int N = 10010;
int d[N][N];

void solve()
{
	int n, m;
	cin >> n >> m;
	//memset(d, 0, sizeof(d));
	for (int i = 0; i < m; i++)
	{
		int x1=0, y1=0, x2=0, y2=0;
		cin >> x1>> y1>>x2>>y2;
		d[x1][y1] ++;
		d[x1][y2 + 1] --;
		d[x2 + 1][y1]--;
		d[x2 + 1][y2 + 1]++;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++)             //d[1][1]对应a[1][1]原点为1,10 
		{
			d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
			cout << d[i][j]<<' ';
		}
		cout << endl;
	}
}
signed main()
{
	ios::sync_with_stdio;
	cin.tie(0);
	cout.tie(0);
	int num = 1;
	while (num--)
	{
		solve();
	}
}

你可能感兴趣的:(算法,蓝桥杯,java)