HDU3308 LCIS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308

题意是给出一个序列,有两种操作,第一种是询问某个区间的最长的连续上升序列(即LCIS)是多长,第二种是把第A个数字修改为B,题目下标都是从0开始的。

对于一个区间,我们假设知道它左右两个子区间的LCIS,那么这个区间的LCIS可能是两者的较大值,也有可能是合并后中间的那一段,比如1 2 3 1 2 ,3 4 1 2 3 这两个区间合并后的情形。那么我们用线段树维护一下一个区间的左端点向右延伸的最长位置和右端点向左延伸的最长位置,比如上面那个例子,就是3、8,同时我们再维护一下这个区间的LCIS。查询的时候也要区间合并,由于是单点更新,只需要更新log n个节点即可。

#include<bits/stdc++.h>
using namespace std;
#define  LONG long long
#define lson l , mid , rt<< 1
#define rson mid + 1 ,  r , rt<<1|1
#define root 1,n,1
const int MAXN = 1e5 +20 ;
int val[100010] ;
struct Tree
{
    int rr , ll , Max ;
    int l, r ;
};
Tree tree[MAXN<<2] ;
int n ;
void Push_up( int l , int r , int rt)
{
    int mid = (l + r) / 2 ;
    if(val[mid] < val[mid + 1])
    {
        tree[rt].Max = max( (mid - tree[rt<<1].ll + 1 ) + (tree[rt<<1|1].rr - mid ) , tree[rt<<1].Max );
        tree[rt].Max = max(tree[rt].Max , tree[rt<<1|1].Max) ;
        if(tree[rt<<1].rr == mid )
            tree[rt].rr = tree[rt<<1|1].rr ;
            else tree[rt].rr = tree[rt<<1].rr ;
        if(tree[rt<<1|1].ll == mid + 1)
                tree[rt].ll = tree[rt<<1].ll ;
            else tree[rt].ll = tree[rt<<1|1].ll ;
    }
    else
    {
        tree[rt].Max = max(tree[rt<<1].Max ,tree[rt<<1|1].Max) ;
        tree[rt].rr = tree[rt<<1].rr ;
        tree[rt].ll = tree[rt<<1|1].ll ;
    }
    tree[rt].l = tree[rt<<1].l ;
    tree[rt].r = tree[rt<<1|1].r ;
}

void Build_tree(int l ,int r, int rt)
{
    if(l == r)
    {
        tree[rt].Max = 1;
        tree[rt].ll = l;
        tree[rt].rr = r ;
        tree[rt].l = l ;
        tree[rt].r = r;
        return ;
    }
    int mid = (l + r) /2;
    Build_tree(lson ) ;
    Build_tree(rson) ;
    Push_up( l , r , rt) ;
}
Tree Query(int L, int R ,int l , int r , int rt )
{
    if( L == l && r == R)
        return tree[rt] ;
    int mid = ( l + r ) / 2;
    Tree temp1 , temp2;
    int judge1 = 0 , judge2 = 0 ;
    if(L <= mid)
        temp1 = Query( L , min(mid , R),lson) ,judge1 = 1;
    if(R > mid)
        temp2 = Query( max(mid + 1 , L) , R , rson ) ,judge2 = 1;
    Tree res ;
    if(judge1 && judge2 )
    {
    if(val[mid] < val[mid + 1])
    {
        res.Max = max( (mid - temp1.ll + 1 ) + (temp2.rr - mid ) , temp1.Max );
        res.Max = max(res.Max , temp2.Max) ;
        if(temp1.rr == mid )res.rr = temp2.rr ;
        else res.rr = temp1.rr ;
        if(temp2.ll == mid + 1) res.ll = temp1.ll ;
        else res.ll = temp2.ll ;
    }
    else
    {
        res.Max = max(temp1.Max , temp2.Max) ;
        res.rr = temp1.rr ;
        res.ll = temp2.ll ;
    }
    return res;
    }
    else if(judge1)
        return temp1 ;
    else if(judge2) return temp2 ;
}
void Update(  int rt )
{
    if(rt <= 0)return ;
    Push_up(tree[rt>>1].l ,tree[rt>>1].r , rt>>1 ) ;
    rt >>= 1 ;
    Update(rt) ;
}
void Find(int l ,int r , int rt , int p)
{
    if(l == r && l == p)
    {
        Update(rt) ;
        return ;
    }
    int mid= (l + r ) / 2;
    if(p <= mid)
        Find(lson , p) ;
    else Find(rson , p) ;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n , Q ;
        char OP[5] ;

        cin>>n>>Q;
        for(int i =1; i<= n ;++i)
            scanf("%d", & val[i]) ;
        Build_tree(root) ;
        int B , A ;
        for(int i = 1; i<= Q ;++i)
        {
            scanf("%s%d%d", OP , & A, & B ) ;
            if(OP[0] == 'Q')
            {
                A ++ , B ++ ;
                Tree ans ;
                ans = Query(A , B ,root  ) ;
                printf("%d\n",ans.Max);
            }
            else
            {
                A ++;
                val[A] = B ;
                Find(root , A) ;
            }
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值