码迷,mamicode.com
首页 > 其他好文 > 详细

LCIS

时间:2015-11-03 00:25:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

 

LCIS
Time Limit: 3000 MS Memory Limit: 32768 K
Total Submit: 70(28 users) Total Accepted: 33(26 users) Rating: 技术分享技术分享技术分享 Special Judge: No
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<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
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
Recommend
`Wind @Hrbust
Author
shǎ崽

 

#include<cstdio>
#include<cmath>
#include<queue>
const int SIZE=100005;
int LC[SIZE<<2];
int RC[SIZE<<2];
int CS[SIZE<<2];
int a[SIZE];
int N,M;
template <class typeone>
typeone max(typeone x,typeone y)
{
    return x>y? x:y;
}
template <class typeone >
typeone min(typeone x,typeone y)
{
    return x<y? x:y;
}
void pushup(int l,int r,int lr)
{
    LC[lr]=LC[lr<<1];
    RC[lr]=RC[lr<<1|1];
    CS[lr]=0;
    int m=(l+r)>>1;
    if(a[m]<a[m+1])
    {
        CS[lr]=RC[lr<<1]+LC[lr<<1|1];
        if(LC[lr]==m-l+1)
        {
            LC[lr]+=LC[lr<<1|1];
        }
        if(RC[lr]==r-m)
        {
            RC[lr]+=RC[lr<<1];
        }
    }
    CS[lr]=max(CS[lr],max(CS[lr<<1],CS[lr<<1|1]));
    return;
}

void build(int l,int r,int rt)
{
    LC[rt]=RC[rt]=CS[rt]=0;
    if(l==r)
    {
        LC[rt]=RC[rt]=CS[rt]=1;
        scanf("%d",&a[l]);
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(l,r,rt);
    return ;
}
void update(int pos,int l,int r,int rt,int val)
{
    if(l==pos&&r==pos)
    {
        a[pos]=val;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)
    {
        update(pos,l,m,rt<<1,val);
    }
    else
    {
        update(pos,m+1,r,rt<<1|1,val);
    }
    pushup(l,r,rt);
    return ;
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return CS[rt];
    }
    int m=(l+r)>>1;
    if(R<=m)
    {
        return query(L,R,l,m,rt<<1);
    }
    else if(L>m)
    {
        return  query(L,R,m+1,r,rt<<1|1);
    }
    else
    {
        int t1=0,t2=0,t3=0;
        t1=query(L,R,l,m,rt<<1);
        t2=query(L,R,m+1,r,rt<<1|1);
        if(a[m]<a[m+1])
        {
            t3=min(LC[rt<<1|1],R-m)+min(RC[rt<<1],m-L+1);
        }
        return max(t3,max(t1,t2));
    }
}
int main()
{
    int T;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            char k[2];
            scanf("%d %d",&N,&M);
            build(1,N,1);
            int x,y;
        //    getchar();
            while(M--)
            {
                scanf("%s %d %d",&k,&x,&y);
                if(strcmp(k,"Q")==0)
                {
                    printf("%d\n",query(x+1,y+1,1,N,1));    
                }
                else
                {
                    update(x+1,1,N,1,y);
                }
            }
        }
    }
    return 0;
}

 

LCIS

标签:

原文地址:http://www.cnblogs.com/beige1315402725/p/4931906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!