WA on #10萌新求助

P1253 扶苏的问题

Rorre_y @ 2024-09-27 20:21:04

rt,玄关求调谢谢喵

#include<cstdio>
using namespace std;
long long n,m,a[1000010],tag[4000020],tag2[4000020],tr[4000020];
long long cnt=-9000000000000000001;
//            -9223372036854775808
inline long long max(long long x,long long y){
    if(x>=y)return x;
    return y;
}
inline void up(long long x){//gengxin
    tr[x]=max(tr[x*2],tr[x*2+1]);
}
inline void tagg2(long long x,long long k){//biaoji2
    tag[x]=0;
    tr[x]=k;
    tag2[x]=k;
}
inline void down2(long long x){//xiafang(gai)
    if(tag2[x]!=cnt){
        tagg2(x*2,tag2[x]);
        tagg2(x*2+1,tag2[x]);
        tag2[x]=cnt;
    }
}
inline void tagg(long long x,long long k){//biaoji
    down2(x);
    tr[x]+=k;
    tag[x]+=k;
}
inline void down1(long long x){//xiafang(jia)
    if(tag[x]){
        tagg(x*2,tag[x]);
        tagg(x*2+1,tag[x]);
        tag[x]=0;
    }
}
inline void down(long long x){//xiafang
    down2(x);
    down1(x);
}
inline void build(long long x,long long l,long long r){
    if(l==r){
        tr[x]=a[l];
        tag2[x]=cnt;
        tag[x]=0;
        return;
    }
    long long mid=(l+r)/2;
    build(x*2,l,mid);
    build(x*2+1,mid+1,r);
    up(x);
}
inline void gai1(long long nl,long long nr,long long l,long long r,long long x,long long k){
    if(nl<=l&&r<=nr){
        down2(x);
        tr[x]+=k;
        tag[x]+=k;
        return;
    }
    down(x);
    long long mid=(l+r)/2;
    if(nl<=mid)gai1(nl,nr,l,mid,x*2,k);
    if(nr>mid)gai1(nl,nr,mid+1,r,x*2+1,k);
    up(x);
}
inline void gai2(long long nl,long long nr,long long l,long long r,long long x,long long k){
    if(nl<=l&&r<=nr){
        tag[x]=0;
        tr[x]=k;
        tag2[x]=k;
        return;
    }
    down(x);
    long long mid=(l+r)/2;
    if(nl<=mid)gai2(nl,nr,l,mid,x*2,k);
    if(nr>mid)gai2(nl,nr,mid+1,r,x*2+1,k);
    up(x);
}
inline long long cha(long long nl,long long nr,long long l,long long r,long long x){
    if(nl<=l&&r<=nr){
        return tr[x];
    }
    long long mid=(l+r)/2;
    down(x);
    long long ans=cnt; 
    if(nl<=mid)ans=cha(nl,nr,l,mid,x*2);
    if(nr>mid)ans=max(cha(nl,nr,mid+1,r,x*2+1),ans);
    return ans;
}
int main()
{
    long long aa,b,c,d;
    scanf("%lld %lld",&n,&m);
    for(long long i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    for(long long i=1;i<=n*4+10;i++){
        tag2[i]=cnt;
    }
    build(1,1,n);
    for(long long i=1;i<=m;i++){
        scanf("%lld",&aa);
        scanf("%lld %lld",&b,&c);
        if(aa==1){
            scanf("%lld",&d);
            gai2(b,c,1,n,1,d);
        }
        else if(aa==2){
            scanf("%lld",&d);
            gai1(b,c,1,n,1,d);
        }
        else{
            printf("%lld\n",cha(b,c,1,n,1));
        }
    }
}

by aimoyudexianyu @ 2024-10-06 19:57:29

定义改成这样就过了

const long long M=1000500; 
long long n,m,a[M<<1],tag[M<<3],tag2[M<<3],tr[M<<3];

原因是线段树一般开 4n,但是你WA了#10,所以我考虑开了八倍,原因是你的代码可能下放了子节点的标记导致越界,请自己检查。

@Rorre_y


by aimoyudexianyu @ 2024-10-06 19:58:26


by Rorre_y @ 2024-10-06 20:03:28

@aimoyudexianyu 好,谢谢大佬指导,已过orz


|