Luogu P3165 [CQOI2014]排序机械臂

先讲一下和这题一起四倍经验的题:

这题作为一道十分经典的平衡树维护序列的问题,自然是值得一做的了。

写完翻了下题解发现都是写Splay的dalao,少有的暴力FHQ_Treap党还是用指针实现的。

所以这里略微讲解下数组实现的FHQ_Treap好了,感觉写起来比Splay舒服些。

首先我们要抽象化一下题意:给你\(n\)个数,第\(i\)次操作在\([i,n]\)中找到最小值的位置\(p_i\),并翻转\([i,p_i]\)。最后输出所有\(p_i\)的值。

然后我们考虑转化问题(因为貌似FHQ_Treap不能同时支持基于权值split基于排名的分裂)。

所以离散化是必须的,尤其注意这里不能直接对数组排序(因为会有权值相等的点)。

然后我们记一下每个值原来的位置,再考虑对一个基本序列(即初始时为\(1,2,3,\dots,n\))进行翻转。

手动推导一下我们发现其实就是先找出每次操作位置的排名,然后再基本序列上不停翻转区间即可。

由于FHQ_Treap树高期望\(\log\)的特性,所以我们查询排名的时候可以直接暴力从一个点跳到根然后反着算回来。

最后提一下那种以权值为保证堆性质的值的做法是错误的!这样会导致树高不平衡,一旦遇到单调的数据就卡到\(O(n^2)\)了。

CODE

#include<cstdio>
#include<cctype>
#include<algorithm>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int N=100005;
struct data
{
    int val,id;
    inline friend bool operator <(const data& A,const data& B)
    {
        return A.val<B.val||(A.val==B.val&&A.id<B.id);
    }
}a[N]; int n,rk;
class FileInputOutput
{
    private:
        static const int S=1<<21;
        #define tc() (A==B&&(B=(A=Fin)+fread(Fin,1,S,stdin),A==B)?EOF:*A++)
        #define pc(ch) (Ftop<S?Fout[Ftop++]=ch:(fwrite(Fout,1,S,stdout),Fout[(Ftop=0)++]=ch))
        char Fin[S],Fout[S],*A,*B; int Ftop,pt[15];
    public:
        Tp inline void read(T& x)
        {
            x=0; char ch; while (!isdigit(ch=tc()));
            while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
        }
        Tp inline void write(T x)
        {
            if (!x) return (void)(pc('0'),pc(' ')); RI ptop=0;
            while (x) pt[++ptop]=x%10,x/=10; while (ptop) pc(pt[ptop--]+48); pc(' ');
        }
        inline void Fend(void)
        {
            fwrite(Fout,1,Ftop,stdout);
        }
        #undef tc
        #undef pc
}F;
class FHQ_Treap
{
    private:
        struct treap
        {
            int ch[2],size,dat,fa; bool rev;
            inline treap(CI Dat=0,CI Size=0)
            {
                ch[0]=ch[1]=rev=fa=0; dat=Dat; size=Size;
            }
        }node[N]; int tot,rt,seed,stack[N],top;
        #define lc(x) node[x].ch[0]
        #define rc(x) node[x].ch[1]
        #define fa(x) node[x].fa
        inline int rand(void)
        {
            return seed=(int)seed*482711LL%2147483647;
        }
        inline void swap(int& x,int& y)
        {
            int t=x; x=y; y=t;
        }
        inline void rever(CI x)
        {
            swap(lc(x),rc(x)); node[x].rev^=1;
        }
        inline void pushup(CI x)
        {
            node[x].size=node[lc(x)].size+node[rc(x)].size+1; fa(lc(x))=fa(rc(x))=x;
        }
        inline void pushdown(CI x)
        {
            if (node[x].rev) rever(lc(x)),rever(rc(x)),node[x].rev=0;
        }
        inline void merge(int& now,int x,int y)
        {
            if (!x||!y) return (void)(now=x|y); if (node[x].dat>node[y].dat)
            pushdown(x),now=x,merge(rc(now),rc(x),y),pushup(x); else
            pushdown(y),now=y,merge(lc(now),x,lc(y)),pushup(y);
        }
        inline void split(int now,int& x,int& y,int rk)
        {
            if (!now) return (void)(x=y=0); pushdown(now); if (node[lc(now)].size<rk)
            x=now,split(rc(now),rc(x),y,rk-node[lc(now)].size-1); else
            y=now,split(lc(now),x,lc(y),rk); pushup(now);
        }
    public:
        FHQ_Treap() { seed=233; }
        inline void insert(CI val)
        {
            node[++tot]=treap(rand(),1); merge(rt,rt,tot);
        }
        inline void reverse(RI l,RI r)
        {
            int x,y,z; split(rt,x,y,l-1); split(y,y,z,r-l+1);
            rever(y); merge(y,y,z); merge(rt,x,y);
        }
        inline int get_rk(int now)
        {
            stack[top=1]=now; for (int t=now;fa(t);t=fa(t))	stack[++top]=fa(t);
            while (top) pushdown(stack[top--]); int ret=node[lc(now)].size;
            for (;now;now=fa(now)) if (now==rc(fa(now))) ret+=node[lc(fa(now))].size+1;
            return ret+1;
        }
        #undef lc
        #undef rc
}T;
int main()
{
    //freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
    RI i; for (F.read(n),i=1;i<=n;++i) F.read(a[i].val),a[i].id=i;
    for (sort(a+1,a+n+1),i=1;i<=n;++i) T.insert(i); for (i=1;i<=n;++i)
    rk=T.get_rk(a[i].id),F.write(rk),T.reverse(i,rk); return F.Fend(),0;
}
posted @ 2019-01-14 17:33  空気力学の詩  阅读(168)  评论(0编辑  收藏  举报