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个农药,每个农药有一个喷洒区域和一个植物编号,代表只能喷洒这种植物
,如果农药不同于植物的编号,那么这个植物就会死亡,最后求农田里面死亡的植物数量 思路:我们看到了喷洒的是一个矩阵,如果我们是暴力肯定会超时,我们就可以想优化方法,有什么是同时操作一个矩阵里的东西的呢,我们就可以想到,树状数组
树状数组可以操作一个区间,二维树状数组就可以操作一个矩阵,又因为他是操作一个矩阵,所以是区间修改,单点查询的板子,其中用到了差分的概念,但是同时喷洒的我们又可能重复记录
这时我们就可以记录植物所在的位置,具体看代码注释
这里给篇大佬博客 二维树状数组:https://blog.csdn.net/z309241990/article/details/9615259
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#define NN 1000010
using namespace std;
struct sss
{
int x,y;
sss(){};
sss(int a,int b){x=a;y=b;};
};
struct node
{
int x1,y1;
int x2,y2;
node(int a,int b,int c,int d){x1=a;y1=b;x2=c;y2=d;};
};
vector<sss> mp[NN];
vector<int> c[NN];
vector<node> z[NN];
int n,m,k;
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int num)
{
for(int i=x;i<=n;i+=lowbit(i))
{
for(int j=y;j<=m;j+=lowbit(j))
{
c[i][j]+=num;
}
}
}
int sum(int x,int y)
{
int num=;
for(int i=x;i>;i-=lowbit(i))
{
for(int j=y;j>;j-=lowbit(j))
{
num+=c[i][j];
}
}
return num;
}
void sa(int x1,int y1,int x2,int y2,int num)
{
add(x1,y1,num);
add(x2+,y1,-num);
add(x1,y2+,-num);
add(x2+,y2+,num);
}
int main()
{
int x;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)//因为只给出了n*m的范围,范围无法确定,我们就动态分配空间
{
c[i].resize(m+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&x);
mp[x].push_back(sss(i,j));//这里存下了每种植物分别的(x,y)坐标
}
}
int a,b,c,d,e;
for(int i=;i<=k;i++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
z[e].push_back(node(a,b,c,d));//存下了每种农药的喷洒区域
sa(a,b,c,d,);//我们把洒了农药的区域都加一,这里就应用到了二维树状数组
}
int ans=;
for(int i=;i<=n*m;i++)//因为n*m的格子上每个植物可能都不同,所以我们要从1-n*m所有的都遍历一遍
{
if(mp[i].size())//因为他的植物编号可能不同,所以我们判断长度判断是否存在
{
for(int j=;j<z[i].size();j++)//我们先把编号i的植物农药所喷洒的区域全部去掉
{
struct node x=z[i][j];
sa(x.x1,x.y1,x.x2,x.y2,-);
}
for(int j=;j<mp[i].size();j++)//我们再判断编号i的植物是否还被喷洒过,如果还有,说明不止被同编号的农药喷洒过,就会死亡,所以计数加1
{
struct sss x=mp[i][j];
if(sum(x.x,x.y)) ans++;
}
for(int j=;j<z[i].size();j++)//我们再把刚刚减去的区域恢复过来,用同种方法测试其他植物
{
struct node x=z[i][j];
sa(x.x1,x.y1,x.x2,x.y2,);
}
}
}
printf("%d",ans);
}
 

牛客第二场 J farm的更多相关文章

  1. 牛客第二场A-run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 White Cloud is exercising in the playground. Wh ...

  2. 牛客第二场Dmoney

    链接:https://www.nowcoder.com/acm/contest/140/D 来源:牛客网 题目描述 White Cloud has built n stores numbered to ...

  3. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

  4. 18牛客多校训练第二场 J farm

    题意:一个n×m的农田, 每个小格子都有一种作物, 现在喷t次农药,每次农药覆盖一个矩形, 该矩形里面与农药类型不同的植物都会死掉, 求最后植物的死亡数是多少. 题解:二维树状数组. 每次喷农药的时候 ...

  5. 牛客第二场-J-farm-二维树状数组

    二维树状数组真的还挺神奇的,更新也很神奇,比如我要更新一个区域内的和,我们的更新操作是这样的 add(x1,y1,z); add(x2+1,y2+1,z); add(x1,y2+1,-z); add( ...

  6. 第k小团(Bitset+bfs)牛客第二场 -- Kth Minimum Clique

    题意: 给你n个点的权值和连边的信息,问你第k小团的值是多少. 思路: 用bitset存信息,暴力跑一下就行了,因为满足树形结构,所以bfs+优先队列就ok了,其中记录下最后进入的点(以免重复跑). ...

  7. 牛客第二场 C.message(计算几何+二分)

    题目传送:https://www.nowcoder.com/acm/contest/140/C 题意:有n个云层,每个云层可以表示为y=ax+b.每个飞机的航线可以表示为时间x时,坐标为(x,cx+d ...

  8. 走环概率问题(至今有点迷)--牛客第二场( Eddy Walker)

    思路: 概率结论题,好像属于线性递推,现在也不太懂(lll¬ω¬) #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include < ...

  9. 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论

    LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...

随机推荐

  1. 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,使用过程的一个坑

    一.mysql表结构中存在如下设计时 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,如下 `updated_time` datetime NOT NU ...

  2. 2019/01/17 基于windows使用fabric将gitlab的文件远程同步到服务器(git)

    觉得django项目把本地更新push到gitlab,再执行fabric脚本从gitlab更新服务器项目挺方便的,当然从本地直接到服务器就比较灵活. 2019/01/17 基于windows使用fab ...

  3. 框架中如何根据fileupload工具包实现文件上传功能

    工具包 Apache-fileupload.jar – 文件上传核心包. Apache-commons-io.jar – 这个包是fileupload的依赖包.同时又是一个工具包. 代码 servle ...

  4. 反射API(一)

    <?php function classData(ReflectionClass $class) { echo '<hr>'; $details = '当前文件:'; $detail ...

  5. Excel文件的读写

    import xlsxwriter,xlrd import sys,os.path fname = 'zm6.xlsx' if not os.path.isfile(fname): print ('文 ...

  6. Matlab-1:jacobi迭代法工具箱

    function [u,n]=Jacobi(A,b,u0,eps,varargin) %Jacobi.m函数为用于雅可比迭代法求解线性方程组 %A为线性方程组的系数矩阵 %b为线性方程组的常数向量 % ...

  7. Oracle 多表查询、查询运算符和集合运算

    一.多表查询 1.内连接 一般使用INNER JOIN关键字指定内连接,INNER可以省略,默认表示内连接.查询结果中只包含两表的公共字段值相等的行,列可以是两表中的任意列 2.外连接 包括左外连接. ...

  8. vue element upload图片 回显问题

      beforeUpload (file) { var _this = this; var reader = new FileReader(); reader.readAsDataURL(file); ...

  9. Parse error: syntax error, unexpected end of file in * 的解决办法

    这个原因很简单,就是你的php语法错误. 在你的php代码种出现了<?  ?>  标准的是<?php ?>

  10. WDA基础十:OVS搜索帮助的使用

    对于WDA来说,常用的搜索帮助有OVS,标准SH,Interface view等.今天来说说两种常用的OVS的使用: 一:普通字段,表字段的搜索帮助(在创建节点的时候指定搜索帮助OVS,或者后面加上去 ...