HDU 3308 LCIS

本文介绍了解决LCIS(最长连续递增子串)问题的一种高效算法,该算法利用了线段树进行区间合并操作,支持单点更新及查询指定区间内最长连续递增子串的长度。

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

LCIS

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5890    Accepted Submission(s): 2555


Problem Description
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].
 

Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
 

Output
For each Q, output the answer.
 

Sample Input
  
1 10 10 7 7 3 3 5 9 9 8 1 8 Q 6 6 U 3 4 Q 0 1 Q 0 5 Q 4 7 Q 3 5 Q 0 2 Q 4 6 U 6 10 Q 0 9
 

Sample Output
  
1 1 4 2 3 1 2 5
 

Author
shǎ崽
 

Source
 


线段树区间合并

和Hotel那道题有些相似,基本做法是一致的。

题目大意:

一串数字,可以单点更新,可以查询某一区间内的最长单调递增字串(是子串,而不是子序列;字串是连续的,子序列不需要连续)的长度。


注意题目的区间索引是从0开始的。


#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN=100005;

int num[MAXN];
int left[MAXN<<2],right[MAXN<<2],middle[MAXN<<2];

void pushUp(int x,int l,int r)
{
        left[x]=left[x<<1];
        right[x]=right[x<<1|1];
        middle[x]=max(middle[x<<1],middle[x<<1|1]);
        int center=(l+r)/2;
        int length=r-l+1;
        if(num[center]<num[center+1])
        {
                if(left[x]==length-length/2)
                        left[x]+=left[x<<1|1];
                if(right[x]==length/2)
                        right[x]+=right[x<<1];
                middle[x]=max(right[x<<1]+left[x<<1|1],middle[x]);
        }
}

void build(int x,int l,int r)
{
        if(l==r)
        {
                left[x]=right[x]=middle[x]=1;
                return;
        }
        int center=(l+r)/2;
        build(x<<1,l,center);
        build(x<<1|1,center+1,r);
        pushUp(x,l,r);
}

void update(int x,int var,int l,int r,int p)
{
        if(l==r)
        {
                num[l]=var;
                return;
        }
        int center=(l+r)/2;
        if(p<=center)
                update(x<<1,var,l,center,p);
        else
                update(x<<1|1,var,center+1,r,p);
        pushUp(x,l,r);
}

int query(int x,int l,int r,int L,int R)
{
        if(L<=l && r<=R)
                return middle[x];
        int center=(l+r)/2;
        if(R<=center)
                return query(x<<1,l,center,L,R);
        else if(center<L)
                return query(x<<1|1,center+1,r,L,R);
        else
        {
                int temp = 0;
                if(num[center] < num[center+1])
                        temp = min(right[x<<1],center-L+1) + min(left[x<<1|1],R - center);
                int t1 = query(x<<1,l,center,L,R);
                int t2 = query(x<<1|1,center+1,r,L,R);
                return max(temp,max(t1,t2));
        }
}

int main()
{
        int t,n,m,a,b;
        char option[5];
        scanf("%d",&t);
        while(t--)
        {
                scanf("%d%d",&n,&m);
                for(int i=1;i<=n;i++)
                        scanf("%d",&num[i]);
                build(1,1,n);
                while(m--)
                {
                        scanf("%s",option);
                        scanf("%d%d",&a,&b);
                        if(option[0]=='U')
                                update(1,b,1,n,a+1);
                        else
                                printf("%d\n",query(1,1,n,a+1,b+1));
                }
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值