【HDU】 3308 LCIS

LCIS

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


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
 
题解:这一题跟白书上面一道题很像,只不过白书上是最大连续和,这里变成了最大连续上升子序列的长度,想法基本一致。

用pre,suf,len存放一段区间上最大连续的前缀长度,最大连续后缀长度,最大连续长度。然后就是合并区间了,在查询和合并区间的时候需要注意不能忽略掉最大长度的子串覆盖区间中点的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

struct tree
{
    int l,r,len,pre,suf;
};
tree d[400005];
int T,n,m,a[100005],A,B;
char s[5];

void buildtree(int i,int l,int r)
{
    int mid=(l+r)/2;
    d[i].l=l; d[i].r=r; d[i].len=0; d[i].pre=0; d[i].suf=0;
    if (l==r)
    {
        d[i].pre=1; d[i].suf=1; d[i].len=1;
        return;
    }
    buildtree(i*2,l,(r+l)/2);
    buildtree(i*2+1,(l+r)/2+1,r);
    d[i].len=max(d[i*2].len,d[i*2+1].len);
    if (a[mid]<a[mid+1]) d[i].len=max(d[i*2].suf+d[i*2+1].pre,d[i].len);
    if (a[mid]<a[mid+1] && d[i*2].pre==mid-l+1) d[i].pre=d[i*2].pre+d[i*2+1].pre;
    else d[i].pre=d[i*2].pre;
    if (a[mid]<a[mid+1] && d[i*2+1].suf==r-mid) d[i].suf=d[i*2].suf+d[i*2+1].suf;
    else d[i].suf=d[i*2+1].suf;
}

int _count(int i,int l,int r)
{
    int mid=(d[i].l+d[i].r)/2;
    if (d[i].l==l && d[i].r==r) return d[i].len;
    else if (r<=mid) return _count(i*2,l,r);
    else if (l>mid) return _count(i*2+1,l,r);
    else
    {
        int x,y,len=0;
        x=_count(i*2,l,mid);
        y=_count(i*2+1,mid+1,r);
        if (a[mid]<a[mid+1]) len=min(d[i*2].suf,mid-l+1)+min(d[i*2+1].pre,r-mid);
        len=max(len,max(x,y));
        return len;
    }
}

void _insert(int i,int l,int k)
{
    int mid=(d[i].l+d[i].r)/2;
    if (d[i].l==d[i].r && d[i].l==l)
    {
        a[l]=k;
        return ;
    }
    else if (l<=mid) _insert(i*2,l,k);
    else _insert(i*2+1,l,k);
    d[i].len=max(d[i*2].len,d[i*2+1].len);
    if (a[mid]<a[mid+1]) d[i].len=max(d[i*2].suf+d[i*2+1].pre,d[i].len);
    if (a[mid]<a[mid+1] && d[i*2].pre==mid-d[i].l+1) d[i].pre=d[i*2].pre+d[i*2+1].pre;
    else d[i].pre=d[i*2].pre;
    if (a[mid]<a[mid+1] && d[i*2+1].suf==d[i].r-mid) d[i].suf=d[i*2].suf+d[i*2+1].suf;
    else d[i].suf=d[i*2+1].suf;
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        memset(d,0,sizeof(d));
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++) scanf("%d",&a[i]);
        buildtree(1,0,n-1);
        for (int i=1;i<=m;i++)
        {
            scanf("%s%d%d",s,&A,&B);
            if (s[0]=='Q') printf("%d\n",_count(1,A,B));
            if (s[0]=='U') _insert(1,A,B);
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值