[P6780] [Ynoi2009] pmrllcsrms 题解

· · 题解

直接做 r'-l'+1\le c 的限制难以解决,考虑按 c 分块。分为两种情况,块内以及块间。

考虑块内,由于块长为 c,直接线段树维护区间最大子段和即可。

考虑块间,发现可以拆成左块的后缀最大子段和加上右块的前缀最大子段和,对于每个块间建一棵类值域线段树,维护左块长度为 c\sim 1 的后缀及右块长度为 1\sim c 的前缀。但是这样查询最大值的时间复杂度是 \mathcal O(c) 的,考虑前缀 [l,c] 和后缀 [1,r] 的下标关系,由 r-l+1\le c 得到 r-c<l,发现恰好与维护的前后缀 [1,r]+[l,c] 契合,且满足单调关系,可以塞到线段树上维护。

由于需要跨块查询,还需要维护两棵线段树,维护前两棵线段树的答案。

修改比较简单,第一棵线段树单点修改,第二棵线段树区间修改前后缀,第三四棵线段树单点修改答案。

查询细节比较多:

时间复杂度 \mathcal O(m\log n)。

:::info[Code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000100;
const ll Inf=2e15;
int n,c,B;
ll a[N],pre[N],suf[N];
int b[N];
struct Node1{
    ll sum,pre,suf,cur;
    Node1(){sum=0;pre=suf=cur=-Inf;}
};
struct Seg1{
    Node1 tree[N<<2];
    inline int ls(int p){return p*2;}
    inline int rs(int p){return p*2+1;}
    inline Node1 merge(Node1 lt,Node1 rt){
        Node1 res;
        res.sum=lt.sum+rt.sum;
        res.cur=max({
            lt.suf+rt.pre,
            lt.cur,rt.cur
        });
        res.pre=max(lt.pre,lt.sum+rt.pre);
        res.suf=max(rt.suf,rt.sum+lt.suf);
        return res;
    }
    inline void build(int p,int l,int r){
        if (l==r){
            tree[p].sum=tree[p].pre=tree[p].suf=tree[p].cur=a[l];
            return;
        }
        int mid=(l+r)/2;
        build(ls(p),l,mid);
        build(rs(p),mid+1,r);
        tree[p]=merge(tree[ls(p)],tree[rs(p)]);
        return;
    }
    inline void chg(int p,int l,int r,int k,ll x){
        if (l==r){
            tree[p].sum=tree[p].pre=tree[p].suf=tree[p].cur=x;
            return;
        }
        int mid=(l+r)/2;
        if (k<=mid) chg(ls(p),l,mid,k,x);
        if (k>mid)  chg(rs(p),mid+1,r,k,x);
        tree[p]=merge(tree[ls(p)],tree[rs(p)]);
        return;
    }
    inline Node1 qry(int p,int l,int r,int le,int ri){
        if (le<=l and ri>=r) return tree[p];
        int mid=(l+r)/2;
        if (le<=mid and ri>mid){
            Node1 lt,rt;
            lt=qry(ls(p),l,mid,le,ri);
            rt=qry(rs(p),mid+1,r,le,ri);
            return merge(lt,rt);
        }
        if (le<=mid) return qry(ls(p),l,mid,le,ri);
        if (ri>mid)  return qry(rs(p),mid+1,r,le,ri);
        return Node1{};
    }
}tree1;
struct Node2{
    int l,r;
    ll ans,pre,suf;
    ll tgl,tgr;
    Node2(){l=r=0;ans=pre=suf=-Inf;tgl=tgr=0;}
};
struct Seg2{
    // 类似于值域线段树,叶子节点 k 维护 [k,r] [l,k+c-1] 的值
    Node2 tree[N<<2];
    int tot=0;
    int rt[N];
    inline Node2 merge(Node2 lt,Node2 rt){
        Node2 res;
        res.pre=max(lt.pre,rt.pre);
        res.suf=max(lt.suf,rt.suf);
        res.ans=max({
            lt.ans,rt.ans,
            lt.pre+rt.suf
        });
        return res;
    }
    inline void pushdown(int p){
        if (tree[p].tgl){
            tree[tree[p].l].tgl+=tree[p].tgl;
            tree[tree[p].l].suf+=tree[p].tgl;
            tree[tree[p].l].ans+=tree[p].tgl;
            tree[tree[p].r].tgl+=tree[p].tgl;
            tree[tree[p].r].suf+=tree[p].tgl;
            tree[tree[p].r].ans+=tree[p].tgl;
            tree[p].tgl=0;
        }
        if (tree[p].tgr){
            tree[tree[p].l].tgr+=tree[p].tgr;
            tree[tree[p].l].pre+=tree[p].tgr;
            tree[tree[p].l].ans+=tree[p].tgr;
            tree[tree[p].r].tgr+=tree[p].tgr;
            tree[tree[p].r].pre+=tree[p].tgr;
            tree[tree[p].r].ans+=tree[p].tgr;
            tree[p].tgr=0;
        }
        return;
    }
    inline void build(int& p,int l,int r,int k){
        if (!p) p=++tot;
        if (l==r){
            if (k*c+l>n) tree[p].pre=-Inf;
            else tree[p].pre=pre[(k)*c+l]-pre[(k)*c];
            tree[p].suf=suf[(k-1)*c+l]-suf[min(k*c+1,n+1)];
            tree[p].ans=-Inf;
            return;
        }
        int mid=(l+r)/2;
        build(tree[p].l,l,mid,k);
        build(tree[p].r,mid+1,r,k);
        int ls=tree[p].l,rs=tree[p].r;
        tree[p]=merge(tree[tree[p].l],tree[tree[p].r]);
        tree[p].l=ls,tree[p].r=rs;
        return;
    }
    inline Node2 qry(int p,int l,int r,int le,int ri){
        if (le>ri) return Node2{};
        if (le<=l and ri>=r) return tree[p];
        int mid=(l+r)/2;
        pushdown(p);
        if (le<=mid and ri>mid){
            Node2 lt,rt;
            lt=qry(tree[p].l,l,mid,le,ri);
            rt=qry(tree[p].r,mid+1,r,le,ri);
            return merge(lt,rt);
        }
        if (le<=mid) return qry(tree[p].l,l,mid,le,ri);
        if (ri>mid)  return qry(tree[p].r,mid+1,r,le,ri);
        return Node2{};
    }
    inline void chg(int p,int l,int r,int le,int ri,bool op,ll x){
        if (le<=l and ri>=r){
            if (op==0){
                tree[p].tgl+=x;
                tree[p].suf+=x;
            }
            if (op==1){
                tree[p].tgr+=x;
                tree[p].pre+=x;
            }
            tree[p].ans+=x;
            return;
        }
        int mid=(l+r)/2;
        pushdown(p);
        if (le<=mid) chg(tree[p].l,l,mid,le,ri,op,x);
        if (ri>mid)  chg(tree[p].r,mid+1,r,le,ri,op,x);
        int ls=tree[p].l,rs=tree[p].r;
        tree[p]=merge(tree[tree[p].l],tree[tree[p].r]);
        tree[p].l=ls,tree[p].r=rs;
        return;
    }
}tree2;
struct Node3{
    ll x;
};
struct Seg3{
    Node3 tree[N<<2];
    int ls(int p){return p*2;}
    int rs(int p){return p*2+1;}
    inline void pushup(int p){
        tree[p].x=max(tree[ls(p)].x,tree[rs(p)].x);
        return;
    }
    inline void build(int p,int l,int r){
        if (l==r){
            tree[p].x=tree1.qry(1,1,n,(l-1)*c+1,min(l*c,n)).cur;
            return;
        }
        int mid=(l+r)/2;
        build(ls(p),l,mid);
        build(rs(p),mid+1,r);
        pushup(p);
        return;
    }
    inline ll qry(int p,int l,int r,int le,int ri){
        if (le<=l and ri>=r) return tree[p].x;
        int mid=(l+r)/2;
        ll res=-Inf;
        if (le<=mid) res=max(res,qry(ls(p),l,mid,le,ri));
        if (ri>mid)  res=max(res,qry(rs(p),mid+1,r,le,ri));
        return res;
    }
    inline void chg(int p,int l,int r,int k,ll x){
        if (l==r){
            tree[p].x=x;
            return;
        }
        int mid=(l+r)/2;
        if (k<=mid) chg(ls(p),l,mid,k,x);
        if (k>mid)  chg(rs(p),mid+1,r,k,x);
        pushup(p);
        return;
    }
}tree3;
struct Node4{
    ll x;
};
struct Seg4{
    Node4 tree[N<<2];
    inline int ls(int p){return p*2;}
    inline int rs(int p){return p*2+1;}
    inline void pushup(int p){
        tree[p].x=max(tree[ls(p)].x,tree[rs(p)].x);
        return;
    }
    inline void build(int p,int l,int r){
        if (l==r){
            tree[p].x=tree2.qry(tree2.rt[l],1,c,1,c).ans;
            return;
        }
        int mid=(l+r)/2;
        build(ls(p),l,mid);
        build(rs(p),mid+1,r);
        pushup(p);
    }
    inline ll qry(int p,int l,int r,int le,int ri){
        if (le<=l and ri>=r) return tree[p].x;
        int mid=(l+r)/2;
        ll res=-Inf;
        if (le<=mid) res=max(res,qry(ls(p),l,mid,le,ri));
        if (ri>mid)  res=max(res,qry(rs(p),mid+1,r,le,ri));
        return res;
    }
    inline void chg(int p,int l,int r,int k,ll x){
        if (l==r){
            tree[p].x=x;
            return;
        }
        int mid=(l+r)/2;
        if (k<=mid) chg(ls(p),l,mid,k,x);
        if (k>mid)  chg(rs(p),mid+1,r,k,x);
        pushup(p);
        return;
    }
}tree4;
inline int kth(int k){
    return (k-1)%c+1;
}
inline ll rd(){
    ll x=0;
    int b=1;
    char c=getchar();
    while ((c<'0' or c>'9') and c!='-') c=getchar();
    if (c=='-') b=-1,c=getchar();
    while (c>='0' and c<='9') x=x*10+(c-'0'),c=getchar();
    return x*b;
}
int main(){
    int m;
    scanf("%d%d%d",&n,&m,&c);
    for (int i=1;i<=n;i++) a[i]=rd();
    for (int i=1;i<=n;i++) b[i]=(i-1)/c+1;
    B=(n+c-1)/c;
    fill(pre+1,pre+n+1,-Inf);
    fill(suf+1,suf+n+1,-Inf);
    pre[0]=suf[n+1]=0;
    for (int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
    for (int i=n;i>=1;i--) suf[i]=suf[i+1]+a[i];
    tree1.build(1,1,n);
    for (int i=1;i<B;i++) tree2.build(tree2.rt[i],1,c,i);
    tree3.build(1,1,B);
    tree4.build(1,1,B-1);
    while (m--){
        int op=rd();
        if (op==1){
            int k=rd();
            ll x=rd();
            tree1.chg(1,1,n,k,x);
            tree3.chg(1,1,B,b[k],tree1.qry(1,1,n,(b[k]-1)*c+1,b[k]*c).cur);
            if (b[k]>1){
                tree2.chg(tree2.rt[b[k]-1],1,c,kth(k),c,1,x-a[k]);
                tree4.chg(1,1,B-1,b[k]-1,tree2.qry(tree2.rt[b[k]-1],1,c,1,c).ans);
            }
            if (b[k]<B){
                tree2.chg(tree2.rt[b[k]],1,c,1,kth(k),0,x-a[k]);
                tree4.chg(1,1,B-1,b[k],tree2.qry(tree2.rt[b[k]],1,c,1,c).ans);
            }
            a[k]=x;
        }
        if (op==2){
            int l=rd(),r=rd();
            if (r-l+1<=c){
                ll res=0;
                res=max(res,tree1.qry(1,1,n,l,r).cur);
                printf("%lld\n",res);
            }
            else if (b[l]+1==b[r]){
                ll res=0;
                res=max(res,tree1.qry(1,1,n,l,l+c-1).cur);
                res=max(res,tree1.qry(1,1,n,r-c+1,r).cur);
                res=max(res,
                    tree2.qry(tree2.rt[b[l]],1,c,1,kth(l)-1).pre+
                    tree2.qry(tree2.rt[b[l]],1,c,kth(l),c).suf
                );
                res=max(res,
                    tree2.qry(tree2.rt[b[l]],1,c,1,kth(r)).pre+
                    tree2.qry(tree2.rt[b[l]],1,c,kth(r)+1,c).suf
                );
                res=max(res,tree2.qry(tree2.rt[b[l]],1,c,kth(l),kth(r)).ans);
                printf("%lld\n",res);
            }
            else {
                ll res=0;
                res=max(res,tree1.qry(1,1,n,l,l+c-1).cur);
                res=max(res,tree1.qry(1,1,n,r-c+1,r).cur);
                res=max(res,
                    tree2.qry(tree2.rt[b[l]],1,c,kth(l),c).suf+
                    tree2.qry(tree2.rt[b[l]],1,c,1,kth(l)-1).pre
                );
                res=max(res,tree2.qry(tree2.rt[b[l]],1,c,kth(l),c).ans);
                res=max(res,
                    tree2.qry(tree2.rt[b[r]-1],1,c,1,kth(r)).pre+
                    tree2.qry(tree2.rt[b[r]-1],1,c,kth(r)+1,c).suf
                );
                res=max(res,tree2.qry(tree2.rt[b[r]-1],1,c,1,kth(r)).ans);
                res=max(res,tree3.qry(1,1,B,b[l]+1,b[r]-1));
                if (b[l]+1<=b[r]-2) res=max(res,tree4.qry(1,1,B-1,b[l]+1,b[r]-2));
                printf("%lld\n",res);
            }
        }
    }
    return 0;
}
// 0 5 -3 8 -3

:::