牛客网暑期ACM多校训练营(第二场)J.farm (随机数+二维树状数组)

题目链接

时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.
White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle [x1[i]...x2[i]][y1[i]...y2[i]].
White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

输入描述:

The first line of input contains 3 integers n,m,T(n*m<=1000000,T<=1000000)
For the next n lines, each line contains m integers in range[1,n*m] denoting the type of plant in each grid.
For the next T lines, the i-th line contains 5 integers x1,y1,x2,y2,k(1<=x1<=x2<=n,1<=y1<=y2<=m,1<=k<=n*m)

输出描述:

Print an integer, denoting the number of plants which would die.

示例1

输入

2 2 2

1 2

2 3

1 1 2 2 2

2 1 2 1 1

输出

3

题意:给出一个N*M的矩阵药田,药田中每个格子都种有一种编号的为1~N*M的药,随后一个T表示有T次浇水操作,之后先是给出N行M列的矩阵(药田),再给出T行,每行有5个数,X1,Y1,X2,Y2和W,表示这次操作会在左上角为(X1,Y1),右下角为(X2,Y2)的矩阵中浇W这种药水,若是该某个药格中的药编号与被浇到的药水不同,则死亡,问你T次浇水操作后整个药田死了多少颗药?

扫描二维码关注公众号,回复: 2390035 查看本文章

题解:这里对每个药的编号进行随机hash得到一个大于最大编号1e6的数值,之后每次浇水操作,我们通过使用二维树状数组区间更新的巧妙办法,在已知更新的矩阵左上角和右下角情况下update四个点的值即可在O(logn*logm)时间内实现树状数组的区间更新(需要注意的是这里更新的值也是药对应药水的hash值才行).那么最终想要知道某个格子的药是否存活只需要判断他的树状数组的值%本身的hash值是否为0即可,在随机数足够优秀的条件下,能整除的情况说明该药格每次浇到的药水的是合适的(或者没有浇到任何药水),那么它是存活的,反之死亡.

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
vector<ll>mp[maxn];     //存二维矩阵
vector<ll>tre[maxn];    //存二维树状数组结果
ll has[maxn];
int n, m;
void fun() {           //预处理出1e6以内的所有数的has值(这里指随机生成的大于1e6的数)
	srand(time(NULL));
	for (int i = 1; i < maxn - 5; i++)
		has[i] = (ll)rand()*1e6 + rand()*rand();
}
//一下三个函数使用的二维树状数组
ll lowbit(ll x) {     
	return x&(-x);
}
void update(int x, int y, ll z)
{
	for (int i = x; i <= n; i += lowbit(i))
		for (int j = y; j <= m; j += lowbit(j))
			tre[i][j] += z;
}
ll sum(int x, int y)
{
	ll ret = 0;
	for (int i = x; i >= 1; i -= lowbit(i))
		for (int j = y; j >= 1; j -= lowbit(j))
			ret += tre[i][j];
	return ret;
}
int main() {
	int t, x;
	scanf("%d%d%d", &n, &m, &t);
	fun();
	for (int i = 1; i <= n; i++) {
		mp[i].push_back(0);         //使mp的列从1开始(方便后面查询)
		tre[i].push_back(0);
		for (int j = 1; j <= m; j++) {
			scanf("%d", &x);
			mp[i].push_back(has[x]);//将给出的值进行随机化
			tre[i].push_back(0);   //初始化树状数组
		}
	}
	int x1, y1, x2, y2;
	ll w;
	while (t--) {                //给的是左上角和右下角的坐标
		scanf("%d%d%d%d%lld", &x1, &y1, &x2, &y2, &w);
		update(x1, y1, has[w]);  //通过更新这4个几个实现区间更新
		update(x2 + 1, y2 + 1, has[w]);
		update(x2 + 1, y1, -has[w]);
		update(x1, y2 + 1, -has[w]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) //若是最终在该点撒过得农药总和值不是原值得倍数,则表示死亡
		for (int j = 1; j <= m; j++)
			if (sum(i, j) % mp[i][j] != 0) ans++;
	printf("%d\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41156591/article/details/81150890
今日推荐