hdu 3308 线段树+区间合并

有条件的区间合并。

另设一个数组储存数据。

判断条件成立后才进行区间合并。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int Max=100000+100;
int date[Max<<2],rmaxn[Max<<2],lmaxn[Max<<2],maxn[Max<<2];
void pushup(int l,int r,int rt)
{
	int m=(l+r)>>1;
    lmaxn[rt]=lmaxn[rt<<1];
	rmaxn[rt]=rmaxn[rt<<1|1];
	maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
	if(date[m]<date[m+1])      //能否合并的前提条件。
	{
	    if(lmaxn[rt]==m-l+1) lmaxn[rt]+=lmaxn[rt<<1|1];       //区间合并的三条语句。
		if(rmaxn[rt]==r-m)   rmaxn[rt]+=rmaxn[rt<<1];
		maxn[rt]=max(maxn[rt],lmaxn[rt<<1|1]+rmaxn[rt<<1]);
	}
}
void build(int l,int r,int rt)
{
    if(l==r)
	{
	    scanf("%d",&date[l]);
		maxn[rt]=rmaxn[rt]=lmaxn[rt]=1;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	pushup(l,r,rt);
}
void update(int a,int b,int l,int r,int rt)
{
	if(l==r)
	{
	    date[l]=b;
		return ;
	}
	int m=(l+r)>>1;
	if(a<=m) update(a,b,lson);
	if(a>m)  update(a,b,rson);
	pushup(l,r,rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L==l&&r==R)
		return maxn[rt];
	int m=(l+r)>>1;
	if(R<=m) return query(L,R,lson);
	if(L>m)	 return query(L,R,rson);
	int ans=max(query(L,m,lson),query(m+1,R,rson));
	int ret=0;
	if(date[m]<date[m+1])            //判断能否合并。
	    ret=min(lmaxn[rt<<1|1],R-m)+min(rmaxn[rt<<1],m-L+1);   //合并Ing.
	return max(ans,ret);
}
int main()
{
	int cas;
	scanf("%d",&cas);
	while(cas--)
	{
	    int n,m;
		scanf("%d %d",&n,&m);
		n--;
		build(0,n,1);
		while(m--)
		{
		    char ch[2];
			int a,b;
			scanf("%s %d %d",ch,&a,&b);
			if(ch[0]=='U')
				update(a,b,0,n,1);
			else
				printf("%d\n",query(a,b,0,n,1));
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值