题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5091
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 <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=1e4+;
int n,w,h;
struct Node
{
int x,y,v;
}node[*N];
struct TNode
{
int f;
int m;
}tr[*N]; bool cmp(const Node s1,const Node s2)
{
if(s1.x==s2.x) return s1.v>s2.v;
return s1.x<s2.x;
}
void build(int l,int r,int i)
{
tr[i].f=; tr[i].m=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,i<<);
build(mid+,r,i<<|);
}
void pushdown(int i)
{
tr[i<<].f+=tr[i].f;
tr[i<<|].f+=tr[i].f;
tr[i<<].m+=tr[i].f;
tr[i<<|].m+=tr[i].f;
tr[i].f=;
}
void update(int l,int r,int i,int t)
{
if(l>=node[t].y&&r<=node[t].y+h)
{
tr[i].f+=node[t].v;
tr[i].m+=node[t].v;
return ;
}
if(tr[i].f!=) pushdown(i);
int mid=(l+r)>>;
if(node[t].y<=mid) update(l,mid,i<<,t);
if(node[t].y+h>mid) update(mid+,r,(i<<|),t);
tr[i].m=max(tr[i<<].m,tr[i<<|].m);
}
int main()
{
while(scanf("%d",&n)&&n>)
{
scanf("%d%d",&w,&h);
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
node[*i-].x=x;
node[*i-].y=y+*N;
node[*i-].v=;
node[*i].x=x+w;
node[*i].y=y+*N;
node[*i].v=-;
}
sort(node+,node+*n+,cmp);
build(,*N,);
int sum=;
for(int i=;i<=*n;i++)
{
update(,*N,,i);
sum=max(sum,tr[].m);
}
printf("%d\n",sum);
}
return ;
}
补充一下:HDU 4007 和这题相似(2016 ICPC大连站热身赛)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4007
题意:给了n个点,求一个正方形能围住的最大点数,同样正方形平行于坐标轴;
思路:与上面的题一样,但是这题数据范围很大,线段树的数组开不了这么大,那么必须要进行离散化;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
LL L,R;
map<LL,LL>p;
struct Node
{
LL x,y;
LL v;
}node[];
struct TNode
{
LL m;
LL f;
}tr[];
bool cmp1(const Node s1,const Node s2)
{
if(s1.x==s2.x) return s1.v>s2.v;
return s1.x<s2.x;
}
bool cmp2(const Node s1,const Node s2)
{
return s1.y<s2.y;
}
void build(LL l,LL r,LL i)
{
tr[i].m=;
tr[i].f=;
if(l==r) return ;
LL mid=(l+r)>>;
build(l,mid,i<<);
build(mid+,r,i<<|);
}
void pushdown(LL i)
{
tr[i<<].f+=tr[i].f;
tr[i<<|].f+=tr[i].f;
tr[i<<].m+=tr[i].f;
tr[i<<|].m+=tr[i].f;
tr[i].f=;
}
void update(LL l,LL r,LL i,LL t)
{
if(l>=L&&r<=R){
tr[i].f+=t;
tr[i].m+=t;
return ;
}
pushdown(i);
LL mid=(l+r)>>;
if(L<=mid) update(l,mid,i<<,t);
if(R>mid) update(mid+,r,i<<|,t);
tr[i].m=max(tr[i<<].m,tr[i<<|].m);
}
int main()
{
LL n,r;
while(scanf("%lld%lld",&n,&r)!=EOF)
{
p.clear();
for(LL i=;i<=n;i++)
{
LL x,y;
scanf("%lld%lld",&x,&y);
node[*i-].x=x;
node[*i-].y=y;
node[*i-].v=;
node[*i-].x=x+r;
node[*i-].y=y;
node[*i-].v=-;
node[*i].x=x;
node[*i].y=y+r;
node[*i].v=;
}
sort(node+,node+*n+,cmp2);
///离散化
LL tot=,pre=-;
for(LL i=;i<=*n;i++)
{
if(node[i].y!=pre){
pre=node[i].y;
p[pre]=++tot;
}
}
sort(node+,node+*n+,cmp1);
build(,tot,);
LL sum=;
for(LL i=;i<=*n;i++)
{
if(node[i].v==) continue;
L=p[node[i].y];
R=p[node[i].y+r];
update(,tot,,node[i].v);
sum=max(sum,tr[].m);
}
printf("%lld\n",sum);
}
return ;
}