#include "tree.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<int> p, w;
vector<int> v[2005];
vector<pair<ll, ll>> hn[2005];
ll l, r;
ll terev = 0;
ll ans =0;
ll Dfs(int g)
{
if(v[g].size() == 0)
{
ans += w[g] * l;
return l;
}
ll gg = 0, gg1 = r - l;
hn[g].push_back({w[g], 1e9});
for(auto to: v[g])
{
gg+=Dfs(to);
for (int i = 0; i < hn[to].size(); i++)
{
hn[g].push_back(hn[to][i]);
gg1 += hn[g].back().second;
}
}
ll anc = -1, blbl = -1;
sort(hn[g].begin(), hn[g].end());
reverse(hn[g].begin(), hn[g].end());
while (gg > r)
{
ll han = min(gg - r, hn[g].back().second);
ans += han * hn[g].back().first;
gg1 -= han;
gg -= han;
if(hn[g].back().second == 0)
hn[g].pop_back();
}
reverse(hn[g].begin(), hn[g].end());
while (gg1 > gg - l)
{
anc = hn[g].back().first;
blbl = hn[g].back().second;
gg1 -= blbl;
hn[g].pop_back();
}
if(gg - l - gg1 > 0)
{
hn[g].push_back({anc, min(gg - l - gg1, blbl)});//
}
return gg;
}
void init(vector<int> P, vector<int> W) {
w = W;
p = P;
n = (int)p.size();
for (int i = 1; i < n; i++)
{
v[p[i]].push_back(i);
}
}
long long query(int L, int R) {
l = L;
r = R;
for (int i = 0; i < n; i++)
{
hn[i].clear();
}
ans = 0;
Dfs(0);
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... |