前缀和、差分

本文参考于:https://blog.csdn.net/K_R_forever/article/details/81775899
      https://blog.csdn.net/Lj_victor/article/details/81590852

一维前缀和

  其实可以把它理解为数学上的一维数列的前n项和。
  对于一个给定的数列 A,他的前缀和数列 S 是通过地推能求出来的基本信息之一:
  S[i] = ∑A[j] ( j ∈ [1,i] ) 一个部分和,即数列 A 某个下表区间内的数的和,也可以表示为前缀和相减的形式:sum(l,r) = ∑A[i] ( i ∈ [l,r] ) = S[r] - S[l-1]

	int sum[1] = a[1];
	for(int i=2;i<=n;i++){
		sum[i] = sum[i-1] + a[i];
	}

一维差分

  对于一个数列A,他的差分数列B定义为:
    B[1] = A[1] ,B[i] = A[i] - A[i-1] ( 2<=i<=n)
  容易发现,“前缀和” 和 “差分” 是一对互逆运算,差分序列B的前缀和序列就是原序列A,前缀和序列S的差分序列也是原序列A。
  把序列A的区间 [l,r] 加上 d(即把 Al,Al+1……Ar 都加上 d),其差分序列 B 的变化为 Bl 加 d,Br+1 减 d,其余位置不变。
  列如数列 1 3 6 3 9 10 20,让位置在[2,5]范围内加 10,
先转化为他的差分序列:1 2 3 -3 6 1 10,处理后:1 12 3 -3 6 -9 10
他的原序列就是差分序列的前缀和:1 13 16 13 19 10 20

	b[1] = a[1];
	for(int i=2;i<=n;i++)
		b[i] = a[i] - a[i-1];
	b[l] += d;
	b[r+1] -= d;
	new_a[1] = b[1];
	for(int i=2;i<=n;i++){
		new_a[i] = new_a[i-1] + b[i];
	}

二维前缀和

与一维前缀和类似,设s[x][y]表示所有a[i][j]的和。(1≤i≤x,1≤j≤y)
有一点像“矩形的面积”那样,把一整块区域的值都加起来。

给定一个n*m大小的矩阵a,有q次询问,每次询问给定x1,y1,x2,y2四个数,求以(x1,y1)为左下角坐标和(x2,y2)为右上角坐标的子矩阵的所有元素和。注意仍然包含左上角和右下角的元素。
在这里插入图片描述
按照题目要求我们要求解区域2的面积,很明显他的面积等于总面积减去3和4区间,减去1和4区间,再加上4区间,即 ans = a[x2][y2] - a[x1-1][y2] - a[x2,y1-1] + a[x1-1][y2-1],其中 a 表示前缀和数组。
联想到二维前缀和:
在这里插入图片描述
假如我想求a[2][4]的前缀和,我得先加上a[1][4]的前缀和,再加上a[2][3]的前缀和,然后这个时候我们发现实际上a[1][3]这个部分我们加了两遍,所以我们需要再减去一遍a[1][3]。
在这里插入图片描述
于是得出公式a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]。
接下来看完整代码吧。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+9;
int a[maxn][maxn];
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];
		}
	}
	for(i=1;i<=n;i++){	
		for(j=1;j<=m;j++){
			a[i][j] += a[i][j-1]+a[i-1][j]-a[i-1][j-1];
		}
	}
	for(i=1;i<=q;i++){
		int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		int ans = a[x2][y2] - a[x1-1][y2] - a[x2][y1-1] + a[x1-1][y1-1];
		cout<<ans<<endl;
	}
}

二维差分

  方法是和一维类似的,我们也是需要另开一个数组记录修改操作,最后求前缀和时统计修改操作,只是二维每一次操作需要记录4个位置,一维只需要记录2个位置。

for(int i=0;i<m;i++){	//m是修改操作次数 
	int x1,y1,x2,y2,p;
	cin>>x1>>y1>>x2>>y2>>p;
	b[x1][y1] += p;		b[x2+1][y2+1] += p;
	b[x2+1][y1] -= p;	b[x1][y2+1]-=p;
}

