C++ pbds 库平衡树(tree)

头文件

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//或者直接用
#include <bits/extc++.h>

命名空间

using namespace __gnu_pbds;

定义

tree<double, null_type, greater<double>, rb_tree_tag, tree_order_statistics_node_update> T;
//这个东西有一点点长
//第一个参数是数据类型
//第二个要填null_type,低版本编译器填null_mapped_type
//第三个填比较函数 std::greater<> or std::less<> or cmp
//第四个填树的类型,有rb_tree_tag红黑树和splay_tree_tag
//第五个是为了支持查询第k大和排名的一个参数
//tree_order_statistics_node_update

使用

这个东西和\(set\)一样不支持重复元素,所以一般用\(double\),或者自定义结构体变量或者用\(pair\)都是可以的,只要记住千万不要插入重复元素就好了。

洛谷模板:普通平衡树

#include <cstdio>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

tree<double, null_mapped_type, greater<double>, rb_tree_tag, tree_order_statistics_node_update> T;

int main()
{
    //freopen("3369.in", "r", stdin);
    //freopen("3369.out", "w", stdout);

    int q, opt, x;

    scanf("%d", &q);
    for (int i = 1; i <= q; ++ i)
    {
        scanf("%d%d", &opt, &x);
        if(opt == 1) 
            T.insert(x + i * 1e-6);
        //插入一个数
        if(opt == 2) 
            T.erase(T.lower_bound(x));
        //删除一个数
        if(opt == 3) 
            printf("%d\n", (int)T.order_of_key(x) + 1);
        //查询一个数的排名
        if(opt == 4) 
            printf("%d\n", (int)*T.find_by_order(x - 1));
        //查询第k小的数 返回的是一个迭代器 这里k是从0开始算的,意思是最小的数是第0小的
        if(opt == 5) 
            printf("%d\n", (int)round(*(-- T.lower_bound(x))));
        //查询一个数的前驱
        if(opt == 6) 
            printf("%d\n", (int)round(*T.lower_bound(x + 1)));
        //查询一个数的后继
    }

    return 0;
}

这个东西在比赛中是可以用的,所以如果嫌打平衡树太麻烦就可以直接用啦。

在这里插入图片描述

转载于:https://www.cnblogs.com/brunch/p/9929832.html

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL 中的 `tree` 是一个非标准的容器,需要使用第三方 `GCC-Tree` 才能使用。下面是一个简单的例子,演示如何使用 `tree` 存储一组整数,并对其进行遍历和查找操作: ```c++ #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int main() { ordered_set s; s.insert(10); s.insert(20); s.insert(30); s.insert(40); s.insert(50); cout << "size = " << s.size() << endl; cout << "s[1] = " << *s.find_by_order(1) << endl; cout << "s[2] = " << *s.find_by_order(2) << endl; cout << "s.rank(30) = " << s.order_of_key(30) << endl; cout << "s.rank(40) = " << s.order_of_key(40) << endl; for (auto x : s) { cout << x << " "; } return 0; } ``` 输出结果为: ``` size = 5 s[1] = 20 s[2] = 30 s.rank(30) = 2 s.rank(40) = 3 10 20 30 40 50 ``` 在这个例子中,我们首先定义了一个 `ordered_set` 类型的变量 `s`,它是一个存储整数类型元素的 `tree` 容器。然后,我们向 `s` 中插入了一些元素,使用 `size` 函数查看容器中元素的个数,使用 `find_by_order` 函数查找元素,使用 `order_of_key` 函数查找元素在容器中的排名,最后使用一个 for 循环遍历容器中的元素并输出。 需要注意的是,`tree` 是一个非标准的容器,使用时需要在编译选项中添加 `-std=c++11 -D_GLIBCXX_DEBUG`。此外,`tree` 容器的功能与 `set` 和 `map` 类似,但是由于 `tree` 使用的是红黑树,因此插入和删除操作的时间复杂度要比 `set` 和 `map` 高一些,但是查找操作的时间复杂度相对较低。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值