hdu 3308:LCIS

Problem - 3308

线段树加一点dp

这里维护一个每个区间的{fmax,lmax,rmax},分别代表最长递增序列长度,以左端点开始的递增区间长度,以右端点结尾的递增序列长度。

当前根的fmax=max{left.fmax, right.fmax, left.rmax+right.lmax}

当然,第三项需要左右区间的大小关系满足才行。

当前根的lmax=left.lmax+rmax.lmax,此处的第二项需要满足left.lmax等于左区间长度并且大小关系满足。

接下来写好线段树就ok了。

代码如下所示:

#include <iostream>
#include <algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int MAXN=1e5+1;
int w[MAXN];
struct node{
    int l, r;
    int lmax,rmax,fmax;
}tree[MAXN<<2];

void push_up(node &rt, const node& l_rt, const node& r_rt){
    const int llen = l_rt.r-l_rt.l+1, rlen = r_rt.r-r_rt.l+1;
    const int LR = w[l_rt.r], RL = w[r_rt.l];// 区间值
    rt.fmax = max({l_rt.fmax, r_rt.fmax, RL>LR ? l_rt.rmax+r_rt.lmax : 0});
    rt.lmax = l_rt.lmax + (l_rt.lmax==llen && RL>LR ? r_rt.lmax : 0);// 只有满足大小关系才有机会扩大序列大小
    rt.rmax = r_rt.rmax + (r_rt.rmax==rlen && RL>LR ? l_rt.rmax : 0);
}

void push_up(int rt){
    push_up(tree[rt], tree[rt<<1], tree[rt<<1|1]);
}

void build(int l,int r,int rt){
    tree[rt] = {l,r,1,1,1};
    if (l>=r){
        return;
    }
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int a,int val,int l,int r,int rt){
    if (tree[rt].l==tree[rt].r){
        w[a]=val;
        return;
    }
    int mid = (l+r)>>1;
    if (a<=mid){
        update(a,val,lson);
    }else{
        update(a,val,rson);
    }
    push_up(rt);
}

node query(int ul,int ur,int l,int r,int rt){
    if (ul<=l && r<=ur) return tree[rt];
    int mid = (l+r)>>1;

    if (ur<=mid) return query(ul,ur,lson);
    if (ul>mid) return query(ul,ur,rson);
    node left = query(ul,ur,lson);
    node right = query(ul,ur,rson);
    node res={left.l,right.r,0,0,0};
    push_up(res, left, right);
    return res;
}

int main(){
    int kase;
    cin>>kase;
    while (kase--){
        int n,m,a,b;
        char op[2];
        cin>>n>>m;
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &w[i]);
        }
        build(1,n,1);
        for (int i = 0; i < m; ++i) {
            scanf("%s", op);
            scanf("%d %d", &a,&b);
//            cout<<op<<","<<a<<","<<b<<endl;
            if (op[0]=='U'){
                update(++a,b,1,n,1);
            } else {
                a++,b++;
                printf("%d\n", query(a,b,1,n,1).fmax);

            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值