跳到正文

剪贴板

剪贴板 3b0klr34

原文
剪贴板信息

剪贴板信息

收录
更新

测试若干种平衡树的 pushup\text{pushup} 操作次数

〇、测试用代码

编译命令:g++ test.cpp -o test -std=c++17 -O2。

代码时间跨度大,风格并不很统一。

1. Splay1.\text{ Splay}

C++
#include <bits/stdc++.h>
using namespace std;

// 快读

int pushup_O;

template<typename T>
class Splay
{
 private:
	struct SplayNode
	{
		T val;
		int siz,cnt;
		SplayNode *lc,*rc,*fa;
		SplayNode(const T &v): val(v),siz(1),cnt(1)
			{ lc=rc=fa=nullptr; }
	}*rt;
	typedef SplayNode *pNode;
#define islc(i) (i==i->fa->lc)
	inline void pushup(pNode i)
	{
		pushup_O++;
		i->siz=i->cnt;
		if(i->lc) i->siz+=i->lc->siz;
		if(i->rc) i->siz+=i->rc->siz;
	}
	inline void rotate(pNode i)
	{
		pNode f=i->fa,gf=f->fa;
		if(islc(i))
		{
			f->lc=i->rc,i->rc=f;
			if(f->lc) f->lc->fa=f;
		}
		else
		{
			f->rc=i->lc,i->lc=f;
			if(f->rc) f->rc->fa=f;
		}
		if(gf) (islc(f)?gf->lc:gf->rc)=i;
		i->fa=gf,f->fa=i,pushup(f);
	}
	inline void splay(pNode x,pNode y=nullptr)
	{
		for(pNode f;(f=x->fa)!=y;rotate(x))
			if(f->fa!=y)
				rotate(islc(f)==islc(x)?f:x);
		pushup(x);
		if(!y) rt=x;
	}
#undef islc
 public:
	Splay(): rt(nullptr){}
	inline void insert(const T &v)
	{
		pNode i=rt,f=nullptr;
		while(i)
		{
			if(v<i->val) f=i,i=i->lc;
			else if(i->val<v) f=i,i=i->rc;
			else break;
		}
		if(i) i->siz++,i->cnt++;
		else
		{
			i=new SplayNode(v),i->fa=f;
			if(f) (v<f->val?f->lc:f->rc)=i;
		}
		splay(i);
	}
	inline void remove(const T &v)
	{
		pNode i=rt,j;
		while(i)
			if(v<i->val) i=i->lc;
			else if(i->val<v) i=i->rc;
			else break;
		if(!i) return;
		i->siz--,i->cnt--,splay(i);
		if(!i->cnt)
		{
			if(i->lc && i->rc)
			{
				for(j=i->rc;j->lc;j=j->lc);
				splay(j),j->lc=i->lc;
				if(i->lc) i->lc->fa=j;
			}
			else if(i->lc)
				i->lc->fa=nullptr,rt=i->lc;
			else if(i->rc)
				i->rc->fa=nullptr,rt=i->rc;
			else rt=nullptr;
			delete i;
		}
	}
	inline int getRank(const T &v)
	{
		if(!rt)	return 1;
		int res=1;
		pNode i=rt,f=nullptr;
		while(i)
		{
			f=i;
			if(v<i->val) i=i->lc;
			else if(i->val<v) res+=(i->lc?i->lc->siz:0)+i->cnt,i=i->rc;
			else { res+=(i->lc?i->lc->siz:0);break; }
		}
		return splay(f),res;
	}
	inline T getValue(int rk)
	{
		if(!rt || rk>rt->siz) return T();
		pNode i=rt;
		while(true)
		{
			int sz=(i->lc?i->lc->siz:0);
			if(sz>=rk) i=i->lc;
			else if((rk-=sz+i->cnt)<=0) break;
			else i=i->rc;
		}
		return splay(i),i->val;
	}
	inline T getPre(const T &v)
	{
		pNode i=rt,x=nullptr;
		while(i)
			if(i->val<v) x=i,i=i->rc;
			else i=i->lc;
		if(x) return splay(x),x->val;
		else return T();
	}
	inline T getNxt(const T &v)
	{
		pNode i=rt,x=nullptr;
		while(i)
			if(v<i->val) x=i,i=i->lc;
			else i=i->rc;
		if(x) return splay(x),x->val;
		else return T();
	}
};

