hdu 4007 Dave 求矩形圈点最大值

对于每个点,扩成一个矩形,变成求最大重叠层数;

注意边界的处理,这里是横纵均加一;

可能在边界上会产生一些问题,在排序的时候修正;

#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
const int dir[4][2]={0,1,0,-1,1,0,-1,0};
#define maxn 2005

struct Tree
{
	int l,r;//左右端点
	int c;//覆盖情况
	int cnt,lf,rf;//cnt为层数,lf为实际左坐标,rf为实际右坐标
}tree[maxn*4];

struct Line
{
	int x,y1,y2;
	int f;//直线的标记
}line[maxn];

bool cmp(Line a,Line b)//sort排序的函数
{
    if(a.x!=b.x)
    	return a.x < b.x;
    else
	return a.f < b.f;
}  

int y[maxn];//记录y坐标

void build(int l,int r,int root)
{
	tree[root].l=l;tree[root].r=r;
	tree[root].c=tree[root].cnt=0;
	tree[root].lf=y[l];
	tree[root].rf=y[r];
	if(l+1==r)return;
	int mid=(l+r)>>1;
	build(l,mid,root<<1);
	build(mid,r,root<<1|1);
}

void calen(int root)
{
	if(tree[root].l+1==tree[root].r) tree[root].cnt=tree[root].c;
	else tree[root].cnt=max(tree[root<<1].cnt,tree[root<<1|1].cnt)+tree[root].c;
}

void update(int root,struct Line e)
{
	if(e.y1==tree[root].lf&&e.y2==tree[root].rf)
	{
		tree[root].c+=e.f;
		calen(root);
		return;
	}
	if(e.y2<=tree[root<<1].rf)update(root<<1,e);
	else if(e.y1>=tree[root<<1|1].lf)update(root<<1|1,e);
	else
	{
		Line tmp=e;
		tmp.y2=tree[root<<1].rf;
		update(root<<1,tmp);
		tmp=e;
		tmp.y1=tree[root<<1|1].lf;
		update(root<<1|1,tmp);
	}
	calen(root);
}

int main()
{
	int i,j;
	int n,r,k;
	int x1,y1,x2,y2;
	while(scanf("%d%d",&n,&r)!=EOF)
	{
		k=1;
		for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x1,&y1);
			x2=x1+r+1;
			y2=y1+r+1;
            line[k].x=x1;
            line[k].y1=y1;
            line[k].y2=y2;
            line[k].f=1;
            y[k]=y1;
            k++;
            line[k].x=x2;
            line[k].y1=y1;
            line[k].y2=y2;
            line[k].f=-1;
            y[k]=y2;
            k++;
        }
		k--;
		sort(line+1,line+k+1,cmp);
        sort(y+1,y+k+1);
		int mm;
		mm=unique(y+1,y+1+k)-y-1;
		build(1,mm,1);
		int ans=0;
		for(i=1;i<=k;i++)
		{
			update(1,line[i]);
			if(tree[1].cnt>ans)
				ans=tree[1].cnt;
		}
		printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值