C++ 二维前缀和与差分

二维前缀和:

#include <bits/stdc++.h>
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 <bits/stdc++.h>
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<bits/stdc++.h>
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<bits/stdc++.h>
using namespace std;
int main(){
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		vector<vector<int>>a;
		a.resize(n + 3);
		for (int i = 0; i <= n + 1; i++)
			a[i].resize(m + 2);
		vector<vector<int>>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;
}

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值