hdu5091-Beam Cannon 线段树+扫描线 求矩形内最多能包含多少个点

Beam Cannon

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1110    Accepted Submission(s): 433


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
题目大意:给你一个矩形的宽度和长度,然后给你一些点的坐标,询问你,这个矩形能装多少个点。
解题思路:我们可以先对这些点进行排序,然后通过扫描线从下到上,依次求得最多点,得到最大的值即可。
ac代码
#include<stdio.h>
#include<string.h>#include<algorithm>
using namespace std;
#define maxn 20005
int tmp;
int maxv[maxn<<3],addv[maxn<<3];
struct dian{
    int x,y1,y2,w;
}a[maxn];
int cmp(dian n1,dian n2)
{
    if (n1.x<n2.x||(n1.x==n2.x&&n1.w>n2.w)) return 1;
    return 0;
}
void update(int root,int l,int r,int y1,int y2,int v)
{
    if (y1<=l&&y2>=r) addv[root]+=v;
    else{
        int mid=l+(r-l)/2;
        if (y1<=mid) update(root*2,l,mid,y1,y2,v);
        if (y2>mid) update(root*2+1,mid+1,r,y1,y2,v);
    }
    if (r==l) maxv[root]=0;
   else maxv[root]=max(maxv[root*2],maxv[root*2+1]);
    maxv[root]+=addv[root];
}
void query(int root,int l,int r,int y1,int y2,int add)
{
    if (y1<=l&&y2>=r) tmp=max(tmp,maxv[root]+add);
    else{
        int mid=l+(r-l)/2;
        if (y1<=mid) query(root*2,l,mid,y1,y2,add+addv[root]);
        if (y2>mid) query(root*2+1,mid+1,r,y1,y2,add+addv[root]);
    }
}
int main()
{
    int T,n,N,i,w,h,xx,yy,ans;
   while (~scanf("%d",&n)&&n!=-1)
   {
       scanf("%d%d",&w,&h); N=0;
       for (i=1;i<=n;i++)
       {
           scanf("%d%d",&xx,&yy);
           a[2*i].x=xx; a[2*i].y1=yy+20001; a[2*i].y2=yy+h+20001; a[2*i].w=1;//对y进行扫描 
           a[2*i-1].x=xx+w; a[2*i-1].y1=yy+20001; a[2*i-1].y2=yy+h+20001; a[2*i-1].w=-1;
           if (h+yy+20001>N) N=h+yy+20001;
       }
       sort(a+1,a+2*n+1,cmp);//离散化去重 
       memset(maxv,0,sizeof(maxv));
       memset(addv,0,sizeof(addv));
       ans=0;
       for (i=1;i<=2*n;i++)
       {
           update(1,1,N,a[i].y1,a[i].y2,a[i].w);
           tmp=0;
           query(1,1,N,1,N,0);
           ans=max(ans,tmp);
       }
       printf("%d\n",ans);
   }
}

题目链接: 点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=5091




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值