本蒟蒻写了一发 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
的引用并没有起到作用。
求问各位大佬为什么会发生这样的事情,以及如何改正。