时间限制: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次打药,每次在一个矩形中打药,问最终死了多少植物?
分析:二位树状数组+随机化。对每种植物适应的药性值进行随机化,然后用二位树状数组来维护T次矩形区域中每种植物所施加的药性Hash值的总和,最终判断药性总和是否为植物所适应的药性Hash值的倍数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <vector>
#include <stack>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
vector<ll> G[maxn];
vector<ll> ans[maxn];
ll Hash[maxn];
int n,m,t;
void init()
{
for(int i=1;i<=1e6+6;i++)
{
Hash[i]=rand()*1e6+rand()*rand();
}
}
void update(int x,int y,ll val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
for(int j=y;j<=m;j+=lowbit(j))
{
ans[i][j]+=val;
}
}
}
ll qury(int x,int y)
{
ll res=0;
for(int i=x;i>=1;i-=lowbit(i))
{
for(int j=y;j>=1;j-=lowbit(j))
{
res+=ans[i][j];
}
}
return res;
}
int main()
{
init();
scanf("%d%d%d",&n,&m,&t);
for(int i=1;i<=n;i++)
{
G[i].push_back(0);
ans[i].push_back(0);
for(int j=1;j<=m;j++)
{
int x;
scanf("%d",&x);
G[i].push_back(Hash[x]);
ans[i].push_back(0);
}
}
for(int i=1;i<=t;i++)
{
int x1,x2,y1,y2,k;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
update(x1,y1,Hash[k]);
update(x2+1,y2+1,Hash[k]);
update(x1,y2+1,-Hash[k]);
update(x2+1,y1,-Hash[k]);
}
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ll s=qury(i,j);
if(s%G[i][j])
sum++;
}
}
printf("%d\n",sum);
return 0;
}