int m;
Splay<int> t;
int main()
{
	freopen("input.in","r",stdin);
	freopen("output.out","w",stdout);
	qin>>m;
	for(int i=1,opt,x;i<=m;i++)
	{
		qin>>opt>>x;
		if(opt==1) t.insert(x);
		else if(opt==2) t.remove(x);
		else if(opt==3) qout<<t.getRank(x)<<'\n';
		else if(opt==4) qout<<t.getValue(x)<<'\n';
		else if(opt==5) qout<<t.getPre(x)<<'\n';
		else qout<<t.getNxt(x)<<'\n';
	}
	cerr<<"pushup was used "<<pushup_O<<" times.\n";
	return 0;
}

2. Leafy-Splay2.\text{ Leafy-Splay}

C++
#include <bits/stdc++.h>
using namespace std;

// 快读

int pushup_O;

template<typename KeyT>
class LeafySplay
{
 private:
	struct SplayNode
	{
		KeyT key;
		int siz;
		SplayNode *lc,*rc,*fa;
		SplayNode(): key(),siz(0),lc(nullptr),rc(nullptr),fa(nullptr){}
		SplayNode(const KeyT &x): key(x),siz(1),lc(nullptr),rc(nullptr),fa(nullptr){}
	}*rt;
	using pNode=SplayNode*;
#define islc(x) (x->fa->lc==x)
	inline void pushup(pNode i)
	{
		pushup_O++;
		i->siz=(i->lc?i->lc->siz:0)+(i->rc?i->rc->siz:0);
		if(i->lc) i->key=i->lc->key;
	}
	inline void rotate(pNode i)
	{
		pNode f=i->fa,gf=f->fa;
		if(islc(i)) f->lc=i->rc,i->rc=f,f->lc&&(f->lc->fa=f);
		else f->rc=i->lc,i->lc=f,f->rc&&(f->rc->fa=f);
		if(gf) (islc(f)?gf->lc:gf->rc)=i;
		f->fa=i,i->fa=gf,pushup(f);
	}
	inline void splay(pNode x,pNode goal=nullptr)
	{
		for(pNode f;(f=x->fa)!=goal;rotate(x))
			if(f->fa!=goal)
				rotate(islc(f)==islc(x)?f:x);
		pushup(x),goal||(rt=x);
	}
	inline pNode &__find(const KeyT &k,pNode &i)
	{
		if(!i->rc) return i;
		if(k<i->rc->key) return __find(k,i->lc);
		else return __find(k,i->rc);
	}
	inline void __insert(const KeyT &k,pNode &i)
	{
		if(i->rc) return __insert(k,k<i->rc->key?i->lc:i->rc);
		pNode x=new SplayNode(),y=new SplayNode(k);
		x->fa=i->fa,swap(x,i);
		if(y->key<x->key) swap(x,y);
		i->lc=x,i->rc=y,x->fa=y->fa=i,splay(i);
	}
	inline bool __erase(const KeyT &k,pNode &i)
	{
		if(!i->rc) return !(i->key<k);
		bool d=(k<i->rc->key);
		if(__erase(k,d?i->lc:i->rc))
		{
			pNode x=(d?i->lc:i->rc),y=(d?i->rc:i->lc);
			y->fa=i->fa,swap(i,y);
			delete x;delete y;
			if(i->fa) splay(i->fa);
		}
		return false;
	}
#undef islc
 public:
	inline void insert(const KeyT &k)
		{ if(rt) __insert(k,rt); else rt=new SplayNode(k); }
	inline void erase(const KeyT &k)
		{ if(rt && __erase(k,rt)) { delete rt;rt=nullptr; } }
	inline int getRank(const KeyT &k) // |{x|x<=k}|
	{
		if(!rt) return 0;
		int res=0;
		pNode i=rt;
		while(i->rc)
			if(k<i->rc->key) i=i->lc;
			else res+=i->lc->siz,i=i->rc;
		if(i->fa) splay(i->fa);
		return res+!(k<i->key);
	}
	inline const KeyT &getKth(int k)
	{
		if(!rt) cerr<<"empty tree.\n",abort();
		pNode i=rt;
		while(i->rc)
			if(i->lc->siz>=k) i=i->lc;
			else k-=i->lc->siz,i=i->rc;
		if(i->fa) splay(i->fa);
		if(k<=1) return i->key;
		cerr<<"no kth exists.\n",abort();
	}
	inline const KeyT &getPre(const KeyT &k) // max{x|x<=k}
	{
		if(!rt) cerr<<"empty tree.\n",abort();
		pNode i=rt;
		while(i->rc)
			if(k<i->rc->key) i=i->lc;
			else i=i->rc;
		if(i->fa) splay(i->fa);
		if(!(k<i->key)) return i->key;
		cerr<<"no prev exists.\n",abort();
	}
	inline const KeyT &getNxt(const KeyT &k) // min{x|x>k}
	{
		if(!rt) cerr<<"empty tree.\n",abort();
		pNode i=rt,res=nullptr;
		while(i->rc)
			if(k<i->rc->key) res=i->rc,i=i->lc;
			else i=i->rc;
		if(i->fa) splay(i->fa);
		if(k<i->key) return i->key;
		else if(res) return res->key;
		cerr<<"no next exists.\n",abort();
	}
};

