跳到正文

题解

P2486

System32约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年2月13日
收录
2026年2月13日
更新
2026年11月8日
篇幅
约 14 分钟
题目
P2486

版本

  1. v1当前

我不喜欢线段树,所以写了分块。

考虑树剖后对于每条重链分块,这样直接做是 O(nnlog⁡n)O(n\sqrt n \log n) 的,理论能过。

交上去发现被卡了,考虑优化常数。

发现对于大部分在链上的操作和查询都只在一端有散块,因此在操作和查询时可以特判两端是否为整块,再微调一下块长就能过了。

Code

C++
const int B = 350;
vector<int> G[100010];
int a[100010], dfn[100010], id[100010], top[100010], dcnt, son[100010];
int sz[100010], root[100010], tcnt, f[100010], lst[100010], mp[100010];
int b[100010], tag[100010];

struct node {
    bool gc;
    int lc, rc, dl;
    int l, r;
} bs[100010];

node merge(node a, node b) {
    if (a.dl == -1) return b;
    node res;
    res.gc = a.gc && b.gc && a.rc == b.lc;
    res.lc = a.lc;
    res.rc = b.rc;
    res.dl = a.dl + b.dl - (a.rc == b.lc);
    res.l = a.l;
    res.r = b.r;
    return res;
}

node ops(node a) {
    node res;
    res.gc = a.gc;
    res.lc = a.rc;
    res.rc = a.lc;
    res.dl = a.dl;
    res.l = a.r;
    res.r = a.l;
    return res;
}

void dfs(int u, int fa) {
    sz[u] = 1;
    f[u] = fa;
    Fo(v, G[u]) {
        if (v == fa) continue;
        dfs(v, u);
        sz[u] += sz[v];
        if (sz[v] > sz[son[u]]) son[u] = v;
    }
}

int bcnt;

void build(int l, int r) {
    for (int i = l; i <= r; i += B) {
        bcnt++;
        bs[bcnt].l = i;
        bs[bcnt].r = min(i + B - 1, r);
        bs[bcnt].dl = 1;
        bs[bcnt] = {0, -1, 0, -1, bs[bcnt].l, bs[bcnt].r};
        int ls = -1;
        For(i, bs[bcnt].l, bs[bcnt].r) {
            if (b[i] != ls) {
                bs[bcnt].dl++;
                if (bs[bcnt].lc == -1) {
                    bs[bcnt].lc = b[i];
                }
                ls = b[i];
            }
        }
        bs[bcnt].rc = ls;
        bs[bcnt].dl++;
        if (bs[bcnt].dl == 1) {
            bs[bcnt].gc = true;
        }
    }
}

void xfs(int u, int fa, int tp) {
    dfn[u] = ++dcnt;
    top[u] = tp;
    lst[u] = u;
    mp[dfn[u]] = u;
    if (son[u]) {
        xfs(son[u], u, tp);
        lst[u] = lst[son[u]];
    } else
        build(dfn[top[u]], dcnt);
    Fo(v, G[u]) {
        if (v == fa || v == son[u]) continue;
        xfs(v, u, v);
    }
}

int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (dfn[top[u]] > dfn[top[v]]) swap(u, v);
        // sw << u << " " << top[v] << endl;
        v = f[top[v]];
        // sw << u << " " << v << endl;
        // exit(0);
    }
    if (dfn[u] > dfn[v]) swap(u, v);
    return u;
}

void rebuild(int p) {
    if (tag[p]) {
        For(i, bs[p].l, bs[p].r) b[i] = tag[p];
        tag[p] = 0;
    }
    int ls = -1;
    bs[p] = {0, -1, 0, -1, bs[p].l, bs[p].r};
    For(i, bs[p].l, bs[p].r) {
        if (b[i] != ls) {
            bs[p].dl++;
            if (bs[p].lc == -1) {
                bs[p].lc = b[i];
            }
            ls = b[i];
        }
    }
    bs[p].rc = ls;
    bs[p].dl++;
    if (bs[p].dl == 1) {
        bs[p].gc = true;
    }
}

void update(int l, int r, int v) {
    int L = id[l], R = id[r];
    if (L == R) {
        if (tag[L]) {
            rebuild(L);
        }
        For(i, l, r) { b[i] = v; }
        rebuild(L);
        return;
    }
    if (tag[L]) {
        rebuild(L);
    }
    if (tag[R]) {
        rebuild(R);
    }
    For(i, l, bs[L].r) b[i] = v;
    rebuild(L);
    if (r != bs[R].r) {
        For(i, bs[R].l, r) b[i] = v;
        rebuild(R);
    } else {
        R++;
    }
    For(i, L + 1, R - 1) {
        bs[i] = {true, v, v, 1, bs[i].l, bs[i].r};
        tag[i] = v;
    }
}

