Submission #1241849

#TimeUsernameProblemLanguageResultExecution timeMemory
1241849nibertTree (IOI24_tree)C++20
0 / 100
2094 ms22212 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAXN = 200005;

int N;
vector<int> tree[MAXN];
int weight[MAXN];
bool isLeaf[MAXN];
int leafCount[MAXN];

void dfs_leafcount(int u) {
    isLeaf[u] = true;
    leafCount[u] = 0;
    for (int v : tree[u]) {
        isLeaf[u] = false;
        dfs_leafcount(v);
        leafCount[u] += leafCount[v];
    }
    if (isLeaf[u]) leafCount[u] = 1;
}

ll compute_cost(int u, int L, int R) {
    int k = leafCount[u];
    if (isLeaf[u]) return 1LL * L * weight[u];

    ll cost = 0;
    if (1LL * k * L > R) {
        cost += (1LL * k * L - R) * weight[u];
    }
    for (int v : tree[u]) {
        cost += compute_cost(v, L, R);
    }
    return cost;
}

void init(vector<int> P, vector<int> W) {
    N = P.size();
    for (int i = 0; i < N; ++i) {
        tree[i].clear();
        isLeaf[i] = false;
    }

    for (int i = 1; i < N; ++i) {
        tree[P[i]].push_back(i);
    }

    for (int i = 0; i < N; ++i) weight[i] = W[i];

    dfs_leafcount(0);
}

ll query(int L, int R) {
    return compute_cost(0, L, R);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...