int m;
LeafySplay<int> t;
int main()
{
	freopen("input.in","r",stdin);
	freopen("output.out","w",stdout);
	qin>>m;
	for(int i=1,opt,x;i<=m;i++)
	{
		qin>>opt>>x;
		if(opt==1) t.insert(x);
		else if(opt==2) t.erase(x);
		else if(opt==3) qout<<t.getRank(x-1)+1<<'\n';
		else if(opt==4) qout<<t.getKth(x)<<'\n';
		else if(opt==5) qout<<t.getPre(x-1)<<'\n';
		else qout<<t.getNxt(x)<<'\n';
	}
	cerr<<"pushup was used "<<pushup_O<<" times.\n";
	return 0;
}

3. (FHQ) Treap3.\text{ (FHQ) Treap}

C++
#include <bits/stdc++.h>
using namespace std;

// 快读

int pushup_O;

struct TreapNode
{
	int pri,val,siz;
	TreapNode *lc,*rc;
	TreapNode(int v): val(v),siz(1)
		{ pri=rand(),lc=rc=nullptr; }
}*rt;
using pNode=TreapNode*;
inline void pushup(pNode i)
{
	pushup_O++;
	i->siz=1;
	if(i->lc) i->siz+=i->lc->siz;
	if(i->rc) i->siz+=i->rc->siz;
}
void split(pNode i,int k,pNode &x,pNode &y)
{
	if(!i) x=y=nullptr;
	else if(i->val<=k) x=i,split(i->rc,k,x->rc,y),pushup(x);
	else y=i,split(i->lc,k,x,y->lc),pushup(y);
}
pNode merge(pNode x,pNode y)
{
	if(!x) return y;
	if(!y) return x;
	if(x->pri<y->pri) return x->rc=merge(x->rc,y),pushup(x),x;
	else return y->lc=merge(x,y->lc),pushup(y),y;
}
void insert(int v)
{
	pNode x,y;split(rt,v,x,y);
	rt=merge(merge(x,new TreapNode(v)),y);
}
void remove(int v)
{
	pNode x,y,z,zt;split(rt,v,x,z),split(x,v-1,x,y);
	if(y) delete (zt=y,y=merge(y->lc,y->rc),zt);
	rt=merge(merge(x,y),z);
}
int getRank(int v)
{
	pNode x,y;split(rt,v-1,x,y);
	int res=(x?x->siz:0)+1;
	return rt=merge(x,y),res;
}
int getValue(int rk)
{
	pNode i=rt;
	int sz;
	while(true)
	{
		sz=i->lc?i->lc->siz:0;
		if(sz>=rk) i=i->lc;
		else if(!(rk-=sz+1)) return i->val;
		else i=i->rc;
	}
}
int getPre(int v)
{
	pNode x,y,i;split(rt,v-1,x,y);
	for(i=x;i->rc;i=i->rc);
	int res=i->val;
	return rt=merge(x,y),res;
}
int getNxt(int v)
{
	pNode x,y,i;split(rt,v,x,y);
	for(i=y;i->lc;i=i->lc);
	int res=i->val;
	return rt=merge(x,y),res;
}
int n,opt,x;
int main()
{
	freopen("input.in","r",stdin);
	freopen("output.out","w",stdout);
	qin>>n;
	while(n--)
	{
		qin>>opt>>x;
		if(opt==1) insert(x);
		else if(opt==2) remove(x);
		else if(opt==3) qout.writeln(getRank(x));
		else if(opt==4) qout.writeln(getValue(x));
		else if(opt==5) qout.writeln(getPre(x));
		else qout.writeln(getNxt(x));
	}
	cerr<<"pushup was used "<<pushup_O<<" times.\n";
	return 0;
}

