Beam Cannon
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110 Accepted Submission(s): 433
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.
A test case starting with a negative integer terminates the input and this test case should not to be processed.
#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