HDU3308(LCIS)

LCIS

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

思路

线段树区间合并好题,‘U’ x y将第x的值改为y,‘Q’ x y查询区间[x,y]的最长连续上升子序列长度。

  1. 线段树维护三个东西,ls左起最长连续上升子序列长度,rs右为终点的最长连续上升子序列长度,ms区间最长连续上升子序列长度。

  2. 那么根据上面的理论 向上更新可以写成:ms[父] = max(ms[左孩子],ms[右孩子]),ls[父] = ls[左孩子],rs[父] = rs[右孩子]。如果a[m] < a[m+1]也当前连续子序列横跨左右区间那么ms[父] = max(ms[父],ls[左孩子] + rs[右孩子])。并且如果左区间是一个完整的连续上升子序列那么父节点的最大左起连续还需要加上右孩子的左起连续:ls[父] = ms[左孩子] + ls[右孩子],右区间满同理:rs[父] = ms[左孩子] + rs[右孩子]。

  3. 单点更新直接改就好了,改完继续向上重新更新。查询的操作需要注意查询的区间是横跨左右区间的时候,同时还需要注意左右端点是不是超出了当前的查询区间。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson k<<1,l,m
#define rson k<<1|1,m+1,r
const int maxn = 1e5+5;
struct tree{
	int ls;		//左端点为起点的最大连续 
	int rs;		//右端点为终点的最大连续 
	int ms;		//区间最大连续 
}s[maxn<<2];
int a[maxn]; 
inline void push_up(int k,int l,int r)
{
	int m = (l + r) >> 1;
	s[k].ls = s[k<<1].ls;
	s[k].rs = s[k<<1|1].rs;
	s[k].ms = max(s[k<<1].ms,s[k<<1|1].ms);	
	if(a[m] < a[m+1]){
		if(s[k<<1].ms == m-l+1){			//左孩子区间满 
			s[k].ls += s[k<<1|1].ls;
		}
		if(s[k<<1|1].ms == r-m){			//右孩子区间满 
			s[k].rs += s[k<<1].rs;
		}
		s[k].ms = max(s[k].ms,s[k<<1].rs + s[k<<1|1].ls);
	}
}
inline void build(int k,int l,int r)
{
	if(l == r){
		s[k].ls = s[k].ms = s[k].rs = 1;
		return ;
	}
	int m = (l + r) >> 1;
	build(k<<1,l,m);
	build(k<<1|1,m+1,r);
	push_up(k,l,r);
}
inline void update(int k,int l,int r,int ql,int ans)
{
	if(l == r){
		return;
	}
	int m = (l + r) >> 1;
	if(ql <= m){
		update(lson,ql,ans);
	}
	else{
		update(rson,ql,ans);
	}
	push_up(k,l,r);
}
int query(int k,int l,int r,int ql,int qr)
{
	if(qr < l || r < ql){
		return 0;
	}
	if(ql <= l && r <= qr){
		return s[k].ms;
	}
	int m = (l + r) >> 1;
	int s1 = query(lson,ql,qr);			//左孩子最长 
	int s2 = query(rson,ql,qr);			//右孩子最长 
	int ans = max(s1,s2);
	if(a[m] < a[m+1]){
		//由于左孩子的连续区间可能超过ql,所以只能在左孩子的右连续 和 区间长度中取一个最小值
		//右孩子同上。 
		ans = max(ans,min(s[k<<1].rs,m-ql+1) + min(s[k<<1|1].ls,qr-m));
	}
	return ans;
}
int main()
{
	int t,n,m;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i = 1;i <= n;i++){
			scanf("%d",&a[i]);
		}
		build(1,1,n);
		while(m--){
			char op[5];
			int x,y;
			scanf("%s%d%d",op,&x,&y);
			if(op[0] == 'U'){
				a[x+1] = y;
				update(1,1,n,x+1,y);
			}
			else{
				int ans = query(1,1,n,x+1,y+1);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}

愿你走出半生,归来仍是少年~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值