关于 Treap
  • 板块学术版
  • 楼主Zxx200611
  • 当前回复0
  • 已保存回复0
  • 发布时间2020/8/18 19:21
  • 上次更新2023/11/6 20:00:18
查看原帖
关于 Treap
175590
Zxx200611楼主2020/8/18 19:21

本蒟蒻写了一发 Treap,其中有两个函数长这样(请注意输出的调试信息):

int create(int val)
{
	t.push_back((node){val,__random(),1,1,0,0});
	return t.size()-1;
}
void insert(int &u,int val)
{
	cout<<"Insert "<<u<<" "<<val<<" thval = "<<t[u].val<<endl;
	if(u==0)
	{
		u=create(val);
		cout<<"Create "<<u<<endl;
		return;
	}
	if(t[u].val==val)
	{
		cout<<"Plus "<<u<<endl;
		t[u].cnt++;
	}
	if(t[u].val>val)
	{
		cout<<"Go left"<<endl;
		insert(t[u].lson,val);
		cout<<"Getl "<<t[u].lson<<endl;
		if(t[t[u].lson].rnd>t[u].rnd)
			right_rotate(u);
	}
	else if(t[u].val<val)
	{
		cout<<"Go right"<<endl;
		insert(t[u].rson,val);
		cout<<"Getr "<<t[u].rson<<endl;
		if(t[t[u].rson].rnd>t[u].rnd)
			left_rotate(u);
	}
	push_up(u);
	return;
}

然后 WA 了。
下了一组数据,跑了一遍,其中输出有一段长这样:

Insert 3 317721 thval = 106465
Go right
Insert 0 317721 thval = 0
Create 4
Getr 0

很明显,第五行应为 Getr 4
也就是说,insert 函数参数中对 u 的引用并没有起到作用。

求问各位大佬为什么会发生这样的事情,以及如何改正。

2020/8/18 19:21
加载中...