JoyLosingK @ 2024-10-17 17:07:53
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5,inf=1e9+5;
int n,m,a[N];
int op,ql,qr;
struct tree{
int tl,tr,t,s,l,r;
#define tl(p) h[p].tl
#define tr(p) h[p].tr
#define t(p) h[p].t
#define s(p) h[p].s
#define l(p) h[p].l
#define r(p) h[p].r
} h[N<<2];
inline int read(){
int x=0,f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
for(;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;}
void build(int l,int r,int k){ l(k)=l,r(k)=r;
if(l==r){s(k)=tl(k)=t(k)=tr(k)=a[r];return;}
int mid=l+r>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
s(k)=s(k<<1)+s(k<<1|1);
t(k)=tr(k<<1)+tl(k<<1|1);
t(k)=max(t(k),max(t(k<<1),t(k<<1|1)));
tl(k)=max(tl(k<<1),s(k<<1)+tl(k<<1|1));
tr(k)=max(tr(k<<1|1),s(k<<1|1)+tr(k<<1));}
tree query(int k,int x,int y){
if(x<=l(k)&&r(k)<=y) {return h[k];}
int mid=l(k)+r(k)>>1;
if(y<=mid) return query(k<<1,x,y);
return query(k<<1|1,x,y);
tree res,a=query(k<<1,x,y),b=query(k<<1|1,x,y);
res.tl=max(a.tl,a.s+b.tl);
res.tr=max(b.tr,a.tr+b.s);
res.t=max(max(a.t,b.t),a.tr+b.tl);
return res;}
void change(int k,int x,int y){
if(l(k)==r(k)){s(k)=tl(k)=t(k)=tr(k)=y;return;}
int mid=l(k)+r(k)>>1;
if(x<=mid)change(k<<1,x,y);
else change(k<<1|1,x,y);
s(k)=s(k<<1)+s(k<<1|1);
t(k)=tr(k<<1)+tl(k<<1|1);
t(k)=max(t(k),max(t(k<<1),t(k<<1|1)));
tl(k)=max(tl(k<<1),s(k<<1)+tl(k<<1|1));
tr(k)=max(tr(k<<1|1),s(k<<1|1)+tr(k<<1));}
int main(){ n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read();
build(1,1,n);
while(m--){ op=read(),ql=read(),qr=read();
if(op==1){if(ql>qr) swap(ql,qr);
cout<<query(1,ql,qr).t<<endl;
} else change(1,ql,qr);
} return 0;
}
线段树0tps求调,全RE
by dou_zi_ @ 2024-10-17 17:49:24
re是因为你build的时候传参传错了(
这不是不过样例吗
by JoyLosingK @ 2024-10-17 18:01:30
@douzi 谢谢大佬,谢谢!