[BZOJ1568][JSOI2008]Blue Mary开公司(超哥线段树)

211 篇文章 0 订阅
70 篇文章 0 订阅

题目描述

传送门

题解

超哥线段树模板题。
由于是第一次写超哥线段树,刚开始的时候写的有一些问题。超哥线段树具体做法如下:
修改
用一条新的直线k,b更新,首先求出当前区间直线k1,b1和新的直线k2,b2在mid处的函数值val1和val2。然后分情况讨论(kb为最终选择)
①k1=k2 b=max(b1,b2);
②k1>k2
1° val1>=val2 change(leftchild,k2,b2);
2° val1 < <script type="math/tex" id="MathJax-Element-1"><</script>val2 change(rightchild,k1,b1);k=k2,b=b2;
③k1 < <script type="math/tex" id="MathJax-Element-2"><</script>k2
1° val1>=val2 change(rightchild,k2,b2);
2° val1 < <script type="math/tex" id="MathJax-Element-3"><</script>val2 change(leftchile,k1,b1);k=k2,b=b2;
单点查询:为根到此节点路径上的max

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 100005

int n,t;
char opt[10];
struct hp{double s,p;}tr[N*4];
double s,p,ans;

void change(int now,int l,int r,double s,double p)
{
    int mid=(l+r)>>1;
    if (tr[now].s==0&&tr[now].p==0)
    {
        tr[now].s=s,tr[now].p=p;
        return;
    }
    double val1=tr[now].s+(mid-1.0)*tr[now].p;
    double val2=s+(mid-1.0)*p;
    if (l==r)
    {
        if (val2>val1) tr[now].s=s,tr[now].p=p;
        return;
    }
    if (tr[now].p==p)
    {
        tr[now].s=max(tr[now].s,s);
        return;
    }
    else if (tr[now].p>p)
    {
        if (val1>=val2)
            change(now<<1,l,mid,s,p);
        else
        {
            change(now<<1|1,mid+1,r,tr[now].s,tr[now].p);
            tr[now].s=s,tr[now].p=p;
        }
    }
    else
    {
        if (val1>=val2)
            change(now<<1|1,mid+1,r,s,p);
        else
        {
            change(now<<1,l,mid,tr[now].s,tr[now].p);
            tr[now].s=s,tr[now].p=p;
        }
    }
}
double query(int now,int l,int r,int x)
{
    int mid=(l+r)>>1;double ans=0;
    ans=max(ans,tr[now].s+(x-1.0)*tr[now].p);
    if (l==r) return ans;
    if (x<=mid) ans=max(ans,query(now<<1,l,mid,x));
    else ans=max(ans,query(now<<1|1,mid+1,r,x));
    return ans;
}
void print(double x)
{
    int ans=floor(x);
    ans/=100;
    printf("%d\n",ans);
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%s",opt);
        if (opt[0]=='P')
        {
            scanf("%lf%lf",&s,&p);
            change(1,1,50000,s,p);
        }
        else
        {
            scanf("%d",&t);
            ans=query(1,1,50000,t);
            print(ans);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值