Splay 68pts求调。

P6136 【模板】普通平衡树(数据加强版)

_XHY20180718_ @ 2023-07-12 12:04:17

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+9,inf=2e9+9;
int n,m,q,rt,tot;
int ans,last;
int fa[N],son[N][2];
struct tree{
    int w,fa,son[2],cnt,sz;
}tr[N];
inline void pushup(int u)
{tr[u].sz=tr[tr[u].son[0]].sz+tr[tr[u].son[1]].sz+tr[u].cnt;}
inline void rotate(int x)
{
    int y=tr[x].fa,z=tr[y].fa,k;
    bool xy=tr[y].son[1]==x,yz=tr[z].son[1]==y;
    tr[z].son[yz]=x;tr[x].fa=z;
    k=tr[y].son[xy]=tr[x].son[!xy],tr[k].fa=y;
    tr[x].son[!xy]=y;tr[y].fa=x;
    pushup(y),pushup(x);
}
inline void splay(int x,int goal)
{
    while(tr[x].fa!=goal)
    {
        int y=tr[x].fa,z=tr[y].fa;
        if(z!=goal)
            rotate((tr[z].son[0]==y)^(tr[y].son[0]==x)?x:y);
        rotate(x);
    }
    if(!goal)rt=x;
}
inline void find(int x)
{
    int u=rt;
    if(!u)return;
    while(x!=tr[u].w&&tr[u].son[x>tr[u].w])
        u=tr[u].son[x>tr[u].w];
    splay(u,0);
}
inline void ins(int x)
{
    int u=rt,fa=0;
    while(u&&tr[u].w!=x)
        fa=u,u=tr[u].son[x>tr[u].w];
    if(u)tr[u].cnt++;
    else{
        u=++tot;
        if(fa)tr[fa].son[x>tr[fa].w]=u;
        // tr[u].son[0]=tr[u].son[1]=0;
        tr[u].fa=fa,tr[u].w=x;
        tr[u].cnt=tr[u].sz=1;
    }
    splay(u,0);
}
inline int kth(int x)
{
    int u=rt;
    if(tr[u].sz<x)return 0;
    while(1)
    {
        int v=tr[u].son[0];
        if(x>tr[v].sz+tr[u].cnt)
        {
            x-=tr[v].sz+tr[u].cnt;
            u=tr[u].son[1];
        }
        else if(x<=tr[v].sz)u=v;
        else {splay(u,0);return tr[u].w;}
    }
}
inline int lnt(int x,bool fl)
{
    find(x);int u=rt;
    if(tr[u].w<x&&!fl)return u;
    if(tr[u].w>x&&fl)return u;
    u=tr[u].son[fl];
    while(tr[u].son[!fl])
        u=tr[u].son[!fl];
    splay(u,0);
    return u;
}
inline void del(int x)
{
    int lst=lnt(x,0);
    int nxt=lnt(x,1);
    splay(lst,0),splay(nxt,lst);
    int u=tr[nxt].son[0];
    if(tr[u].cnt>1)
        tr[u].cnt--,splay(u,0);
    else tr[nxt].son[0]=0,splay(nxt,0);
}
inline int lst(int x)
{
    int u=lnt(x,0);
    return tr[u].w;
}
inline int nxt(int x)
{
    int u=lnt(x,1);
    return tr[u].w;
}
inline int rnk(int x)
{
    //find(x);int u=tr[rt].son[0];
    //return tr[u].sz+1;
  find(x);
  if(tr[rt].w==x)return tr[tr[rt].son[0]].sz;
  int p=lnt(x,0);find(tr[p].w);
  return tr[tr[rt].son[0]].sz+1;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;int op,x;
    ins(-inf),ins(inf);
    for(int i=1; i<=n; i++)cin>>x,ins(x);
    while(m--)
    {
        cin>>op>>x;x^=last;
        if(op==1)ins(x);
        if(op==2)del(x);
        if(op==3)last=rnk(x),ans^=last;
        if(op==4)last=kth(x+1),ans^=last;
        if(op==5)last=lst(x),ans^=last;
        if(op==6)last=nxt(x),ans^=last;
    }
    cout<<ans<<'\n';
    return 0;
}

by _XHY20180718_ @ 2023-07-12 13:30:43

https://www.luogu.com.cn/record/115045309


by _XHY20180718_ @ 2023-07-12 14:01:08

inline int rnk(int x)
{
    //find(x);int u=tr[rt].son[0];
    //return tr[u].sz+1;
  find(x);
  if(tr[rt].w==x)return tr[tr[rt].son[0]].sz;
  int p=lnt(x,0);find(tr[p].w);
  return tr[tr[rt].son[0]].sz+1;
}

应改成:

inline int rnk(int x)
{
    //find(x);int u=tr[rt].son[0];
    //return tr[u].sz+1;
  find(x);
  if(tr[rt].w==x)return tr[tr[rt].son[0]].sz;
  int p=lnt(x,0);find(tr[p].w);
  return tr[tr[rt].son[0]].sz+tr[rt].cnt;
}

by _XHY20180718_ @ 2023-07-12 14:01:32

此贴结。


|