题目描述
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
思路: 对于每一个田地的数 他的二进制每一位都是由 0 1 组成,我们在更改的数中 只要能够找到和当前位不一样的 0 1 那么就一定能够使这个植物死掉。
num 中 S[i] 表示当前位 为 0 或者 1 的总数 T[ i ] 表示 当前位为 1 的总数
代码:
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N =1e6+5;
int S[N],T[N];
int n,m,q;
int x1[N],x2[N],yy1[N],y2[N];
ll num[N];
int id(int x,int y)
{
return (x-1)*m+y;
}
void add(int i,int *a)
{
++a[id(x1[i],yy1[i])];
if(y2[i]+1<=m) --a[id(x1[i],y2[i]+1)];
if(x2[i]+1<=n) --a[id(x2[i]+1,yy1[i])];
if(x2[i]+1<=n&&y2[i]+1<=m) ++a[id( x2[i]+1, y2[i]+1 )];
}
void work(int *a)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>1) a[id(i,j)]+=a[id(i-1,j)];
if(j>1) a[id(i,j)]+=a[id(i,j-1)];
if(i>1&&j>1) a[id(i,j)]-=a[id(i-1,j-1)];
}
}
}
ll a[N];
int die[N];
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=n*m;i++) scanf("%lld",&a[i]);
for(int i=1;i<=q;i++){
scanf("%d %d %d %d %lld",&x1[i],&yy1[i],&x2[i],&y2[i],&num[i]);
add(i,S);
}
work(S);
for(int u=0;u<20;u++) // 表示第 u 位
{
for(int i=0;i<=n*m;i++) T[i]=0;
for(int i=1;i<=q;i++){
if((num[i]>>u)&1){ // 表示 num[i] 的第 u 位为1
add(i,T);
}
}
work(T);
for(int i=1;i<=n*m;i++){
int xx=(a[i]>>u); xx=xx&1;
if(xx==0){
if(T[i]) die[i]=1; // a[i] 的当前位 为 0 但是在更改的数中当前位有为1 的数
}
else{
if(S[i]>T[i]) die[i]=1; // a[i] 的当前位 为 1 但是在更改的数中当前位有为0 的数
}
}
}
int cnt=0;
for (int i=1;i<=n*m;i++) {
if(die[i]) cnt++;
}
printf("%d\n",cnt);
return 0;
}