题意:
White Rabbit has a rectangular farmland of nm. 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.
大意: 在一个二维图里,每一格都有一种类型的花,现在要每次给一个矩形范围施肥,每一种花只能接受相应的肥,若是不符合该花就死亡,问施T次肥后,最终有多少朵花死亡?
思路: 这道题范围为nm<=1e6,所以我们用vector来存图或者用一维数组模拟二维图,这道题有多种解法
第一种:
我们可以用随机化的解法,将每一种花类型映射成一个很大的树,使得不会出现施两次3类型的肥3+3到类型为6的花上,刚好施完肥的总价值val%type==0,出现误算,映射完后出现这种情况的概率会变得很小
可以说这种算法看运气吧
注意边界问题,用二维前缀和维护就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int n,m,T;
vector<ll> a[maxn];
ll has[maxn];
vector<ll> b[maxn];
void myhash(int m)
{
srand(time(NULL));
for(int i=1;i<=m;i++) {has[i]=(ll)rand()*1e6+rand()*rand();
//cout<<has[i]<<endl;
}
}
void add(int x1,int y1,int x2,int y2,ll k)
{
b[x1][y1]+=k;
if(y2<m)
b[x1][y2+1]-=k;
if(x2<n)
b[x2+1][y1]-=k;
if(x2<n&&y2<m)
b[x2+1][y2+1]+=k;
}
int main()
{
scanf("%d%d%d",&n,&m,&T);
myhash(n*m);
for(int i=1;i<=n;i++)
{
a[i].clear(); a[i].push_back(0);
b[i].clear(); b[i].push_back(0);
for(int j=1,type;j<=m;j++)
{
scanf("%d",&type);
a[i].push_back(has[type]);
b[i].push_back(0);
}
}
for(int i=1;i<=T;i++)
{
int x1,y1,x2,y2,k;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
add(x1,y1,x2,y2,has[k]);
}
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i!=1)
{
b[i][j]+=b[i-1][j];
}
if(j!=1)
{
b[i][j]+=b[i][j-1];
}
if(i!=1&&j!=1)
{
b[i][j]-=b[i-1][j-1];
}
if(b[i][j]%a[i][j] != 0) ans++;
}
}
cout<<ans<<endl;
}
第二种:
利用二进制,统计每一位被浇的0与1次数,若是花类型在该位上没有0,统计的时候有,则死亡
题解来源: 突然找不到那位博主的链接,感谢那位博主
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int n, m, t;
int a[maxn], bn[2][maxn];// bn[0]和bn[1]记录每一位分别为0和1时候的差分数组,每运算一次清一次零
bool vis[maxn];//标记哪些位置是死掉的
struct p {
int x1, x2, y1, y2, k;
} b[maxn];
int id(int x, int y) {//将二维坐标转化成一维
return (x - 1) * m + y - 1;
}
void add(int i, int *s) {//二维差分数初始处理
s[id(b[i].x1, b[i].y1)]++;
if(b[i].y2 < m)//超出矩阵边界的不操作
s[id(b[i].x1, b[i].y2 + 1)]--;
if(b[i].x2 < n)//同上
s[id(b[i].x2 + 1, b[i].y1)]--;
if(b[i].x2 < n && b[i].y2 < m)//同上
s[id(b[i].x2 + 1, b[i].y2 + 1)]++;
}
void work(int *s) {//二维差分数组求前缀和
for(int i = 0; i < n * m; i++) {
if(i % m != 0)
s[i] += s[i - 1];
if(i >= m)
s[i] += s[i - m];
if(i % m != 0 && i >= m)
s[i] -= s[i - m - 1];
}
}
int main() {
while(~scanf("%d%d%d", &n, &m, &t)) {
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n * m; i++)
scanf("%d", &a[i]);
for(int i = 0; i < t; i++)
scanf("%d%d%d%d%d", &b[i].x1, &b[i].y1, &b[i].x2, &b[i].y2, &b[i].k);
for(int i = 0; i < 20; i++) {
for(int j = 0; j < n * m; j++)
bn[0][j] = bn[1][j] = 0;//每次枚举每一位需清零
for(int j = 0; j < t; j++)
add(j, bn[b[j].k >> i & 1]);//分别统计0和1的覆盖次数
work(bn[0]);//用差分数组前缀和完成这一位0的全覆盖
work(bn[1]);//用差分数组前缀和完成这一位1的全覆盖
for(int j = 0; j < n * m; j++) {
if((a[j] >> i & 1) == 0 && bn[1][j] != 0) //如果这个植物的这一位是0且全覆盖(撒完农药)后这一位有1,则这一位死掉
vis[j] = true;
else if((a[j] >> i & 1) == 1 && bn[0][j] != 0)//如果这个植物的这一位是1且全覆盖(撒完农药)后这一位有0,则这一位死掉
vis[j] = true;
}
}
int ans = 0;
for(int i = 0; i < n * m; i++) {
if(vis[i])
ans++;
}
printf("%d\n", ans);
}
return 0;
}