20 分线段树求助

P1253 扶苏的问题

Fast_IO @ 2022-04-15 17:21:20

RT

#include<iostream>
using namespace std;
#define int long long
const int INF = 0x3f3f3f3f;
const int N = 1e6+5;
struct Node{int l,r,v,lazy_tag1,lazy_tag2;}tr[4*N];
int a[N],q,n;
inline void pushup(int p){
    tr[p].v=max(tr[p*2].v,tr[p*2+1].v);
}
inline void bulid(int p,int l,int r){
    int mid=l+r>>1;
    tr[p].l=l;
    tr[p].r=r;
    if(l==r){
        tr[p].v=a[l];
        return ;
    }
    bulid(p*2,l,mid);
    bulid(p*2+1,mid+1,r);
    pushup(p);
} 
inline void pushdown(int p){
    tr[p*2].lazy_tag1+=tr[p].lazy_tag1,tr[p*2].v+=tr[p].lazy_tag1; 
    tr[p*2+1].lazy_tag1+=tr[p].lazy_tag1,tr[p*2+1].v+=tr[p].lazy_tag1; 
    tr[p].lazy_tag1=0;
    if(tr[p].lazy_tag2!=INF){
        tr[p*2].v=tr[p*2].lazy_tag2=tr[p].lazy_tag2,tr[p*2].lazy_tag1=0;
        tr[p*2+1].v=tr[p*2+1].lazy_tag2=tr[p].lazy_tag2,tr[p*2+1].lazy_tag1=0;
        tr[p].lazy_tag2=INF;
    }
}
inline int query(int p,int ql,int qr){
    if(ql<=tr[p].l && qr>=tr[p].r) return tr[p].v;
    int mid=tr[p].l+tr[p].r>>1;
    pushdown(p);
    if(qr<=mid) return query(p*2,ql,qr);
    else if(ql>mid) return query(p*2+1,ql,qr);
    else return max(query(p*2,ql,qr),query(p*2+1,ql,qr));
}
inline void update(int p,int ul,int ur,int d){
    if(ul<=tr[p].l && ur>=tr[p].r){
        tr[p].lazy_tag1+=d;
        tr[p].v+=d;
        return ;
    }
    pushdown(p);
    int mid=tr[p].l+tr[p].r>>1;
    if(ur<=mid){
        update(p*2,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }else if(ul>mid){
        update(p*2+1,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }else{
        update(p*2,ul,ur,d);
        update(p*2+1,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }
    pushup(p);
}
inline void update2(int p,int ul,int ur,int d){
    if(ul<=tr[p].l && ur>=tr[p].r){
        tr[p].v=tr[p].lazy_tag2=d;
        tr[p].lazy_tag1=0;
        return ;
    }
    pushdown(p);
    int mid=tr[p].l+tr[p].r>>1;
    if(ur<=mid){
        update2(p*2,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }else if(ul>mid){
        update2(p*2+1,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }else{
        update2(p*2,ul,ur,d);
        update2(p*2+1,ul,ur,d);
        tr[p].v=max(tr[2*p].v,tr[2*p+1].v);
    }
    pushup(p);
}
signed main(){
    ios::sync_with_stdio(0);
    cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];
    bulid(1,1,n);
    while(q--){
        int op,l,r,x;
        cin>>op>>l>>r;
        if(op==3) cout<<query(1,l,r)<<endl;
        else if(op==2){
            cin>>x;
            update(1,l,r,x);
        }else{
            cin>>x;
            update2(1,l,r,x);
        }
    } 
    return 0;
}

by RockyYue @ 2022-04-15 17:23:38

乖 一会上课了就不要上谷啦


by Fast_IO @ 2022-04-15 17:24:35

……


by Fast_IO @ 2022-04-15 17:26:50

@RockyYue_ 所以 bug 在哪啊啊啊


by Fast_IO @ 2022-04-15 17:30:34

悬赏 2 个关注!


by StillEmpty @ 2022-04-15 17:39:08

@Fast_IO 帮你找到了,a_i可能为0


by StillEmpty @ 2022-04-15 17:39:41

@Fast_IO 还有递归函数别写inline


by StillEmpty @ 2022-04-15 17:42:03

@Fast_IO 说的话有点小不严谨,是修改操作的x可能为0


by StillEmpty @ 2022-04-15 17:43:04

@Fast_IO 艹,我是sb,忽略上面的话,是build里修改的懒标记没设inf


by StillEmpty @ 2022-04-15 17:49:30

https://www.luogu.com.cn/record/73957375 变60了


by Fast_IO @ 2022-04-15 17:55:59

谢谢大佬


| 下一页