[BZOJ1568][JSOI2008]Blue Mary开公司(不下放标记的线段树)

本文介绍了一种使用线段树解决递增一次函数区间更新及查询的问题。通过递归方式实现区间覆盖,适用于需要频繁进行区间更新与查询操作的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

我是超链接

题解:

这绝对是我见过最敷衍的输出样例
可以发现这都是递增的一次函数
那么每次修改的时候我们都分三种情况:两端都大于(修改走人),两端都小于(走人),递归解决问题

代码:

#include <cstdio>
#include <iostream>
using namespace std;
const int N=50005;
struct hh{double s,p;}t[N*4];
double ans=0;
double Y(int x,double s,double p){return x*p+s;}
void change(int now,int l,int r,double s,double p)
{
    double yl=Y(l,t[now].s,t[now].p),nl=Y(l,s,p);
    double yr=Y(r,t[now].s,t[now].p),nr=Y(r,s,p);
    if (yl>=nl && yr>=nr) return;
    if (nl>=yl && nr>=yr){t[now].s=s;t[now].p=p;return;}
    int mid=(l+r)>>1;
    change(now<<1,l,mid,s,p);
    change(now<<1|1,mid+1,r,s,p);
}
void qurry(int now,int l,int r,int x)
{
    ans=max(ans,t[now].s+t[now].p*x);
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) qurry(now<<1,l,mid,x);
    else qurry(now<<1|1,mid+1,r,x); 
}
int main()
{
    int n;scanf("%d",&n);int T=50000;
    for (int i=1;i<=n;i++)
    {
        char st[10];scanf("%s",st);int x;double s,p;
        if (st[0]=='Q')
        {
            scanf("%d",&x);ans=0;
            qurry(1,1,T,x);
            printf("%d\n",(int)(ans/100));
        }
        else
        {
            scanf("%lf%lf",&s,&p);s-=p;
            change(1,1,T,s,p);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值