FHQTreap

文章详细介绍了如何使用C++实现一个自平衡二叉搜索树(如AVL或红黑树),包括插入、删除、查找、获取第k小元素、前驱和后继等操作。
#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
#include <random>
using namespace std;


inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

#define lson(x) tr[x].l
#define rson(x) tr[x].r

const int maxn = 1e5 + 7;
std::mt19937 rnd(233);

struct Node {
	int l, r, key;
	int nRand;
	int size;
}tr[maxn];

int idx = 0,root = 0;
int getNode(int k) {
	tr[++idx].key = k;
	tr[idx].nRand = rnd();
	tr[idx].size = 1;
	return idx;
}

void update(int f) {
	tr[f].size = tr[lson(f)].size + tr[rson(f)].size + 1;
}

void split(int p,int k,int &x,int &y) {
	if (!p) x = y = 0;
	else {
		if (tr[p].key <= k) {
			x = p;
			split(rson(p), k, rson(p), y);
		}
		else {
			y = p;
			split(lson(p), k, x, lson(p));
		}
		update(p);
	}
}

int merge(int x, int y) {
	if (!x || !y) return x + y;
	if (tr[x].nRand > tr[y].nRand) {
		rson(x) = merge(rson(x),y);
		update(x);
		return x;
	}
	else {
		lson(y) = merge(x, lson(y));
		update(y);
		return y;
	}
}

void insert(int k) {
	int x, y;
	split(root,k,x,y);
	root = merge(merge(x, getNode(k)), y);
}
void del(int k) {
	int x, y, z;
	split(root, k, x, y);
	split(x, k - 1, x, z);
	z = merge(lson(z),rson(z));
	root = merge(merge(x,z),y);
}

int getRank(int k) {
	int x, y;
	split(root, k - 1, x, y);
	int ans = tr[x].size + 1;
	root = merge(x, y);
	return ans;
}

int getKth(int rank) {
	int cur = root;
	while (cur) {
		if (tr[lson(cur)].size + 1 == rank) break;
		else if (tr[lson(cur)].size >= rank) {
			cur = lson(cur);
		}
		else {
			rank -= tr[lson(cur)].size + 1;
			cur = rson(cur);
		}
	}
	return tr[cur].key;
}

int getPre(int k) {
	int x, y;
	split(root, k - 1, x, y);
	int cur = x;
	while (rson(cur)) cur = rson(cur);
	int ans = tr[cur].key;
	root = merge(x, y);
	return ans;
}

int getNext(int k) {
	int x, y;
	split(root, k, x, y);
	int cur = y;
	while (lson(cur)) cur = lson(cur);
	int ans = tr[cur].key;
	root = merge(x, y);
	return ans;
}

int n;
int main()
{
	cin >> n;

	int a, b;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		switch (a)
		{
		case 1:insert(b); break;
		case 2:del(b); break;
		case 3:cout << getRank(b) << endl;  break;
		case 4:
		{
			cout << getKth(b) << endl;
			break;
		}
		case 5:
		{
			cout << getPre(b) << endl;
			break;
		}
		case 6:
		{
			cout << getNext(b) << endl;
			break;
		}
		default:
			break;
		}
	}
	return 0;
}

c++# P6136 【模板】普通平衡树(数据加强版) ## 题目背景 本题是 P3369 数据加强版,**扩大数据范围**并增加了**强制在线**。 **题目的输入、输出和原题略有不同**,但需要支持的操作相同。 ## 题目描述 您需要动态地维护一个可重集合 $M$,并且提供以下操作: 1. 向 $M$ 中插入一个数 $x$。 2. 从 $M$ 中删除一个数 $x$(若有多个相同的数,应只删除一个)。 3. 查询 $M$ 中有多少个数比 $x$ 小,并且将得到的答案加一。 4. 查询如果将 $M$ 从小到大排列后,排名位于第 $x$ 位的数。 5. 查询 $M$ 中 $x$ 的前驱(前驱定义为小于 $x$,且最大的数)。 6. 查询 $M$ 中 $x$ 的后继(后继定义为大于 $x$,且最小的数)。 本题**强制在线**,保证所有操作合法(操作 $2$ 保证存在至少一个 $x$,操作 $4,5,6$ 保证存在答案)。 ## 输入格式 第一行两个正整数 $n,m$,表示**初始数的个数**和操作的个数。 **第二行 $n$ 个整数 $a_1,a_2,a_3,\ldots,a_n$,表示初始的数**。 接下来 $m$ 行,每行有两个整数 $\text{opt}$ 和 $x'$,$\text{opt}$ 表示操作的序号($ 1 \leq \text{opt} \leq 6 $),$x'$ 表示加密后的操作数。 我们记 $\text{last}$ 表示上一次 $3,4,5,6$ 操作的答案,则每次操作的 $x'$ 都要**异或**上 $\text{last}$ 才是真实的 $x$。初始 $\text{last}$ 为 $0$。 ## 输出格式 输出一行一个整数,表示所有 $3,4,5,6$ 操作的答案的**异或和**。 ## 输入输出样例 #1 ### 输入 #1 ``` 6 7 1 1 4 5 1 4 2 1 1 9 4 1 5 8 3 13 6 7 1 4 ``` ### 输出 #1 ``` 6 ``` ## 说明/提示 ### 样例解释 样例加密前为: ```plain 6 7 1 1 4 5 1 4 2 1 1 9 4 1 5 9 3 8 6 1 1 0 ``` ::::info[每一个操作的输出] 执行第一个操作前,$M=\{1,1,1,4,4,5\}$,完成后 $M=\{1,1,4,4,5\}$。 执行第二个操作后 $M=\{1,1,4,4,5,9\}$。 第三个操作查询 $M$ 中第 $1$ 小的数字,答案为 $1$。 第四个操作查询 $M$ 中 $9$ 的前驱,答案为 $5$。 第五个操作查询 $M$ 中有多少个数比 $8$ 小,并且将答案加 $1$,答案为 $6$。 第六个操作查询 $M$ 中 $1$ 的后继,答案为 $4$。 第七个操作完成后 $M=\{0,1,1,4,4,5,9\}$。 输出 $1\oplus5\oplus6\oplus4=6$。 :::: ### 限制与约定 对于 $100\%$ 的数据,$1\leq n\leq 10^5$,$1\leq m\leq 10^6$,$0\leq a_i,x\lt 2^{30}$。 **本题输入数据较大,请使用较快的读入方式。** --- $\text{upd 2022.7.22}$:新增加 $9$ 组 Hack 数据。
11-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值