20 分玄关求条

P1253 扶苏的问题

CC__DIAMOND @ 2024-06-22 10:03:35

#include <bits/stdc++.h>
#define BAR cout<<"*******\n";
#define fst first
#define snd second
#define INFILE(x) freopen(x,"r",stdin)
#define OUTFILE(x) freopen(x,"w",stdout)
#define eb_ emplace_back
#define ep_ emplace
#define mp_ make_pair
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll=long long;
using ul=unsigned long long;
using ui=unsigned int;
using db=double;
using pii=pair<int,int>;
using pil=pair<int,ll>;
using pll=pair<ll,ll>;
constexpr int int_inf=0x3f3f3f3f;
constexpr ll ll_inf=0x3f3f3f3f3f3f3f3fll;

namespace Solution{
constexpr int max_n=1e6+10;
ll a[max_n];
struct {
    struct node{
        int ls,rs;
        ll val,lazy1,lazy2;
        void init(){
            lazy2=-ll_inf;
        }
    } nodes[2*max_n];
    int tot=0;
    node &root=nodes[0];

    void push_up(node &p){
        p.val=max(nodes[p.ls].val,nodes[p.rs].val);
    }

    void push_down(node &p,int nl,int nr){
        node &ls=nodes[p.ls],&rs=nodes[p.rs];
        if(p.lazy2!=-ll_inf){
            p.lazy2+=p.lazy1;
            p.lazy1=0;
            ls.lazy1=rs.lazy1=0;
            ls.lazy2=rs.lazy2=p.lazy2;
            ls.val=p.lazy2;
            rs.val=p.lazy2;
            p.lazy2=-ll_inf;
        }else if(p.lazy1){
            ls.lazy1+=p.lazy1;
            rs.lazy1+=p.lazy1;
            ls.val+=p.lazy1;
            ls.val+=p.lazy1;
            p.lazy1=0;
        }
    }

    void build(node &p,int nl,int nr){
        p.init();
        if(nl==nr){
            p.val=a[nl];
            return;
        }
        p.ls=++tot;
        p.rs=++tot;
        int mid=(nl+nr)>>1;
        build(nodes[p.ls],nl,mid);
        build(nodes[p.rs],mid+1,nr);
        push_up(p);
    }

    void modify(int l,int r,ll val,node &p,int nl,int nr){
        if(l<=nl&&nr<=r){
            p.lazy1=0;
            p.lazy2=val;
            p.val=val;
            return;
        }
        push_down(p,nl,nr);
        int mid=(nl+nr)>>1;
        if(l<=mid) modify(l,r,val,nodes[p.ls],nl,mid);
        if(r>mid) modify(l,r,val,nodes[p.rs],mid+1,nr);
        push_up(p);
    }

    void add(int l,int r,ll val,node &p,int nl,int nr){
        if(l<=nl&&nr<=r){
            p.lazy1+=val;
            p.val+=val;
            return;
        }
        push_down(p,nl,nr);
        int mid=(nl+nr)>>1;
        if(l<=mid) add(l,r,val,nodes[p.ls],nl,mid);
        if(r>mid) add(l,r,val,nodes[p.rs],mid+1,nr);
        push_up(p);
    }

    ll query(int l,int r,node &p,int nl,int nr){
        if(l<=nl&&nr<=r){
            return p.val;
        }
        push_down(p,nl,nr);
        int mid=(nl+nr)>>1;
        ll ans=-ll_inf;
        if(l<=mid) ans=max(ans,query(l,r,nodes[p.ls],nl,mid));
        if(r>mid) ans=max(ans,query(l,r,nodes[p.rs],mid+1,nr));
        return ans;
    }
} tree;
void solve() {
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;++i) cin>>a[i];
    tree.build(tree.root,1,n);
    for(int i=1;i<=q;++i){
        int op,l,r,x;
        cin>>op;
        if(op==1){
            cin>>l>>r>>x;
            tree.modify(l,r,x,tree.root,1,n);
        }else if(op==2){
            cin>>l>>r>>x;
            tree.add(l,r,x,tree.root,1,n);
        }else if(op==3){
            cin>>l>>r;
            cout<<tree.query(l,r,tree.root,1,n)<<'\n';
        }
    }
}
}

int main() {
    //INFILE("IOFile/P1253_2.in");
    //freopen("IOFile/P1253_2.my.out","w",stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int T=1;
    // cin>>T;
    while(T--) {
        Solution::solve();
    }
    return 0;
}

