激光导弹&Gundam Unicorn(二维前缀和and二维差分)

激光炸弹和Gundam Unicorn是二维前缀和和二位差分的综合应用。

首先是一二维差分,前缀和的模板前缀和与差分 图文并茂 超级详细整理(全网最通俗易懂)_林深不见鹿 的博客-优快云博客_前缀和与差分

1.一维前缀和,求某个区间内几个数的和

#include<iostream>
#include<algorithm>
using namespace std;
int a[101];
int n;
int s[101];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cout << s[i] << " ";
	}
	return 0;
}

2.一维差分,让某个区间都加上一个常数

#include<iostream>
#include<algorithm>
using namespace std;
int n, m, l, r, c;
int a[100001];
int b[100001];
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		b[i] = a[i] - a[i - 1];
	}	
	while (m--)
	{
		cin >> l >> r >> c;
		b[l] += c;
		b[r + 1] -= c;
	}
	for (int i = 1; i <= n; i++)
	{
		a[i] = a[i - 1] + b[i];
		cout << a[i] << " ";
	}
	return 0;
}

3.二维前缀和,求矩阵的某个子矩阵中元素的和

#include<iostream>
using namespace std;
const int N = 1010;
long long  a[N][N];

int main()
{
	int n, m, q;
	//cin>>n>>m>>q;
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
		{
			//cin>>a[i][j];
			scanf("%lld", &a[i][j]);
			a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
		}
	while (q--)
	{
		int x1, x2, y1, y2;
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		printf("%lld", a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]);
		if (q != 0) printf("\n");
	}
	return 0;
}

具体是为什么这样写,可以搜一下原理图

4.二维差分,让矩阵的某个子矩阵中元素都加上一个常数

#include<iostream>
using namespace std;
const int N=1010;
long long a[N][N];
long long s[N][N];

int main()
{
	int n,m,q;
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) 
			scanf("%lld",&a[i][j]);
	while(q--)
	{
		int x1,x2,y1,y2,c;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&c);
		s[x1][y1]+=c;
		s[x1][y2+1]-=c;
		s[x2+1][y1]-=c;
		s[x2+1][y2+1]+=c;//理解见下图	
    }
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
			printf("%lld ",a[i][j]+s[i][j]);
		}
		if(i!=n) printf("\n");
	}
	return 0;
} 

1.激光炸弹:

99. 激光炸弹 - AcWing题库

#include <algorithm>
#include <iostream>
using namespace std;

const int N = 5e3 + 10; //不能开 1e5+10, 内存限制比较严格
int s[N][N];
int n, r;

int main() {
    cin >> n >> r;
    for (int i = 0; i < n; i++) {
        int x, y, w;
        cin >> x >> y >> w;
//        s[++x][++y]=w;  //错误
        s[++x][++y] += w; //右移一位, 就不需要考虑边界了, 并且必须是+=, 不能是=, 因为1个位置可能有多个目标
    }
    for (int i = 1; i <= 5001; i++) {
        for (int j = 1; j <= 5001; j++) {
//            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + s[i][j];
            s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
        }
    }
    int ans = 0;
    for (int x1 = 1; x1 <= 5001; x1++) {
        for (int y1 = 1;y1 <= 5001; y1++) {
               int x2 = min(x1 + r - 1, 5001);
                int y2 = min(y1 + r - 1, 5001);//处理的最好的就是这个地方了,正好能够保证矩形的子矩阵
            ans = max(ans, s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1-1][y1 - 1]);
        }
    }
    cout << ans << endl;
    return 0;
}

2.Gundam Unicorn

WeJudge - 基于实时评测的实践教学辅助平台 (bnuz.edu.cn)

#include<iostream>
using namespace std;
int main()
{
    int n, m;
    int w, h;
    while (scanf_s("%d%d", &n, &m) != EOF) {//题目要求输入EOF才结束
        int ai[35][35];
        int bi[35][35];
        int ans = 0;
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                cin >> ai[i][j];
            }
        }
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                bi[i][j] = ai[i][j] + bi[i - 1][j] + bi[i][j - 1] - bi[i - 1][j - 1];
            }
        }
        cin >> w >> h;
        for (int x1 = 1; x1 <= m; x1++) {
            for (int y1 = 1; y1 <= n; y1++) {
                int x2 = min(x1 + h - 1, m);
                int y2 = min(y1 + w - 1, n);//和上一道题目一样,巧妙的避免了越界的问题
                ans = max(ans, bi[x2][y2] - bi[x2][y1 - 1] - bi[x1 - 1][y2] + bi[x1 - 1][y1 - 1]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值