RT,蒟蒻写李超线段树时写了下面的一段代码(题目P4097)
void update(int &u,int l,int r,seg s)
{
if(u==0)
{
u=newnode();
}
cout<<"U "<<u<<" "<<l<<" "<<r<<" s="<<s.id<<endl;
int mid=(l+r)>>1;
if(s.l<=l&&r<=s.r)
{
...
}
if(s.l<=mid)
{
update(t[u].ls,l,mid,s);
cout<<"lson of "<<u<<" is "<<t[u].ls<<endl;
}
if(s.r>mid)
{
update(t[u].rs,mid+1,r,s);
cout<<"rson of "<<u<<" is "<<t[u].rs<<endl;
}
}
(因为第一个插入就会出锅,所以把没有进去过的 if
隐藏了
这是调试时的一段输入输出:
1 8 5 10 8
Decoded: 8 5 10 8
U 1 1 40000 s=1
U 2 1 20000 s=1
U 3 1 10000 s=1
U 4 1 5000 s=1
U 5 1 2500 s=1
U 6 1 1250 s=1
U 7 1 625 s=1
U 8 1 313 s=1
U 9 1 157 s=1
U 10 1 79 s=1
U 11 1 40 s=1
U 12 1 20 s=1
U 13 1 10 s=1
U 14 6 10 s=1
U 15 6 8 s=1
U 16 8 8 s=1
rson of 15 is 0 //!
lson of 14 is 15
U 17 9 10 s=1
rson of 14 is 17
rson of 13 is 14
lson of 12 is 13
lson of 11 is 12
lson of 10 is 11
lson of 9 is 10
lson of 8 is 9
lson of 7 is 0 //!
lson of 6 is 7
lson of 5 is 6
lson of 4 is 5
lson of 3 is 0 //!
lson of 2 is 3
lson of 1 is 0 //!
可以看到在标 ! 的几行中,回溯时并没有更改引用的值,导致出现错误。
求文为何会发生这种错误,和解决方法。