「WC2013」糖果公园

「WC2013」糖果公园

传送门
树上带修莫队板子题。
看懂题意后就是板子题了。
参考代码:

#include <algorithm>
#include <cstdio>
#include <cmath>
#define rg register
#define int long long 
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using std ::sort; using std ::swap;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 100010;

int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }, head[u] = tot; }

int n, m, q, v[_], w[_], c[_];
int gap, pos[_ << 1], cnt[_], ans, res[_];
int len, vis[_], ord[_ << 1], fir[_], las[_];
int dep[_], son[_], siz[_], top[_], fa[_];
int Cnum; struct Change { int x, y; } C[_];
int Qnum; struct Query { int l, r, lca, t, id; } Q[_];
inline bool cmp(const Query& x, const Query& y)
{ return pos[x.l] ^ pos[y.l] ? pos[x.l] < pos[y.l] : (pos[x.r] ^ pos[y.r] ? pos[x.r] < pos[y.r] : x.t < y.t); }

inline void dfs(int u, int f) {
    dep[u] = dep[f] + 1, siz[u] = 1, fa[u] = f;
    ord[fir[u] = ++len] = u;
    for (rg int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].ver; if (v == f) continue ;
    dfs(v, u), siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
    }
    ord[las[u] = ++len] = u;
}

inline void dfs(int u, int f, int topf) {
    top[u] = topf;
    if (son[u]) dfs(son[u], u, topf);
    for (rg int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].ver; if (v == f || v == son[u]) continue ;
    dfs(v, u, v);
    }
}

inline int LCA(int x, int y) {
    int fx = top[x], fy = top[y];
    while (fx != fy) {
    if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
    x = fa[fx], fx = top[x];
    }
    return dep[x] < dep[y] ? x : y;
}

inline void calc(int x) { vis[x] ? ans -= 1ll * w[cnt[c[x]]--] * v[c[x]] : ans += 1ll * w[++cnt[c[x]]] * v[c[x]], vis[x] ^= 1; }

inline void work(int tim) {
    if (vis[C[tim].x])
    ans += 1ll * w[++cnt[C[tim].y]] * v[C[tim].y] - 1ll * w[cnt[c[C[tim].x]]--] * v[c[C[tim].x]];
    swap(c[C[tim].x], C[tim].y);
}

signed main() {
    read(n), read(m), read(q);
    for (rg int i = 1; i <= m; ++i) read(v[i]);
    for (rg int i = 1; i <= n; ++i) read(w[i]);
    for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
    for (rg int i = 1; i <= n; ++i) read(c[i]);
    dfs(1, 0), dfs(1, 0, 1), gap = (long long) pow(len, 2.0 / 3.0);
    for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / gap + 1;
    for (rg int type, x, y, i = 1; i <= q; ++i) {
        read(type), read(x), read(y);
        if (type == 0) C[++Cnum] = (Change) { x, y };
        else {
            if (fir[x] > fir[y]) swap(x, y);
            int lca = LCA(x, y);
            if (x == lca)
                Q[++Qnum] = (Query) { fir[x], fir[y], 0, Cnum, Qnum };
            else
                Q[++Qnum] = (Query) { las[x], fir[y], lca, Cnum, Qnum };
        }
    }
    sort(Q + 1, Q + Qnum + 1, cmp);
    for (rg int l = 1, r = 0, tim = 0, i = 1; i <= Qnum; ++i) {
        while (l > Q[i].l) calc(ord[--l]);
        while (l < Q[i].l) calc(ord[l++]);
        while (r < Q[i].r) calc(ord[++r]);
        while (r > Q[i].r) calc(ord[r--]);
        while (tim < Q[i].t) work(++tim);
        while (tim > Q[i].t) work(tim--);
        if (Q[i].lca) calc(Q[i].lca);
        res[Q[i].id] = ans;
        if (Q[i].lca) calc(Q[i].lca);
    }
    for (rg int i = 1; i <= Qnum; ++i) printf("%lld\n", res[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zsbzsb/p/12231731.html
今日推荐