其他分享
首页 > 其他分享> > 题解P2596 [ZJOI2006]书架

题解P2596 [ZJOI2006]书架

作者:互联网

P2596 [ZJOI2006]书架
一道简单的数据结构题
对于每一个操作
Top:将s转到根节点,并将s的左子树移给s的后继
Bottom:将s转到根节点,并将s的右子树移给s的前驱
Insert:与前驱/后继交换信息
Ask:询问s的前驱的排名
Query:询问排名为s的编号
用Splay实现

代码

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e5 + 5;

struct tr
{
    int s[2], id, p, siz;
    void init(int _id, int _p)
    {
        id = _id, p = _p, siz = 1;
    }
} tree[N];
int root, idx, id[N], poi[N];

void pushup(int p)
{
    tree[p].siz = tree[tree[p].s[0]].siz + tree[tree[p].s[1]].siz + 1;
}

void rotate(int x)
{
    int y = tree[x].p, z = tree[y].p;
    int k = tree[y].s[1] == x;
    tree[z].s[tree[z].s[1] == y] = x, tree[x].p = z;
    tree[y].s[k] = tree[x].s[k ^ 1], tree[tree[x].s[k ^ 1]].p = y;
    tree[x].s[k ^ 1] = y, tree[y].p = x;
    pushup(y), pushup(x);
}

void splay(int x, int k)
{
    while (tree[x].p != k)
    {
        int y = tree[x].p, z = tree[y].p;
        if (z != k)
        {
            if ((tree[y].s[1] == x) ^ (tree[z].s[1] == y))
                rotate(x);
            else
                rotate(y);
        }
        rotate(x);
    }
    if (!k)
        root = x;
}

int build(int l, int r, int p)
{
    int mid = (l + r) >> 1, u = ++idx;
    poi[id[mid]] = idx;
    tree[u].init(id[mid], p);
    if (l < mid)
        tree[u].s[0] = build(l, mid - 1, u);
    if (r > mid)
        tree[u].s[1] = build(mid + 1, r, u);
    pushup(u);
    return u;
}

int get_k(int k)
{
    int u = root;
    while (u)
    {
        if (tree[tree[u].s[0]].siz >= k)
            u = tree[u].s[0];
        else if (tree[tree[u].s[0]].siz + 1 == k)
            return u;
        else
            k -= tree[tree[u].s[0]].siz + 1, u = tree[u].s[1];
    }
    return 0;
}

void Top(int s)
{
    int u = poi[s];
    splay(u, 0);
    int v = get_k(tree[tree[u].s[0]].siz + 2);
    if (v)
    {
        splay(v, u);
        tree[tree[u].s[0]].p = v, tree[v].s[0] = tree[u].s[0];
        tree[u].s[0] = 0;     
        pushup(v);   
    }
    else
        swap(tree[u].s[0], tree[u].s[1]);
    
}

void Bottom(int s)
{
    int u = poi[s];
    splay(u, 0);
    int v = get_k(tree[tree[u].s[0]].siz);
    if (v)
    {
        splay(v, u);
        tree[tree[u].s[1]].p = v, tree[v].s[1] = tree[u].s[1];
        tree[u].s[1] = 0;    
        pushup(v);    
    }
    else
        swap(tree[u].s[0], tree[u].s[1]);
    
}

void Insert(int s)
{
    int t;
    scanf("%d", &t);
    int u = poi[s];
    splay(u, 0);
    int v = get_k(tree[tree[u].s[0]].siz + t + 1);
    swap(tree[u].id, tree[v].id), swap(poi[tree[u].id], poi[tree[v].id]);
}

int Ask(int s)
{
    int u = poi[s];
    splay(u, 0);
    return tree[tree[u].s[0]].siz;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &id[i]);
    root = build(1, n, 0);
    while (m--)
    {
        char op[10];
        int s;
        scanf("%s%d", &op, &s);
        if (op[0] == 'T')
            Top(s);
        else if (op[0] == 'B')
            Bottom(s);
        else if (op[0] == 'I')
            Insert(s);
        else if (op[0] == 'A')
            printf("%d\n", Ask(s));
        else
            printf("%d\n", tree[get_k(s)].id);
    }
}

标签:int,题解,poi,tree,mid,P2596,siz,ZJOI2006,id
来源: https://www.cnblogs.com/A2484337545/p/14360172.html