洛谷P3369 普通平衡树题解(非旋Treap)题解

题目链接

有旋Treap见此

可持续化情况

题意

您需要写一种数据结构,来维护一些数,其中需要提供以下操作:
case 1: 插入 x x x
case 2: 删除 x x x数(若有多个相同的数,因只删除一个)
case 3: 查询 x x x数的排名(排名定义为比当前数小的数的个数 + 1 +1 +1)
case 4: 查询排名为 x x x的数
case 5: x x x的前驱(前驱定义为小于 x x x,且最大的数)
case 6: x x x的后继(后继定义为大于 x x x,且最小的数)

这就是一个模板题,这里提供一个链接,讲述非常详细,如下链接


实测可以过的 \color{purple}{\text{实测可以过的}} 实测可以过的

代码(有作用注释)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
#define pk putchar(' ')
#define ph puts("")
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
template <class T>
void rd(T &x)
{
    x = 0;
    int f = 1;
    char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    x *= f;
}
template <class T>
void pt(T x)
{
    if (x < 0)
        putchar('-'), x = (~x) + 1;
    if (x > 9)
        pt(x / 10);
    putchar(x % 10 ^ 48);
}
template <class T>
T Max(T a, T b)
{
    return a > b ? a : b;
}
template <class T>
T Min(T a, T b)
{
    return a < b ? a : b;
}
using namespace std;
const int INF = 0x3f3f3f3f, N = 1e5 + 5;
int n;
struct Node 
{
    int val, key, siz, l ,r;
    void clear()
    {
        val = key = siz = l = r = 0;
    }
};
struct Treapnode
{
    Node t[N];
    int tot, root;
    void update(int now)
    {
        t[now].siz = t[t[now].l].siz + t[t[now].r].siz + 1;
    }
    // 更新
    int merge(int u, int v) 
    { 
        if (!u || !v) 
            return u | v; 
        if (t[u].key < t[v].key) 
        {
            t[u].r = merge(t[u].r, v); 
            update(u); 
            return u;
        } 
        else 
        {
            t[v].l = merge(u, t[v].l); 
            update(v); 
            return v;
        }
    }
    // 合并
    void split_val(int now, int k, int& x, int& y) 
    {
        if (!now) 
            return (void)(x = y = 0); 
        if (t[now].val <= k) 
            x = now, split_val(t[now].r, k, t[now].r, y);
        else 
            y = now, split_val(t[now].l, k, x, t[now].l);
        update(now);
    }
    // 按权值大小拆分
    void split_k(int now, int k, int& x, int& y) 
    {
        if (!now) 
            return (void)(x = y = 0);
        update(now);
        if (t[t[now].l].siz < k)
            x = now, split_k(t[now].r, k - t[t[now].l].siz - 1, t[now].r, y);
        else
            y = now, split_k(t[now].l, k, x, t[now].l);
        update(now);
    }
    // 按排名大小拆分
    void ins(int x) 
    {
        int u, a, b;
        t[u = ++tot].key = rand();
        t[u].val = x, t[u].siz = 1;
        split_val(root, x, a, b);
        root = merge(merge(a, u), b);
    }
    // 插入x
    void del(int x) 
    {
        int a ,b ,c ,d;
        split_val(root, x - 1, a, b);
        split_k(b, 1, c, d);
        t[c].clear();
        root = merge(a, d);
    }
    // 删除x
    int get_rank(int x) 
    {
        int a, b, c;
        split_val(root, x - 1, a, b);
        c = t[a].siz + 1;
        root = merge(a, b);
        return c;
    }
    // 得到x的排名
    int get_val(int& now, int x) 
    {
        int a, b, c, d, e;
        split_k(now, x - 1, a, b);
        split_k(b, 1, c, d);
        e = t[c].val;
        now = merge(a, merge(c, d));
        return e;
    }
    // 以root为根的找第x的值
    int pre(int x) 
    {
        int a, b, c;
        split_val(root, x - 1, a, b);
        c = get_val(a, t[a].siz);
        root = merge(a, b);
        return c;
    }
    // 找前驱
    int sub(int x) 
    {
        int a, b, c;
        split_val(root, x, a, b);
        c = get_val(b, 1);
        root = merge(a, b);
        return c;
    }
    // 找后缀
}Treap;
int main() 
{
    rd(n);
    int opt, x;
    while(n--)
    {
        rd(opt), rd(x);
        switch(opt)
        {
            case 1: Treap.ins(x); break;
            case 2: Treap.del(x); break;
            case 3: pt(Treap.get_rank(x)), ph; break;
            case 4: pt(Treap.get_val(Treap.root, x)), ph; break;
            case 5: pt(Treap.pre(x)), ph; break;
            case 6: pt(Treap.sub(x)), ph; break;
        }
    }
    return 0;
}

Thanks!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于洛谷上的p1036题目,我们可以使用Python来解决。下面是一个可能的解法: ```python def dfs(nums, target, selected_nums, index, k, sum): if k == 0 and sum == target: return 1 if index >= len(nums) or k <= 0 or sum > target: return 0 count = 0 for i in range(index, len(nums)): count += dfs(nums, target, selected_nums + [nums[i]], i + 1, k - 1, sum + nums[i]) return count if __name__ == "__main__": n, k = map(int, input().split()) nums = list(map(int, input().split())) target = int(input()) print(dfs(nums, target, [], 0, k, 0)) ``` 在这个解法中,我们使用了深度优先搜索(DFS)来找到满足要求的列。通过递归的方式,我们遍历了所有可能的字组合,并统计满足条件的个。 首先,我们从给定的n和k分别表示字个需要选取的字个。然后,我们输入n个字,并将它们存储在一个列表nums中。接下来,我们输入目标值target。 在dfs函中,我们通过迭代index来选择字,并更新选取的字个k和当前总和sum。如果k等于0且sum等于target,我们就找到了一个满足条件的组合,返回1。如果index超出了列表长度或者k小于等于0或者sum大于target,说明当前组合不满足要求,返回0。 在循环中,我们不断递归调用dfs函,将选取的字添加到selected_nums中,并将index和k更新为下一轮递归所需的值。最终,我们返回所有满足条件的组合个。 最后,我们在主程序中读入输入,并调用dfs函,并输出结果。 这是一种可能的解法,但不一定是最优解。你可以根据题目要求和测试据进行调试和优化。希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值