#include "bits/stdc++.h"
#include "tree.h"
using namespace std;
#define pb push_back
int n;
vector<int> p, w, order;
vector<vector<int> > ch;
void dfs(int node){
for(auto itr: ch[node]){
dfs(itr);
}
order.pb(node);
}
void init(vector<int> P, vector<int> W) {
p = P;
w = W;
n = (int)p.size();
ch.resize(n);
for(int i = 1; i < n; i++){
ch[P[i]].pb(i);
}
dfs(0);
}
long long query(int L, int R){
vector<long long> c(n), sub(n);
for(int i = 0; i < n; i++){
int node = order[i];
for(auto itr: ch[node]){
sub[node] += sub[itr];
}
if(sub[node] > R || sub[node] < L){
if(sub[node] > R) c[node] = sub[node] - R;
else c[node] = L - sub[node];
}
sub[node] += c[node];
}
long long ans = 0;
for(int i = 0; i < n; i++){
ans += c[i];
}
return ans;
}
# | 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... |