【警示后人】正确的右偏树&错误的右偏树
查看原帖
【警示后人】正确的右偏树&错误的右偏树
220799
入户功夫ChaosNet楼主2021/10/19 10:13

RT,两种写法中的其中一种会 MLE40ptsMLE 40pts ,另一种则能通过此题。具体原因仍待商榷

错误:

struct rightist_tree{
	int siz;
	struct nod{
		int a,id;
		int fa,ls,rs;
		int dis;
		const bool operator <(const nod &tmp)const{if(a!=tmp.a)return a>tmp.a;return id<tmp.id;}
		friend ostream& operator <<(ostream &out,const nod &a){out<<a.a<<' '<<a.id<<' '<<a.fa<<' '<<a.ls<<' '<<a.rs<<' '<<a.dis<<' ';return out;}
		nod(){}
		nod(int a_,int id_){a=a_,fa=id=id_;}
	}p[100005];
	friend ostream& operator <<(ostream &out,const rightist_tree &a)
	{
		for(int i=1;i<=a.siz;i++) out<<a.p[i]<<'\n';
		return out;
	}
	rightist_tree(){}
	rightist_tree(int siz_){siz=siz_;}
	
	int find(int x)
	{
		if(p[x].fa==x) return x;
		return p[x].fa=find(p[x].fa);
	}
	int merge(int x,int y)
	{
		if(!x||!y) return x|y;
		if(p[x]<p[y]) swap(x,y);
		p[x].fa=y;p[y].ls=merge(p[y].ls,x);//diffrent here
		if(p[p[y].ls].dis>p[p[y].rs].dis) swap(p[y].ls,p[y].rs);
		p[y].dis=p[p[y].ls].dis+1;
		return y;
	}
	inline void pop(int x)
	{
		p[x].a/=2;
		int rt=p[p[x].ls].fa=p[p[x].rs].fa=merge(p[x].ls,p[x].rs);p[x].ls=p[x].rs=0;
		p[rt].fa=p[x].fa=merge(x,rt);
	}
}RT;

正确:

struct rightist_tree{
	int siz;
	struct nod{
		int a,id;
		int fa,ls,rs;
		int dis;
		const bool operator <(const nod &tmp)const{if(a!=tmp.a)return a>tmp.a;return id<tmp.id;}
		friend ostream& operator <<(ostream &out,const nod &a){out<<a.a<<' '<<a.id<<' '<<a.fa<<' '<<a.ls<<' '<<a.rs<<' '<<a.dis<<' ';return out;}
		nod(){}
		nod(int a_,int id_){a=a_,fa=id=id_;}
	}p[100005];
	friend ostream& operator <<(ostream &out,const rightist_tree &a)
	{
		for(int i=1;i<=a.siz;i++) out<<a.p[i]<<'\n';
		return out;
	}
	rightist_tree(){}
	rightist_tree(int siz_){siz=siz_;memset(p,0,sizeof p);}
	
	int find(int x)
	{
		if(p[x].fa==x) return x;
		return p[x].fa=find(p[x].fa);
	}
	int merge(int x,int y)
	{
		if(!x||!y) return x|y;
		if(p[x]<p[y]) swap(x,y);
		p[y].ls=merge(p[y].ls,x);p[p[y].ls].fa=y;//diffrent here
		if(p[p[y].ls].dis>p[p[y].rs].dis) swap(p[y].ls,p[y].rs);
		p[y].dis=p[p[y].ls].dis+1;
		return y;
	}
	inline void pop(int x)
	{
		p[x].a/=2;
		int rt=p[p[x].ls].fa=p[p[x].rs].fa=merge(p[x].ls,p[x].rs);p[x].ls=p[x].rs=0;
		p[rt].fa=p[x].fa=merge(x,rt);
	}
}RT;
2021/10/19 10:13
加载中...