C++ 二维前缀和与差分

二维前缀和:

#include 
using namespace std;
int a[1010][1010];
int main(){
	int n, m;
	cin >> n >> m;
	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++)
			a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
	for (int i = 1; i <= n; i++, cout << endl)
		for (int j = 1; j <= m; j++)
			printf("%d ", a[i][j]);
	int q;
	cin >> q;
	while (q--) //查询, x1,y1是左上方坐标, x2,y2是右下方坐标
	{
		int x1, x2, y1, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		cout << a[x2][y2] + a[x1 - 1][y1 - 1] - a[x1 - 1][y2] - a[x2][y1 - 1] << endl;
	}
	return 0;
}

二维差分:

#include 
using namespace std;
int a[1010][1010];   
int main(){
	int n, m, t;
	cin >> n >> m >> t;
	while (t--)
	{
		int x1, x2, y1, y2, c;
		cin >> x1 >> y1 >> x2 >> y2 >> c;
		a[x1][y1] += c;
		a[x2 + 1][y1] -= c;
		a[x1][y2 + 1] -= c;
		a[x2 + 1][y2 + 1] += c;
	}
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
		{
			a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
		}
	for (int i = 1; i <= n; i++, cout << endl)
		for (int j = 1; j <= m; j++)
			printf("%d ", a[i][j]);
	return 0;
}

例题

ZZULIOJ 2476: 矿产含量

http://acm.zzuli.edu.cn/problem.php?id=2476

#include
using namespace std;
int a[1010][1010];
int main(){
	int t;
	cin >> t;
	while (t--)
	{
		int  MX = 0;
		memset(a, 0, sizeof a);
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			int x1, y1, x2, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			MX = max({ MX, x1, x2, y1, y2 });
			if (x1 > x2)
				swap(x1, x2);
			if (y1 > y2)
				swap(y1, y2);
			x1++;
			y1++;
			a[x1][y1]++;
			a[x2 + 1][y2 + 1]++;
			a[x2 + 1][y1]--;
			a[x1][y2 + 1]--;
		}
		for (int i = 1; i <= MX + 10; i++)
			for (int j = 1; j <= MX + 10; j++)
			{
				a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
			}
		int cnt = 0;
		for (int i = 1; i <= MX + 10; i++)
			for (int j = 1; j <= MX + 10; j++)
				if (a[i][j])
					cnt++;
		cout << cnt << endl;
	}
	return 0;
}

HDU 6514 Monitor

http://acm.hdu.edu.cn/showproblem.php?pid=6514

#include
using namespace std;
int main(){
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		vector>a;
		a.resize(n + 3);
		for (int i = 0; i <= n + 1; i++)
			a[i].resize(m + 2);
		vector>b;
		b.resize(n + 3);
		for (int i = 0; i <= n + 1; i++)
			b[i].resize(m + 2);
		int t;
		cin >> t;
		while (t--)
		{
			int x1, y1, x2, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			++a[x1][y1];
			++a[x2 + 1][y2 + 1];
			--a[x1][y2 + 1];
			--a[x2 + 1][y1];
		}
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				a[i][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++)
			{
				if (a[i][j])
					b[i][j]++;
				b[i][j] = b[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
			}
		int qury;
		cin >> qury;
		while (qury--)
		{
			int x1, y1, x2, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			int ans = b[x2][y2] + b[x1 - 1][y1 - 1] - b[x2][y1 - 1] - b[x1 - 1][y2];
			if ((y2 - y1 + 1) * (x2 - x1 + 1) == ans)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}

 

你可能感兴趣的:(差分)