[BZOJ1861][Zjoi2006]Book 书架 && splay

5 篇文章 0 订阅

加个build 几下每个元素在树中的位置就行了

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<iostream>  
#include<queue>  
#define SF scanf  
#define PF printf
using namespace std;  
typedef long long LL;
const int MAXN = 80000;
int n, m, A[MAXN+10], idx[MAXN+10];
struct Splay_Tree {
    int root, ncnt;
    int ch[MAXN+10][2], val[MAXN+10];
    int sz[MAXN+10], cnt[MAXN+10], fa[MAXN+10];
    void up(int x) {
        sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x];
    }
    void Rotate(int x) {
        int y = fa[x], z = fa[y];
        bool d = x == ch[y][0];
        if(ch[y][!d] = ch[x][d]) fa[ch[x][d]] = y;
        if(fa[x] = z) ch[z][y == ch[z][1]] = x;
        up(ch[x][d] = y);
        fa[y] = x;
    }
    void splay(int x, int goal) {
        for(int y = fa[x]; y != goal; Rotate(x), y = fa[x])
            if(fa[y] != goal) Rotate( (x == ch[y][0]) == (y == ch[fa[y]][0]) ? y : x);
        up(x); if(!goal) root = x;
    }
    void NewNode(int &x, int key, int pre) {
        x = ++ncnt; val[x] = key; fa[x] = pre;
        sz[x] = cnt[x] = 1;
        ch[x][0] = ch[x][1] = 0;
    }
    int Find(int key) {
        int x = root;
        while(x && key != val[x]) x = ch[x][key > val[x]];
        splay(x, root);
        return x;
    }
    void del(int key) {
        int x = root;
        while(x && key != val[x]) x = ch[x][key > val[x]];
        if(!x) return ;
        if(cnt[x] == 1) {
            bool f = !ch[x][0];
            int y = ch[x][f] ? ch[x][f] : x;
            while(ch[y][!f]) y = ch[y][!f];
            bool d = ch[fa[y]][1] == y;
            if(fa[y]) ch[fa[y]][d] = ch[y][f];
            if(ch[y][f]) fa[ch[y][f]] = fa[y];
            val[x] = val[y]; cnt[x] = cnt[y];
            for(cnt[x = y] = 0; x ; x = fa[x]) up(x);
            if(root == y) root = 0;
        }
        else for(cnt[x]--; x; x = fa[x]) sz[x]--;
    }
    int Rank(int key) {
        int x = root, ret = 0;
        while(x && key != val[x])
            if(key < val[x]) x = ch[x][0];
            else ret += sz[ch[x][0]] + cnt[x], x = ch[x][1];
        return ret + sz[ch[x][0]] + 1;
    }
    int Find_kth(int k) {
        if(k < 0 || k > sz[root]) return 0;
        int x = root;
        while(k <= sz[ch[x][0]] || k > sz[ch[x][0]] + cnt[x])
            if(k <= sz[ch[x][0]]) x = ch[x][0];
            else k -= sz[ch[x][0]] + cnt[x], x = ch[x][1];
        //splay(x, 0);  此处若splay(x, 0) 则无法维持Move中x在根的位置
        return x;
    }
    void Build(int &x, int l, int r, int fa) {
        if(l > r) return ;
        int mid = (l + r) >> 1;
        NewNode(x, A[mid], fa);
        idx[A[mid]] = x;
        Build(ch[x][0], l, mid-1, x);
        Build(ch[x][1], mid+1, r, x);
        up(x);
    }
    void Move(int x, int p) {
        // delete
        splay(x, 0);
        int pre = Find_kth(sz[ch[x][0]]), nex = Find_kth(sz[ch[x][0]] + 2);
        splay(pre, 0); splay(nex, root);
        ch[nex][0] = 0; fa[x] = 0;
        splay(nex, 0);
        // insert
        pre = Find_kth(p-1), nex = Find_kth(p);
        splay(pre, 0); splay(nex, root);
        ch[nex][0] = x; fa[x] = nex;
        splay(x, 0);
    }
} sp;
char s[10];
int main() {
    SF("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) SF("%d", &A[i]);
    sp.Build(sp.root, 0, n+1, 0);
    for(int i = 1; i <= m; i++) {
        int x;
        SF("%s%d", s, &x);
        if(s[0] == 'T')
            sp.Move(idx[x], 2);
        if(s[0] == 'B')
            sp.Move(idx[x], n+1);
        if(s[0] == 'I') {
            int T; SF("%d", &T);
            sp.splay(idx[x], 0);
            sp.Move(idx[x], sp.sz[sp.ch[idx[x]][0]] + 1 + T);
        }
        if(s[0] == 'A') {
            sp.splay(idx[x], 0);
            PF("%d\n", sp.sz[sp.ch[idx[x]][0]] - 1);
        }
        if(s[0] == 'Q') {
            int pos = sp.Find_kth(x+1);
            PF("%d\n", sp.val[pos]);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值