splay模板

关键部分代码:

void rotate(int x){
    if(x == rt)    return ;
    int p = f[x], y = (x == r(p)), z = (p == r(f[p]));
    c[z][f[p]] = x, f[x] = f[p];
    c[y][p] = c[!y][x], f[c[!y][x]] = p;
    c[!y][x] = p, f[p] = x;
    update(p);    update(x);
    if(rt == p)    rt = x;
}
void splay(int x){
    for(int a[2] = {f[x], x}; f[x] && f[f[x]]; rotate(x), a[0] = f[x])
        rotate(a[(r(f[x]) == x) ^ (r(f[f[x]]) == f[x])]);
    if(f[x])    rotate(x);    
}

 

posted @ 2018-08-24 20:59  junk_yao  阅读(117)  评论(0编辑  收藏  举报