hdu 5091(线段树+扫描线)

时间:2023-07-21 22:54:14
hdu 5091(线段树+扫描线)

上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题。 最近学习了下线段树扫描线才发现确实是挺水的一道题。

hdu5091

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <stdlib.h>
using namespace std;
#define N 10010
#define SN 500000 struct node
{
int x,y;
}g[N]; int n,w,h;
int l[SN],r[SN],mark[SN],num[SN];//肯定是要标号的 int cmp(node t,node t1)
{
return t.y<t1.y;
} void build(int tl,int tr,int s)
{
l[s]=tl; r[s]=tr;
mark[s]=; num[s]=;
if(tl==tr) return ;
int mid=(tl+tr)/;
build(tl,mid,*s);
build(mid+,tr,*s+);
} void up(int s)
{
if(mark[s]>)
{
if(l[s]==r[s])
num[s]=mark[s];
else
{
num[s]=max(num[*s],num[*s+])+mark[s];
}
}
else
{
if(l[s]==r[s])
num[s]=;
else num[s]=max(num[*s],num[*s+]);
}
} void update(int tl,int tr,int sign,int s)
{
if(tl==l[s]&&tr==r[s])
{
mark[s]+=sign;
up(s);
return ;
}
int mid=(l[s]+r[s])/;
if(tr<=mid) update(tl,tr,sign,*s);
else if(tl>mid) update(tl,tr,sign,*s+);
else
{
update(tl,mid,sign,*s);
update(mid+,tr,sign,*s+);
}
up(s);
} int main()
{
while(scanf("%d",&n)&&n>=)
{
scanf("%d%d",&w,&h);
int mxx=-,mix=; for(int i=;i<n;i++)
{
scanf("%d%d",&g[i].x,&g[i].y);
g[i].x+=;
g[i].y+=;//这个没有关系吧
mxx=max(mxx,g[i].x);
mix=min(mix,g[i].x);
} //又有负数 。。。
if(n==)
{
printf("1\n");
continue;
}
sort(g,g+n,cmp);
int ans=;
build(mix,mxx+w,);
update(g[].x,g[].x+w,,);
int i=,j=; while()
{
if(g[j].y-g[i].y<=h) //表示j这边还可以添加
{
update(g[j].x,g[j].x+w,,);
ans=max(ans,num[]);
j++;
if(j>=n) break;
}
else
{
update(g[i].x,g[i].x+w,-,);
i++;
}
}
printf("%d\n",ans);
}
return ;
}