HDU 5091---Beam Cannon(线段树+扫描线)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5091
To simplify the problem, the Beam Cannon can shot at any area in the space, and the attack area is rectangular. The rectangle parallels to the coordinate axes and cannot rotate. It can only move horizontally or vertically. The enemy spaceship in the space can be considered as a point projected to the attack plane. If the point is in the rectangular attack area of the Beam Cannon(including border), the spaceship will be destroyed.
A test case starting with a negative integer terminates the input and this test case should not to be processed.
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=1e4+;
int n,w,h;
struct Node
{
int x,y,v;
}node[*N];
struct TNode
{
int f;
int m;
}tr[*N]; bool cmp(const Node s1,const Node s2)
{
if(s1.x==s2.x) return s1.v>s2.v;
return s1.x<s2.x;
}
void build(int l,int r,int i)
{
tr[i].f=; tr[i].m=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,i<<);
build(mid+,r,i<<|);
}
void pushdown(int i)
{
tr[i<<].f+=tr[i].f;
tr[i<<|].f+=tr[i].f;
tr[i<<].m+=tr[i].f;
tr[i<<|].m+=tr[i].f;
tr[i].f=;
}
void update(int l,int r,int i,int t)
{
if(l>=node[t].y&&r<=node[t].y+h)
{
tr[i].f+=node[t].v;
tr[i].m+=node[t].v;
return ;
}
if(tr[i].f!=) pushdown(i);
int mid=(l+r)>>;
if(node[t].y<=mid) update(l,mid,i<<,t);
if(node[t].y+h>mid) update(mid+,r,(i<<|),t);
tr[i].m=max(tr[i<<].m,tr[i<<|].m);
}
int main()
{
while(scanf("%d",&n)&&n>)
{
scanf("%d%d",&w,&h);
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
node[*i-].x=x;
node[*i-].y=y+*N;
node[*i-].v=;
node[*i].x=x+w;
node[*i].y=y+*N;
node[*i].v=-;
}
sort(node+,node+*n+,cmp);
build(,*N,);
int sum=;
for(int i=;i<=*n;i++)
{
update(,*N,,i);
sum=max(sum,tr[].m);
}
printf("%d\n",sum);
}
return ;
}
补充一下:HDU 4007 和这题相似(2016 ICPC大连站热身赛)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4007
题意:给了n个点,求一个正方形能围住的最大点数,同样正方形平行于坐标轴;
思路:与上面的题一样,但是这题数据范围很大,线段树的数组开不了这么大,那么必须要进行离散化;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
LL L,R;
map<LL,LL>p;
struct Node
{
LL x,y;
LL v;
}node[];
struct TNode
{
LL m;
LL f;
}tr[];
bool cmp1(const Node s1,const Node s2)
{
if(s1.x==s2.x) return s1.v>s2.v;
return s1.x<s2.x;
}
bool cmp2(const Node s1,const Node s2)
{
return s1.y<s2.y;
}
void build(LL l,LL r,LL i)
{
tr[i].m=;
tr[i].f=;
if(l==r) return ;
LL mid=(l+r)>>;
build(l,mid,i<<);
build(mid+,r,i<<|);
}
void pushdown(LL i)
{
tr[i<<].f+=tr[i].f;
tr[i<<|].f+=tr[i].f;
tr[i<<].m+=tr[i].f;
tr[i<<|].m+=tr[i].f;
tr[i].f=;
}
void update(LL l,LL r,LL i,LL t)
{
if(l>=L&&r<=R){
tr[i].f+=t;
tr[i].m+=t;
return ;
}
pushdown(i);
LL mid=(l+r)>>;
if(L<=mid) update(l,mid,i<<,t);
if(R>mid) update(mid+,r,i<<|,t);
tr[i].m=max(tr[i<<].m,tr[i<<|].m);
}
int main()
{
LL n,r;
while(scanf("%lld%lld",&n,&r)!=EOF)
{
p.clear();
for(LL i=;i<=n;i++)
{
LL x,y;
scanf("%lld%lld",&x,&y);
node[*i-].x=x;
node[*i-].y=y;
node[*i-].v=;
node[*i-].x=x+r;
node[*i-].y=y;
node[*i-].v=-;
node[*i].x=x;
node[*i].y=y+r;
node[*i].v=;
}
sort(node+,node+*n+,cmp2);
///离散化
LL tot=,pre=-;
for(LL i=;i<=*n;i++)
{
if(node[i].y!=pre){
pre=node[i].y;
p[pre]=++tot;
}
}
sort(node+,node+*n+,cmp1);
build(,tot,);
LL sum=;
for(LL i=;i<=*n;i++)
{
if(node[i].v==) continue;
L=p[node[i].y];
R=p[node[i].y+r];
update(,tot,,node[i].v);
sum=max(sum,tr[].m);
}
printf("%lld\n",sum);
}
return ;
}
HDU 5091---Beam Cannon(线段树+扫描线)的更多相关文章
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- hdu 5091 Beam Cannon(扫描线段树)
题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
- HDU 6096 String 排序 + 线段树 + 扫描线
String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...
- HDU 5091 Beam Cannon (扫描线思想)
题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 5091 Beam Cannon
题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...
- HDU 3265 Posters ——(线段树+扫描线)
第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...
- 【42.49%】【hdu 1542】Atlantis(线段树扫描线简析)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
随机推荐
- NodeJS系列~第三个小例子,NodeJs与Redis实现高并发的队列存储
返回目录 众所周知 redis量个强大的缓存组件,可以部署在win32和linux环境之上,它有五大存储结构,其中有一种为列表list,它可以实现quene和stack的功能,即队列和堆栈的功能. r ...
- 3.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff。
23.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff. 具体要求如下: (1)Person类中的属性有:姓名name(String类型) ...
- Atitit usrQBK13 html dsl 规范与解决方案
Atitit usrQBK13 html dsl 规范与解决方案 1.1. Vue vs anrular1 1.2. 定义html dsl变量1 1.3. 变量赋值1 1.4. 条件渲染指令1 2 ...
- 常用Math 方法
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 11:26:44 * @version $Id$ */ Math.pow ...
- MySQL5.7.13源码编译安装指南
系统 CenterOs 6.5 1.安装依赖包(cmake make gcc等,其实好多都有了,不需要更新,为了防止世界被破坏,就装下) yum install gcc gcc-c++ -yyum i ...
- 新手入门:史上最全Web端即时通讯技术原理详解
前言 有关IM(InstantMessaging)聊天应用(如:微信,QQ).消息推送技术(如:现今移动端APP标配的消息推送模块)等即时通讯应用场景下,大多数都是桌面应用程序或者native应用较为 ...
- 那些年我们写过的T-SQL(下篇)
下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有?到目前为止很多大型传统企业仍然很依赖存储过程.这部分主要难理解的部分是事务和锁机制这块,本文会进行简单的阐述.虽然很 ...
- 【VS技巧】根据XML自动生成类型
.NET 4.5对应的VS版本(不要问我哪个版本)中新增了一个功能,严重实用,可以根据XML文档生成新类型.这个功能在VS的[编辑]>[选择性粘贴]菜单中.怎么玩?不急,咱们实际操作一下. 以网 ...
- Render OpenCascade Geometry Curves in OpenSceneGraph
在OpenSceneGraph中绘制OpenCascade的曲线 Render OpenCascade Geometry Curves in OpenSceneGraph eryar@163.com ...
- WCF传输1-你是否使用过压缩或Json序列化?
1.当遇到需要传输大量数据时,怎么样传输数据? 2.压缩数据有哪几种常见的方式? 问题1解答:通过压缩来传输数据 问题2解答: (1)WCF自带的压缩方式 (2)自定义WCF binding进行压缩 ...