hdu 5091 线段树+扫描线

Beam Cannon

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


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

/*题意: 给了n个点在平面中 n<=10000,然后给了一个宽为W高为H的矩形,
 然后使得这个矩形可以涵盖最多的点有多少个,然后矩形的宽平行x轴,
 高平行y轴。可以将该矩形水平或者上下移动,求能选中最多多少个点。
 先把每个点(x,y),另添加一个退点(x+w,y),记录边的信息,
 枚举每个x值的点从小到大,选定区间后,把当前边的长度(y,y+h),
 当成一段区间,延迟更新线段树,求区间最多可以覆盖的点的个数*/
#include<stdio.h>
#include<algorithm>
#define N 80000
using namespace std;
struct node
{
    int x,y,z,p;
}bian[N],a[N*4];
bool cmp(node a,node b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    else
        return a.z>b.z;
}
int max(int a,int b)
{
    return a>b?a:b;
}
void build(int x,int y,int t)
{
    a[t].x=x; a[t].y=y; a[t].z=0; a[t].p=0;
    if(x==y)  return;
    int mid=(x+y)>>1,temp=t<<1;
    build(x,mid,temp);
    build(mid+1,y,temp+1);
}
void update(int x,int y,int t,int c)
{
    if(a[t].x==x&&a[t].y==y)
    {
        a[t].p+=c;
        a[t].z+=c;
        return;
    }
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(a[t].p)
    {
        a[temp].p+=a[t].p;
        a[temp].z+=a[t].p;
        a[temp+1].p+=a[t].p;
        a[temp+1].z+=a[t].p;
        a[t].p=0;
    }
    if(x>mid)
        update(x,y,temp+1,c);
    else  if(y<=mid)
        update(x,y,temp,c);
    else
    {
        update(x,mid,temp,c);
        update(mid+1,y,temp+1,c);
    }
    a[t].z=max( a[temp].z, a[temp+1].z);
}
int main()
{
    int n,w,h,i,ans,y,x;
    while(scanf("%d",&n),n>0)
    {
        scanf("%d%d",&w,&h);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);  y+=20000;
            bian[i*2].x=x; bian[i*2].y=y; bian[i*2].z=1;
            bian[i*2+1].x=x+w; bian[i*2+1].y=y; bian[i*2+1].z=-1;
        }
        sort(bian,bian+2*n,cmp);
        build(0,N,1);
        ans=0;
        for(i=0;i<2*n;i++)
        {
            y=bian[i].y+h;
            update(bian[i].y,y,1,bian[i].z);
            ans=max(ans,a[1].z);
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值