• HDU 1823 Luck and Love (二维线段树&区间最值)题解


    思路:

    树套树,先维护x树,再维护y树,多练练应该就能懂了

    代码:

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int N = 300+5;
    double node[205<<2][1005<<2];
    int n;
    void push_upx(int deep,int rt){
        node[deep][rt] = max(node[deep<<1][rt],node[deep<<1|1][rt]);
    }
    void push_upy(int deep,int rt){
        node[deep][rt] = max(node[deep][rt<<1],node[deep][rt<<1|1]);
    }
    void buildy(int ly,int ry,int deep,int rt){
        node[deep][rt] = -1;
        if(ly == ry) return;
        int m = (ly + ry) >> 1;
        buildy(ly,m,deep,rt << 1);
        buildy(m+1,ry,deep,rt << 1 | 1);
    }
    void buildx(int lx,int rx,int rt){
        buildy(0,1000,rt,1);
        if(lx == rx) return;
        int m = (lx + rx) >> 1;
        buildx(lx,m,rt << 1);
        buildx(m+1,rx,rt << 1 | 1);
    }
    void updatey(int act,double val,int ly,int ry,int deep,int rt){
        node[deep][rt] = max(node[deep][rt],val);
        if(ly == ry) return;
        int m = (ly + ry) >> 1;
        if(act <= m) updatey(act,val,ly,m,deep,rt << 1);
        else updatey(act,val,m + 1,ry,deep,rt << 1 | 1);
        push_upy(deep,rt);
    }
    void updatex(int h,int act,double val,int lx,int rx,int rt){    //更新h,act
        updatey(act,val,0,1000,rt,1);
        if(lx == rx) return;
        int m = (lx + rx) >> 1;
        if(h <= m) updatex(h,act,val,lx,m,rt << 1);
        else updatex(h,act,val,m + 1,rx,rt << 1 | 1);
    }
    double queryy(int actl,int actr,int ly,int ry,int deep,int rt){
        if(actl <= ly && ry <= actr) return node[deep][rt];
        int m = (ly + ry) >> 1;
        if(m >= actr)
            return queryy(actl,actr,ly,m,deep,rt << 1);
        else if(m < actl)
            return queryy(actl,actr,m + 1,ry,deep,rt << 1 | 1);
        return max(queryy(actl,actr,ly,m,deep,rt << 1),queryy(actl,actr,m + 1,ry,deep,rt << 1 | 1));
    }
    double queryx(int hl,int hr,int actl,int actr,int lx,int rx,int rt){
        if(hl <= lx && rx <= hr){
            return queryy(actl,actr,0,1000,rt,1);
        }
        int m = (lx + rx) >> 1;
        if(m >= hr)
            return queryx(hl,hr,actl,actr,lx,m,rt << 1);
        else if(m < hl)
            return queryx(hl,hr,actl,actr,m + 1,rx,rt << 1 | 1);
        return max(queryx(hl,hr,actl,actr,lx,m,rt << 1),queryx(hl,hr,actl,actr,m + 1,rx,rt << 1 | 1));
    }
    int main(){
        char o[2];
        int h1,h2;
        double l,a1,a2;
        while(scanf("%d",&n) && n){
            buildx(100,200,1);
            for(int i = 0;i < n;i++){
                scanf("%s",o);
                if(o[0] == 'I'){
                    scanf("%d%lf%lf",&h1,&a1,&l);
                    int A = (int)(a1*10);
                    updatex(h1,A,l,100,200,1);
                }
                else{
                    scanf("%d%d%lf%lf",&h1,&h2,&a1,&a2);
                    int A1 = (int)(a1*10),A2 = (int)(a2*10);
                    if(h1 > h2) swap(h1,h2);
                    if(A1 > A2) swap(A1,A2);
                    double ans = queryx(h1,h2,A1,A2,100,200,1);
                    if(ans == -1.0)
                        printf("-1
    ");
                    else
                        printf("%.1lf
    ",ans);
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    shell内置命令eval的具有什么作用
    openwrt中如何在一个软件包中使能busybox中的工具
    go语言中strings包中的Trim函数的作用是什么
    RedisTemplate的各种操作(set、hash、list、string)
    Spring data redis-StringRedisTemplate 用法
    Spring-data-redis 第一天
    Java操作Redis数据
    BootStrap之X-editable插件使用
    bootstrap editable有默认值
    bootstrap editable初始化后表单
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9408783.html
Copyright © 2020-2023  润新知