???我感觉我的update出了问题却找不到,有dalao能救一下蒟蒻吗
查看原帖
???我感觉我的update出了问题却找不到,有dalao能救一下蒟蒻吗
670826
NianFeng楼主2022/7/16 16:03
#include <bits/stdc++.h>
using namespace std;
struct TREE{
	int l,r,num,front,back,ans;
}tree[200020];
int a[50010],n,m;

void build(int x , int Left , int Right) {
	tree[x].l = Left ;
	tree[x].r = Right ;
	if(Left == Right) {
		tree[x].ans = a[Left];
		tree[x].front = a[Left];
		tree[x].back = a[Left];
		tree[x].num = a[Left];
		return;
	}
	int leftroot=x*2;
	int rightroot=x*2+1;
	int mid=Left+Right>>1;
	build(leftroot, Left , mid);
	build(rightroot, mid + 1 ,Right);
	tree[x].num = tree[leftroot].num + tree[rightroot].num;
	tree[x].front = max(tree[leftroot].front, tree[leftroot].num + tree[rightroot].front);
	tree[x].back = max(tree[rightroot].back, tree[rightroot].num + tree[leftroot].back);
	tree[x].ans = max(max(tree[leftroot].ans, tree[rightroot].ans), tree[leftroot].back + tree[rightroot].front);
	return;
}

void update(int root,int x,int y){
	if(tree[root].l==x&&tree[root].r==x){
		tree[root].num=tree[root].back=tree[root].front=y;
		return;
	}
	int leftroot=root*2;
	int rightroot=root*2+1;
	int mid=tree[root].l+tree[root].r>>1;
	tree[root].num = tree[leftroot].num + tree[rightroot].num;
	tree[root].front = max(tree[leftroot].front, tree[leftroot].num + tree[rightroot].front);
	tree[root].back = max(tree[rightroot].back, tree[rightroot].num + tree[leftroot].back);
	tree[root].ans = max(max(tree[leftroot].ans, tree[rightroot].ans), tree[leftroot].back + tree[rightroot].front);
    return;
	
}

TREE query(int now,int l,int r)
{
    if(tree[now].l>=l&&tree[now].r<=r)
		return tree[now];
    int mid=tree[now].l+tree[now].r>>1;
    if(mid<l)
    	return query(now*2+1,l,r);
    else if(mid>=r)
		return query(now*2,l,r);
    TREE left,right,ans;
    left=query(now*2,l,mid);
    right=query(now*2+1,mid+1,r);
    ans.ans=max(left.back+right.front,max(left.ans,right.ans));
    return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	build(1,1,n);
	scanf("%d",&m);
	int q,x,y;
	while(m--){
		scanf("%d%d%d",&q,&x,&y);
		if(q==0){
			update(1,x,y);
		}
		if(q==1){
			printf("%d\n",query(1,x,y).ans);
		}
	}
}

build我测过了,木有问题,应该是update出了点问题(或者是query),dalao帮个

2022/7/16 16:03
加载中...