题目链接: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.

输入

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

输出

3

题意:

现有n行m列的矩阵a[1:n][1:m],代表一个农场,

而a[i][j]等于一个整数k,代表这个位置上的农作物是第 k 种,

且第 k 种农作物只能施加第 k 种肥料,施加别的种类肥料会使其死亡,

然后给出T次施肥方案(x1, y1, x2, y2, k),代表对(x1, y1)到(x2, y2)这一块矩阵上所有农作物施加第 k 种肥料,

现在要求知道这T次施肥方案过后,总共死掉了多少格农作物

题解:

先假设我们就用a[i][j]存储了这个格子上的农作物种类,

使用2D-BIT,不难想到,每次施加第 k 种肥,就给这整个矩阵所有格子全加上k,

那么最后我们枚举一遍整个矩阵的所有元素,若某一个元素不能整除a[i][j]就是死掉了,

但是这样一来会出问题,比如第3种农作物上试了一次第1种肥料,又施了一次第2种肥料,那么最后还是能整除3,

这时候需要用随机数哈希,把每个种类编号映射到一个比较大的随机数(大于本题的种类数最大值1e6),这样就能降低上述问题出现概率到一个可以接受的程度。

时间复杂度:预处理哈希$O\left( {nm} \right)$,输入农场$O\left( {nm} \right)$,T次施肥$O\left( {T \cdot \log n \cdot \log m} \right)$,暴力枚举n*m格子查询是否死亡$O\left( {nm \cdot \log n \cdot \log m} \right)$,在nm≤1e6的情况下,$nm \cdot \log n \cdot \log m$不会超过1e8。

(本题使用的是区间修改、单点查询2D-BIT,模板以及详解请传送门:树状数组进阶

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+; struct BIT_2D
{
int n,m;
vector<ll> C[maxn];
int lowbit(int x){return x&(-x);}
void init(int n,int m) //初始化n行m列矩阵
{
this->n=n;
this->m=m;
for(int i=;i<=n;i++)
{
C[i].clear();
for(int j=;j<=m;j++) C[i].push_back();
}
}
void add(int x,int y,ll val)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=m;j+=lowbit(j)) C[i][j]+=val;
}
void range_add(int x1,int y1,int x2,int y2,ll x) //左上角为(x1,y1)右下角为(x2,y2)的矩阵全部加上x
{
add(x1,y1,x);
add(x1,y2+,-x);
add(x2+,y1,-x);
add(x2+,y2+,x);
}
ll ask(int x,int y) //查询点(x,y)的值
{
ll ret=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j)) ret+=C[i][j];
return ret;
}
}BIT; int n,m,T;
vector<ll> a[maxn]; ll has[maxn];
inline ll get_rand(int m,int n)
{
return rand()%(n-m+)+m;
}
void myhash(int m)
{
srand(time(NULL));
for(int i=;i<=m;i++) has[i]=get_rand(1e6,9e6);
} int main()
{
scanf("%d%d%d",&n,&m,&T);
myhash(n*m);
BIT.init(n,m);
for(int i=;i<=n;i++)
{
a[i].clear(); a[i].push_back();
for(int j=,type;j<=m;j++)
{
scanf("%d",&type);
a[i].push_back(has[type]);
}
}
for(int i=;i<=T;i++)
{
int x1,y1,x2,y2,k;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
BIT.range_add(x1,y1,x2,y2,has[k]);
} int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(BIT.ask(i,j)%a[i][j] != ) ans++;
}
}
cout<<ans<<endl;
}

注:由于本题n和m给出的范围是n*m≤1e6,显然不能使用1e6*1e6的二维数组,只能用1e6个vector<long long>来代替。

2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  5. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  6. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  7. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  8. 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 NullPointerException【转载】

    PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEv ...

  2. 线程同步 –AutoResetEvent和ManualResetEvent

    上一篇介绍了通过lock关键字和Monitor类型进行线程同步,本篇中就介绍一下通过同步句柄进行线程同步. 在Windows系统中,可以使用内核对象进行线程同步,内核对象由系统创建并维护.内核对象为内 ...

  3. ios开发之--实现TextView提示文字并且输入字数限制

    实现字数控制和限制,并展示当前输入的字符串长度,以及按钮的点击状态,代码如下: 1,设置代理 @interface FKViewController ()<UITextViewDelegate& ...

  4. script 里写 html 模版

    js模版引擎(例如:template.js 或 handlebars.js)一般都用<script>标签来存放模版的内容 1)模版写在<script>标签和写在<div& ...

  5. Hbase 管理页面

    进入Hbase首页可以查看各种信息: http://192.168.23.128:16010/master-status

  6. Django SimpleCMDB WSGI

    一.WSGI 介绍 (1) 在前面的学习中,我们是通过 python manage.py runserver 0.0.0.0:8000 来启动并访问开发服务器的:(2) 但在实际中我们是通过直接访问 ...

  7. open-falcon之graph

    功能 存储agent push的数据 为query 提供查询数据接口 参考RRDtool的理念,在数据每次存入的时候,会自动进行采样.归档.在默认的归档策略,一分钟push一次的频率下, 历史数据保存 ...

  8. WP8.1学习系列(第二十四章)——Json解析

    .net已经集成了json解析,类名叫DataContractJsonSerializer DataContractJsonSerializer 类型公开以下成员. 构造函数     名称 说明 Da ...

  9. 【easyswoole】 解决安装报错

    在使用swoole 创建项目时候,报错 创建命令 composer create-project easyswoole/app easyswoole 错误信息: 解决办法,切换composer 源 镜 ...

  10. 浅谈CSS盒子模型

    [摘要]盒子模型是CSS中的一个重要概念,虽然CSS中没有盒子这个单独的属性对象,但它却是CSS中无处不在的一个重要组成部分.掌握盒子模型的原理和使用方法可以极大地丰富HTML元素的表现效果,同时对于 ...