求助Splay

P3391 【模板】文艺平衡树

AAA404 @ 2023-08-19 16:03:25

真滴难受,调三小时了,样例只输出5

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,cnt,l,r,root,a[N];
struct node{
    int ch[2],fa,size,tag,val;
}spl[N];
#define ls(x) spl[x].ch[0]
#define rs(x) spl[x].ch[1]
inline int ident(int x,int f){return rs(x)==x;}
inline void update(int now)
{
    spl[now].size=spl[ls(now)].size+spl[rs(now)].size+1;
    return;
}
inline void pushdown(int now)
{
    if(!spl[now].tag||!now)return;
    spl[ls(now)].tag^=1;
    spl[rs(now)].tag^=1;
    swap(ls(now),rs(now));
    spl[now].tag=0;
    return;
}
inline void connect(int x,int f,int s)
{
    spl[f].ch[s]=x;
    spl[x].fa=f;
    return;
}
inline void newnode(int &now,int fa,int val)
{
    spl[now=++cnt].val=val;
    spl[cnt].fa=fa;
    spl[cnt].size=1;
    return;
}
inline void rotate(int x)
{
    int f=spl[x].fa,ff=spl[f].fa,k=ident(x,f);
    pushdown(f),pushdown(x);
    connect(spl[x].ch[k^1],f,k);
    connect(x,ff,ident(f,ff));
    connect(f,x,k^1);
    update(f),update(x);
    return;
}
inline void splaying(int x,int top)
{
    if(!top)root=x;
    while(spl[x].fa!=top)
    {
        int f=spl[x].fa,ff=spl[f].fa;
        if(ff!=top)ident(f,ff)^ident(x,f)?rotate(x):rotate(f);
        rotate(x);
    }
    return;
}
inline int  build(int l,int r,int fa=0)
{
    if(l>r)return 0;
    int mid=l+r>>1;
    int now=++cnt;
    spl[now]={{0,0},fa,1,0,a[mid]};
    ls(now)=build(l,mid-1,now);
    rs(now)=build(mid+1,r,now);
    update(now);
    return now;
}
inline int getnum(int rank)
{
    int x=root,y=rank;
    while(x)
    {
        pushdown(x);
        if(y<=spl[ls(x)].size)x=ls(x);
        else
        {
            y-=spl[ls(x)].size+1;
            if(!y)return x;
            x=rs(x);
        }
    }
}
inline void reverse(int l,int r)
{
    l=getnum(l-1),r=getnum(r+1);
    splaying(l,0);
    splaying(r,l);
    spl[ls(rs(root))].tag^=1;
    return;
}
inline void ldr(int now)
{
    pushdown(now);
    if(ls(now))ldr(ls(now));
    if(spl[now].val!=-0x3f3f3f3f&&spl[now].val!=0x3f3f3f3f)cout<<spl[now].val<<" ";
    if(rs(now))ldr(rs(now));
    return;
}
int main()
{
    clock_t c1=clock();
#ifdef LOCAL
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    a[1]=-0x3f3f3f3f,a[n+2]=0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        a[i+1]=i;
    }
    root=build(1,n+2);
    while(m--)
    {
        cin>>l>>r;
        int tx=getnum(l),ty=getnum(r+2);
        splaying(tx,0);
        splaying(ty,tx);
        spl[ls(rs(root))].tag^=1;
    }
    ldr(root);
#ifdef LOCAL
    cerr<<"Time used:"<<clock()-c1<<"ms";
#endif
    return 0;
}

by AAA404 @ 2023-08-19 21:51:13

代码重发一下

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,cnt,l,r,root,a[N];
struct node{
    int ch[2],fa,size,tag,val;
}spl[N];
#define ls(x) spl[x].ch[0]
#define rs(x) spl[x].ch[1]
inline int ident(int x,int f){return rs(x)==x;}
inline void update(int now)
{
    spl[now].size=spl[ls(now)].size+spl[rs(now)].size+1;
    return;
}
inline void pushdown(int x)
{
    if(x&&spl[x].tag)
    {
        swap(ls(x),rs(x));
        spl[ls(x)].tag^=1;
        spl[rs(x)].tag^=1;
        spl[x].tag=0;
    }
    return;
}
inline void connect(int x,int f,int s)
{
    spl[f].ch[s]=x;
    spl[x].fa=f;
    return;
}
inline void rotate(int x)
{
    int f=spl[x].fa,ff=spl[f].fa,k=ident(x,f);
    connect(spl[x].ch[k^1],f,k);
    connect(x,ff,ident(f,ff));
    connect(f,x,k^1);
    update(f),update(x);
    return;
}
inline void splaying(int x,int top)
{
    if(!top)root=x;
    while(spl[x].fa!=top)
    {
        int f=spl[x].fa,ff=spl[f].fa;
        if(ff!=top)ident(f,ff)^ident(x,f)?rotate(x):rotate(f);
        rotate(x);
    }
    return;
}
inline int  build(int l,int r,int fa=0)
{
    if(l>r)return 0;
    int mid=l+r>>1;
    int now=++cnt;
    spl[now]={{0,0},fa,1,0,a[mid]};
    ls(now)=build(l,mid-1,now);
    rs(now)=build(mid+1,r,now);
    update(now);
    return now;
}
inline int getnum(int rank)
{
    int x=root,y=rank;
    while(x)
    {
        pushdown(x);
        if(y<=spl[ls(x)].size)x=ls(x);
        else
        {
            y-=spl[ls(x)].size+1;
            if(!y)break;
            x=rs(x);
        }
    }
    return x;
}
inline void ldr(int now)
{
    pushdown(now);
    if(ls(now))ldr(ls(now));
    if(spl[now].val!=-0x3f3f3f3f&&spl[now].val!=0x3f3f3f3f)cout<<spl[now].val<<" ";
    if(rs(now))ldr(rs(now));
    return;
}
int main()
{
    clock_t c1=clock();
#ifdef LOCAL
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    a[1]=-0x3f3f3f3f,a[n+2]=0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        a[i+1]=i;
    }
    root=build(1,n+2);
    while(m--)
    {
        cin>>l>>r;
        int tx=getnum(l);
        int ty=getnum(r+2);
        splaying(tx,0);
        splaying(ty,tx);
        spl[ls(rs(root))].tag^=1;
    }
    ldr(root);
#ifdef LOCAL
    cerr<<"Time used:"<<clock()-c1<<"ms";
#endif
    return 0;
}

by AAA404 @ 2023-08-19 22:27:28

此贴终,死于ident函数


|