4. RB Tree4.\text{ RB Tree}

C++
#include <bits/stdc++.h>
using namespace std;

// 快读

int pushup_O;

template<typename T>
class RBTree
{
 private:
	struct RBNode
	{
		T val;
		bool red;
		int siz,cnt;
		RBNode *lc,*rc,*fa;
		RBNode(const T &v): val(v),red(true),siz(1),cnt(1)
			{ lc=rc=fa=nullptr; }
	}*rt;
	typedef RBNode *pNode;
#define islc(i) (i->fa->lc==i)
	inline void pushup(pNode i)
	{
		pushup_O++;
		i->siz=i->cnt;
		if(i->lc) i->siz+=i->lc->siz;
		if(i->rc) i->siz+=i->rc->siz;
	}
	inline void rotate(pNode i)
	{
		pNode f=i->fa,gf=f->fa;
		if(gf) (islc(f)?gf->lc:gf->rc)=i;
		if(islc(i))
		{
			f->lc=i->rc,i->rc=f,f->fa=i;
			if(f->lc) f->lc->fa=f;
		}
		else
		{
			f->rc=i->lc,i->lc=f,f->fa=i;
			if(f->rc) f->rc->fa=f;
		}
		i->fa=gf,f->fa=i,pushup(f),pushup(i);
	}
	inline void insertFixup(pNode i)
	{
		pNode u,f,gf;
		while(i->fa && i->fa->red)
		{
			f=i->fa,gf=f->fa,u=islc(f)?gf->rc:gf->lc;
			if(u && u->red)
				u->red=f->red=false,gf->red=true,i=gf;
			else
			{
				if(gf==rt) rt=(islc(i)==islc(f))?f:i;
				if(islc(i)==islc(f))
					rotate(f),f->red=false,gf->red=true;
				else
					rotate(i),rotate(i),i->red=false,gf->red=true;
				return;
			}
		}
		if(i==rt)	i->red=false;
	}
	inline void removeFixup(pNode i)
	{
		pNode b,f,c;
		while(i!=rt)
		{
			f=i->fa,b=islc(i)?f->rc:f->lc,c=nullptr;
			if(b->red)
			{
				if(f==rt) rt=b;
				rotate(b),b->red=false,f->red=true;
				continue;
			}
			else
			{
				if(b->lc && b->lc->red) c=b->lc;
				if(b->rc && b->rc->red) c=b->rc;
				if(c)
				{
					if(f==rt) rt=(islc(c)==islc(b))?b:c;
					if(islc(c)==islc(b))
						rotate(b),b->red=f->red,f->red=c->red=false;
					else
						rotate(c),rotate(c),c->red=f->red,f->red=false;
					return;
				}
			}
			if(f->red) return f->red=false,b->red=true,void();
			else b->red=true,i=f;
		}
	}
 public:
	RBTree(): rt(nullptr){}
	inline void insert(const T &v)
	{
		pNode i=rt,f=nullptr;
		while(i)
		{
			i->siz++;
			if(v<i->val) f=i,i=i->lc;
			else if(i->val<v) f=i,i=i->rc;
			else break;
		}
		if(i) i->cnt++;
		else
		{
			i=new RBNode(v);
			if(!rt)	rt=i;
			else i->fa=f,(v<f->val?f->lc:f->rc)=i;
			insertFixup(i);
		}
	}
	inline void remove(const T &v)
	{
		pNode i=rt,j;
		while(i)
		{
			i->siz--;
			if(v<i->val) i=i->lc;
			else if(i->val<v) i=i->rc;
			else break;
		}
		if(i && !(--i->cnt))
		{
			if(i->lc && i->rc)
			{
				j=i->rc;
				while(j->lc) j=j->lc;
				int x=j->cnt;
				i->val=j->val,i->cnt=x,j->cnt=0;
				i=i->rc;
				while(i!=j) i->siz-=x,i=i->lc;
				i->siz-=x;
			}
			j=i->lc?i->lc:i->rc;
			if(!i->red)
			{
				if(!j || !j->red) removeFixup(i);
				else if(j) j->red=false;
			}
			if(j) j->fa=i->fa;
			if(i->fa) (islc(i)?i->fa->lc:i->fa->rc)=j;
			if(i==rt) { rt=j;if(j)j->red=false; }
			delete i;
		}
	}
	inline int getRank(const T &v)
	{
		pNode i=rt;
		int res=1;
		while(i)
			if(v<i->val) i=i->lc;
			else if(i->val<v) res+=(i->lc?i->lc->siz:0)+i->cnt,i=i->rc;
			else return res+(i->lc?i->lc->siz:0);
		return res;
	}
	inline T getValue(int rk)
	{
		pNode i=rt;
		int sz;
		while(i)
		{
			sz=(i->lc?i->lc->siz:0);
			if(sz>=rk) i=i->lc;
			else if((rk-=sz+i->cnt)<=0)	return i->val;
			else i=i->rc;
		}
		return T();
	}
	inline T getPre(const T &v)
	{
		pNode i=rt;
		T res;
		while(i)
			if(i->val<v) res=i->val,i=i->rc;
			else i=i->lc;
		return res;
	}
	inline T getNxt(const T &v)
	{
		pNode i=rt;
		T res;
		while(i)
			if(v<i->val) res=i->val,i=i->lc;
			else i=i->rc;
		return res;
	}
#undef islc
};