void modify(int u, int v, int val) {
    int lc = lca(u, v);
    while (top[u] != top[lc]) {
        update(dfn[top[u]], dfn[u], val);
        u = f[top[u]];
    }
    update(dfn[lc], dfn[u], val);
    while (top[v] != top[lc]) {
        update(dfn[top[v]], dfn[v], val);
        v = f[top[v]];
    }
    update(dfn[lc], dfn[v], val);
}

node query(int l, int r) {
    int L = id[l], R = id[r];
    if (L == R) {
        if (tag[L]) {
            rebuild(L);
        }
        node res = {0, -1, 0, -1, bs[L].l, bs[L].r};
        int ls = -1;
        For(i, l, r) {
            if (b[i] != ls) {
                res.dl++;
                if (res.lc == -1) {
                    res.lc = b[i];
                }
                ls = b[i];
            }
        }
        res.rc = ls;
        res.dl++;
        if (res.dl == 1) {
            res.gc = true;
        }
        return res;
    }
    if (tag[L]) {
        rebuild(L);
    }
    if (tag[R]) {
        rebuild(R);
    }
    node res = {0, -1, 0, -1, 0, 0};
    int ls = -1;
    For(i, l, bs[L].r) {
        if (b[i] != ls) {
            res.dl++;
            if (res.lc == -1) {
                res.lc = b[i];
            }
            ls = b[i];
        }
    }
    res.rc = ls;
    res.dl++;
    if (res.dl == 1) {
        res.gc = true;
    }
    if (r != bs[R].r) {
        For(i, L + 1, R - 1) res = merge(res, bs[i]);
        node tmp = {0, -1, 0, -1, bs[R].l, bs[R].r};
        ls = -1;
        For(i, bs[R].l, r) {
            if (b[i] != ls) {
                tmp.dl++;
                if (tmp.lc == -1) {
                    tmp.lc = b[i];
                }
                ls = b[i];
            }
        }
        tmp.rc = ls;
        tmp.dl++;
        if (tmp.dl == 1) {
            tmp.gc = true;
        }
        res = merge(res, tmp);
    } else {
        For(i, L + 1, R) res = merge(res, bs[i]);
    }
    return res;
}

node get(int u, int v) {
    int lc = lca(u, v);
    node res;
    res.dl = -1;
    while (top[u] != top[lc]) {
        res = merge(res, ops(query(dfn[top[u]], dfn[u])));
        u = f[top[u]];
    }
    // sw << "xxx" << res.rc << endl;
    res = merge(res, ops(query(dfn[lc], dfn[u])));
    // sw << "xxx" << res.dl << endl;
    // sw << "xxx" << res.rc << endl;
    // sw << "xx" << bs[5].lc << " " << bs[6].lc << endl;
    // sw << "x" << b[5] << endl;
    stack<pair<int, int>> st;
    while (top[v] != top[lc]) {
        st.push({dfn[top[v]], dfn[v]});
        v = f[top[v]];
    }
    st.push({dfn[lc], dfn[v]});
    while (!st.empty()) {
        res = merge(res, query(st.top().fi, st.top().se));
        // sw << "xxx" << st.top().fi << " " << st.top().se << endl;
        // sw << "xxxx" << query(st.top().fi, st.top().se).lc << " " << query(st.top().fi, st.top().se).rc
        //    << endl;
        st.pop();
    }
    return res;
}

int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    // FISI sr("data.in");
    // inter();
    int n, q;
    sr >> n >> q;
    For(i, 1, n) sr >> a[i];
    For(i, 1, n - 1) {
        int u, v;
        sr >> u >> v;
        G[u].pb(v);
        G[v].pb(u);
    }
    dfs(1, 0);
    xfs(1, 0, 1);
    For(i, 1, n) b[i] = a[mp[i]];
    // sw << dfn[6] << endl;
    // sw << son[1] << endl;
    // sw << son[9] << endl;
    // sw << a[5] << " " << a[9] << " " << a[12] << " " << a[4] << " " << a[8] << " " << a[14] << " " << a[1]
    //    << endl;
    // for (int i = 1; i <= 7; i++) {
    //     sw << bs[i].lc << endl;
    // }
    while (q--) {
        string str;
        sr >> str;
        if (str == "Q") {
            int u, v;
            sr >> u >> v;
            sw << get(u, v).dl << endl;
        } else {
            int u, v, val;
            sr >> u >> v >> val;
            modify(u, v, val);
        }
    }
    return 0;
}
原文