hdu 3308 LCIS(线段树单点更新+区间合并)中等难度的题目

1、http://acm.hdu.edu.cn/showproblem.php?pid=3308

2、题目大意:

给出一个由n个数字组成的序列,有两种操作,U A B是将A位置的数字替换成B,Q A B是查询A-B区间有多少个递增的数字,也就是求A-B区间的最长上升子序列的长度

这道题目有对区间的查询及对单点的更新,很容易想到线段树,只是在具体实现的时候还是有些困难,具体看代码

要记录区间里的最长的序列长度是多少,用smax来记录。由于最长的连续上升序列可以在区间的左端点,右端点,所以在线段树里增加两个域lmax和rmax,分别表示,在区间的端点处,分别向右向左的最长的满足条件的序列长度。

        在更新当前区间的lmax时,如果左子区间的lmax等于左子区间的长度,那么当前区间的lmax就要加上右儿子的lmax。更新当前区间的rmax时同理。
        另外在查询的时候,要注意,有可能当前区间的lmax大于mid-st+1,所以要在其中取一个较小值,同理rmax有可能大于ed-mid。

3、题目:

LCIS

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


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


 

Recommend
wxl   |   We have carefully selected several similar problems for you:  2795 3397 1542 1828 2871

4、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 400005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int lmax[N],rmax[N],smax[N];
int a[N];
void pushUp(int d,int m,int rt)
{
    lmax[rt]=lmax[rt<<1];
    rmax[rt]=rmax[rt<<1|1];
    smax[rt]=max(smax[rt<<1],smax[rt<<1|1]);
    if(a[m]<a[m+1])
    {
        if(lmax[rt<<1]==(d-(d>>1)))
        lmax[rt]+=lmax[rt<<1|1];
        if(rmax[rt<<1|1]==(d>>1))
        rmax[rt]+=rmax[rt<<1];
        smax[rt]=max(smax[rt],rmax[rt<<1]+lmax[rt<<1|1]);
    }
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&a[l]);
        lmax[rt]=1;
        rmax[rt]=1;
        smax[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(r-l+1,m,rt);
}
void update(int b,int c,int l,int r,int rt)
{
    if(l==r)
    {
        a[b]=c;
        return ;
    }
    int m=(l+r)>>1;
    if(b<=m)
    update(b,c,lson);
    //错在if(r>m)
    else
    update(b,c,rson);
    pushUp(r-l+1,m,rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
    {
        return smax[rt];
    }
    int m=(l+r)>>1;
    int ans=-1;
    if(L<=m)
    ans=max(ans,query(L,R,lson));
    if(R>m)
    ans=max(ans,query(L,R,rson));
    if(a[m]<a[m+1])
    {
        int tmpR=lmax[rt<<1|1]+m;
        int tmpL=m-rmax[rt<<1]+1;
        ans=max(ans,min(R,tmpR)-max(L,tmpL)+1);
    }
    return ans;
}
int main()
{
    int t,b,c,n,m;
    char ch[3];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(0,n-1,1);
        for(int i=1;i<=m;i++)
        {
            scanf("%s%d%d",ch,&b,&c);
            //printf("%c %d %d\n",ch[0],b,c);
            if(ch[0]=='Q')
            {
                int ans=query(b,c,0,n-1,1);
                printf("%d\n",ans);
            }
            else
            {
                update(b,c,0,n-1,1);
            }
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值