社区讨论

关于在线树状数组实现平衡树

学术版参与者 5已保存回复 16

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
16 条
当前快照
1 份
快照标识符
@lo10g6fw
此快照首次捕获于
2023/10/22 13:10
2 年前
此快照最后确认于
2023/11/02 12:40
2 年前
查看原帖
蒟蒻今天搞出来了一个在线的树状数组实现平衡树,但是 6565 分。
思路:
  1. 建一颗权值树状数组,删除和插入搞定。
  2. 查询排名直接在权值树状数组上查询即可。
  3. 求第 kk 小的方式类似于 P5356。
  4. 剩下的都可以转化为 2、3 操作。
样例过了。
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 100001

int n, ind;
set<int> st;
map<int, int> mp, mp2;
bitset<MAXN> vis;

struct Que{
	int opt, x;
}que[MAXN];

struct BIT{
#define lowbit(x) (x & -x)
	int c[MAXN];
	void add(int x, int k){
		while (x <= ind){
			c[x] += k;
			x += lowbit(x);
		}
	}

	int query(int x){
		int res(0);
		while (x){
			res += c[x];
			x -= lowbit(x);
		}
		return res;
	}
}tr;

int get_rank(int k){
	return tr.query(mp[k]-1)+1;
}

int get_kth(int k){
	int l(1), r(ind), mid, res;
	while (l <= r){
		mid = (l+r) >> 1;
		if (tr.query(mid) < k) l = mid+1;
		else r = (res=mid)-1;
	}
	return mp2[res];
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n;
	for (int i(1); i<=n; ++i){
		cin >> que[i].opt >> que[i].x;
		if (que[i].opt ^ 4) st.insert(que[i].x);
	}
	for (auto i: st){
		mp[i] = ++ind;
		mp2[ind] = i;
	}

	for (int i(1); i<=n; ++i){
		if (que[i].opt == 1){
			tr.add(mp[que[i].x], 1);
			vis.set(mp[que[i].x]);
		}else if (que[i].opt == 2) tr.add(mp[que[i].x], -1);
		else if (que[i].opt == 3) cout << get_rank(que[i].x) << '\n';
		else if (que[i].opt == 4) cout << get_kth(que[i].x) << '\n';
		else if (que[i].opt == 5) cout << get_kth(get_rank(que[i].x)-!vis.test(mp[que[i].x])) << '\n';
		else  cout << get_kth(get_rank(que[i].x)+vis.test(mp[que[i].x])) << '\n';
	}

	return 0;
}

回复

16 条回复,欢迎继续交流。

正在加载回复...