社区讨论

问一个问题

SP16580QTREE7 - Query on a tree VII参与者 3已保存回复 9

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
9 条
当前快照
1 份
快照标识符
@mi86ls5x
此快照首次捕获于
2025/11/21 09:28
4 个月前
此快照最后确认于
2025/11/21 09:28
4 个月前
查看原帖
RT,这题还要维护虚儿子的 max 值,我之前在 rotate 中 pushup 两次,后来在 rotate 中 pushup 一次,splay 中 pushup 一次就 AC 了,请问是为什么?
AC:
CPP
inline bool nrt(int x) {return ch[fa[x]][0]==x||ch[fa[x]][1]==x;}
inline void rotate(int x)
{
    int y=fa[x],z=fa[y],k=(ch[y][1]==x),u=ch[x][k^1];
    if(nrt(y)) ch[z][ch[z][1]==y]=x;
    ch[y][k]=u;ch[x][k^1]=y;
    if(u) fa[u]=y;fa[y]=x;fa[x]=z;
    pushup(y);
}
inline void splay(int x)
{
    int y,z;
    while(nrt(x))
    {
        y=fa[x],z=fa[y];
        if(nrt(y)) rotate((ch[y][1]==x)^(ch[z][1]==y)?x:y);
        rotate(x);
    }
    pushup(x);
}
WA:
CPP
inline bool nrt(int x) {return ch[fa[x]][0]==x||ch[fa[x]][1]==x;}
inline void rotate(int x)
{
    int y=fa[x],z=fa[y],k=(ch[y][1]==x),u=ch[x][k^1];
    if(nrt(y)) ch[z][ch[z][1]==y]=x;
    ch[y][k]=u;ch[x][k^1]=y;
    if(u) fa[u]=y;fa[y]=x;fa[x]=z;
    pushup(y);pushup(x);
}
inline void splay(int x)
{
    int y,z;
    while(nrt(x))
    {
        y=fa[x],z=fa[y];
        if(nrt(y)) rotate((ch[y][1]==x)^(ch[z][1]==y)?x:y);
        rotate(x);
    }
}

回复

9 条回复,欢迎继续交流。

正在加载回复...