HDU多校 - 5316 Magician 线段树

更新的操作很简单 关键是0操作怎么做  我们要求一个子序列的最大和 这个子序列满足每两个相邻的项在原数组中的下标都是奇偶相交的

那么肯定有4种情况(用o表示奇,e表示偶):oo,ee,oe,eo

用线段树去维护这四种就行啦 注意初值应该是负无穷而不是0

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
int a[N];
typedef long long ll;
const int inf = 0x3f3f3f3f;
struct node{
	ll oe,oo,ee,eo;
	void clear(){
		oe=oo=ee=eo=-inf;
	}
}T[N<<2];
#define ls (id<<1)
#define rs (id<<1|1)
#define lson ls,l,mid
#define rson rs,mid+1,r
node pushup(node a,node b){
	node ans;
	ans.ee=max(a.ee,b.ee);
	ans.ee=max(max(ans.ee,a.eo+b.ee),a.ee+b.oe);
	ans.oo=max(a.oo,b.oo);
	ans.oo=max(max(ans.oo,a.oe+b.oo),a.oo+b.eo);
	ans.oe=max(a.oe,b.oe);
	ans.oe=max(max(ans.oe,a.oo+b.ee),a.oe+b.oe);
	ans.eo=max(a.eo,b.eo);
	ans.eo=max(max(ans.eo,a.ee+b.oo),a.eo+b.eo);
	return ans; 
}
void build(int id,int l,int r){
	if(l==r){
		T[id].clear();
		if(l%2==0) T[id].ee=a[l];
		else {T[id].oo=a[l];}
		return;
	} 
	int mid = l+r>>1;
	build(lson);build(rson);
	T[id]=pushup(T[ls],T[rs]); 
}
void update(int id,int l,int r,int pos,ll val){
	if(l==r){
		T[id].clear();
		if(l%2==0) T[id].ee=val;
		else {T[id].oo=val;}
		return;
	}
	int mid = l+r>>1;
	if(pos<=mid) update(lson,pos,val);
	else {update(rson,pos,val);}
	T[id]=pushup(T[ls],T[rs]);
}
node query(int id,int l,int r,int L,int R){
	if(L<=l&&R>=r) return T[id];
	node a,b;
	int mid = l+r>>1;
	a.clear(),b.clear();
	if(L<=mid) a=query(lson,L,R);
	if(R>mid) b=query(rson,L,R);
	return pushup(a,b);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
		build(1,1,n);
		for(int i = 1; i <= m; i++){
			int op,l,r;
			scanf("%d%d%d",&op,&l,&r);
			if(op==0){
				node ret;
				ret=query(1,1,n,l,r);
				printf("%lld\n",max(max(ret.ee,ret.oo),max(ret.eo,ret.oe)));
			}else {update(1,1,n,l,r);}
		}
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值