链接:https://www.nowcoder.com/acm/contest/140/J
来源:牛客网
时间限制: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 的药田,里面有不同种类的植物,每种植物只有被同种类的化肥施肥才不会死亡,否则就死去;白云开始对药田施肥,他一共施了t次肥,每次对左上角为(x1, y1),右下角为(x2+1, y2+1)的矩阵药田施种类为i的化肥,问经过t次施肥后,有多少植物死去;
思路:看的别人的题解,先随机哈希1~1e6的数,之后处理维护二维前缀和,就是在每次操作的区间内的格子加上要施的化肥的种类,(这个用不用二维树状数组感觉都行吧);然后我们遍历整个药田,假如说这个格子内的植物每次施的都是同种类的化肥,那么处理后的这个格子内的和一定是这个植物随机后的哈希值的整数倍,否则就证明了这个格子内被施了不同种类的化肥,统计一下就好了;
下面附上我的代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<ctime>
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
const int maxn = 1e6 + 10;
LL has[maxn];
vector<LL> a[maxn], sum[maxn];
int n, m , t;
LL x;
void Hsh()
{
srand(time(NULL));
for(int i = 1; i <= 1e6; i++)
has[i] = (LL)rand() * 1e6 + (LL)rand();
}
int lowbit(int x)
{
return x & (-x);
}
void add(int x, int y, LL v)
{
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= m; j += lowbit(j))
sum[i][j] += v;
}
LL s(int x, int y)
{
LL ret = 0;
for(int i = x; i > 0; i -= lowbit(i))
for(int j = y; j > 0; j -= lowbit(j))
ret += sum[i][j];
return ret;
}
int main()
{
Hsh();
cin >> n >> m >> t;
for(int i = 1; i <= n; i++)
{
a[i].push_back(0);
sum[i].push_back(0);
for(int j = 1; j <= m; j++)
{
scanf("%lld", &x);
a[i].push_back(has[x]);
sum[i].push_back(0);
}
}
int x1, x2, y1, y2;
LL w;
for(int i = 0; i < t; i++)
{
scanf("%d %d %d %d %lld", &x1, &y1, &x2, &y2, &w);
add(x1, y1, has[w]);
add(x2 + 1, y2 + 1, has[w]);
add(x1, y2 + 1, -has[w]);
add(x2 + 1, y1, -has[w]);
}
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m ;j++)
{
if(s(i, j) % a[i][j])
ans++;
}
printf("%d\n", ans);
return 0;
}