[二维线段树](数组实现) HDU 1823 Luck and Love

思路:二维线段树模板题。第一维是身高,范围100-200,第二维是活泼度,范围0-1000。

一开始还在想浮点数要怎么建树,结果愣了半天发现只有一位小数。。。

网上大部分的代码是基于结构体实现,所以自己打了一个数组版本(因为是求最值又没有初始值,所以数组版本不需要Build函数)


#include <cmath>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(a) printf("Debug: %d\n",a)

#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define lhs l,m,lson(rt)
#define rhs m+1,r,rson(rt)
#define half(x) ((x)>>1) 

const int Mod = 1000000007;
const int maxn =1e5+50;
const int INF = 0x3f3f3f3f;
const double eps=1e-12;

int Max[820][4050];

void PushUp(int pa,int rt)
{
    Max[pa][rt]=max(Max[pa][lson(rt)],Max[pa][rson(rt)]);
}

void UpdateSub(int p,int pa,int add,int l,int r,int rt)
{
 // printf("UpdateSub p:%d pa:%d add:%d l:%d r:%d rt:%d\n",p,pa,add,l,r,rt);
    if (l==r)
    {
        Max[pa][rt]=max(add,Max[pa][rt]);
        return;
    }
    int m=half(l+r);
    if (p<=m) UpdateSub(p,pa,add,lhs);
    if (p>m) UpdateSub(p,pa,add,rhs);
    PushUp(pa,rt);
}

void Update(int p,int sonid,int add,int l,int r,int rt)
{
//  printf("Update p:%d sonid:%d add:%d l:%d r:%d rt:%d\n",p,sonid,add,l,r,rt);
    UpdateSub(sonid,rt,add,0,1000,1);
    if (l==r) return;
    int m=half(l+r);
    if (p<=m) Update(p,sonid,add,lhs);
    if (p>m) Update(p,sonid,add,rhs);
}

int QuerySub(int pa,int L,int R,int l,int r,int rt)
{
//	printf("QuerySub pa:%d L:%d R:%d l:%d r:%d rt:%d\n",pa,L,R,l,r,rt);
    if (L<=l && R>=r) return Max[pa][rt];
    int m=half(l+r);
    int res=-1;
    if (L<=m) res=max(res,QuerySub(pa,L,R,lhs));
    if (R>m)  res=max(res,QuerySub(pa,L,R,rhs));
    return res;
}

int Query(int L,int R,int Ls,int Rs,int l,int r,int rt)
{
//	printf("Query L:%d R:%d Ls:%d Rs:%d l:%d r:%d rt:%d\n",L,R,Ls,Rs,l,r,rt);
    if (L<=l && R>=r) return QuerySub(rt,Ls,Rs,0,1000,1);
    int m=half(l+r);
    int res=-1;
    if (L<=m) res=max(res,Query(L,R,Ls,Rs,lhs));
    if (R>m) res=max(res,Query(L,R,Ls,Rs,rhs));
    return res;
}

int N;
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif
    while (scanf("%d",&N)!=EOF && N)
    {
        mem(Max,-1);
        char c; int a,b; double d,e;
        
        for (int i=1;i<=N;i++)
        {
            cin>>c;
            if (c=='I')
            {
                scanf("%d %lf %lf",&a,&e,&d);
                Update(a,int(e*10),int(d*10),100,200,1);
            }
            else
            {
            	scanf("%d %d %lf %lf",&a,&b,&d,&e);
                if (a>b) swap(a,b);
                if (d>e) swap(e,d);
                double ans=double(Query(a,b,int(d*10),int(e*10),100,200,1));
                if (ans<0) puts("-1");
                else printf("%.1lf\n",ans/10);
            }
        }
    }
    return 0;
}

//结构体版参考资料
//http://www.tuicool.com/articles/EFb22u
//http://blog.csdn.net/leolin_/article/details/7265278


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值