hdu 5091 Beam Cannon(线段树+扫描线+离散化)

Beam Cannon

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 457    Accepted Submission(s): 175

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

题意:在一个平面内有N个人,用一个W*H的矩形去围这些人(边上的也算), 求最大人数。

思路:以x从小到大排序,y值离散化,投影到y轴上,那么对于每个人的纵坐标,y,y+h就是

每个星星可以影响到的矩形 然后x,x+w+1就是一个进入事件和一个出去事件,其所带的值互

为相反数. node[1].val 保存当前的最大值 当所有的矩形都遍历一遍 取其中的最大值就是ans。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=20005;

struct node
{
    int x,y1,y2,val;
    void fun(int xx,int yy1,int yy2,int v)
    {
        x=xx,y1=yy1,y2=yy2;
        val=v;
    }
}b[maxn];
struct tree
{
    int l,r,add,val;
}a[maxn*4];

int n,w,h,cnt,Y[maxn];

bool cmp(node p,node q)
{
    return p.x<q.x;
}

void build(int l,int r,int k)
{
    a[k].l=l,a[k].r=r;
    a[k].add=a[k].val=0;
    if(l==r)  return ;
    int mid=(l+r)/2;
    build(l,mid,2*k);
    build(mid+1,r,2*k+1);
}

void pushdown(int k)
{
    a[2*k].val+=a[k].add;
    a[2*k].add+=a[k].add;
    a[2*k+1].val+=a[k].add;
    a[2*k+1].add+=a[k].add;
    a[k].add=0;
}

void insert(int l,int r,int c,int k)
{
    if(Y[a[k].l]==l && Y[a[k].r]==r)
    {
         a[k].val+=c;
         a[k].add+=c;
    }
    else
    {
         pushdown(k);
         int mid=(a[k].l+a[k].r)/2;
         if(Y[mid]>=r)       insert(l,r,c,2*k);
         else if(Y[mid]<l)   insert(l,r,c,2*k+1);
         else
         {
              insert(l,Y[mid],c,2*k);
              insert(Y[mid+1],r,c,2*k+1);
         }
         a[k].val=max(a[2*k].val,a[2*k+1].val);
    }
}

void input()
{
    cnt=0;
    int x,y;
    scanf("%d %d",&w,&h);
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        y+=20000;
        Y[cnt]=y;
        b[cnt++].fun(x,y,y+h,1);
        Y[cnt]=y+h;
        b[cnt++].fun(x+w+1,y,y+h,-1);
    }
    sort(Y,Y+cnt);
    sort(b,b+cnt,cmp);
    cnt=unique(Y,Y+cnt)-Y;
}

void solve()
{
    build(0,cnt-1,1);
    int ans=0;
    for(int i=0;i<2*n;i++)
    {
        insert(b[i].y1,b[i].y2,b[i].val,1);
        ans=max(ans,a[1].val);
    }
    printf("%d\n",ans);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n<0)  break;
        input();
        solve();
    }
    return 0;
}
时间: 2024-10-06 13:43:37

hdu 5091 Beam Cannon(线段树+扫描线+离散化)的相关文章

hdu 5091 Beam Cannon(线段树扫描线)

题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,现在要有一个W?H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,假设有点(x,y),那么(x,y)~(x+W,y+H)形成的矩形,以框的右下角落的位置是可以圈住(x,y) 点,所以N个点即为N个矩形,求覆盖的最大次数,扫描线裸题. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&

[POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to retire by the end of the year. The Goldmine management would like to reward him in acknowledgment of his conscientious work. As a reward Byteman may rece

HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11551    Accepted Submission(s): 4906 Problem Description There are several ancient Greek texts that contain descriptions of the fabled

HDU5091 Beam Cannon(线段树扫描线)

#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn=40005; const int bw=20000; #define lson(x) ((x)<<1) #define rson(x) (((x)<<1)|1) int lc[maxn<<2],rc[maxn&

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <

hdu1542 Atlantis (线段树+扫描线+离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9032    Accepted Submission(s): 3873 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 5091 Beam Cannon 离散化+扫描线+线段树

Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 207 Problem Description Recently, the γ galaxies broke out Star Wars. Each planet is warring for resou

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1255 覆盖的面积 (线段树+扫描线+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化一下,数据大小就缩小了,那么之后只需要线段树单点更新就好了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <algor