题解:CF2242F Summer Vacation

· · 题解

upd 8.4:修复了出现大量的重复键值的时候,unite 实现不佳导致时间复杂度退化。

思路

设当前钱数为 y,则有函数:

f_{a_i}y=\begin{cases}y+a_i, & y<a_i\\y-a_i, & y\ge a_i\end{cases}

此时我们可以用平衡树来维护。当我们处理到第 i 天时,平衡树里存的是从第 j 天开始到第 i 天结束时的钱数,这里 j\in[1,i]。这时因为要经历第 i 天,所以平衡树中每个数都会变成 f_{a_i}y,同时插入一个值为 0 的节点,代表从第 i 天开始第 i 天结束的钱数,也就是 0。
现在考虑如何使平衡树中每个数变成 f_{a_i}y。因为 f_{a_i}y 按照 a_i 分成两组,小于它的加上 a_i,其他的减去 a_i,所以我们可以将平衡树按 a_i 分裂,左子树加上 a_i,右子树减去 a_i,接着合并。
此时我们会发现一个问题:我们正常的合并长这样:

int merge(int L,int R){
    if(L==0||R==0)return L+R;
    if(t[L].pri>t[R].pri){
        pushdown(L);
        t[L].r=merge(t[L].r,R);
        update(L);
        return L;
    }else{
        pushdown(R);
        t[R].l=merge(L,t[R].l);
        update(R);
        return R;
    }
}

这样的合并只有在左子树严格小于右子树的时候才能合并,然而我们这样处理完之后可能会遇到不是这样的情况,此时这样合并就会得到错误的答案。这是我们就需要写一个通用的合并:

int getmi(int p){
    if(!p)return inf;
    while(t[p].l)pushdown(p),p=t[p].l;
    pushdown(p);
    return t[p].key;
}
int unite(int L,int R){
    int M=0;
    while(L&&R){
        int p,l=getmi(L),r=getmi(R);
        if(l>r)swap(l,r),swap(L,R);
        split(L,r,p,L);
        M=merge(M,p);
    }
    M=merge(M,L);
    M=merge(M,R);
    return M;
}

这样合并就能保证树的结构。这里不加证明的给出单次操作的最坏时间复杂度是 O(n\log n),其中 n=size_L+size_R。
有了这个之后我们就可以用带懒标记的平衡树直接做了。

submission

#include <bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f3f3f3f3f
#define elif else if
#define debug(...) fprintf(stderr,##__VA_ARGS__)
using namespace std;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
class Input{
    #define MX 1000000
    private:
        char buf[MX],*p1=buf,*p2=buf;
        inline char gc(){
            if(p1==p2)p2=(p1=buf)+fread(buf,1,MX,stdin);
            return p1==p2?EOF:*(p1++);
        }
    public:
        template<typename T>
        inline Input&operator>>(T&x){
            x=0;int f=1;char a=gc();
            for(;!isdigit(a);a=gc())if(a=='-')f=-1;
            for(;isdigit(a);a=gc())x=x*10+a-'0';
            x*=f;
            return *this;
        }
        inline Input&operator>>(char&ch){
            while(1){
                ch=gc();
                if(ch!='\n'&&ch!=' ')return *this;
            }
        }
        inline Input&operator>>(char*s){
            int p=0;
            while(1){
                s[p]=gc();
                if(s[p]=='\n'||s[p]==' '||s[p]==EOF)break;
                p++;
            }
            s[p]='\0';
            return *this;
        }
    #undef MX
}Fin;
class Output{
    #define MX 100000
    private:
        char ouf[MX],*p1=ouf,*p2=ouf,Of[105],*o1=Of,*o2=Of;
        void flush(){fwrite(ouf,1,p2-p1,stdout);p2=p1;}
        inline void pc(char ch){
            *(p2++)=ch;
            if(p2==p1+MX)flush();
        }
    public:
        template<typename T>
        inline Output&operator<<(T n){
            if(n<0)pc('-'),n=-n;
            if(n==0)pc('0');
            while(n)*(o1++)=(n%10)^48,n/=10;
            while(o1!=o2)pc(*(--o1));
            return *this;
        }
        inline Output&operator<<(char ch){pc(ch);return *this;}
        inline Output&operator<<(const char *ch){
            const char *p=ch;
            while(*p!='\0')pc(*p++);
            return *this;
        }
        ~Output(){flush();}
    #undef MX
}Fout;
#define cin Fin
#define cout Fout
const int maxn=1e5+5;
int cnt,rt,n,ans[maxn];
struct node{
    int l,r,key,pri,size,lazy;
}t[maxn];
void newnode(int x){
    t[++cnt].size=1;
    t[cnt].l=t[cnt].r=t[cnt].lazy=0;
    t[cnt].key=x;
    t[cnt].pri=rnd();
}
void update(int p){
    t[p].size=t[t[p].l].size+t[t[p].r].size+1;
}
void pushdown(int p){
    if(!p)return;
    if(t[p].lazy){
        if(t[p].l)t[t[p].l].key+=t[p].lazy,t[t[p].l].lazy+=t[p].lazy;
        if(t[p].r)t[t[p].r].key+=t[p].lazy,t[t[p].r].lazy+=t[p].lazy;
        t[p].lazy=0;
    }
}
void split(int p,int x,int &L,int &R){
    if(p==0){
        L=R=0;
        return;
    }
    pushdown(p);
    if(t[p].key<=x){
        L=p;
        split(t[p].r,x,t[p].r,R);
    }else{
        R=p;
        split(t[p].l,x,L,t[p].l);
    }
    update(p);
}
int merge(int L,int R){
    if(L==0||R==0)return L+R;
    if(t[L].pri>t[R].pri){
        pushdown(L);
        t[L].r=merge(t[L].r,R);
        update(L);
        return L;
    }else{
        pushdown(R);
        t[R].l=merge(L,t[R].l);
        update(R);
        return R;
    }
}
int unite(int L,int R){
    if(L==0||R==0)return L+R;
    if(t[L].pri<t[R].pri)swap(L,R);
    int l,r;
    pushdown(L);
    split(R,t[L].key,l,r);
    t[L].l=unite(t[L].l,l);
    t[L].r=unite(t[L].r,r);
    update(L);
    return L;
}
void print(int p){
    if(p==0)return;
    pushdown(p);
    ans[p]=t[p].key;
    print(t[p].l);
    print(t[p].r);
}
void solve(){
    cin>>n;
    for(int i=1,x;i<=n;i++){
        cin>>x;
        newnode(0);
        rt=merge(cnt,rt);
        int L,R;
        split(rt,x-1,L,R);
        if(L)t[L].key+=x,t[L].lazy+=x;
        if(R)t[R].key-=x,t[R].lazy-=x;
        rt=unite(L,R);
    }
    print(rt);
    for(int i=n;i>=1;i--)cout<<ans[i]<<" ";
}
signed main(){
    int Case=1;
//  cin>>Case;
    while(Case--)solve();
}