牛客网暑期ACM多校训练营(第二场)J farm (二维区间更新,单点查询 + 哈希)

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

题意很简单,就是给你一个N*M矩阵,矩阵里面有不同的植物(用一个数表示),然后有T次操作,在一个小矩阵中放入一个k类农药,如果农药和植物不同(数值不同)植物会死去,问你经过T次后,有多少个植物会死去。

思路:看了这篇博客链接,很巧妙,直接利用二维树状数组进行区间更新,再单点查询每个点最后的和,我们只要判断这个和是不是那个植物数值的倍数就行了,如果不是就证明这个点被喷上了其他的农药。不过因为最开始的植物的数值都是比较小的在1e6以内,特别容易发生碰撞,我们要把对于数值映射到大于1e6的范围,这样才能得到正确的结果。

代码如下:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pb push_back
const int MAX = 1000010;
const ll Base = (ll)1e6;
ll Hash[MAX];
int N,M;
vector<ll> G[MAX],bit[MAX];
void init(){
    srand(time(NULL));
    for(int i=1;i<=MAX-5;++i){
        Hash[i] = (ll)rand()*Base+rand()*(ll)i;
        //我觉得映射某个数还是要结合这个数字,有的是没有用i,直接是rand()*rand(),我觉得这样映射会发生碰撞。
    }
    for(int i=1;i<=N;++i){
        G[i].resize(M+1);
        bit[i].resize(M+1);
    }
    //设置二维矩阵大小
}
/**
二维数组数组部分
*/
int lowbit(int x){
    return x&-x;
}
ll Sum(int x,int y){
    ll s = 0;
    for(int i=x;i>=1;i-=lowbit(i)){
        for(int j=y;j>=1;j-=lowbit(j)){
            s += bit[i][j];
        }
    }
    return s;
}
void Add(int x,int y,ll v){
    for(int i=x;i<=N;i+=lowbit(i)){
        for(int j=y;j<=M;j+=lowbit(j)){
            bit[i][j] += v;
        }
    }
}
int main(void){
    int T;
    //这里必须要加速,否则会超时。
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> M >> T;
    init();
    ll x;
    for(int i=1;i<=N;++i){
        for(int j=1;j<=M;++j){
            cin >> x;
            G[i][j] = x;
        }
    }
    int x1,y1,x2,y2,k;
    for(int i=1;i<=T;++i){
        cin >> x1 >> y1 >> x2 >> y2 >> k;
        Add(x1,y1,Hash[k]);
        Add(x2+1,y2+1,Hash[k]);
        Add(x1,y2+1,-Hash[k]);
        Add(x2+1,y1,-Hash[k]);
    }
    int sum = 0;
    for(int i=1;i<=N;++i){
        for(int j=1;j<=M;++j){
            if(Sum(i,j) % Hash[G[i][j]] != 0)
                sum++;
        }
    }
    cout << sum << endl;


    return 0;
}

为什么要这样更新,是和区间有关的,可以看一下下图
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值