P3466 [POI2008] KLO-Building blocks

· · 题解

题目传送门

0. 前言

这是一道恶心题。

1. 正文

首先,我们把题目分割一下。

1.1 如何处理将一段长度为 k 的区间的所有数变成一个数?

结论:最优的操作方案一定是将所有数变为中位数

证明:假设当前选择的数不是中位数,且大小为 x。设总共有 n 个数,当前选择的数有 n1 个数小于他,和为 sum1,则有 n-n1 个数大于等于他,和为 sum2。

则计算方式为:x \times n1-sum1+sum2-x \times n2

若 n1<n2,则我们可以考虑将选择的数右移一位,则不难观察到上述计算式值一定不增。

若 n1>n2,则我们可以考虑将选择的数左移一位,则不难观察到上述计算式值一定不增。

得出结论,当 abs(n1-n2)\le1 时,操作次数最小。

1.2 如何高效的找到中位数。

我们可以使用 splay。对于 sum 的维护,我们可以考虑在 splay 维护的东西中加一个 sum。

1.3 如何快速找到所有长为 k 的区间

考虑使用一种类似滑动窗口的思想,每次删除窗口最前面的数,加入窗口最后面的数,巧的是,我们发现 splay 也支持这种操作,所以我们就快乐切掉了。

2. 注意事项

请确保你写了足够多的 update,否则可能会没更新到。

请确保你开了足够多的 long long。

附一组数据。

10 5
0
6
2
8
9
3
9
4
2
6
10

样例解释:将最后五个数都变成 4。

