牛客网暑期ACM多校训练营(第二场) J - farm (随机化+二维前缀和)

题目

题目描述 

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个矩阵,每个格子里有种类为k的花。

给你t次操作,每次在一个子矩阵里施肥,如果种类为k的肥料被施到不同种类的花上,花就死掉。问你死了多少花。

POINT:

若一个格子里施肥的种类加起来能整除k,那么这个格子里的花【可能】没死。

比如k=3,但是施了两次肥1,肥2。这个策略就有错了。

那么我们利用随机化,把这些种类对应一个随机的数,大一点。这个情况就可以尽量被避免了。

然后用二位前缀和就可以解题了。

注意数组的开法,你可以用vector,要不用动态数组。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000000+2;
#define LL long long
int x1,Y1,x2,y2;
LL Hash[maxn];
int main()
{
    srand(time(0));
    LL n,m,t;
    scanf("%lld%lld%lld",&n,&m,&t);
    for(LL i=1;i<=n*m;i++){
        Hash[i]=i*i+i*rand()+1;
    }
    LL **a=new LL *[n+5];
    LL **b=new LL *[n+5];
    for(int i=0;i<n+5;i++){
        b[i]=new LL[m+5];
        a[i]=new LL[m+5];
    }

   for(int i=0;i<=n+1;i++){
        for(int j=0;j<=m+1;j++){
            a[i][j]=b[i][j]=0;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%lld",&b[i][j]);
            b[i][j]=Hash[b[i][j]];
            a[i][j]=0;
        }
    }
    while(t--){
        LL k;
        scanf("%d%d%d%d%lld",&x1,&Y1,&x2,&y2,&k);
        k=Hash[k];
        a[x1][Y1]+=k;
        if(y2+1<=m)
            a[x1][y2+1]-=k;
        if(x2+1<=n)
            a[x2+1][Y1]-=k;
        if(y2+1<=m&&x2+1<=n)
            a[x2+1][y2+1]+=k;
    }
    LL ans=0;
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++){
            a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
            if(a[i][j]%b[i][j]!=0){
                ans++;
            }
        }
    }
    printf("%lld\n",ans);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/mr_treeeee/article/details/81183171