#include "tree.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
vector<int> W;
vector<vector<int>> adj;
void init(std::vector<int> P, std::vector<int> _W) {
N = (int)P.size();
W = _W;
adj.resize(N);
for (int i = 1; i < N; ++i) {
adj[P[i]].push_back(i);
adj[i].push_back(P[i]);
}
}
long long query(int L, int R) {
// cerr << "QUERY WITH " << L << " " << R << endl;
ll res = 0;
auto dfs = [&](auto& dfs, int v, int p) -> ll {
ll now = 0;
for (auto& u : adj[v]) {
if (u == p) continue;
now += dfs(dfs, u, v);
}
// cerr << "Vertice " << v << " , with total weight = " << now << endl;
if (now < L) {
res += (L - now) * W[v];
now = L;
} else if (now > R) {
res += (now - R) * W[v];
now = R;
}
return now;
};
dfs(dfs, 0, -1);
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |