悬棺求条,遇到负区间就输出0,其他正确
查看原帖
悬棺求条,遇到负区间就输出0,其他正确
1226067
o__ll楼主2024/7/14 14:45

rt
die码如下

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,p;
struct node{
	int l,r,sum,ma,mal,mar;
}t[5000005];
void up(int c){
	if(t[c*2].mar<0&&t[c*2+1].mal<0) t[c].ma=max(t[c*2].mar,t[c*2+1].mal);
	else{
		t[c].ma=0;
		if(t[c*2].mar>0) t[c].ma+=t[c*2].mar;
		if(t[c*2+1].mal>0) t[c].ma+=t[c*2+1].mal;
	}
	t[c].ma=max(t[c].ma,max(t[c*2].ma,t[c*2+1].ma));
	t[c].mal=max(t[c*2].mal,t[c*2].sum+t[c*2+1].mal);
	t[c].mar=max(t[c*2+1].mar,t[c*2+1].sum+t[c*2].mar);
	t[c].sum=t[c*2].sum+t[c*2+1].sum;
}
void bd(int x,int y,int c){
	t[c].l=x,t[c].r=y;
	if(x==y){
		cin>>t[c].sum;
		t[c].ma=t[c].mal=t[c].mar=t[c].sum;
		return;
	}
	int mid=(x+y)/2;
	bd(x,mid,c*2);
	bd(mid+1,y,c*2+1);
	up(c);
}
void ad(int c,int x,int k){
	if(t[c].l==t[c].r){
		t[c].sum=t[c].ma=t[c].mal=t[c].mar=k;
		return;
	}
	int mid=(t[c].l+t[c].r)/2;
	if(x<=mid) ad(c*2,x,k);
	else ad(c*2+1,x,k);
	up(c);
}
node fd(int x,int y,int c){
	if(t[c].l>=x&&t[c].r<=y) return t[c];
	int mid=(t[c].l+t[c].r)/2;
	if(x<=mid&&y>mid){
		node ans,lc=fd(x,y,c*2),rc=fd(x,y,c*2+1);
		if(ans.mar<0&&rc.mal<0) ans.ma=max(lc.mar,rc.mal);
		else{
			ans.ma=0;
			if(lc.mar>0) ans.ma+=lc.mar;
			if(rc.mal>0) ans.ma+=rc.mal;
		}
		ans.ma=max(ans.ma,max(lc.ma,rc.ma));
		ans.mal=max(lc.mal,lc.sum+rc.mal);
		ans.mar=max(rc.mar,rc.sum+lc.mar);
		ans.sum=lc.sum+rc.sum;
		return ans; 
	}	
	else if(x<=mid) return fd(x,y,c*2);
	else return fd(x,y,c*2+1);
}
signed main(){
	cin>>n>>m;
	bd(1,n,1);
	while(m--){
		cin>>p;
		if(p==1){
			int x,y;
			cin>>x>>y;
			if(x>y) swap(x,y);
			cout<<fd(x,y,1).ma<<endl;
		}
		else{
			int x,k;
			cin>>x>>k;
			ad(1,x,k);
		}
	}
	return 0;
}
2024/7/14 14:45
加载中...