hdu 1823 Luck and Love 二维线段树

第一次写二维线段树,题目算是简单,就是很多的陷阱;

题意比较的简单,就不说了;

二维线段树:就是在线段树里面嵌套一个线段树,可是由于结构的原因,一层线段树必须更新到底。如果各位大神有在一维不更新到底的方法,请提出来,我一定改正!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

#define maxn 1000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct par
{
    int d;
    int l,r;
    double s[maxn<<2];
}mp[200<<2];
int n;


void updateson(int h,int hp,double yf,int l,int r,int rt)
{
    if(l==r)
    {
        mp[h].s[rt]=mp[h].s[rt]>yf?mp[h].s[rt]:yf;
        return;
    }
    int m=(l+r)>>1;
    if(hp<=m)
        updateson(h,hp,yf,lson);
    if(m<hp)
        updateson(h,hp,yf,rson);
    mp[h].s[rt]=max(mp[h].s[rt<<1],mp[h].s[rt<<1|1]);
}

void update(int h,int hp,double yf,int l,int r,int rt)
{
    if(l==r)
    {
        updateson(h,hp,yf,0,1000,1);
        return ;
    }
    int m=(l+r)>>1;
    if(h<=m)
        update(h,hp,yf,lson);
    if(m<h)
        update(h,hp,yf,rson);
}
//在孩子上进行更新
double search_tree_son(int h,int h1,int h2,int l,int r,int rt)
{
    if(h1<=l&&r<=h2)
    {
        double x=mp[h].s[rt];
        return x;
    }
    int m=(l+r)>>1;
    double t1=-1;
    double t2=-1;
    if(h1<=m)
        t1=search_tree_son(h,h1,h2,lson);
    if(h2>m)
        t2=search_tree_son(h,h1,h2,rson);
    return max(t1,t2);
}

double search_tree(int L,int R,int h1,int h2,int l,int r,int rt)
{
    if(l==r)
    {
        return search_tree_son( l, h1, h2, 0, 1000, 1);//在身高的地方更新到底,想了想,如果不更新到底,是不太可能的,不知道大家有什么可以不更新到底的方法
    }
    int m=(l+r)>>1;
    double t1=-1;
    double t2=-1;
    if(L<=m)
        t1=search_tree(L,R,h1,h2,lson);
    if(m<R)
        t2=search_tree(L,R,h1,h2,rson);
    return max(t1,t2);
}

int main()
{
    char ch;
    double hp,yf;
    int h;
    int h1,h2;
    while(scanf("%d",&n),n)
    {
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
        {
           scanf("%*c");
           scanf("%c",&ch);
           if(ch=='I')
           {
               scanf("%d%lf%lf",&h,&hp,&yf);
               h-=100;
               hp*=10;//具体化
               int HP=hp;
               yf+=1.0;//加1.0,只是为了控制没有结果的情况
               update(h,HP,yf,0,100,1);
           }
           if(ch=='Q')
           {
               scanf("%d%d%lf%lf",&h1,&h2,&hp,&yf);
               h1-=100;
               h2-=100;
               hp*=10;
               yf*=10;
               int HP=hp;
               int YF=yf;
               if(h1>h2)//在这个地方WA了几次
                swap(h1,h2);
               if(HP>YF)
                swap(HP,YF);
               double x=search_tree(h1,h2,HP,YF,0,100,1);
               if(x==0)
                   printf("-1\n");
               else
                   printf("%.1f\n",x-1.0);
           }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值