60pts码风优良,求调J

P1253 扶苏的问题

Lingyu_fly @ 2024-12-03 13:31:45

#include<bits/stdc++.h>
#define int long long
#define ls (u<<1)
#define rs (u<<1|1)
using namespace std;
const int N=1e6+10;
int n,q;
int a[N];
struct node{
    int l,r,maxx,lazy,chg;
}tree[N<<3];
void pushup(int u){
    tree[u].maxx=max(tree[ls].maxx,tree[rs].maxx);
}
void build(int u,int l,int r){
    tree[u]={l,r,0,0,(long long)-1e18};
    if(l==r){
        tree[u].maxx=a[l];
        return ;
    }
    int mid=l+r>>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(u);
}
void pushdown(int u){
    if(tree[u].chg>(long long)-1e18){
        tree[ls].maxx=tree[u].chg;
        tree[rs].maxx=tree[u].chg;
        tree[ls].chg=tree[u].chg;
        tree[rs].chg=tree[u].chg;
        tree[u].chg=(long long)-1e18;
    }
    tree[ls].lazy+=tree[u].lazy;
    tree[rs].lazy+=tree[u].lazy;
    tree[ls].maxx+=tree[u].lazy;
    tree[rs].maxx+=tree[u].lazy;
    tree[u].lazy=0;pushup(u);
}
void modify(int u,int l,int r,int k,int op){
    if(l<=tree[u].l&&tree[u].r<=r){
        if(op==1){
            tree[u].maxx=k;
            tree[u].chg=k;
            tree[u].lazy=0;
        }else{
            tree[u].lazy+=k;
            tree[u].maxx+=k;
        }
        return ;
    }
    pushdown(u);
    int mid=tree[u].l+tree[u].r>>1;
    if(l<=mid) modify(ls,l,r,k,op);
    if(mid<r) modify(rs,l,r,k,op);
    pushup(u);
}
int query(int u,int l,int r){
    if(l<=tree[u].l&&tree[u].r<=r){
        return tree[u].maxx;
    }
    pushdown(u);
    int mid=tree[u].l+tree[u].r>>1;
    int ans=(long long)-1e18;
    if(l<=mid) ans=max(ans,query(ls,l,r));
    if(mid<r) ans=max(ans,query(rs,l,r));
    return ans;
}
signed main(){
    scanf("%lld%lld",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    build(1,1,n);
    int op,l,r,x;
    while(q--){
        scanf("%lld%lld%lld",&op,&l,&r);
        if(op!=3){
            scanf("%lld",&x);
            modify(1,l,r,x,op);
        }else{
            printf("%lld\n",query(1,l,r));
        }
    }
    return 0;
}

by Lingyu_fly @ 2024-12-03 17:35:38

悬赏


by holo_23 @ 2024-12-05 10:55:35

lazytag在赋值传递的时候不能用+=,因为已经赋值了,说明下面的lazytag已经无效了,直接覆盖掉就行了。

void pushdown(int u){
    if(tree[u].chg>(long long)-1e18){
        tree[ls].maxx=tree[u].chg;
        tree[rs].maxx=tree[u].chg;
        tree[ls].chg=tree[u].chg;
        tree[rs].chg=tree[u].chg;
        tree[u].chg=(long long)-1e18;
        tree[ls].lazy=tree[u].lazy;
        tree[rs].lazy=tree[u].lazy;
    }
    else{
        tree[ls].lazy+=tree[u].lazy;
        tree[rs].lazy+=tree[u].lazy;
    }
    tree[ls].maxx+=tree[u].lazy;
    tree[rs].maxx+=tree[u].lazy;
    tree[u].lazy=0;
}

by Lingyu_fly @ 2024-12-06 15:30:31

@holo_23感谢,已过~


|