线段树50分求助

P1253 扶苏的问题

platfi @ 2023-05-20 16:23:46

#define maxn -0x3f3f3f3f3f3f3f3f
#define ll long long
using namespace std;
struct unit{
    int l,r;
    int v;
    ll swi;
    ll add;
}t[4000005];
ll a[1000005];
inline void build(int x,int l,int r)
{
    t[x].l=l;
    t[x].r=r;
    t[x].add=0;
    t[x].swi=maxn;
    if(l==r)
    {
        t[x].v=a[l];
        return;
    }
    int m=(l+r)/2;
    build(x<<1,l,m);
    build(x<<1|1,m+1,r);
    t[x].v=max(t[x<<1].v,t[x<<1|1].v);
    return;
}
inline void spread(int x)
{
    int m=(t[x].l+t[x].r)/2;
    if(t[x].swi>maxn)
        {
            t[x<<1].v=t[x].swi;
            t[x<<1].add=0;
            t[x<<1].swi=t[x].swi;
            t[x<<1|1].v=t[x].swi;
            t[x<<1|1].add=0;
            t[x<<1|1].swi=t[x].swi;
            t[x].swi=maxn;
        }
    if(t[x].add)
        {
            t[x<<1].v+=t[x].add;
            t[x<<1|1].v+=t[x].add;
            t[x<<1].add+=t[x].add;
            t[x<<1|1].add+=t[x].add;
            t[x].add=0;
        }
    t[x].v=max(t[x<<1].v,t[x<<1|1].v);
}
inline void pluse(int x,int l,int r,int k)
{
    if(t[x].l>=l&&t[x].r<=r)
        {
            t[x].v+=k;
            t[x].add+=k;
            return;
        }
    spread(x);
    int m=(t[x].l+t[x].r)/2;
    if(r>m)pluse(x<<1|1,l,r,k);
    if(l<=m)pluse(x<<1,l,r,k);
    t[x].v=max(t[x<<1].v,t[x<<1|1].v);
    return;
}
inline void change(int x,int l,int r,ll k)
{
    if(t[x].l>=l&&t[x].r<=r)
        {
            t[x].v=k;
            t[x].add=0;
            t[x].swi=k;
            return;
        }
    spread(x);
    int m=(t[x].l+t[x].r)/2;
    if(r>m)change(x<<1|1,l,r,k);
    if(l<=m)change(x<<1,l,r,k);
    t[x].v=max(t[x<<1].v,t[x<<1|1].v);
    return;
}
inline ll ask(int x,int l,int r)
{
    if(t[x].l>=l&&t[x].r<=r)
        return t[x].v;
    spread(x);
    ll ans=maxn;
    int m=(t[x].l+t[x].r)/2;
    if(r>m)
        ans=max(ans,ask(x<<1|1,l,r));
    if(l<=m)
        ans=max(ans,ask(x<<1,l,r));
    return ans;
}
int main()
{
    int n,q,op,l,r,x;
    scanf("%d %d",&n,&q);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    build(1,1,n);
    while(q--)
    {
        scanf("%d",&op);
        if(op==1)
            {
                scanf("%d %d %d",&l,&r,&x);
                change(1,l,r,x);
            }
        else if(op==2)
            {
                scanf("%d %d %d",&l,&r,&x);
                pluse(1,l,r,x);
            }
        else
            {
                scanf("%d %d",&l,&r);
                ll ans=0;
                ans=ask(1,l,r);
                printf("%d\n",ans);
            }
    }
    return 0;
}

by Miss_SGT @ 2023-05-20 16:44:29

头文件被吃了?


by lwyzhx @ 2023-05-20 16:56:15

@2082225053cui long long 的问题,你看你哪里没开 long long


by Miss_SGT @ 2023-05-20 17:06:40

@blue_wine 对,printf里面和v


by platfi @ 2023-05-23 14:56:39

@zhouchenqiao1 第一次求助,不太会格式


by platfi @ 2023-05-23 14:57:11

@blue_wine 已经ac了,谢谢大佬


|