splay写挂了
  • 板块题目总版
  • 楼主EurekaStriker
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/11/16 19:58
  • 上次更新2023/10/27 02:45:07
查看原帖
splay写挂了
469470
EurekaStriker楼主2022/11/16 19:58

重写了一遍,又挂了,哪个数据结构之神帮我调一下(别说建议重写,求求了)
题目是文艺平衡树

#include<bits/stdc++.h>
using namespace std;
int n,m;
struct node{
    int s[2],x,y;
    int size,flag;
}tree[100010];
int root,idx=0;
#define lson tree[a].s[0]
#define rson tree[a].s[1]
void pushup(int a){
    tree[a].size=tree[lson].size+tree[rson].size+1;
}
void pushdown(int a){
    if(tree[a].flag)
    {
        swap(lson,rson);
        tree[lson].flag^=1;
        tree[rson].flag^=1;
        tree[a].flag=0;
    }
}
void rotate(int a){
    int y=tree[a].x,z=tree[y].x;
    int k;
    if(tree[y].s[1]==a)
        k=1;
    else
        k=0;
    tree[z].s[tree[z].size==y]=a;
    tree[a].x=z;
    tree[y].s[k]=tree[a].s[k^1];
    tree[tree[a].s[k^1]].x=y;
    tree[a].s[k^1]=y;
    tree[y].x=a;
    pushup(y),pushup(a);
}
void splay(int a,int k)
{
    while(tree[a].x!=k){
        int y=tree[a].x,z=tree[y].x;
        if(z!=k)
        {
            if((tree[y].s[1]==a)^(tree[z].s[1]==y))
                rotate(a);
            else rotate(y);
        }
        rotate(a);
    }
    if(!k) root=a;
}
void insert(int v)
{
    int u=root,p=0;
    while(u) p=u,u=tree[u].s[v>tree[u].x];
    u=++idx;
    if(p) tree[p].s[v>tree[p].x]=u;
    tree[u].size=1;
    tree[u].x=v;
    tree[u].y=p;
    splay(u,0);
}

int got(int k)
{
    int u=root;
    while(1)
    {
        pushdown(u);
        if(tree[tree[u].s[0]].size>=k) u=tree[u].s[0];
        else if(tree[tree[u].s[0]].size+1==k) return u;
        else k-=tree[tree[u].s[0]].size+1,u=tree[u].s[1];
    }
    return -1;
}
void output(int a)
{
    pushdown(a);
    if(lson) output(lson);
    if(tree[a].y>=1&&tree[a].y<=n) 
        cout<<tree[a].y<<' ';
    if(rson) output(rson);
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<=n+1;i++)
        insert(i);
    for(int i=1;i<=m;i++)
    {
        int l,r;
        cin>>l>>r;
        scanf("%d%d",&l,&r);
        l=got(l),r=got(r+2);
        splay(l,0);
        splay(r,l);
        tree[tree[r].s[0]].flag^=1;
    }
    output(root);
    return 0;
}

ps:自学splay真的难写

2022/11/16 19:58
加载中...