HDOJ 5091 Beam Cannon走査線

4326 ワード

線分ツリー+走査線:
矩形の中心点でこの矩形を記述し、各敵艦について、矩形の中心の活動範囲を確立する.すなわち、矩形の中心がこの範囲内で活動すれば敵艦を覆うことができる.では、私たちが要求する問題は、いずれかの領域(矩形に違いない)が矩形でカバーできる最大値になる.
Beam Cannon
Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 159    Accepted Submission(s): 59
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
2014上海全国招待試合——テーマ再現(上海大学提供テーマに感謝)
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

const int maxn=30100;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct SEG
{
  double y1,y2,h;
  int d;
}seg[maxn<<2];

bool cmp(SEG a,SEG b)
{
  if(a.h==b.h) return a.d>b.d;
  return a.h<b.h;
}

int add[maxn<<2],mx[maxn<<2];
int n,sn;
double W,H;

double Y[maxn<<2];
int ny;

void push_up(int rt)
{
  mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}

void push_down(int rt)
{
  if(add[rt])
    {
      mx[rt<<1]+=add[rt]; mx[rt<<1|1]+=add[rt];
      add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt];
      add[rt]=0;
    }
}

void update(int L,int R,int D,int l,int r,int rt)
{
  if(L<=l&&r<=R)
    {
      add[rt]+=D;
      mx[rt]+=D;
      return ;
    }
  push_down(rt);
  int m=(l+r)/2;
  if(L<=m) update(L,R,D,lson);
  if(R>m) update(L,R,D,rson);
  push_up(rt);
}

int main()
{
  while(scanf("%d",&n)!=EOF&&n>0)
    {
      scanf("%lf%lf",&W,&H);
      sn=0; ny=0;
      for(int i=0;i<n;i++)
        {
          double x,y;
          scanf("%lf%lf",&x,&y);
          seg[sn++]=(SEG){y-H/2,y+H/2,x-W/2,1};
          seg[sn++]=(SEG){y-H/2,y+H/2,x+W/2,-1};
          Y[ny++]=y-H/2; Y[ny++]=y+H/2;
        }

      sort(seg,seg+sn,cmp);
      sort(Y,Y+ny);
      ny=unique(Y,Y+ny)-Y;

      memset(add,0,sizeof(add));
      memset(mx,0,sizeof(mx));

      int ans=0;
      for(int i=0;i<sn;i++)
        {
          int y1=lower_bound(Y,Y+ny,seg[i].y1)-Y+1;
          int y2=lower_bound(Y,Y+ny,seg[i].y2)-Y+1;
          update(y1,y2,seg[i].d,1,ny,1);
          ans=max(ans,mx[1]);
        }
      printf("%d
",ans); } return 0; }