HDU 3308 线段树区间合并

LCIS

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


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

查找一个连续区间内的最长连续递增子序列

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct ssss
{
    int a,b,c;
    int m,ml,mr;
    int numl,numr;
}ss[500000];
int s[100010];
void Updata(int i)
{
    ss[i].numl = ss[2*i].numl;//父亲节点左顶点的值为左子树的左顶点的值
    ss[i].numr = ss[2*i+1].numr;//父亲节点右顶点的只为右子树的右顶点的值
    ss[i].ml = ss[2*i].ml;//父亲节点左顶点的连续递增子序列为左子树的左顶点连续递增子序列长度
    ss[i].mr = ss[2*i+1].mr;//父亲节点右顶点的连续递增子序列长度为右子树的右顶点连续递增子序列长度
    ss[i].m = max(ss[2*i].m,ss[2*i+1].m);//父亲节点的最长连续递增子序列长度为左右子树的最长连续递增子序列长度
    if(ss[2*i].numr<ss[2*i+1].numl)//如果左子树的右边界值小于右子树的左边界值,要合并左子树的右边界和右子树的左边界进行计算
    {
        if(ss[2*i].ml == ss[2*i].c)//左子树全部连续
            ss[i].ml+=ss[2*i+1].ml;
        if(ss[2*i+1].mr == ss[2*i+1].c)//右子树全部连续
            ss[i].mr+=ss[2*i].mr;
        ss[i].m = max(ss[i].m,ss[2*i].mr+ss[2*i+1].ml);//更新父亲节点的最长连续递增子序列长度
    }
}
void Intree(int i,int a,int b)
{
    ss[i].a=a;
    ss[i].b=b;
    ss[i].c=b-a+1;
    if(a==b)
    {
        ss[i].numr=ss[i].numl=s[a];
        ss[i].m=ss[i].ml=ss[i].mr=1;
        return ;
    }
    Intree(i*2,a,(a+b)/2);
    Intree(i*2+1,(a+b)/2+1,b);
    Updata(i);
}
void Insert(int i,int a,int b)
{
    if(ss[i].a==ss[i].b)
    {
        //cout<<ss[i].a<<" "<<ss[i].b<<" "<<b<<" "<<a<<endl;
        ss[i].numl=ss[i].numr=b;
        return ;
    }
    int mid=(ss[i].a+ss[i].b)/2;
    if(a<=mid)
    Insert(i*2,a,b);
    if(a>mid)
    Insert(i*2+1,a,b);
    Updata(i);
}
int find(int i,int a,int b)
{
    if(ss[i].a>=a&&ss[i].b<=b)
    {
        return ss[i].m;
    }
    int mid=ss[i].a+ss[i].b,ans=0;
    mid/=2;
    if(a<=mid)
    ans=max(ans,find(i*2,a,b));
    if(b>mid)
    ans=max(ans,find(i*2+1,a,b));
    if(ss[i*2].numr<ss[i*2+1].numl)
    {
        ans=max(ans,min(mid-a+1,ss[i*2].mr)+min(b-mid,ss[i*2+1].ml)) ;//当两段分开的节点可以连接时,连接时,左边取右连续,若右连续的左起点小于所求区间的左边界,则取左边界到左子树右边界的长度。右子树同理。
    }
    return ans;
}
int main()
{
    int n,m,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
        Intree(1,1,n);
        //getchar();
        while(m--)
        {
            char ch[5];int a,b;
            //getchar();
            scanf("%s%d%d",&ch,&a,&b);
            //cout<<ch<<a<<b<<endl;
            if(ch[0]=='Q')
            {
                printf("%d\n",find(1,a+1,b+1));
            }
            else if(ch[0]=='U')
            {
                //cout<<a+1<<" "<<b<<endl;
                Insert(1,a+1,b);
            }
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值