3308 LCIS(线段树水题&最长连续递增序列)

LCIS

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


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
HDOJ Monthly Contest – 2010.02.06
 

Recommend
wxl   |   We have carefully selected several similar problems for you:   1542  1828  1255  3333  3265 
 

题意:

给你个长度为n的数组。然后可以进行两种操作。

1.U   a   b。把数组第a个数变为b。

2  Q  a   b。询问[a,b]里最长的连续递增序列的长度。

思路:

很简单。处理有点类似于hotel。维护左连续和右连续。只是注意中间连接时必须保证val[mid+1]>val[mid]。

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int ml[maxn<<2],mr[maxn<<2],ma[maxn<<2];
int val[maxn];
void pushup(int L,int R,int k)
{
    int ls,rs,mid;
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    ma[k]=max(ma[ls],ma[rs]);
    ml[k]=ml[ls];
    mr[k]=mr[rs];
    if(val[mid+1]>val[mid])
    {
        if(ml[ls]==mid-L+1)
            ml[k]+=ml[rs];
        if(mr[rs]==R-mid)
            mr[k]+=mr[ls];
        ma[k]=max(ma[k],mr[ls]+ml[rs]);
    }
}
void btree(int L,int R,int k)
{
    int ls,rs,mid;
    if(L==R)
    {
        scanf("%d",&val[L]);
        ml[k]=mr[k]=ma[k]=1;
        return;
    }
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    btree(L,mid,ls);
    btree(mid+1,R,rs);
    pushup(L,R,k);
}
void update(int L,int R,int p,int k,int d)
{
    int ls,rs,mid;
    if(L==R)
    {
        val[L]=d;
        return;
    }
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(p>mid)
        update(mid+1,R,p,rs,d);
    else
        update(L,mid,p,ls,d);
    pushup(L,R,k);
}
int qu(int L,int R,int l,int r,int k)
{
    int ls,rs,mid,a,b,ans;
    if(l==L&&r==R)
        return ma[k];
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(l>mid)
        return qu(mid+1,R,l,r,rs);
    else if(r<=mid)
        return qu(L,mid,l,r,ls);
    else
    {
        ans=max(qu(L,mid,l,mid,ls),qu(mid+1,R,mid+1,r,rs));
        if(val[mid+1]>val[mid])
        {
            a=min(mid-l+1,mr[ls]);//注意下这就行了。
            b=min(r-mid,ml[rs]);
            return max(ans,a+b);
        }
        return ans;
    }
}
int main()
{
    int t,n,m,a,b;
    char com[10];

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        btree(1,n,1);
        while(m--)
        {
            scanf("%s%d%d",com,&a,&b);
            if(com[0]=='U')
            {
                a++;
                update(1,n,a,1,b);
            }
            else
            {
                a++,b++;
                printf("%d\n",qu(1,n,a,b,1));
            }
        }
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)