萌新树剖求助
查看原帖
萌新树剖求助
895477
LuckyDay1477楼主2023/5/18 20:54
#include<iostream>
#include<algorithm>
const int sz = 1e5 + 10;
int w[sz], rnk[sz];
struct ST {
    struct node {
        int lpos, rpos, num;
        node operator+(const node &a) const {
            return node{lpos, a.rpos, num + a.num - (rpos == a.lpos)};
        }
    } tree[sz << 2];
    int cov[sz << 2];
    bool iscov[sz << 2];
    void pushdown(int p, int ln, int rn) {
        if (iscov[p]) {
            cov[p << 1] = cov[p << 1 | 1] = cov[p];
            iscov[p << 1] = iscov[p << 1 | 1] = true;
            tree[p << 1] = node{cov[p], cov[p], 1};
            tree[p << 1 | 1] = node{cov[p], cov[p], 1};
            iscov[p] = false;
        }
    }
    void build(int p, int ln, int rn) {
        if (ln == rn) 
            return tree[p] = node{w[rnk[ln]], w[rnk[ln]], 1}, void();
        int mid = ln + rn >> 1;
        build(p << 1, ln, mid);
        build(p << 1 | 1, mid + 1, rn);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    void assign(int p, int ln, int rn, int l, int r, int val) {
        if (ln >= l && rn <= r) {
            tree[p] = node{val, val, 1};
            cov[p] = val, iscov[p] = true;
            return;
        }
        if (ln > r || rn < l) return;
        int mid = ln + rn >> 1;
        pushdown(p, ln, rn);
        assign(p << 1, ln, mid, l, r, val);
        assign(p << 1 | 1, mid + 1, rn, l, r, val);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    node query(int p, int ln, int rn, int l, int r) {
        if (ln >= l && rn <= r) return tree[p];
        node res = node{0, 0, 0};
        int mid = ln + rn >> 1;
        pushdown(p, ln, rn);
        if (l <= mid) res = res + query(p << 1, ln, mid, l, r);
        if (r > mid) res = res + query(p << 1 | 1, mid + 1, rn, l, r);
        return res;
    }
    int getval(int p, int ln, int rn, int pos) {
        if (ln == rn) return tree[p].lpos;
        int mid = ln + rn >> 1;
        pushdown(p, ln, rn);
        if (pos <= mid) return getval(p << 1, ln, mid, pos);
        else return getval(p << 1 | 1, mid + 1, rn, pos);
    }
} st;
struct edge {
    int nxt, to;
} graph[sz << 1];
int hpp, head[sz];
void addEdge(int from, int to) {
    graph[++hpp] = edge{head[from], to};
    head[from] = hpp;
}
int dfn[sz], dpp, dep[sz], hson[sz], top[sz], fa[sz], size[sz];
void buildDFS(int u, int fau) {
    dep[u] = dep[fau] + 1, size[u] = 1, fa[u] = fau;
    for (int p = head[u]; p; p = graph[p].nxt) {
        int v = graph[p].to;
        if (v == fau) continue;
        buildDFS(v, u);
        size[u] += size[v];
        if (size[v] > size[hson[u]]) hson[u] = v;
    }
}
void chainDFS(int u, int t) {
    top[u] = t, dfn[u] = ++dpp, rnk[dpp] = u;
    if (!hson[u]) return;
    chainDFS(hson[u], t);
    for (int p = head[u]; p; p = graph[p].nxt) {
        int v = graph[p].to;
        if (v == hson[u] || v == fa[u]) continue;
        chainDFS(v, v);
    }
}
int n, q;
void assign(int u, int v, int val) {
    while (top[u] != top[v]) {
        if (dep[u] < dep[v]) std::swap(u, v);
        st.assign(1, 1, n, dfn[top[u]], dfn[u], val);
        u = fa[top[u]];
    }
    if (dep[u] > dep[v]) std::swap(u, v);
    st.assign(1, 1, n, dfn[u], dfn[v], val);
}
int query(int u, int v) {
    int res = 0;
    while (top[u] != top[v]) {
        if (dep[u] < dep[v]) std::swap(u, v);
        res += st.query(1, 1, n, dfn[top[u]], dfn[u]).num;
        if (st.getval(1, 1, n, dfn[top[u]]) == st.getval(1, 1, n, dfn[fa[top[u]]])) res--;
        u = fa[top[u]];
    }
    if (dfn[u] > dfn[v]) std::swap(u, v);
    res += st.query(1, 1, n, dfn[u], dfn[v]).num;
    return res;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> q;
    for (int i = 1; i <= n; i++) std::cin >> w[i];
    for (int i = 1, u, v; i < n; i++) 
        std::cin >> u >> v, addEdge(u, v), addEdge(v, u);
    buildDFS(1, 0);
    chainDFS(1, 1);
    st.build(1, 1, n);
    while (q--) {
        char op;
        int u, v, c;
        std::cin >> op >> u >> v;
        if (op == 'Q') std::cout << query(u, v) << "\n";
        else std::cin >> c, assign(u, v, c);
    }
    return 0;
}
2023/5/18 20:54
加载中...