求助,为何代码厌氧

P4513 小白逛公园

rmzls @ 2023-01-17 21:14:45

不开O2 A了,开了之后0分(WA on answer too short),求助

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,m,a[N];
struct node{
    int l,r,sum,rsum,lsum,msum;
    node* left;
    node *right;
};
void build(node* prs,int l,int r){
    prs->l=l;
    prs->r=r;
    if(l==r){
//      if(a[l]>0){
        prs->lsum=prs->msum=prs->rsum=a[l];
//      }
//      else{
//          prs->lsum=prs->msum=prs->rsum=0;
//      }
        prs->sum=a[l];
        return ;
    }
    prs->left=new node;
    prs->right=new node;
    int mid=(l+r)>>1;
    build(prs->left,l,mid);
    build(prs->right,mid+1,r);
    prs->sum=prs->left->sum+prs->right->sum;
    prs->lsum=max(prs->left->lsum,prs->left->sum+prs->right->lsum);
    prs->rsum=max(prs->right->rsum,prs->right->sum+prs->left->rsum);
    prs->msum=max(prs->left->rsum+prs->right->lsum,max(prs->left->msum,prs->right->msum)); 
}
void change(node* prs,int l,int v){
    if(prs->l==prs->r&&prs->l==l){
        prs->sum=v;
//      if(v>0){
        prs->lsum=prs->msum=prs->rsum=v;
//      }
//      else{
//          prs->lsum=prs->msum=prs->rsum=0;
//      }
        return ;
    }
    int mid=(prs->l+prs->r)>>1;
    if(l<=mid){
        change(prs->left,l,v);
    }
    if(l>mid){
        change(prs->right,l,v);
    }
    prs->sum=prs->left->sum+prs->right->sum;
    prs->lsum=max(prs->left->lsum,prs->left->sum+prs->right->lsum);
    prs->rsum=max(prs->right->rsum,prs->right->sum+prs->left->rsum);
    prs->msum=max(prs->left->rsum+prs->right->lsum,max(prs->left->msum,prs->right->msum)); 
}
node serch(node* prs,int l,int r){
    if(l<=prs->l&&r>=prs->r){
        node a;
        a.lsum=prs->lsum;a.msum=prs->msum;
        a.rsum=prs->rsum;a.sum=prs->sum;
        return a;
    }
    int mid=(prs->l+prs->r)>>1;
    bool ha=0,hb=0;
    node a,b,k;
    k.sum=0;
    a.rsum=a.lsum=a.msum=a.sum=b.rsum=b.lsum=b.msum=b.sum=0;
    if(l<=mid){
        a=serch(prs->left,l,r);
        ha=1;
    }
    if(r>mid){
        b=serch(prs->right,l,r);
        hb=1;
    }
    if(ha&&!hb){
        return a;
    }
    if(!ha&&hb){
        return b;
    }
    if(ha&&hb){
        k.msum=max(max(a.msum,b.msum),a.rsum+b.lsum);
        k.rsum=max(b.rsum,a.rsum+b.sum);
        k.lsum=max(a.lsum,a.sum+b.lsum);
        k.sum=a.sum+b.sum;
        return k;
    }
}
signed main(){
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    node* root=new node;
    build(root,1,n);
    int q1,q2,q3;
    for(int i=1;i<=m;i++){
        scanf("%d%lld%lld",&q1,&q2,&q3);
        if(q1==1){
            if(q2>q3){
                swap(q2,q3);
            }
            printf("%lld\n",serch(root,q2,q3).msum);
        }
        else{
            change(root,q2,q3);
        }
    }
}

by strlen_s_ @ 2023-01-17 21:28:03

可能是你 scanf 那里有个 %d。


by rmzls @ 2023-01-27 21:57:31

@seanli a了,感谢


|