hdu5316 线段树

题意有点难懂
找一个区间中奇偶交替的子序列的最大值  
奇偶交替可以是以奇数开头奇数结尾
奇数开头偶数结尾
偶数开头偶数结尾
偶数开头奇数结尾
四种情况

还有就是单点更新 维护线段树

	#include <queue>
	#include <stack>
	#include <deque>
	#include <math.h>
	#include <string>
	#include <vector>
	#include <stdio.h>
	#include <iostream>
	#include <string.h>
	#include <algorithm>
	#include <functional>
	#define mem(a) memset(a,0,sizeof(a));
	#define mem_1(a) memset(a,-1,sizeof(a));
	#define sf(a) scanf("%d",&a)
	#define sff(a,b) scanf("%d%d",&a,&b)
	#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
	#define lson l,mid,i<<1
	#define LL long long
	#define rson mid+1,r,i<<1|1
	const LL INF = 1100000000000;
	const int MAXN = 100500;
	const double PI = acos(-1.0);
	const double esp = 1e-10;
	using namespace std;
	struct node
	{
	    int l,r;
		LL oo,jj,oj,jo,num;
	   // int mid(){return (l+r)>>1;}
	}Tree[MAXN<<2];
	LL max(LL a,LL b)
	{
	    return a>b ? a:b;
	}
	void pushUp(int i)
	{
		Tree[i].jj = max(max(max(Tree[i<<1].jj, Tree[i<<1|1].jj),
						Tree[i<<1].jj + Tree[i<<1|1].oj),
						Tree[i<<1].jo + Tree[i<<1|1].jj);
		Tree[i].oo = max(max(max(Tree[i<<1].oo, Tree[i<<1|1].oo),
						Tree[i<<1].oj + Tree[i<<1|1].oo),
						Tree[i<<1].oo + Tree[i<<1|1].jo);
		Tree[i].jo = max(max(max(Tree[i<<1].jo, Tree[i<<1|1].jo),
						Tree[i<<1].jo + Tree[i<<1|1].jo),
						Tree[i<<1].jj + Tree[i<<1|1].oo);
		Tree[i].oj = max(max(max(Tree[i<<1].oj, Tree[i<<1|1].oj),
						Tree[i<<1].oo + Tree[i<<1|1].jj),
						Tree[i<<1].oj + Tree[i<<1|1].oj);
	}
	void build_tree(int l,int r,int i)
	{
		Tree[i].l = l;
		Tree[i].r = r;
		if(l == r)
		{
			//sf(Tree[i].num);
			scanf("%I64d",&Tree[i].num);
			if(l%2)
			{
				Tree[i].jj = Tree[i].num;
				Tree[i].oo = -INF;
			}
			else
			{
				Tree[i].oo = Tree[i].num;
				Tree[i].jj = -INF;
			}
			Tree[i].jo = Tree[i].oj = -INF;
			return ;
		}
		int  mid = (l+r)>>1 ;
		build_tree(lson);
		build_tree(rson);
		pushUp(i);
	}
	void updata_tree(int pos,LL num,int i)
	{
		if(Tree[i].l == pos && Tree[i].r == pos)
		{
			Tree[i].num = num;
			if(Tree[i].l%2)
			{
				Tree[i].jj = Tree[i].num;
				Tree[i].oo = -INF;
			}
			else
			{
				Tree[i].oo = Tree[i].num;
				Tree[i].jj = -INF;
			}
			Tree[i].jo = Tree[i].oj = -INF;
			return ;
		}
		int  mid = (Tree[i].l+Tree[i].r) >> 1;
		if(pos <= mid) updata_tree(pos,num,i<<1);
		else updata_tree(pos,num,i<<1|1);
		pushUp(i);
	}
	node Query(int l,int r,int i)
	{
		if(l == Tree[i].l && r == Tree[i].r)
			return Tree[i];
		int  mid = (Tree[i].l+Tree[i].r) >> 1;
		if(r <= mid) return Query(l,r,i<<1);
		else if(mid < l) return Query(l,r,i<<1|1);
		else
		{
			struct node ans1 = Query(l,mid,i<<1);
			struct node ans2 = Query(mid+1,r,i<<1|1);
			struct node ans;
			ans.oo = max(max(max(ans1.oo,ans2.oo),ans1.oj+ans2.oo),ans1.oo + ans2.jo);
			ans.jj = max(max(max(ans1.jj,ans2.jj),ans1.jo+ans2.jj),ans1.jj + ans2.oj);
			ans.oj = max(max(max(ans1.oj,ans2.oj),ans1.oo+ans2.jj),ans1.oj + ans2.oj);
			ans.jo = max(max(max(ans1.jo,ans2.jo),ans1.jo+ans2.jo),ans1.jj + ans2.oo);
			return ans;
		}
	}
	int main()
	{
		int T,n,m;
		sf(T);
		while(T--)
		{
			sff(n,m);
			build_tree(1,n,1);
			while(m--)
			{
			    int x;
				sf(x);
				if(x == 0)
				{
					int from,to;
					scanf("%d%d",&from,&to);
					struct node ans = Query(from,to,1);
					printf("%I64d\n",max(max(max(ans.oo,ans.jj),ans.oj),ans.jo) );
				}
				else
				{
				    int pos;
				    LL num;
					scanf("%d%I64d",&pos,&num);
					updata_tree(pos,num,1);
				}
			}
		}
		return 0;
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值