zoukankan      html  css  js  c++  java
  • 李超树详解

    李超树是个什么东西呢?

    其实就是一棵线段树。。。。

    我们来看这一道题

    其实就是这样一道题目

    在二维空间中插入一条直线,询问x=k的地方最上面一条直线的编号

    李超树储存的是区间[l,r]中'最优线段',

    最优线段,就是[l,r]中最暴露最长的线段

    可以发现在k处的答案就是

    所有包含此区间的最优线段中的最优解

    代码也好写

    # include<iostream>
    # include<cstdio>
    # include<algorithm>
    # include<cstring>
    # include<cmath>
    using namespace std;
    const int mn = 50005;
    int n,tot,tr[mn*4];
    double b[100005],k[100005];
    char s[200];
    double f(int id,int x)
    {
        return k[id]*(x-1)+b[id];
    }
    void update(int cur,int l,int r,int z)
    {
        if(l==r)
        {
            if(f(z,l) > f(tr[cur],l)) tr[cur]=z;
            return ;
        }
        int mid=l+r>>1;
        if(k[tr[cur]] < k[z])
        {
            if(f(z,mid) > f(tr[cur],mid)){
                update(cur<<1,l,mid,tr[cur]);
                tr[cur]=z;
            }
            else update(cur<<1|1,mid+1,r,z);
        }
        if(k[tr[cur]] > k[z])
        {
            if(f(z,mid) > f(tr[cur],mid))
            {
                update(cur<<1|1,mid+1,r,tr[cur]);
                tr[cur]=z;
            }
            else update(cur<<1,l,mid,z);
        }
    }
    double query(int cur,int l,int r,int x)
    {
        if(l==r)
            return f(tr[cur],x);
        int mid=l+r>>1;
        if(mid>=x)
            return max(f(tr[cur],x),query(cur<<1,l,mid,x));
        else return max(f(tr[cur],x),query(cur<<1|1,mid+1,r,x));
    }
    int main()
    {
        int x;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            if(s[0]=='P')
            {
                ++tot;
                scanf("%lf%lf",&b[tot],&k[tot]);
                update(1,1,mn-10,tot);
            }
            else {
                scanf("%d",&x);
                printf("%d
    ",int(query(1,1,mn-10,x)/100));
            }
        }
        return 0;
    }

     

  • 相关阅读:
    Python print() 函数
    Python issubclass() 函数
    Python execfile() 函数
    Python basestring() 函数
    QTP自动化测试-点滴-步骤
    qtp自动化测试-条件语句 if select case
    学习心态--笔记
    测试计划小记
    QTP自动化测试-笔记 注释、大小写
    win10 新建文件夹没有了
  • 原文地址:https://www.cnblogs.com/logeadd/p/9588240.html
Copyright © 2011-2022 走看看