题解 CF1137F 【Matches Are Not a Child's Play 】

· · 题解

显然在任意时刻最大的那个节点一定是在最后被删的。

于是我们考虑up操作带来的影响。

假设up了x,上一次(up前)最大的那个点是y。

我们发现最后删着删着一定只剩下了x到y路径上的这几个点。

然后从y开始一个一个往x那边删。

于是y\sim x这条链上的点移到了删除序列的末尾。

我们可以把y\sim x看成一个缩起来的点,我们发现其它点的相对位置不变。

接下来的思路感觉跟这题很像。

我们考虑用LCT维护这棵树,LCT中的每棵splay表示一段连续的删除区间。

开始时根当然是最大的。

样例大概是这样一幅图:

删除序列应该是2 4 3 1 5。

根当然就是最大的那个点。

由于一个splay是连续删的。所以我们给每个splay一个标号(具体到实现就是给splay上每个点染色,可以参考下面的代码),把它放在树状数组上,即在该splay的color上加上该splay的size。

inline void dfs(int now)
{
    col[now] = now, siz[now] = 1;
    for(int i = first[now]; i; i = e[i].nxt)
    {
        register int to = e[i].to;
        if(to != fa[now])
        {
            fa[to] = now; dfs(to);
            if(col[to] > col[now]) // 如果它后代有比它大的节点,那么一定会影响他它
            {
                col[now] = col[to];
                s[now][1] = to;
                siz[now] = siz[to] + 1;
            }
        }
    }
    tr.add(col[now], 1); // 树状数组在该color上加1
}

我们考虑查询,就是该点所在splay之前的所有点数加上该splay上所有该结点后面的点数。

大概就是树状数组.query(col[x] - 1) + splay.query(在x之前的节点个数)。

当然也可以写成:

inline int query(int x)
{
    splay(x);
    return tr.query(col[x]) - siz[s[x][0]];
}

最后考虑修改。

我们发现修改就是make_root,当然因为make_root里已经有access了。

至于轻重链切换的时候,只要在树状数组里把原有的splay删掉,新增的splay加上就可以了,具体可以见代码。

inline void access(int x)
{
    for(int y = 0; x; x = fa[y = x])
    {
        splay(x);
        s[x][1] = 0; // 假装它右边什么都没有,这样就可以把需要新增的splay整个提取出来了
        push_up(x);
        tr.add(col[x], -siz[x]);
        tr.add(nowcol, siz[x]);
        s[x][1] = y; // 然后在接上y
        push_up(x);
    }
}

inline void make_root(int x)
{
    nowcol++;
    access(x);
    splay(x);
    fan(x); // 翻转
    col[x] = nowcol; // 最后再把整个splay的color标记打上
}

代码

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 200005;
const int maxq = 200005;

int n, q;

struct Edge
{
    int to, nxt;
} e[maxn << 1];

int first[maxn];

inline void add_edge(int from, int to)
{
    static int cnt = 0;
    e[++cnt].nxt = first[from];
    e[cnt].to = to;
    first[from] = cnt;
    e[++cnt].nxt = first[to];
    e[cnt].to = from;
    first[to] = cnt;
}

#define lowbit(x) (x & -x)

struct BIT
{
    int xx[maxn << 1];

    inline void add(int pla, int x)
    {
        for(; pla <= n + q; pla += lowbit(pla))
            xx[pla] += x;
    }

    inline int query(int pla)
    {
        int ans = 0;
        for(; pla; pla -= lowbit(pla))
            ans += xx[pla];
        return ans;
    }
} tr;

int fa[maxn];
int s[maxn][2];
int siz[maxn];
int col[maxn];
int lzy[maxn];

inline void dfs(int now)
{
    col[now] = now;
    siz[now] = 1;
    for(int i = first[now]; i; i = e[i].nxt)
    {
        register int to = e[i].to;
        if(to != fa[now])
        {
            fa[to] = now;
            dfs(to);
            if(col[to] > col[now])
            {
                col[now] = col[to];
                s[now][1] = to;
                siz[now] = siz[to] + 1;
            }
        }
    }
    tr.add(col[now], 1);
}

inline bool nroot(int x)
{
    return s[fa[x]][0] == x || s[fa[x]][1] == x;
}

inline void push_up(int x)
{
    siz[x] = siz[s[x][0]] + siz[s[x][1]] + 1;
}

inline void fan(int x)
{
    lzy[x] ^= 1;
    swap(s[x][0], s[x][1]);
}

inline void push_down(int x)
{
    if(lzy[x])
    {
        lzy[x] = 0;
        if(s[x][0])
            fan(s[x][0]);
        if(s[x][1])
            fan(s[x][1]);
    }
    if(s[x][0])
        col[s[x][0]] = col[x];
    if(s[x][1])
        col[s[x][1]] = col[x];
}

inline void rotate(int x)
{
    int y = fa[x], z = fa[y];
    int d = s[y][1] == x, k = s[x][!d];
    if(nroot(y))
        s[z][s[z][1] == y] = x;
    fa[x] = z;
    s[x][!d] = y;
    fa[y] = x;
    s[y][d] = k;
    if(k)
        fa[k] = y;
    push_up(y);
    push_up(x);
}

int sta[maxn];

inline void splay(int x)
{
    int tx = x;
    int top = 0;
    sta[++top] = tx;
    while(nroot(tx))
        sta[++top] = (tx = fa[tx]);
    while(top)
        push_down(sta[top--]);
    while(nroot(x))
    {
        register int y = fa[x];
        if(nroot(y))
        {
            register int z = fa[y];
            rotate(((s[z][1] == y) ^ (s[y][1] == x)) ? x : y);
        }
        rotate(x);
    }
}

int nowcol;

inline void access(int x)
{
    for(int y = 0; x; x = fa[y = x])
    {
        splay(x);
        s[x][1] = 0;
        push_up(x);
        tr.add(col[x], -siz[x]);
        tr.add(nowcol, siz[x]);
        s[x][1] = y;
        push_up(x);
    }
}

inline void make_root(int x)
{
    nowcol++;
    access(x);
    splay(x);
    fan(x);
    col[x] = nowcol;
}

char opt[23];

inline int query(int x)
{
    splay(x);
    return tr.query(col[x]) - siz[s[x][0]];
}

int main()
{
    scanf("%d%d", &n, &q);
    nowcol = n;
    for(int i = 1, f, t; i < n; ++i)
    {
        scanf("%d%d", &f, &t);
        add_edge(f, t);
    }
    dfs(n);
    int _u, _v;
    for(int i = 1; i <= q; ++i)
    {
        scanf("%s", opt);
        if(opt[0] == 'u')
        {
            scanf("%d", &_v);
            make_root(_v);
        }
        else if(opt[0] == 'w')
        {
            scanf("%d", &_v);
            printf("%d\n", query(_v));
        }
        else
        {
            scanf("%d%d", &_u, &_v);
            int ans1 = query(_u);
            int ans2 = query(_v);
            printf("%d\n", ans1 < ans2 ? _u : _v);
        }
    }
    return 0;
}