HDU 5316 区间合并

题意:
        给你n个数字的序列,有两种操作。第一种操作是修改某个位置的值,第二种操作是询问l,r内美丽子序列的最大和。所谓美丽子序列,就是指按顺序选出一些数,这些数中任意相邻的两个数的原位置编号必须满足奇偶相间(即一奇一偶)。
题解:

    这道题要用到线段树的区间合并,修改操作简单,难的是怎么区间合并,我们可以设置四个变量,oo,jj,oj,jo,分别是以偶数下标开始偶数下标结束, 以奇数下标开始奇数下标结束,以偶数下标开始奇数下标结束,以奇数下标开始偶数下标结束。然后就开始想象怎么向上更新了,以oo为例,更新oo的时候可以先比较左右子树的oo的大小,然后比较左子树的oj+右子树的oo,再比较左子树的oo+右子树的jo,比较四个值,取最大值即可。然后是询问操作,如果l,r都在左边就往左边寻找,如果l,r在右边就往右边查找,难就在于l在左边,r在右边(这是我认为区间合并恶心的地方,也是难点),我们可以用两个结构体分别存放l在左子树的值,r在右子树的值,然后再用类似用向上更新的操作来维护l,r的oo,jj,oj,jo,最后比较这四个值的大小,取最大即可。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define LL long long int
const int MAXN=100000+7;
const LL INF=0x7fffffff;
struct node
{
	int l,r;
	LL oo,jj,oj,jo;
}t[MAXN<<2];
LL a[MAXN];
void pushup(int root)
{
	t[root].jj=max(-INF,t[root<<1].jj);
	t[root].jj=max(t[root].jj,t[root<<1|1].jj);
	t[root].jj=max(t[root].jj,t[root<<1].jo+t[root<<1|1].jj);
	t[root].jj=max(t[root].jj,t[root<<1|1].oj+t[root<<1].jj);
	
	t[root].oj=max(-INF,t[root<<1].oj);
	t[root].oj=max(t[root].oj,t[root<<1|1].oj);
	t[root].oj=max(t[root].oj,t[root<<1].oj+t[root<<1|1].oj);
	t[root].oj=max(t[root].oj,t[root<<1|1].jj+t[root<<1].oo);
	
	t[root].jo=max(-INF,t[root<<1].jo);
	t[root].jo=max(t[root].jo,t[root<<1|1].jo);
	t[root].jo=max(t[root].jo,t[root<<1].jo+t[root<<1|1].jo);
	t[root].jo=max(t[root].jo,t[root<<1|1].oo+t[root<<1].jj);
	
	t[root].oo=max(-INF,t[root<<1].oo);
	t[root].oo=max(t[root].oo,t[root<<1|1].oo);
	t[root].oo=max(t[root].oo,t[root<<1].oo+t[root<<1|1].jo);
	t[root].oo=max(t[root].oo,t[root<<1|1].oo+t[root<<1].oj);
}
void build(int root,int l,int r)
{
	t[root].l=l;
	t[root].r=r;
	if(l==r)
	{
		if(l%2)
		{
			t[root].jj=a[l];
			t[root].oo=t[root].oj=t[root].jo=-INF;
		}
		else
		{
			t[root].oo=a[l];
			t[root].jj=t[root].oj=t[root].jo=-INF;
		}
		return ;
	}
	int mid=(l+r)/2; 
	build(lson);
	build(rson);
	pushup(root);
}
void update(int root,int L,LL val)
{
	if(t[root].l==t[root].r)
	{
		if(L%2)	
		t[root].jj=val;
		else
		t[root].oo=val;			
		return ;
	}
	int mid=(t[root].l+t[root].r)/2;
	if(L<=mid) update(root<<1,L,val);
	else update(root<<1|1,L,val);
	pushup(root);
}
node getmax(node l,node r)
{
	node ans;
	ans.jj=ans.jo=ans.oj=ans.oo=-INF;
	ans.jj=max(ans.jj,l.jj);
	ans.jj=max(ans.jj,r.jj);
	ans.jj=max(ans.jj,l.jo+r.jj);
	ans.jj=max(ans.jj,r.oj+l.jj);
	
	ans.oj=max(ans.oj,l.oj);
	ans.oj=max(ans.oj,r.oj);
	ans.oj=max(ans.oj,l.oj+r.oj);
	ans.oj=max(ans.oj,r.jj+l.oo);
	
	ans.jo=max(ans.jo,l.jo);
	ans.jo=max(ans.jo,r.jo);
	ans.jo=max(ans.jo,l.jo+r.jo);
	ans.jo=max(ans.jo,r.oo+l.jj);
	
	ans.oo=max(ans.oo,l.oo);
	ans.oo=max(ans.oo,r.oo);
	ans.oo=max(ans.oo,l.oo+r.jo);
	ans.oo=max(ans.oo,r.oo+l.oj);	
	return ans;	
}
node query(int root,int L,int R)
{
//	printf("%d %d  %d  %d\n",t[root].l,t[root].r,L,R);
	if(L<=t[root].l&&R>=t[root].r)
		return t[root];
	int mid=(t[root].l+t[root].r)/2;
	if(R<=mid) return query(root<<1,L,R);
	else if(L>mid) return query(root<<1|1,L,R);
	else{
		node l=query(root<<1,L,mid);
		node r=query(root<<1|1,mid+1,R);
		return getmax(l,r);		
	}	
}
void debug(int root,int l,int r)
{
	printf("%d %d :%lld %lld %lld %lld\n",l,r,t[root].jj,t[root].oo,t[root].jo,t[root].oj);
	if(l==r)
	return ;
	int mid=(l+r)/2;
	debug(lson);
	debug(rson);
}
int main()
{
	int T,n,m;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
		build(1,1,n);
	//	debug(1,1,n);
		for(int i=1;i<=m;i++)
		{
			int z,x,y;
			LL w;
			scanf("%d",&z);
			if(z==0)
			{
				scanf("%d%d",&x,&y);
				node ans=query(1,x,y);
				printf("%lld\n",max(max(ans.jj,ans.oo),max(ans.jo,ans.oj)));
			}
			else{
				scanf("%d%lld",&x,&w);
				update(1,x,w);
			}
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值