HDU 3308 LCIS 线段树区间合并

LCIS

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


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

题意:单点更新查询区间连续上升子序列。


单点更新很简单,值得注意的是Push_Up()和查询。Push_Up()中当左子树最右边数字小于右子树最左边数字的时候,可以把左子树右区间rsum[id*2]和右子树左区间lsum[id*2]加起来用来更新区间最长连续上升子序列msum[id]。在查询里面,查询的区间分成两半时,如果能在mid这个地方能连续,则需要把中间这个区间也加进来判断最大值,而且当所求的这个区间的[L,mid]这一段比这个连续的区间rmun[id*2]短,就只取所求区间这一段就是了,否则就取rsum[ld*2],[mid+1,R]这一段一样的。




#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#include"queue"
#include"math.h"
#define lson l,mid,id*2
#define rson mid+1,r,id*2+1
const int maxn = 100000+10;
typedef long long LL;
using namespace std;

int ln[maxn*4];    ///区间最左数字
int rn[maxn*4];    ///区间最右数字
int msum[maxn*4];  ///区间最长连续区间
int lsum[maxn*4];  ///区间最左连续区间
int rsum[maxn*4];  ///区间最右连续区间

void Push_Up(int id,int len)  ///向上更新
{
    ln[id] = ln[id*2];
    rn[id] = rn[id*2+1];
    lsum[id] = lsum[id*2];
    rsum[id] = rsum[id*2+1];
    if(rn[id*2] < ln[id*2+1]) ///左子树最右数字小于右子树最左数字
    {
        if(lsum[id*2] == (len-(len/2)))
            lsum[id] += lsum[id*2+1];
        if(rsum[id*2+1] == len/2)
            rsum[id] += rsum[id*2];
        msum[id] = max(lsum[id*2+1]+rsum[id*2],max(msum[id*2],msum[id*2+1])); ///左右子树msum与中间连续起来的sum值中取最大值
    }
    else
    {
        msum[id] = max(msum[id*2],msum[id*2+1]);
    }
}

void build(int l,int r,int id)
{
    if(l == r)
    {
        scanf("%d",&ln[id]);
        rn[id] = ln[id];
        msum[id] = lsum[id] = rsum[id] = 1;
        return;
    }
    int mid = (l+r)/2;
    build(lson);
    build(rson);
    Push_Up(id,r-l+1);
}

void update(int pos,int c,int l,int r,int id)
{
    if(l == r)
    {
        msum[id] = lsum[id] = rsum[id] = 1;
        ln[id] = rn[id] = c;
        return;
    }
    int mid = (l+r)/2;
    if(pos <= mid) update(pos,c,lson);
    else           update(pos,c,rson);
    Push_Up(id,r-l+1);
}

int query(int L,int R,int l,int r,int id)
{
    if(L <= l && r <= R)
    {
        return msum[id];
    }
    int mid = (l+r)/2;
    int ans = 0;
    if(L <= mid)
        ans = max(ans,query(L,R,lson));
    if(mid < R)
        ans = max(ans,query(L,R,rson));
    if(rn[id*2] < ln[id*2+1])
        ans = max(ans,min(rsum[id*2],mid-L+1)+min(lsum[id*2+1],R-mid));
        ///求的区间不一定比中间这一段连续的区间不一定完全匹配,取小,画图模拟可理解
    return ans;
}
int main(void)
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--)
        {
            char  s[2];
            scanf("%s",s);
            int a,b;
            scanf("%d%d",&a,&b);
            if(s[0] == 'Q')
            {
                printf("%d\n",query(a+1,b+1,1,n,1));
            }
            else
            {
                update(a+1,b,1,n,1);
            }
        }
    }
    return 0;
}





你可能感兴趣的:(线段树,区间合并,hduoj)