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. ADO.NET Entity Framework学习笔记(3)ObjectContext

    ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转]   说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject ...

  2. Mycat水平拆分之十种分片规则

    水平切分分片实现   配置schema.xml  在同一个mysql数据库中,创建了三个数据库 testdb1,testdb2,testdb3.并在每个库中都创建了user表     <?xml ...

  3. Spring Boot 获得帮助

    如果你在使用 Spring Boot 的时候遇到了问题,我们很乐意为你提供帮助. 请访问 IX. How-to指南 中的内容 — 在这个指南中为常见的多数问题提供了解决方案. 学习更多有关 Sprin ...

  4. Spring Boot 针对 Java 开发人员的安装指南

    Spring Boot 可以使用经典的开发工具或者使用安装的命令行工具.不管使用何种方式,你都需要确定你的 Java 版本为 Java SDK v1.8 或者更高的版本.在你开始安装之前,你需要确定你 ...

  5. Centos7 下coreseek的安装

    Coreseek介绍: Sphinx默认不支持中文索引及检索,基于Sphinx开发了Coreseek 全文检索服务器,Coreseek应该是现在用的最多的Sphinx中文全文检索,它提供了为Sphin ...

  6. 03 爬虫之selenium模块

    selenium模块 1.概念,了解selenium 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. seleniu ...

  7. css 选择器二

    2.4 盒模型 2.4.1 定义 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型 ...

  8. leetcode-algorithms-28 Implement strStr()

    leetcode-algorithms-28 Implement strStr() mplement strStr(). Return the index of the first occurrenc ...

  9. 完全揭秘log file sync等待事件-转自itpub

    原贴地址:http://www.itpub.net/thread-1777234-1-1.html   谢谢 guoyJoe 老大 这里先引用一下tanel poder大师的图: 什么是log fil ...

  10. css层叠性冲突中的优先级

    一.首先从CSS级别来进行优先级划分: CSS控制页面样式的四种方法: 1.行内样式 通过style特性 <p style=”color:#F00; background:#CCC; font- ...