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;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章