首页 > 其他分享 >洛谷P2596 [ZJOI2006] 书架 题解 splay tree 模板题

洛谷P2596 [ZJOI2006] 书架 题解 splay tree 模板题

时间:2024-10-22 16:32:40浏览次数:1  
标签:sz 洛谷 int 题解 void tree tr splay push

题目链接:https://www.luogu.com.cn/problem/P2596

主要涉及的操作就是:

  • 找到某一个编号的点(这个操作可以不用 splay tree 维护)
  • 删除某个点
  • 将某一个点插入到最前面,最后面,或者某一个位置
  • 查询前序遍历为 \(k\) 的节点编号

因为每次删除都会又把这个点加回去,所以可以复用 \(n\) 个点。

好久没写 splay tree 了代码 略()丑

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 8e4 + 5;

struct Node {
    int s[2], p, sz, id;
    Node() {}
    Node(int _p) { s[0] = s[1] = 0; p = _p; sz = 1; }

    void _init(int _p) {
        s[0] = s[1] = 0;
        p = _p;
        sz = 1;
    }

} tr[maxn];
int root, idx;

void push_up(int x) {
    auto &u = tr[x], &l = tr[u.s[0]], &r = tr[u.s[1]];
    u.sz = l.sz + 1 + r.sz;
}

void f_s(int p, int u, int k) {
    tr[p].s[k] = u;
    tr[u].p = p;
}

void rot(int x) {
    int y = tr[x].p, z = tr[y].p;
    int k = tr[y].s[1] == x;
    f_s(z, x, tr[z].s[1] == y);
    f_s(y, tr[x].s[k^1], k);
    f_s(x, y, k^1);
    push_up(y), push_up(x);
}

void splay(int x, int k) {
    while (tr[x].p != k) {
        int y = tr[x].p, z = tr[y].p;
        if (z != k)
            (tr[y].s[1]==x)^(tr[z].s[1]==y) ? rot(x) : rot(y);
        rot(x);
    }
    if (!k) root = x;
}

int n, m, P[maxn], id[maxn]; // id[s] 表示编号为 s 的书对应的节点编号

int build(int l, int r, int p) {
    if (l > r) return 0;
    int mid = (l + r) / 2, x = mid;
    tr[x] = Node(p);
    tr[x].id = P[mid];
    tr[x].s[0] = build(l, mid-1, x);
    tr[x].s[1] = build(mid+1, r, x);
    push_up(x);
    if (l == 1 && r == n) root = x;
    return x;
}

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

void del(int x) {
    int y, z;
    splay(x, 0);
    y = tr[x].s[0];
    z = tr[x].s[1];
    if (!y || !z) {
        root = y + z;
        tr[y + z].p = 0;
        return;
    }
    while (tr[y].s[1]) y = tr[y].s[1];
    while (tr[z].s[0]) z = tr[z].s[0];
    splay(y, 0);
    splay(z, y);
    tr[z].s[0] = 0;
    f_s(y, z, 1);
    push_up(z), push_up(y);
}

void ins_tb(int p, int k, int x) {
    if (!tr[p].s[k]) {
        tr[p].s[k] = x;
        tr[x]._init(p);
    }
    else
        ins_tb(tr[p].s[k], k, x);
    push_up(p);
}

void dfs(int u) {
    if (!u)
        return;
    dfs(tr[u].s[0]);
    cout << tr[u].id << " ";
    dfs(tr[u].s[1]);
}

void test() { // 这个函数用来测试
    puts("[test]");
    dfs(root);
    puts("\n");
}

int main() {

    tr[0].s[0] = tr[0].s[1] = tr[0].p = tr[0].sz = tr[0].id = 0;

    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", P+i);
        id[ P[i] ] = i;
    }
    build(1, n, 0);
    idx = n;

    char op[10];
    int s, t;
    while (m--) {
//        test();

        scanf("%s%d", op, &s);
        int x = id[s];
        if (op[0] == 'T') {         // Top
            del(x);
            ins_tb(root, 0, x);
        }
        else if (op[0] == 'B') {    // Bottom
            del(x);
            ins_tb(root, 1, x);
        }
        else if (op[0] == 'I') {    // Insert
            scanf("%d", &t);
            if (!t)
                continue;
            splay(x, 0);
            int rk = tr[ tr[x].s[0] ].sz + t;
            del(x);
            if (rk == 0) {
                ins_tb(root, 0, x);
                continue;
            }
            int y = get_k(rk);
            splay(y, 0);
            assert(tr[y].sz == n - 1);
            if (!tr[y].s[1]) {
                tr[x]._init(y);
                tr[y].s[1] = x;
                push_up(y);
            }
            else {
                int z = tr[y].s[1];
                while (tr[z].s[0]) z = tr[z].s[0];
                splay(z, y);
                assert(!tr[z].s[0]);
                tr[x]._init(z);
                tr[z].s[0] = x;
                f_s(y, z, 1);
                push_up(z), push_up(y);
            }
        }
        else if (op[0] == 'A') {    // Ask
            splay(x, 0);
            int ans = tr[ tr[x].s[0] ].sz;
            printf("%d\n", ans);
        }
        else {                      // Query
            assert(op[0] == 'Q');
            x = get_k(s);
            printf("%d\n", tr[x].id);
        }
    }

    return 0;
}

