样例错误求助

P4513 小白逛公园

liyz007 @ 2024-12-20 21:09:05

样例输出5 5

#include<bits/stdc++.h>
using namespace std;
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
const int N=5e5+5;
struct node{
    ll d,maxl,maxr,ans;
}tree[N<<2];
int n,m;
void pushup(int p){
    tree[p].d=tree[ls].d+tree[rs].d;
    tree[p].maxl=max(tree[ls].maxl,tree[ls].d+tree[rs].maxl);
    tree[p].maxr=max(tree[rs].maxr,tree[rs].d+tree[ls].maxr);
    tree[p].ans=max(max(tree[ls].ans,tree[rs].ans),tree[ls].maxr+tree[rs].maxl);
}
void build(int p,int l,int r){
    if(l==r){
        cin>>tree[l].d;
        tree[l].maxl=tree[l].maxr=tree[l].ans=tree[l].d;
        return;
    }
    int mid=(l+r)>>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(p);
}
void change(int p,int l,int r,int x,int k){
    if(l==r){
        tree[p].d=tree[p].ans=tree[p].maxl=tree[p].maxr=k;
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid) change(ls,l,mid,x,k);
    else change(rs,mid+1,r,x,k);
    pushup(p);
}
node query(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y) return tree[p];
    int mid=(l+r)>>1;
    if(y<=mid) return query(ls,l,mid,x,y);
    else{
        if(x>mid) return query(rs,mid+1,r,x,y);
        else{
            node t,a=query(ls,l,mid,x,y),b=query(rs,mid+1,r,x,y);
            t.maxl=max(a.maxl,a.d+b.maxl);
            t.maxr=max(b.maxr,a.maxr+b.d);
            t.ans=max(max(a.ans,b.ans),a.maxr+b.maxl);
            return t;
        }
    }

}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    build(1,1,n);
    while(m--){
        int opt,x,y;
        cin>>opt>>x>>y;
        if(opt==1){
            if(x>y) swap(x,y);
            cout<<query(1,1,n,x,y).ans<<'\n';
        }else change(1,1,n,x,y);
    }
    return 0;
}

|