HDU 3308 LCIS(区间合并)转自`Wind

本文介绍了一种基于线段树的数据结构实现方法,用于解决包含区间更新与查询最长连续递增子序列的问题。该算法利用了特殊的节点属性来优化查询效率,并提供了完整的C++代码实现。

转自`Wind

题意:Given n integers.
        You have two operations:
        U A B: replace the Ath number by B. (index counting from 0)
        Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].

分析:区间合并类线段树。

        las[maxn<<2]     区间左端起最长的序列长度
        ras[maxn<<2]    区间右端起最长的序列长度
        mov[maxn<<2]  区间最优值

好像12年省赛题差不多

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define havemid int m=(l+r)>>1
#define left (rt<<1)
#define right (rt<<1|1)
const int maxn=100100;
int las[maxn<<2];
int ras[maxn<<2];
int mov[maxn<<2];
int va[maxn<<2];
void pushup(int l,int r,int m,int rt){
    las[rt]=las[left];
    ras[rt]=ras[right];
    mov[rt]=0;
    if(va[m]<va[m+1]){
        mov[rt]=ras[left]+las[right];
        if(las[rt]==m-l+1)las[rt]+=las[right];
        if(ras[rt]==r-m)ras[rt]+=ras[left];
    }
    mov[rt]=max(mov[rt],max(mov[left],mov[right]));
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%d",&va[l]);
        mov[rt]=las[rt]=ras[rt]=1;
        return ;
    }
    havemid;
    build(lson);
    build(rson);
    pushup(l,r,m,rt);
}
void update(int p,int x,int l,int r,int rt){
    if(l==r){
        va[l]=x;
        return ;
    }
    havemid;
    if(p<=m)update(p,x,lson);
    else update(p,x,rson);
    pushup(l,r,m,rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
        return mov[rt];
    }
    havemid;
    if(R<=m)return query(L,R,lson);
    else if(L>m)return query(L,R,rson);
    else {
        int tmp=0;
        if(va[m]<va[m+1])
            tmp=min(ras[left],m-L+1)+min(las[right],R-m);
        int t1=query(L,R,lson);
        int t2=query(L,R,rson);
        int ret=max(tmp,max(t1,t2));
        return ret;
    }
}
int main(){
    int T,n,m,a,b;
    char s[10];
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--){
            scanf("%s",s);
            scanf("%d%d",&a,&b);
            if(s[0]=='Q')printf("%d\n",query(a+1,b+1,1,n,1));
            else update(a+1,b,1,n,1);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值