2018牛客暑期ACM多校第二场J(二维数组数组+hash)

链接:https://www.nowcoder.com/acm/contest/140/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.

输入

2 2 2
1 2
2 3
1 1 2 2 2
2 1 2 1 1

输出

3

题目描述:

给出一个n*m(n*m<=1000000)的农场,以及每个格子中植物的种类编号∈[1,n*m],

接着给出T(T<=1000000)次施肥的信息,信息包括x1,y1,x2,y2,k(1<=x1<=x2<=n,1<=y1<=y2<=m,1<=k<=n*m)

表示给在坐标(x1,y1)到坐标(x2,y2)的区间中全部植物施加编号为k的肥料。

已知植物被不同于自身编号的肥料施加后会死去,问全部施肥操作后有多少株植物会死去。

题目分析:

chen_jr_的做法:https://blog.csdn.net/weixin_39453270/article/details/81152567

初始条件:首先将全部施肥操作区域+1加入到二维树状数组中

按植物种类进行处理,对于第 k 种植物死亡数的统计过程如下:

①将全部施加编号为k的肥料的操作撤回,也就是将编号为k 的施肥区域全部-1。

②查询所有编号为k的植物的格子被施加了多少次肥料,如果不为0则该植物死亡,统计答案ans++(因为如果还有肥料的话,这些肥料已经不是编号为k的了);

③将全部施加编号为k的肥料的操作回复,也就是将编号为k 的施肥区域全部+1。

④若k<n*m,k=k+1,回到①。

另外本文写的做法是随机hash:

①先给每个编号一个随机的hash值(记为_rand[ ])。

②给区域施加编号为k的化肥的操作,是让该区域全部加上编号k对应的hash值_rand[k]。

③拥有编号k植物的格子,如果查询到的值是_rand[k]的倍数,那么说明这个格子只有第k种化肥施加过,如果不是_rand[k]的倍数(说明存在其他肥料),那么该植物死亡,统计答案ans++

以下是该做法的代码

代码:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define INF 0x3f3f3f3f//3f3f3f3f
#define ll long long
#define ull unsigned long long
const long long MOD = 1000000007;
#define lowbit(a) ((a)&(-(a)))
using namespace std;
int n, m, t;
int _rand[1000005];
vector<ll>bit[1000005];//需要开ll,因为hash值加和之后会爆int
vector<int>a[1000005];
//二维bit数组单点修改
void add(int x,int y,int k){
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=m;j+=lowbit(j))
            bit[i][j]+=k;
}
//二维bit数组区域修改
void update(int x,int y,int _x,int _y,int k){
    add(x,y,k);
    add(_x+1,_y+1,k);
    add(x,_y+1,-k);
    add(_x+1,y,-k);
}
//二维bit数组单点查询
ll query(int x,int y){
    ll res=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            res+=bit[i][j];
    return res;
}
int main(){
    int x,y,_x,_y,k;
    for(int i=1;i<=1000000;i++)//瞎jr随机,hash值
        _rand[i]=abs(rand()*13131+17)%100000000+13131;//取模数的大小决定了hash值的范围,这个范围过小会发生碰撞
    while(~scanf("%d %d %d",&n,&m,&t)){
        for(int i=1;i<=n;i++){//给vector分配大小
            bit[i].resize(m+2);a[i].resize(m+2);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&x);
                a[i][j]=x;
            }
        }
        for(int i=1;i<=t;i++){
            scanf("%d %d %d %d %d",&x,&y,&_x,&_y,&k);
            update(x,y,_x,_y,_rand[k]);//区域更新
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(query(i,j)%_rand[a[i][j]]!=0)ans++;
            }
        }
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/kzn2683331518/article/details/81154399