pbds学习笔记

## 头文件:
```cpp
#include <bits/extc++.h>
```

# 堆篇:

建议使用
```cpp
__gnu_pbds::priority_queue<int,greater<int> ,__gnu_pbds::pairing_heap_tag> q;
```

有以下常用的函数:
```cpp
push,
pop,
top,
size,
join
```
## 可并堆模板:
```cpp
#include<bits/stdc++.h>
#include<bits/extc++.h>

using namespace std;
bool vis[1919810];
int f[1919810];
int find(int x){
    if(f[x]==x){
        return x;
    }
    return f[x]=find(f[x]);
}
void merge(int x,int y){
    if(find(x)==find(y)){
        return;
    }
    f[find(x)]=find(y);
}
__gnu_pbds::priority_queue<pair<int,int>,greater<pair<int,int> > ,__gnu_pbds::pairing_heap_tag> q[1919810];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        q[i].push(make_pair(x,i));
    }
    for(int i=1;i<=m;i++){
        int opt;
        cin>>opt;
        if(opt==1){
            int x,y;
            cin>>x>>y;
            if(vis[x] || vis[y])continue;
            x=find(x);
            y=find(y);
            if(x!=y){
                q[y].join(q[x]);
                merge(x,y);
            }
        }else if(opt==2){
            int x;
            cin>>x;
            if(vis[x]){
                cout<<-1<<"\n";
            }else{
                x=find(x);
                // cerr<<x<<endl;
                // cerr<<q[x].size()<<"\n";
                cout<<q[x].top().first<<"\n";
                vis[q[x].top().second]=1;
                q[x].pop();
            }
        }
    }
    return 0;
}
```

# 平衡树篇

建议使用
```cpp
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;

__gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> Tree;
/*
insert(x):插入x
erase(x):删除x
find_by_order(k):求平衡树内排名为k的值是多少
order_of_key (x):求x的排名
能当set用
*/
int main(){

    return 0;
}
```
~~(其实在很多时候没必要这么麻烦,vector就行了)~~
# 哈希表篇:
用gp_hash_table或cc_hash_table代替map或unordered_map即可

### pbds 哈希原理 pbds(Policy-Based Data Structures)库中的哈希表实现采用了不同的解决哈希冲突的方法,提供了两种哈希表容器:`gp_hash_table` 和 `cc_hash_table`。`gp` 代表探测法,`cc` 代表拉链法,其中 `cc_hash_table` 稍快于其他,例如在实测中,`unordered_map` 用时 1369ms,`gp_hash_table` 用时 1302ms,`cc_hash_table` 用时 1181ms [^2]。 ### pbds 哈希使用方法及代码示例 以下是使用 `cc_hash_table` 的代码示例: ```cpp #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> using namespace __gnu_pbds; using namespace std; int main() { // 声明哈希表,每个 value 表示对应的 key 出现了多少次 cc_hash_table<int, int> f; // 插入元素 f[1] = 5; f[2] = 3; f[3] = 7; // 访问元素 cout << "Key 2 的值为: " << f[2] << endl; // 检查元素是否存在 if (f.find(3) != f.end()) { cout << "Key 3 存在于哈希表中" << endl; } // 删除元素 f.erase(2); // 再次检查元素是否存在 if (f.find(2) == f.end()) { cout << "Key 2 已从哈希表中删除" << endl; } return 0; } ``` ### 代码解释 - **头文件包含**:引入 `ext/pb_ds/assoc_container.hpp` 和 `ext/pb_ds/hash_policy.hpp` 头文件以使用 pbds 哈希表。 - **哈希表声明**:`cc_hash_table<int, int> f;` 声明了一个 `cc_hash_table` 类型的哈希表,键和值的类型均为 `int`。 - **元素插入**:通过 `f[key] = value;` 的方式插入元素。 - **元素访问**:使用 `f[key]` 访问元素。 - **元素查找**:使用 `f.find(key)` 检查元素是否存在。 - **元素删除**:使用 `f.erase(key)` 删除元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值