牛客多校第二场 J(随机+矩阵二维前缀和)

链接: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

随机生成1~1e6对应的权值。

更新的时候,对整个矩阵加对应位置的权值,并记录这个店被更新的次数。

之后用二维前缀和来得到(i,j)上的总权值sum(i,j)和被更新的次数cnt(i,j)

如果sum(i,j)!=cnt(i,j)*rand(a(i,j)) 说明存在一个数更新的时候跟他本身不等,让答案+1

#include<bits/stdc++.h>
#define mp make_pair
#define fir first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const int maxn=1e6+10;
const ll mod=1e9+7;
const int maxm=1e6+10;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
vector<ll> v[maxn],cnt[maxn],sum[maxn];
int n,m,t;
ll w[maxn];
int main(){
    srand(time(0));
    for(int i=0;i<maxn;i++)
        w[i]=(rand()%maxn)+1;
    cin>>n>>m>>t;
    for (int i=0;i<=n+1;i++){
        for (int j=0;j<=m+1;j++){
            v[i].pb(inf);
            cnt[i].pb(0);
            sum[i].pb(0);
        }
    }
    for  (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            scanf("%d",&v[i][j]);
        }
    }
    while (t--){
        int a,b,c,d,e;
        scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
        cnt[a][b]++;
        cnt[c+1][d+1]++;
        cnt[c+1][b]--;
        cnt[a][d+1]--;
        sum[a][b]+=w[e];
        sum[c+1][d+1]+=w[e];
        sum[c+1][b]-=w[e];
        sum[a][d+1]-=w[e];
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            sum[i][j]=sum[i][j]-sum[i-1][j-1]+sum[i][j-1]+sum[i-1][j];
            cnt[i][j]=cnt[i][j]-cnt[i-1][j-1]+cnt[i][j-1]+cnt[i-1][j];
        }
    }
    int ans=0;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
             if (cnt[i][j]==0) continue;
             if (sum[i][j]!=w[v[i][j]]*cnt[i][j]){
                  ans++;
             }
        }
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wyj_alone_smile/article/details/81197370