by danlao @ 2024-06-22 10:15:46

@CC__DIAMOND

#include <iostream>
using namespace std;
const int N=1e6+10;
#define hh printf("\n")
#define kg printf(" ")
#define m (l+r>>1)
#define ls (p<<1)
#define rs ((p<<1)|1)
//#define int long long
#define int __int128
namespace quickread{
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void write(int x){
        if(x<0){putchar('-');x=-x;}
        if(x>9)write(x/10);
        putchar(x%10+'0');
    }
}
using namespace quickread;
struct Segment_tree{
    int maxn,tag_add,tag_cov;
} segment_tree[N*5];
int n,q,a[N],inf=0x3f3f3f3f3f3f3f3f;
void push_up(int p){
    segment_tree[p].maxn=max(segment_tree[ls].maxn,segment_tree[rs].maxn);
}
void push_down_add(int l,int r,int p){
    if(!segment_tree[p].tag_add) return;
    segment_tree[ls].maxn+=segment_tree[p].tag_add;
    segment_tree[rs].maxn+=segment_tree[p].tag_add;
    segment_tree[ls].tag_add+=segment_tree[p].tag_add;
    segment_tree[rs].tag_add+=segment_tree[p].tag_add;
    segment_tree[p].tag_add=0;
}
void push_down_cov(int l,int r,int p){
    if(segment_tree[p].tag_cov==-inf) return;
    segment_tree[ls].maxn=segment_tree[p].tag_cov;
    segment_tree[rs].maxn=segment_tree[p].tag_cov;
    segment_tree[ls].tag_cov=segment_tree[p].tag_cov;
    segment_tree[rs].tag_cov=segment_tree[p].tag_cov;
    segment_tree[ls].tag_add=0;
    segment_tree[rs].tag_add=0;
    segment_tree[p].tag_cov=-inf;
}
void push_down(int l,int r,int p){
    push_down_cov(l,r,p);push_down_add(l,r,p);
}
void build(int l,int r,int p){
    segment_tree[p]=Segment_tree{-inf,0,-inf};
    if(l==r){
        segment_tree[p]=Segment_tree{a[l],0,-inf};
        return;
    }
    build(l,m,ls),build(m+1,r,rs);
    push_up(p);
}
void update_add(int lt,int rt,int l,int r,int p,int k){
    if(lt<=l&&r<=rt){
        push_down_cov(l,r,p);
        segment_tree[p].maxn+=k;
        segment_tree[p].tag_add+=k;
        return;
    }
    push_down(l,r,p);
    if(lt<=m) update_add(lt,rt,l,m,ls,k);
    if(rt>m) update_add(lt,rt,m+1,r,rs,k);
    push_up(p);
}
void update_cov(int lt,int rt,int l,int r,int p,int k){
    if(lt<=l&&r<=rt){
        segment_tree[p].tag_add=0;
        segment_tree[p].maxn=k;
        segment_tree[p].tag_cov=k;
        return;
    }
    push_down(l,r,p);
    if(lt<=m) update_cov(lt,rt,l,m,ls,k);
    if(rt>m) update_cov(lt,rt,m+1,r,rs,k);
    push_up(p);
}
int query(int lt,int rt,int l,int r,int p){
    if(lt<=l&&r<=rt) return segment_tree[p].maxn;
    push_down(l,r,p);
    int maxn=-inf;
    if(lt<=m) maxn=query(lt,rt,l,m,ls);
    if(rt>m) maxn=max(maxn,query(lt,rt,m+1,r,rs));
    return maxn;
}
signed main(){
    n=read(),q=read();
    for(int i=1;i<=n;i++) a[i]=read();
    build(1,n,1);
    while(q--){
        int op=read(),l=read(),r=read(),x;
        if(op==2){
            x=read();
            update_add(l,r,1,n,1,x);
        }
        if(op==1){
            x=read();
            update_cov(l,r,1,n,1,x);
        }
        if(op==3) write(query(l,r,1,n,1)),hh;
    }
    return 0;
} 

by danlao @ 2024-06-22 10:17:03

@CC__DIAMOND 这是我的代码,看样子思路差不多,你对照一下


by CC__DIAMOND @ 2024-06-24 17:19:16

@danlao 谢谢,但是不用了,我自己调过了,但是我感觉我们思路还是不一样。


|