#include #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define rep2(i, l, r) for (int i = (l); i < (r); i++) #define rep2r(i, l, r) for (int i = (r) - 1; i >= (l); i--) #define range(a) a.begin(), a.end() using namespace std; using ll = long long; constexpr int U = 1 << 17; ll dat[U * 2]; ll lazy[U * 2]; ll cnt[U * 2]; void apply(int k, ll v) { if (v == -1) return; dat[k] = v * cnt[k]; lazy[k] = v; } void push(int k) { apply(k * 2 + 0, lazy[k]); apply(k * 2 + 1, lazy[k]); lazy[k] = -1; } void update(int a, int b, ll v, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return; if (a <= l && r <= b) { apply(k, v); return; } push(k); update(a, b, v, k * 2 + 0, l, (l + r) / 2); update(a, b, v, k * 2 + 1, (l + r) / 2, r); dat[k] = dat[k * 2] + dat[k * 2 + 1]; } ll query(int a, int b, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return dat[k]; push(k); ll vl = query(a, b, k * 2 + 0, l, (l + r) / 2); ll vr = query(a, b, k * 2 + 1, (l + r) / 2, r); return vl + vr; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int N; cin >> N; vector> G(N); rep(i, N - 1) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } int k = 0; vector par(N, -1); vector L(N); vector R(N); vector L2(N, 1e9); vector R2(N, -1e9); vector label(N); queue q; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); L[u] = k + 1; for (int v : G[u]) if (v != par[u]) { label[v] = ++k; par[v] = u; q.push(v); } R[u] = k + 1; } rep(u, N) for (int v : G[u]) if (v != par[u]) { L2[u] = min(L2[u], L[v]); R2[u] = max(R2[u], R[v]); }; vector A(N); rep(i, N) cin >> A[i]; rep(i, N) { dat[label[i] + U] = A[i]; cnt[i + U] = 1; } rep(i, U * 2) lazy[i] = -1; for (int i = U - 1; i >= 1; i--) { dat[i] = dat[i * 2] + dat[i * 2 + 1]; cnt[i] = cnt[i * 2] + cnt[i * 2 + 1]; } int Q; cin >> Q; while (Q--) { int x; cin >> x; ll s = 0; auto f = [&](int l, int r) { if (l <= r) { s += query(l, r); update(l, r, 0); } }; f(L[x], R[x]); f(L2[x], R2[x]); if (par[x] != -1) { int p = par[x]; f(label[p], label[p] + 1); f(L[p], R[p]); if (par[p] != -1) { int pp = par[p]; f(label[pp], label[pp] + 1); } } update(label[x], label[x] + 1, s); cout << s << '\n'; } }