例题:二维差分 http://acm.hdu.edu.cn/showproblem.php?pid=6514
Monitor
Problem Description

Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m.
But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.
However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are p monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.
Xiao Teng guess that the thieves would also steal q times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.

Input

There are mutiple test cases.
Each case starts with a line containing two integers n,m(1≤n,1≤m,n×m≤107) which represent the area of the land.
And the secend line contain a integer p(1≤p≤106) which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m) ,meaning the lower left corner and upper right corner of the rectangle.
Next line contain a integer q(1≤q≤106) which represent the number of times that thieves will steal the crops.This is followed by q lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m),meaning the lower left corner and upper right corner of the rectangle.

Output

For each case you should print q lines.
Each line containing YES or NO mean the all thieves whether can be seen.

Sample Input

6 6
3
2 2 4 4
3 3 5 6
5 1 6 2
2
3 2 5 4
1 5 6 5

Sample Output

YES
NO

题意

  给你n和m,表示有一个n * m的矩阵(n*m<=1e7),初始全0,接下来有p个矩阵,输入他们的左下角和右上角的坐标,把这位于这些矩阵内的格子置为1,再接下来有q个矩阵,输入他们的左下角和右上角的坐标,对于每个矩阵,如果它包含的每个格子都是1,则输出YES,否则输出NO。

思路

  用前缀和的方法计算出每个格子是否被覆盖(p个矩阵都进行二维前缀和的加1更新操作,然后统计一遍前缀和即可得出每个格子是否被覆盖,大于等于1即为被覆盖)
然后把被覆盖的格子都置为1,重新统计一遍前缀和,这时候前缀和就表示某矩阵包含的被覆盖的格子数了。
然后对于q个矩阵,直接查询(x1,y1,x2,y2)的被覆盖的面积是否等于(x2-x1+1)*(y2-y1+1)。

方案一:Hash + 差分:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=20000010;
const int inf = 0x3f3f3f3f;
int pre[maxn];
int n,m;
int getid(int x,int y)
{
    return x*(m+1) + y;
}
void ud(int x,int y,int v){
    if(x>n||y>m)    return;	//注意这个地方……差分的时候会超过n,m的范围,对n和m分别扩大1即可
    int p = getid(x,y);
    pre[p] += v;
}
int main(void)
{
    int p,q;
    int x1,x2,y1,y2;
    while(~scanf("%d%d",&n,&m)){
        memset(pre,0,sizeof(pre));
        scanf("%d",&p);
        while(p--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            ud(x1,y1,1);    ud(x1,y2+1,-1);
            ud(x2+1,y1,-1);  ud(x2+1,y2+1,1);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                pre[getid(i,j)] += pre[getid(i-1,j)] + pre[getid(i,j-1)] - pre[getid(i-1,j-1)];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(pre[getid(i,j)]) pre[getid(i,j)] = 1;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                pre[getid(i,j)] += pre[getid(i-1,j)] + pre[getid(i,j-1)] - pre[getid(i-1,j-1)];
            }
        }
        scanf("%d",&q);
        while(q--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int ans1 = pre[getid(x2,y2)] + pre[getid(x1-1,y1-1)]
                        - pre[getid(x2,y1-1)] - pre[getid(x1-1,y2)];
            int ans2 = (y2-y1+1)*(x2-x1+1);
            //cout<<ans1<<endl;
            if(ans1==ans2)  puts("YES");
            else puts("NO");
        }
    }
    return 0;
}

方案二:二维前缀和 + 差分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=20000010;
const int inf = 0x3f3f3f3f;
int main(void)
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int a[n+2][m+2]={0};
        memset(a,0,sizeof(a));
        int x1,y1,x2,y2;
        int q;
        scanf("%d",&q);
        for(int i=1;i<=q;i++){
            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][j-1]+a[i-1][j]-a[i-1][j-1];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)
                a[i][j]=(a[i][j]>=1)?1:0;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1];
            }
        }
        scanf("%d",&q);
        for(int i=1;i<=q;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int ans1=(y2-y1+1)*(x2-x1+1);
            int ans2=(a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]);
            
            if(ans1==ans2) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

  • 33
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值