HDU 5091---Beam Cannon(线段树+扫描线)
2018-06-17 23:40:23来源:未知 阅读 ()
题目链接
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+5; int n,w,h; struct Node { int x,y,v; }node[2*N]; struct TNode { int f; int m; }tr[16*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=0; tr[i].m=0; if(l==r) return ; int mid=(l+r)>>1; build(l,mid,i<<1); build(mid+1,r,i<<1|1); } void pushdown(int i) { tr[i<<1].f+=tr[i].f; tr[i<<1|1].f+=tr[i].f; tr[i<<1].m+=tr[i].f; tr[i<<1|1].m+=tr[i].f; tr[i].f=0; } 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!=0) pushdown(i); int mid=(l+r)>>1; if(node[t].y<=mid) update(l,mid,i<<1,t); if(node[t].y+h>mid) update(mid+1,r,(i<<1|1),t); tr[i].m=max(tr[i<<1].m,tr[i<<1|1].m); } int main() { while(scanf("%d",&n)&&n>0) { scanf("%d%d",&w,&h); for(int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); node[2*i-1].x=x; node[2*i-1].y=y+2*N; node[2*i-1].v=1; node[2*i].x=x+w; node[2*i].y=y+2*N; node[2*i].v=-1; } sort(node+1,node+2*n+1,cmp); build(1,4*N,1); int sum=0; for(int i=1;i<=2*n;i++) { update(1,4*N,1,i); sum=max(sum,tr[1].m); } printf("%d\n",sum); } return 0; }
补充一下: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[3005]; struct TNode { LL m; LL f; }tr[20005]; 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=0; tr[i].f=0; if(l==r) return ; LL mid=(l+r)>>1; build(l,mid,i<<1); build(mid+1,r,i<<1|1); } void pushdown(LL i) { tr[i<<1].f+=tr[i].f; tr[i<<1|1].f+=tr[i].f; tr[i<<1].m+=tr[i].f; tr[i<<1|1].m+=tr[i].f; tr[i].f=0; } 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)>>1; if(L<=mid) update(l,mid,i<<1,t); if(R>mid) update(mid+1,r,i<<1|1,t); tr[i].m=max(tr[i<<1].m,tr[i<<1|1].m); } int main() { LL n,r; while(scanf("%lld%lld",&n,&r)!=EOF) { p.clear(); for(LL i=1;i<=n;i++) { LL x,y; scanf("%lld%lld",&x,&y); node[3*i-2].x=x; node[3*i-2].y=y; node[3*i-2].v=1; node[3*i-1].x=x+r; node[3*i-1].y=y; node[3*i-1].v=-1; node[3*i].x=x; node[3*i].y=y+r; node[3*i].v=0; } sort(node+1,node+3*n+1,cmp2); ///离散化 LL tot=0,pre=-1; for(LL i=1;i<=3*n;i++) { if(node[i].y!=pre){ pre=node[i].y; p[pre]=++tot; } } sort(node+1,node+3*n+1,cmp1); build(1,tot,1); LL sum=0; for(LL i=1;i<=3*n;i++) { if(node[i].v==0) continue; L=p[node[i].y]; R=p[node[i].y+r]; update(1,tot,1,node[i].v); sum=max(sum,tr[1].m); } printf("%lld\n",sum); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash