题解:AT_abc392_f [ABC392F] Insert

· · 题解

前言

我爱平衡树

题意

应该很好理解。就是起初有一个序列 A 为空。对于第 i 次操作 p_{i},就是把 i 插入到序列中第 p_{i} 个数的前面,求最后顺序输出序列中的数。

思路

乍一看感觉很简单,只有插入操作,我一开始想的是用 vector 的 insert 操作即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int N=3e5+5;
int n;
int read()
{
    int t=1,x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') t=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return t*x;
}
vector<int>g;
signed main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        int k=read()-1;
        g.insert(g.begin()+k,i);
    }
    for(auto i:g)
    {
        printf("%lld ",i);
    }
    return 0;   
} 

但很显然,TLE。

于是赛时我就放弃了。 我们注意到他是在某个地方插入某个数,这不就是平衡树板子吗?于是赛后我就用两种平衡树把他做出来了。至于还不会平衡树的同学,建议先百度一下,我只讲大致思路。

  1. fhq,按排名把前 p_{i}-1 个数和后面的数分裂,再把前一棵树和新建节点合并,最后和后面那棵树合并,答案就是此树的中序遍历。
  2. splay,先求出第 p_{i}-1 个数,把它转到根节点;在求出第 p_{i} 个数,把它转到根的右子树,那么你要插入的东西,是不是就应该是根的右子树的左子树?因为这样你既能保证插入的数排名大于 p_{i}-1 又小于 p_{i}。答案也是此树的中序遍历。最后注意一下要插入哨兵节点以防越界。

    代码

  3. fhq
#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int N=5e5+5;
int n;
int read()
{
    int t=1,x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') t=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return t*x;
}
std::mt19937 ran(114514);//随机函数
int root;
int idx;
struct node{
    int l,r,v,rd;
    int size;
}t[N];
int build(int s)//建立新节点
{
    t[++idx].size=1;
    t[idx].rd=ran();
    t[idx].v=s;
    return idx;
}
void push_up(int u)//更新size
{
    t[u].size=t[t[u].l].size+t[t[u].r].size+1;
}
int x,y;
void split(int u,int k,int &x,int &y)
{
    if(!u)
    {
        x=y=0;;
        return;
     } 
    if(k<=t[t[u].l].size)
    {
        y=u;
        split(t[u].l,k,x,t[u].l);
    }
    else
    {
        x=u;
        split(t[u].r,k-t[t[u].l].size-1,t[u].r,y);
    }
    push_up(u);
}
int merge(int u,int v)
{
    if(!u||!v)return u|v;
    if(t[u].rd<t[v].rd)
    {
        t[u].r=merge(t[u].r,v);
        push_up(u);
        return u;
    }
    else
    {
        t[v].l=merge(u,t[v].l);
        push_up(v);
        return v;
    }
}
void dfs(int u)
{
    if(t[u].l) dfs(t[u].l);
    printf("%lld ",t[u].v);
    if(t[u].r) dfs(t[u].r);
    return;
}
signed main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        int k=read();
        split(root,k-1,x,y);//按p[i]-1分裂
        root=merge(merge(x,build(i)),y);
    }
    dfs(root);
    return 0;   
} 
  1. splay
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e7+5,inf=1e18;
int read()
{
    int t=1,x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') t=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return t*x;
}
int idx,root,n;
struct node{
    int s[2];
    int v;
    int size;
    int fa;
    void build(int _v,int _fa)
    {
        v=_v;
        fa=_fa;
        size=1;
    }
}t[N];
void push_up(int x)
{
    t[x].size=t[t[x].s[1]].size+t[t[x].s[0]].size+1;
}
void rotate(int x)
{
    int y=t[x].fa,z=t[y].fa;
    int k=t[y].s[1]==x;
    t[z].s[t[z].s[1]==y]=x,t[x].fa=z;
    t[y].s[k]=t[x].s[k^1],t[t[x].s[k^1]].fa=y;
    t[x].s[k^1]=y,t[y].fa=x;
    push_up(y),push_up(x);
}
void splay(int x,int k)
{
    while(t[x].fa!=k)
    {
        int y=t[x].fa,z=t[y].fa;
        if(z!=k)
        {
            if((t[y].s[1]==x)^(t[z].s[1]==y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
    if(!k) root=x;
}
int get_k(int k)
{
    int u=root;
    while(true)
    {
        if(k>t[t[u].s[0]].size+1)
        {
            k-=t[t[u].s[0]].size+1;
            u=t[u].s[1];
        }
        else{
            if(t[t[u].s[0]].size>=k) u=t[u].s[0];
            else return u;
        }
    }
}
void dfs(int u)
{
    if(t[u].s[0]) dfs(t[u].s[0]);
    if(abs(t[u].v)!=inf)printf("%lld ",t[u].v);
    if(t[u].s[1]) dfs(t[u].s[1]);
}
signed main()
{
    n=read();
    t[++idx].build(inf,0);//哨兵
    t[++idx].build(-inf,1);
    t[1].s[1]=2;
    push_up(1);
    root=1;
    for(int i=1;i<=n;i++)
    {
        int p=read();
        int l=get_k(p);//因为前面有哨兵,所以原序列第p[i]-1就应该是现在的第p[i]
        int r=get_k(p+1);//同理
//      cout<<l<<"->"<<r<<endl;
        splay(l,0);
        splay(r,l);
        t[++idx].build(i,r);
        t[r].s[0]=idx;
    }
    dfs(root);
    return 0;
}