HDU 5091---Beam Cannon(线段树+扫描线)

时间:2022-02-08 01:17:03

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5091

Problem Description
Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on the Planet X, and it is very powerful, which can destroy all the spaceships in its attack range in a second. However, it takes a long time to fill the energy of the Beam Cannon after each shot. So, you should make sure each shot can destroy the enemy spaceships as many as possible.

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.

 
Input
Input contains multiple test cases. Each test case contains three integers N(1<=N<=10000, the number of enemy spaceships), W(1<=W<=40000, the width of the Beam Cannon’s attack area), H(1<=H<=40000, the height of the Beam Cannon’s attack area) in the first line, and then N lines follow. Each line contains two integers x,y (-20000<=x,y<=20000, the coordinates of an enemy spaceship).

A test case starting with a negative integer terminates the input and this test case should not to be processed.

 
Output
Output the maximum number of enemy spaceships the Beam Cannon can destroy in a single shot for each case.
 
Sample Input
2 3 4
0 1
1 0
3 1 1
-1 0
0 1
1 0
-1
 
Sample Output
2
2
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 
 
题意:在平面上有n个点,现在有一个平行于坐标轴的矩形,宽为w 高为h,可以上下左右移动,但不能旋转,求这个矩形最多能够围住多少点?
 
思路:将输入的n个点按照横坐标从小到大排序,为了计算方便,在输入的时候可以对每个点记录一个标记点(打标记),如果输入的点是  node[i].x=x  node[i].y=y  node[i].v=1  那么 加一个标记点node[i+1].x=x+w  node[i+1].y=y  node[i+1].v= -1  这样对排序后的2*n个点进行计算时,只会有长为w范围的点会被计算,走出w长度范围的点会在计算node[i+1].v=-1 时清理掉。 那么现在考虑的点都在长为w的范围内,现在只需要考虑高了,可以用线段树计算对当前点node[i] 把node[i].y~node[i].y+h的区间长度都加上node[i].v  那么线段树求得的最大值就是当前区间点的值了, 为什么是这样呢? 因为考虑的点都在长为w范围里,所以只需要考虑y值,所以可以转换为这些点在一条直线上,求长为h的线段能覆盖最多的点数,考虑线段的上端点的位置,那么一个点对上端点的贡献为y~y+h 范围,端点的值就是这条线段的覆盖值;
 
代码如下:
#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(线段树+扫描线)的更多相关文章

  1. &lbrack;POI 2001&plus;2014acm上海邀请赛&rsqb;Gold Mine&sol;Beam Cannon 线段树&plus;扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  2. hdu 5091 Beam Cannon&lpar;扫描线段树&rpar;

    题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...

  3. HDU 1828&OpenCurlyDoubleQuote;Picture”(线段树&plus;扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  4. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  5. HDU 6096 String 排序 &plus; 线段树 &plus; 扫描线

    String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...

  6. HDU 5091 Beam Cannon (扫描线思想)

    题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...

  7. HDU 1542 Atlantis(线段树扫描线&plus;离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. hdu 5091 Beam Cannon

    题目大意: 有n个点(n<=10000),点的坐标绝对值不超过20000,然后问你用一个w*h(1<=w,h<=40000)的矩形,矩形的边平行于坐标轴,最多能盖住多少个点. 刘汝佳 ...

  9. HDU 3265 Posters ——(线段树&plus;扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm&gt ...

  10. 【42&period;49&percnt;】【hdu 1542】Atlantis&lpar;线段树扫描线简析&rpar;

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

随机推荐

  1. &lbrack;No000099&rsqb;软件版本命名规范

    软件版本阶段说明 Base: 此版本表示该软件仅仅是一个假页面链接,通常包括所有的功能和页面布局,但是页面中的功能都没有做完整的实现,只是做为整体网站的一个基础架构. Alpha: 此版本表示该软件在 ...

  2. 如何用Python实现目录遍历

    1. 基本实现 [root@localhost ~]# cat dirfile.py import os path='/tmp' for dirpath,dirnames,filenames in o ...

  3. Hadoop2&period;2&period;0环境下Sqoop1&period;99&period;3安装

    本文转载自http://blog.csdn.net/liuwenbo0920/article/details/40504045 1.安装准备工作: 已经装好的hadoop环境是hadoop 2.2.0 ...

  4. 解决IE9下JQuery的ajax失效的问题

    jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)   1. 设置浏览器安全属性,启用[通过域访问数据源]选项,如图:

  5. mysql数据库事件调度&lpar;Event&rpar;

    mysql中的事件调度器可以定时对数据库增加,删除和执行操作,相当于数据库中的临时触发器,与Linux系统中的执行计划任务一样,这样就可以大大降低工作量. 1.开启事件调度器 [root@node1 ...

  6. sqlserver 常用语句

    1.查询表中的RID RID=RowID=(fileID:pageID:slotID) SELECT sys.fn_PhysLocFormatter(%%physloc%%) AS rid,* FRO ...

  7. Java继承与清理

    [程序实例] import java.util.*; class Characteristic { private String s; Characteristic(String s) { this. ...

  8. 高速掌握sinox2014激动人心的ZFS和RAID技术

    Sinox2014引入激动人心的zfs系统以及其支持的RAID,让用户高速打造便宜的高可靠性文件server. ZFS文件系统的英文名称为Zettabyte File System,也叫动态文件系统( ...

  9. Gym 101673F Keeping On Track

    原题传送门 题意:给定一颗n+1(n≤10000)个结点的树(当然有n条边辣),定义一个结点为critical,当且仅当除去这个点及与其相连的边后,所有不相连的结点对数量最多.现在保证有且仅有一个结点 ...

  10. 海量数据挖掘MMDS week4&colon; 推荐系统Recommendation System

    http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...