int m;
RBTree<int> t;
int main()
{
	freopen("input.in","r",stdin);
	freopen("output.out","w",stdout);
	qin>>m;
	for(int i=1,opt,x;i<=m;i++)
	{
		qin>>opt>>x;
		if(opt==1) t.insert(x);
		else if(opt==2) t.remove(x);
		else if(opt==3) qout<<t.getRank(x)<<'\n';
		else if(opt==4) qout<<t.getValue(x)<<'\n';
		else if(opt==5) qout<<t.getPre(x)<<'\n';
		else qout<<t.getNxt(x)<<'\n';
	}
	cerr<<"pushup was used "<<pushup_O<<" times.\n";
	return 0;
}

一、插入数据

随机生成 10610^6 个整数并依次输出。

1. Splay1.\text{ Splay}

代码
pushup was used 26874418 times.
运行时间 1.65s.

2. Leafy-Splay2.\text{ Leafy-Splay}

代码
pushup was used 26877117 times.
运行时间 2.14s.

3. Treap3.\text{ Treap}

代码
pushup was used 58963790 times.
运行时间 1.71s.

4. RB-Tree4.\text{ RB-Tree}

代码
pushup was used 1163810 times.
运行时间 0.94s.

二、删除数据

随机生成 10610^6 个整数并插入,然后以随机顺序删除。

1. Splay1.\text{ Splay}

代码
pushup was used 64555683 times.
运行时间 3.33s.

2. Leafy-Splay2.\text{ Leafy-Splay}

代码
pushup was used 51840569 times.
运行时间 3.90s.

3. Treap3.\text{ Treap}

代码
pushup was used 120702538 times.
运行时间 3.28s.

4. RB-Tree4.\text{ RB-Tree}

代码
pushup was used 1940758 times.
运行时间 1.85s.