经典问题:不断更新查找区间连续递增序列(区间合并模板+单点更新)(3308)

LCIS

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


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

线段树的问题多借助画图来理解。lsum,rsum,msum分别代表从开始,从末尾,整个序列的最大连续递增子序列,最关键的在于pushup操作时,合并区间的问题:这里需要考虑全面,首先lsum[rt]可能跨过中点m,当然相对应的rsum[rt]也可能会跨过,其次,最长的一段子序列可能是中间一段跨过中点m的子序列,所以这些情况都需要分别考虑。要注意rt的左子树区间长度是len-(len>>1)(一定要加括号,否则会错!且很难察觉!),右子树的区间长度是len>>1。


/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <vector>
#include <set>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define uLL unsigned long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 9973
#define MAX 100050
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
/*---------------------Work-----------------------*/
int num[MAX];
int lsum[MAX<<2],rsum[MAX<<2],msum[MAX<<2]; //线段树空间一定要开四倍,刚开始开两倍WA一次
int n,m;
void pushup(int rt,int l,int r)
{
	lsum[rt]=lsum[rt<<1];
	rsum[rt]=rsum[rt<<1|1];
	msum[rt]=max(msum[rt<<1],msum[rt<<1|1]);
	int m=(l+r)>>1,len=r-l+1; //len是父亲结点的区间长度
	if(num[m]<num[m+1]) //处理区间合并问题,既有lsum[rt],也有rsum[rt]和msum[rt]
	{
		//左右,右左和中间
		if(lsum[rt<<1]==(len-(len>>1))) //注意括号不要忘了
			lsum[rt]+=lsum[rt<<1|1];
		if(rsum[rt<<1|1]==(len>>1))
			rsum[rt]+=rsum[rt<<1];
		msum[rt]=max(msum[rt],rsum[rt<<1]+lsum[rt<<1|1]);
	}
}
void build(int rt,int l,int r)
{
	if(l==r)
	{
		msum[rt]=lsum[rt]=rsum[rt]=1;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	pushup(rt,l,r);
}
void update(int rt,int l,int r,int p)
{
	if(l==r) return ;
	int m=(l+r)>>1;
	if(p<=m) update(lson,p);
	else update(rson,p);
	pushup(rt,l,r);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&R>=r) return msum[rt];
	int m=(l+r)>>1;
	if(R<=m) return query(lson,L,R); //完全包含在左子树区间内
	if(L>m) return query(rson,L,R); //完全包含在右子树区间内
	int a,b; //左右子树区间都有成分在其中
	a=query(lson,L,R);
	b=query(rson,L,R);
	int ans=max(a,b);
	if(num[m]<num[m+1])
	{
		int c;
		c=min(m-L+1,rsum[rt<<1])+min(R-m,lsum[rt<<1|1]); //理解这一步
		ans=max(ans,c);
	}
	return ans;
}
void work()
{
	int T; cin>>T;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			scanf("%d",num+i);
		build(1,1,n);
		char ch[10];
		int a,b;
		while(m--)
		{
			scanf("%s%d%d",ch,&a,&b);
			if(ch[0]=='U')
			{
				a++;
				num[a]=b;
				update(1,1,n,a);
			}
			else if(ch[0]=='Q')
			{
				a++,b++;
				printf("%d\n",query(1,1,n,a,b));
			}
		}
	}
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	work();
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值