3. 代码

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e6;
struct node{
    int siz,cnt;
    long long key,sum;
    node *ch[2],*fa;
}tree[MAXN];
node *root,*NIL,*ncnt;
long long ans,a[MAXN];
void update(node *x){
    if(x==NIL){
        return ;
    }
    x->siz=x->cnt;
    x->sum=1ll*x->cnt*x->key;
    if(x->ch[0]->key!=-1e18&&x->ch[0]->key!=1e18&&x->ch[0]!=NIL){
        x->siz+=x->ch[0]->siz;
        x->sum+=x->ch[0]->sum;
    }
    if(x->ch[1]->key!=1e18&&x->ch[1]->key!=-1e18&&x->ch[1]!=NIL){
        x->siz+=x->ch[1]->siz;
        x->sum+=x->ch[1]->sum;
    }

}
void midprint(node *now){
    if(now==NIL){
        return ;
    }
    midprint(now->ch[0]);
    cout<<now->key<<" "<<now->ch[0]->key<<" "<<now->ch[1]->key<<" "<<now->sum<<" "<<now->ch[0]->sum<<" "<<now->ch[1]->sum<<endl;
    midprint(now->ch[1]);
}
void init(){
    NIL=ncnt=&tree[0];
    NIL->ch[0]=NIL->ch[1]=NIL->fa=NIL;
    NIL->siz=NIL->cnt=0;
    NIL->key=-1e18;
}
node* newnode(long long key,int cnt){
    ++ncnt;
    ncnt->key=key;
    ncnt->sum=1ll*key*cnt;
    ncnt->siz=ncnt->cnt=cnt;
    ncnt->ch[0]=ncnt->ch[1]=ncnt->fa=NIL;
    return ncnt;
}
void rotate(node *x){
    node *y=x->fa;
    int d=(x==y->ch[0]);
    x->fa=y->fa;
    if(y->fa!=NIL){
        y->fa->ch[y==y->fa->ch[1]]=x;
    }
    y->ch[!d]=x->ch[d];
    if(x->ch[d]!=NIL){
        x->ch[d]->fa=y;
    }
    y->fa=x;
    x->ch[d]=y;
    if(root==y){
        root=x;
    }
    update(y);
    update(x);
}
void splay(node *x,node *f){
    node *y,*z;
    if(x==NIL||x==f){
        return ;
    }
    while (x->fa!=f){
        y=x->fa,z=y->fa;
        if(z==f)
            rotate(x);
        else{
            if((x==y->ch[0])^(y==z->ch[0])){
                rotate(x);
            }

            else{
                rotate(y);
            }
            rotate(x);
        }
        update(x);
    }
    update(x);
}
node *select(long long k){
    node *x=root;
    while(x!=NIL){
        int ls=x->ch[0]->siz;
        if(k<=ls){
            x=x->ch[0];
        }
        else if(k<=ls+x->cnt){
            splay(x,NIL);
            return x;
        }
        else{
            k-=ls+x->cnt;
            x=x->ch[1];
        }
    }
}
node *findpre(node *f,long long val){
    node *nw=root,*ret=root;
    while(nw!=NIL){
        int d=nw->key<val;
        if(d==1){
            ret=nw;
        }  
        nw=nw->ch[d];
    }
    splay(ret,f);
    return ret;
}
node *findnxt(node *f,long long val){
    node *nw=root,*ret=root;
    while(nw!=NIL){
        int d=nw->key<=val;
        if(d==0){
            ret=nw;
        }
        nw=nw->ch[d];
    }
    splay(ret,f);
    return ret;
}
void insert(long long val,int cnt){
    node *p=findpre(NIL,val);
    if(p==NIL){
        p=root;
    }
    node *q=findnxt(p,val);
    if(q==NIL){
        q=root;
    }
//  cout<<q->key<<" "<<q->ch[0]->key<<" "<<q->ch[1]->key<<endl;
    if(q->ch[0]!=NIL){
        q->ch[0]->siz+=cnt;
        q->ch[0]->cnt+=cnt;
        q->ch[0]->sum+=val*cnt;
    }
    else{
        node *x=newnode(val,cnt);
        x->fa=q;
        q->ch[0]=x;
    }
    splay(q->ch[0],NIL);
}
int getrank(long long key){
    node *x=root;
    while(x->key!=key){
        int d=x->key<=key;
        if(x->ch[d]==NIL){
            break;
        }
        x=x->ch[d];
    }
    if(x->key<key){
        x=findnxt(NIL,x->key);
    }
    splay(x,NIL);
    return x->ch[0]->siz+1;
}
void delet(long long key){
    node *p=findpre(NIL,key);
    if(p==NIL){
        p=root;
    }
    node *q=findnxt(p,key);
    if (q->ch[0]->siz<=1){
        q->ch[0]=NIL;
    }
    else{
        q->ch[0]->siz--;
        q->ch[0]->cnt--;
    }
    update(q->ch[0]);
    update(q);
    update(p);
    splay(p,NIL);
    return ;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    init();
    int n,m;
    cin>>n>>m;
    node *l=newnode(-1e16,0),*r=newnode(1e16,0);
    root=l,l->ch[1]=r,r->fa=l;
    for(int i=1;i<=m;i++){
        cin>>a[i];
        insert(a[i],1);
    }
    node *ls=select((m+1)/2);
    long long ql=1,sx=ls->key;
    ans=ls->key*ls->ch[0]->siz-ls->ch[0]->sum+ls->ch[1]->sum-ls->key*ls->ch[1]->siz;
    for(int i=m+1;i<=n;i++){
        cin>>a[i];
        insert(a[i],1);
        delet(a[i-m]);
        node *ls=select((m+1)/2);
        splay(ls,NIL);
        long long cal=ls->key*ls->ch[0]->siz-ls->ch[0]->sum+ls->ch[1]->sum-ls->key*ls->ch[1]->siz;
        if(cal<ans){
            ans=cal;
            ql=i-m+1;
            sx=ls->key;
        }
    //    cout<<endl;
      //  midprint(root);
      //  cout<<endl;
      //  cout<<ls->key<<" "<<ls->key*ls->ch[0]->siz-ls->ch[0]->sum<<" "<<ls->ch[1]->sum<<" "<<ls->key*ls->ch[1]->siz<<" "<<ls->ch[1]->siz<<endl;
    }
    cout<<ans<<endl;
    for(int i=1;i<ql;i++){
        cout<<a[i]<<endl;
    }
    for(int i=ql;i<=ql+m-1;i++){
        cout<<sx<<endl;
    }
    for(int i=ql+m;i<=n;i++){
        cout<<a[i]<<endl;
    }
    return 0;//
}

4. 后记

复制的普通平衡树代码,但是少打了个 update,直接挂成 50,调了好久才调出来,所以:不要乱复制代码