线段树RE 9个点求调

P1253 扶苏的问题

Neokaye @ 2024-11-26 21:06:10

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int N=1e6+5;
const ll INF=1e18;
ll a[N];
ll tree[N<<2];
ll add[N<<2],change[N<<2];
ll ls(ll p){return p<<1;}
ll rs(ll p){return p<<1|1;}
ll push_up(ll p){tree[p]=max(tree[ls(p)],tree[rs(p)]);}
void build(ll p,ll pl,ll pr){
    add[p]=0;change[p]=INF;
    if(pl==pr){tree[p]=a[pl];return ;}
    ll mid=pl+pr>>1;
    build(ls(p),pl,mid);
    build(rs(p),mid+1,pr);
    push_up(p);
}
void add_tag(ll p,ll d){
    add[p]+=d;tree[p]+=d;
}
void change_tag(ll p,ll d){
    change[p]=d;add[p]=0;tree[p]=d;
}
void push_down(ll p){
    if(change[p]!=INF){
        change_tag(ls(p),change[p]);
        change_tag(rs(p),change[p]);
        change[p]=INF;
    }
    if(add[p]){
        add_tag(ls(p),add[p]);
        add_tag(rs(p),add[p]);
        add[p]=0; 
    }
}
void update1(ll p,ll pl,ll pr,ll L,ll R,ll d){
    if(L<=pl&&pr<=R){change_tag(p,d);return ;}
    //cout<<111<<'\n';

    push_down(p);
    ll mid=pl+pr>>1;
    if(L<=mid) update1(ls(p),pl,mid,L,R,d);
    if(R>mid)  update1(rs(p),mid+1,pr,L,R,d);
    push_up(p);
}
void update2(ll p,ll pl,ll pr,ll L,ll R,ll d){
    if(L<=pl&&pr<=R){add_tag(p,d);return ;}
    //cout<<111<<'\n';
    push_down(p);
    ll mid=pl+pr>>1;
    if(L<=mid) update2(ls(p),pl,mid,L,R,d);
    if(R>mid)  update2(rs(p),mid+1,pr,L,R,d);
    push_up(p);
}
ll query(ll p,ll pl,ll pr,ll L,ll R){
    if(L<=pl&&pr<=R){return tree[p];}
    //cout<<111<<'\n';
    push_down(p);
    ll mid=pl+pr>>1;
    ll res=-INF;
    if(L<=mid) res=max(res,query(ls(p),pl,mid,L,R));
    if(R>mid)  res=max(res,query(rs(p),mid+1,pr,L,R));
    return res;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,q;cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,n);
    while(q--){
        ll opt,l,r,x;
        cin>>opt>>l>>r;
        if(opt==1){cin>>x;update1(1,1,n,l,r,x);}
        else if(opt==2){cin>>x;update2(1,1,n,l,r,x);}
        else {cout<<query(1,1,n,l,r)<<'\n';}
    }
    return 0;
}

by zzz13579zzz @ 2024-11-26 21:14:37

push_up没有返回值


by Neokaye @ 2024-11-27 12:51:48

@zzz13579zzz 谢谢,已关注


|