hdu 3308 LCIS

线段树,区间合并

最长上升子序列(严格上升)

#include<iostream>
#define maxn 100010
using namespace std;
int n,m;
int a,b;
struct stu
{
	int l,r,mid;
	int ll,rl,ml;
	int d()
	{
		return r-l+1;
	}
};
stu mapp[maxn*4];
int num[maxn];
void push(int count)
{
	if(mapp[count*2].ll==mapp[count*2].d()&&num[mapp[count*2].r]<num[mapp[count*2+1].l]) mapp[count].ll=mapp[count*2].ll+mapp[count*2+1].ll;
	else mapp[count].ll=mapp[count*2].ll;
	if(mapp[count*2+1].rl==mapp[count*2+1].d()&&num[mapp[count*2].r]<num[mapp[count*2+1].l]) mapp[count].rl=mapp[count*2+1].rl+mapp[count*2].rl;
	else mapp[count].rl=mapp[count*2+1].rl;
	mapp[count].ml=max(mapp[count*2].ml,mapp[count*2+1].ml);
	if(num[mapp[count*2].r]<num[mapp[count*2+1].l])
	{
		int x=mapp[count*2].rl+mapp[count*2+1].ll;
		mapp[count].ml=max(mapp[count].ml,x);
	}
}
void build(int l,int r,int count)
{
	mapp[count].l=l;
	mapp[count].r=r;
	mapp[count].mid=(l+r)/2;
	if(l==r)
	{
		mapp[count].ll=mapp[count].rl=mapp[count].ml=1;
		return ;
	}
	build(l,mapp[count].mid,count*2);
	build(mapp[count].mid+1,r,count*2+1);
	push(count);
	//cout<<count<<"~"<<mapp[count].l<<"~"<<mapp[count].r<<"~"<<mapp[count].ll<<"~"<<mapp[count].rl<<"~"<<mapp[count].ml<<endl;
}
void updata(int l,int r,int count)
{
	if(l==r) return;
	int mid=mapp[count].mid;
	if(a+1<=mapp[count].mid) updata(l,mid,count*2);
	else updata(mid+1,r,count*2+1);
	push(count);
}
int que(int l,int r,int count)
{
	//cout<<l<<"~"<<r<<endl; 
	int mid=mapp[count].mid;
	if(mapp[count].l==l&&mapp[count].r==r) return mapp[count].ml;
	if(r<=mid) return que(l,r,count*2);
	else if(l>=mid+1) return que(l,r,count*2+1);
	else
	{
		int x=0;
		int y=max(que(l,mid,count*2),que(mid+1,r,count*2+1));
		if(num[mid]<num[mid+1])
		{
			x=min(mapp[count*2].rl,mid-l+1)+min(r-mid,mapp[count*2+1].ll);
		}
		return max(x,y);
	}
}
int main()
{
	cin.sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++) cin>>num[i];
		build(1,n,1);
		//cout<<mapp[1].ll<<"~"<<mapp[1].rl<<"~"<<mapp[1].ml<<endl;
		//cout<<mapp[2].ll<<"~"<<mapp[2].rl<<"~"<<mapp[2].ml<<endl;
		while(m--)
		{
			string cmd;
			cin>>cmd>>a>>b;
			if(cmd=="Q")
			{
				cout<<que(a+1,b+1,1)<<endl;
			}
			else
			{
				num[a+1]=b;
				updata(1,n,1);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值