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

来源:互联网 发布:淘宝刷单平台推荐 编辑:程序博客网 时间:2024/05/20 19:47

转自`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;}


原创粉丝点击