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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章