这个是对的:
void ins(int x)
{
if(!root)
{
root = ++ tot;
cnt[tot] = 1;
sz[tot] = 1;
val[tot] = x;
return;
}
int cur = root, f = 0;
while(1)
{
if(val[cur] == x)
{
cnt[cur] ++;
sz[cur] ++;
pushup(f);
splay(cur);
return;
}
f = cur;
cur = son[cur][val[cur] < x];
if(!cur)
{
cnt[++ tot] = 1;
val[tot] = x;
sz[tot] = 1;
sz[f] += 1;
fa[tot] = f;
son[f][val[f] < x] = tot;
splay(tot);
return;
}
}
}
这个会寄:
void ins(int x)
{
if(!root)
{
root = ++ tot;
cnt[tot] = 1;
sz[tot] = 1;
val[tot] = x;
return;
}
int cur = root, f = 0;
while(1)
{
if(val[cur] == x)
{
cnt[cur] ++;
sz[cur] ++;
sz[f] ++;
splay(cur);
return;
}
f = cur;
cur = son[cur][val[cur] < x];
if(!cur)
{
cnt[++ tot] = 1;
val[tot] = x;
sz[tot] = 1;
sz[f] += 1;
fa[tot] = f;
son[f][val[f] < x] = tot;
splay(tot);
return;
}
}
}
它俩的区别在于:
if(val[cur] == x)
{
cnt[cur] ++;
sz[cur] ++;
正确的:
pushup(f);
错误的:
sz[f] ++;
splay(cur);
return;
}
但是我感觉这两个语句应该是等效的啊,为什么会出问题呢