跳到正文

个人记录

平衡树板子

Tony2约 12 分钟
原文
文章信息与版本

文章信息

发布
2024年1月27日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 12 分钟
获赞
2

版本

  1. v1当前

平衡树板子!

加载了几乎所有平衡树需要的基础功能,还可以在此基础上自行扩展。

平衡树用 Splay+指针 实现,需要传入维护的信息类型和标记类型。Splay 将自动维护子树信息和,标记下放。Splay 支持数组初始化,添加删除,分裂合并,下标访问,find和lower_bound。

代码很短,希望以后打icpc用得上。

C++
#include <bits/stdc++.h>
using namespace std;
template<typename Info_tp, typename Tag_tp> class node{
public:
	int sz;
	node *fa, *son[2];
	Info_tp sum, cur;
	Tag_tp tag;
	node(){
		sz = 1;
		fa = son[0] = son[1] = nullptr;
	}
	bool chk(node *x) const{return son[1] == x;}
	void up(){
		if (son[0] != nullptr && son[1] != nullptr){
			sz = son[0]->sz + 1 + son[1]->sz;
			sum = son[0]->sum + cur + son[1]->sum;
		}else if (son[0] != nullptr){
			sz = son[0]->sz + 1;
			sum = son[0]->sum + cur;
		}else if (son[1] != nullptr){
			sz = 1 + son[1]->sz;
			sum = cur + son[1]->sum;
		}else{
			sz = 1;
			sum = cur;
		}
	}
	void update(Tag_tp k){
		if (k()) swap(son[0], son[1]); 
		tag = tag + k;
		cur = cur + k;
		sum = sum + k;
	}
	void down(){
		if (son[0] != nullptr) son[0]->update(tag);
		if (son[1] != nullptr) son[1]->update(tag);
		tag = Tag_tp();
	}
	void spin(){
		node *x = fa, *y = x->fa;
		x->down(), down();
		int k = x->chk(this);
		if (y != nullptr) y->son[y->chk(x)] = this;
		fa = x->fa;
		x->son[k] = son[!k];
		if (son[!k] != nullptr) son[!k]->fa = x;
		(son[!k] = x)->fa = this;
		x->up(), up();
	}
};
template<typename Info_tp, typename Tag_tp, typename Comp=less<Info_tp>>
class Splay{
public:
	using snode = node<Info_tp, Tag_tp>;
	snode *rt;
	Splay() : rt(nullptr) {}
	Splay(snode *rt) : rt(rt) {}
	template<typename _Iter> Splay(_Iter bg, _Iter ed){
		snode *lst = nullptr, *p;
		for (_Iter it = bg; it != ed; it++){
			p = new snode();
			p->cur = *it;
			p->son[0] = lst;
			if (it != bg) lst->fa = p;
			p->up();
		}
	}
	template<typename _Iter> int copyout(_Iter it, snode *p){
		if (p == nullptr) return 0;
		*(it += copyout(it, p->son[0])) = p->cur;
		copyout(it + 1, p->son[1]);
		return p->sz;
	}
	int size() const{
		if (rt == nullptr) return 0;
		else return rt->sz;
	}
	bool empty() const{return rt == nullptr;}
	void splay(snode *p, snode *goal = nullptr){
		while (p->fa != goal){
			snode *x = p->fa, *y = x->fa;
			x->down();
			if (y != goal)
				(x->chk(p) ^ y->chk(x) ? p : x)->spin();
			p->spin();
		}
		if (goal == nullptr)
			rt = p;
	}
	snode* at(int x){
		snode *p = rt;
		while (1){
			int lftsz = p->son[0] == nullptr ? 0 : p->son[0]->sz;
			if (lftsz == x) break;
			p->down();
			if (lftsz > x) p = p->son[0];
			else{
				x -= lftsz + 1;
				p = p->son[1];
			}
		}
		splay(p);
		return p;
	}
	snode* operator [] (int x){
		return at(x);
	}
	snode* front(){
		snode *p = rt;
		while (p->son[0] != nullptr) p->down(), p = p->son[1];
		splay(p);
		return p;
	}
	snode* back(){
		snode *p = rt;
		while (p->son[1] != nullptr) p->down(), p = p->son[1];
		splay(p);
		return p;
	}
	snode* next(snode *p){
		if (p == nullptr) return nullptr;
		splay(p);
		p = p->son[1];
		if (p == nullptr) return p;
		while (p->son[0] != nullptr) p->down(), p = p->son[0];
		return p;
	}
	snode* pre(snode *p){
		if (p == nullptr) return back();
		splay(p);
		p = p->son[0];
		if (p == nullptr) return p;
		while (p->son[1] != nullptr) p->down(), p = p->son[1];
		return p;
	}
	friend Splay operator + (Splay a, Splay b){
		if (a.empty()) return b;
		if (b.empty()) return a;
		snode *rt1 = a.back(), *rt2 = b.rt;
		rt1->down();
		(rt1->son[1] = rt2)->fa = rt1;
		rt1->up();
		return a;
	}
	pair<Splay, Splay> split(int x){
		if (empty()) return {Splay(), Splay()};
		else if (x == 0) return {Splay(), *this};
		else if (x == size()) return {*this, Splay()};
		else{
			(*this)[x];
			rt->down();
			Splay lft(rt->son[0]);
			rt->son[0]->fa = nullptr;
			rt->son[0] = nullptr;
			rt->up();
			return {lft, Splay(rt)};
		}
	}
	snode* lower_bound(Info_tp k, Comp cmp = Comp{}){
		snode *p = rt, *lstvis = nullptr, *res = nullptr;
		while (p != nullptr){
			lstvis = p;
			p->down();
			if (cmp(p->cur, k)) p = p->son[1];
			else res = p, p = p->son[0];
		}
		if (lstvis != nullptr) splay(lstvis);
		return res;
	}
	snode* find(Info_tp k, Comp cmp = Comp{}){
		snode *res = lower_bound(k, cmp);
		if (res != nullptr && res->cur != k) res = nullptr;
		return res;
	}
	snode* insert(Info_tp k, Comp cmp = Comp{}){
		if (rt == nullptr){
			rt = new snode();
			rt->cur = rt->sum = k;
			return rt;
		}
		snode *p = rt, *fa = nullptr;
		while (p != nullptr){
			fa = p;
			p->down();
			p = p->son[cmp(p->cur, k)];
		}
		(p = fa->son[cmp(fa->cur, k)] = new snode())->fa = fa;
		p->cur = p->sum = k;
		splay(p);
		return p;
	}
	void erase(snode *p){
		splay(p);
		p->down();
		if (p->son[0] != nullptr) p->son[0]->fa = nullptr;
		if (p->son[1] != nullptr) p->son[1]->fa = nullptr;
		snode *lft = p->son[0], *rgt = p->son[1];
		delete p;
		rt = (Splay(lft) + Splay(rgt)).rt;
	}
	void erase(int x){
		erase((*this)[x]);
	}
	int rank(snode *p){
		if (p == nullptr) return rt->sz;
		splay(p);
		return p->son[0] == nullptr ? 0 : p->son[0]->sz;
	}
};
struct Info{
	int x, sz;
	Info() : x(0) {
		sz = 1;
	}
//	friend bool operator < (const Info &a, const Info &b){
//		return a.x < b.x;
//	}
	friend Info operator + (const Info &a, const Info &b){
		Info res;
		res.x = a.x + b.x;
		res.sz = a.sz + b.sz;
		return res;
	}
};
struct Cmp{
	bool operator () (Info a, Info b){
		return a.x < b.x;
	}
};
struct Tag{
	int x;
	Tag() : x(0) {}
	bool operator () () {return 0;}//区间翻转标记 
	friend Tag operator + (const Tag &a, const Tag &b){
		Tag res;
		res.x = a.x + b.x;
		return res;
	}
};
Info operator + (const Info &a, const Tag &b){
	Info res;
	res.x = a.x + b.x * a.sz;
	res.sz = a.sz;
	return res;
}
Splay<Info, Tag, Cmp> T;
int main(){
	for (int i = 1; i <= 5; i++){
		Info a;
		a.x = i;
		a.sz = 1;
		T.insert(a);
	}
	T.erase(2);
	Info tmp;
	tmp.x = 0, tmp.sz = 1;
	T.insert(tmp);
	vector<Info> vec(5);
	T.copyout(vec.begin(), T.rt);
	for (int i = 0; i < 5; i++)
		cout << vec[i].x << ' ';
	return 0;
}
原文
存档评论4 条抓取于