标签:sz,洛谷,int,题解,void,tree,tr,splay,push
From: https://www.cnblogs.com/quanjun/p/18493242

相关文章

  • 非常牛 dsu on tree
    轩辕4721年,彩笔@硒六爱吃硫,在某日的西艾斯批%你赛中花了两个小时切掉了T1和T2。随后看到T3,心想:“这不是傻逼题吗,建下克鲁斯卡尔重构树然后瞎几把低批不就做完了吗。”发现并不会低批。思考了一个小时发现并不是沙比低批,而是地艾斯优盎吹。@硒六爱吃硫打完暴力。注意到......
  • [题解]CF825E Minimal Labels
    LPhang为什么是神?思路显然可以想到一个错误的贪心:直接拓扑排序,每一次选择当前可以拓展的点中最小的元素进行编号。由于可能存在一个值较小的元素被藏在一个较大的元素后面,这种贪心就会出问题。出问题的本质原因就是我们希望字典序最小,就得使得越小的位置分配到更小的值。不妨......
  • BehaviorTree、QP状态机与有限状态机(FSM)的比较分析
            在现代软件开发中,状态管理是确保系统行为正确性和高效性的关键。BehaviorTree、QP状态机和有限状态机(FSM)是三种常用的状态管理工具,它们各自适用于不同的场景。以下将通过具体例子和伪代码来比较这三种工具的特点和适用性。BehaviorTree:游戏AI的灵活决策Behav......
  • 历年CSP-J初赛真题解析 | 2017年CSP-J初赛完善程序(27-36)
    学习C++从娃娃抓起!记录下CSP-J备考学习过程中的题目,记录每一个瞬间。附上汇总贴:历年CSP-J初赛真题解析|汇总_热爱编程的通信人的博客-CSDN博客(快速幂)请完善下面的程序,该程序使用分治法求x......
  • CF2023D Many Games 题解
    赛时被创四了。思路考虑我们什么时候合并会比原来优。例如,我们现在要合并\(p_1,w_1\)和\(p_2,w_2\),同时保证,\(w_1\gew_2\)。那么有:\[\frac{p_1}{100}\timesw_1\le\frac{p_1}{100}\times\frac{p_2}{100}\times(w_1+w_2)\]\[\frac{p_2}{100}\timesw_2\le\frac{p_1}{......
  • 01 Eclipse使用Maven慢的问题解决
    1.Eclipse使用的是内置的MavenEclipse有可能使用了内置的Maven,而不是独立安装的Maven。如果使用Eclipse内置的Maven,默认的settings.xml可能并未生成。你可以按以下步骤检查或修改Maven设置路径:a.检查Eclipse使用的Maven配置点击Window->Preferences在......
  • 【题解】Solution Set - NOIP2024集训Day58 字符串
    【题解】SolutionSet-NOIP2024集训Day58字符串https://www.becoder.com.cn/contest/5658「CF1466G」SongoftheSirens考虑对于\(s_i\),算钦定必须覆盖到\(t_i\)的匹配个数\(f_i\)。注意到\(s\)每次长度都会\(\times~2\)左右,其长度在\(O(\log|w|)\)的时候就......
  • 洛谷管理员语录(不完整)
    ......
  • [ABC337G] Tree Inversion(换根 dp + 值域线段树)
    link题目形式就很换根dp如果这种题用朴素的做法求,就是暴力以每个点都做一次根跑树,自底向上统计,时间是\(O(n^2)\)而换根dp的思想就是分两步,一般先钦定某个点(如1)为根,统计一遍以1为根时的结果,然后挖掘如果以其他点为根时,变换对结果的影响,一般就是自顶向下更新如果换根后......
  • noi.ac775题解
    Gameb文件OI:gameb时限:1000ms空间:512MiBAlice和Bob正在玩一个游戏。具体来说,这个游戏是这样的,给定一个数列,从Alice开始,两个人轮流操作,每次操作可以从数列的头部或者尾部删去一个数字,当这个数列满足一定条件的时候,最后一次操作的人获胜。如果一开始就满足条......