跳到正文

个人记录

撒旦

LuminousYonderQuiet约 3 分钟
原文

这是 2026年8月14日 存档的第 14 版,不是当前版本。查看当前版本

文章信息与版本
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,m,a[N];
struct node{
	int sum,maxsum,maxl,maxr,l,r;
}t[N*4];
node pin(node a,node b){
	node ret;
	ret.sum=a.sum+b.sum;
	ret.maxsum=max(a.maxsum,max(b.maxsum,a.maxr+b.maxl));
	ret.maxl=max(a.maxl,a.sum+b.maxl);
	ret.maxr=max(b.maxr,b.sum+a.maxr);
	ret.l=a.l;
	ret.r=b.r;
	return ret;
}
void pushup(int p){
	t[p]=pin(t[p*2],t[p*2+1]);
}
void build(int p,int l,int r){
	t[p].l=l;
	t[p].r=r;
	if(l==r){
		t[p].maxsum=a[l];
		t[p].maxr=a[l];
		t[p].maxl=a[l];
		t[p].sum=a[l];
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushup(p);
}
void update(int p,int k,int c){
	if(t[p].l==k&&t[p].r==k){
		t[p].maxsum=c;
		t[p].maxr=c;
		t[p].maxl=c;
		t[p].sum=c;
		return;
	}
	int mid=(t[p].l+t[p].r)/2;
	if(k<=mid){
		update(p*2,k,c);
	}
	if(k>mid){
		update(p*2+1,k,c);
	}
	pushup(p);
}
node query(int p,int l,int r){
	if(l<=t[p].l&&r>=t[p].r){
		return t[p];
	}
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid&&r>mid){
		node n1=query(p*2,l,r);
		node n2=query(p*2+1,l,r);
		return pin(n1,n2);
	}
	if(l<=mid){
		return query(p*2,l,r);
	}
	if(r>mid){
		return query(p*2+1,l,r);
	}
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int op,x,y;
		cin>>op>>x>>y;
		if(op==1){
			cout<<query(1,min(x,y),max(x,y)).maxsum<<'\n';
		}
		if(op==2){
			update(1,x,y);
		}
	}
	return 0;
}