经典系列
void pushup(int i)
{
tr[i].sum=tr[ls].sum+tr[rs].sum;
tr[i].maxl=max(tr[ls].maxl,tr[ls].sum+tr[rs].maxl);
tr[i].maxr=max(tr[rs].maxr,tr[rs].sum+tr[ls].maxr);
tr[i].maxx=max(tr[ls].maxr+tr[rs].maxl,(max(tr[ls].maxx,tr[rs].maxx)));
}
p query(int i,int l,int r)
{
if(l<=tr[i].l&&tr[i].r<=r) return tr[i];
if(tr[ls].r>=l&&tr[rs].l<=r)
{
p t,tpl=query(ls,l,r),tpr=query(rs,l,r);
t.sum=tpl.sum+tpr.sum;
t.maxl=max(tpl.maxl,tpl.sum+tpr.maxl);
t.maxr=max(tpr.maxr,tpr.sum+tpl.maxr);
t.maxx=max(tpl.maxr+tpr.maxl,(max(tpl.maxx,tpr.maxx)));
return t;
}
else if(l<=tr[ls].r) return query(ls,l,r);
else return query(rs,l,r);
}
Think Twice, Code Once
#include
#define il inline
#define get getchar
#define put putchar
#define is isdigit
#define re register
#define int long long
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
#define dfor(i,a,b) for(re int i=a;i<=b;++i)
#define dforr(i,a,b) for(re int i=a;i>=b;--i)
#define dforn(i,a,b) for(re int i=a;i<=b;++i,put(10))
#define mem(a,b) memset(a,b,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof a)
#define pr 114514191981
#define gg(a) cout<<a,put(32)
#define INF 0x7fffffff
#define tt(x) cout<<x<<'\n'
#define ls i<<1
#define rs i<<1|1
#define la(r) tr[r].ch[0]
#define ra(r) tr[r].ch[1]
#define lowbit(x) (x&-x)
using namespace std;
typedef unsigned int ull;
typedef long long ll;
int read(void)
{
re int x=0,f=1;re char c=get();
while(!is(c)) (f=c==45?-1:1),c=get();
while(is(c)) x=(x<<1)+(x<<3)+(c^48),c=get();
return x*f;
}
void write(int x)
{
if(x<0) x=-x,put(45);
if(x>9) write(x/10);
put((x%10)^48);
}
#define writeln(a) write(a),put(10)
#define writesp(a) write(a),put(32)
const int N=5e5+10,M=5e5+10,SN=1e3+10,mod=998244353;
int n,m,a[N];
struct p
{
int l,r,sum,maxx,maxl,maxr;
p(){}
p(int l1,int r1){l=l1,r=r1;}
}tr[N<<2];
void pushup(int i)
{
tr[i].sum=tr[ls].sum+tr[rs].sum;
tr[i].maxl=max(tr[ls].maxl,tr[ls].sum+tr[rs].maxl);
tr[i].maxr=max(tr[rs].maxr,tr[rs].sum+tr[ls].maxr);
tr[i].maxx=max(tr[ls].maxr+tr[rs].maxl,(max(tr[ls].maxx,tr[rs].maxx)));
}
void build(int i,int l,int r)
{
tr[i]=p(l,r);
if(l==r){tr[i].maxx=tr[i].maxl=tr[i].maxr=tr[i].sum=a[l];return ;}
int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r);
pushup(i);
}
void modify(int i,int l,int r,int ind,int k)
{
if(l==r)
{
tr[i].sum=tr[i].maxx=tr[i].maxl=tr[i].maxr=k;
return ;
}
if(tr[ls].r>=ind) modify(ls,l,tr[ls].r,ind,k);
else modify(rs,tr[rs].l,r,ind,k);
pushup(i);
}
p query(int i,int l,int r)
{
if(l<=tr[i].l&&tr[i].r<=r) return tr[i];
if(tr[ls].r>=l&&tr[rs].l<=r)
{
p t,tpl=query(ls,l,r),tpr=query(rs,l,r);
t.sum=tpl.sum+tpr.sum;
t.maxl=max(tpl.maxl,tpl.sum+tpr.maxl);
t.maxr=max(tpr.maxr,tpr.sum+tpl.maxr);
t.maxx=max(tpl.maxr+tpr.maxl,(max(tpl.maxx,tpr.maxx)));
return t;
}
else if(l<=tr[ls].r) return query(ls,l,r);
else return query(rs,l,r);
}
signed main()
{
n=read(),m=read();
dfor(i,1,n) a[i]=read();
build(1,1,n);
re int op,l,r;
while(m--)
{
op=read(),l=read(),r=read();
if(op&1) (l<=r?write(query(1,l,r).maxx):write(query(1,r,l).maxx)),put(10);
else modify(1,1,n,l,r);
}
return 0;
}