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]=0; num[s]=0;
    if(tl==tr) return ;
    int mid=(tl+tr)/2;
    build(tl,mid,2*s);
    build(mid+1,tr,2*s+1);
}

void up(int s)
{
    if(mark[s]>0)
    {
        if(l[s]==r[s])
            num[s]=mark[s];
        else
        {
            num[s]=max(num[2*s],num[2*s+1])+mark[s];
        }
    }
    else
    {
        if(l[s]==r[s])
            num[s]=0;
        else num[s]=max(num[2*s],num[2*s+1]);
    }
}

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])/2;
    if(tr<=mid) update(tl,tr,sign,2*s);
    else if(tl>mid) update(tl,tr,sign,2*s+1);
    else
    {
        update(tl,mid,sign,2*s);
        update(mid+1,tr,sign,2*s+1);
    }
    up(s);
}

int main()
{
    while(scanf("%d",&n)&&n>=1)
    {
        scanf("%d%d",&w,&h);
        int mxx=-100000000,mix=100000000;

        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&g[i].x,&g[i].y);
            g[i].x+=20020;
            g[i].y+=20020;//这个没有关系吧
            mxx=max(mxx,g[i].x);
            mix=min(mix,g[i].x);
        }

        //又有负数 。。。
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        sort(g,g+n,cmp);
        int ans=1;
        build(mix,mxx+w,1);
        update(g[0].x,g[0].x+w,1,1);
        int i=0,j=1;

        while(1)
        {
            if(g[j].y-g[i].y<=h) //表示j这边还可以添加
            {
                update(g[j].x,g[j].x+w,1,1);
                ans=max(ans,num[1]);
                j++;
                if(j>=n) break;
            }
            else
            {
                update(g[i].x,g[i].x+w,-1,1);
                i++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值