Submission #1123724

#TimeUsernameProblemLanguageResultExecution timeMemory
1123724math_rabbit_1028Tree (IOI24_tree)C++20
0 / 100
2095 ms23188 KiB
#include "tree.h"
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

int n;
int par[202020];
vector<int> child[202020];
ll w[202020];

void init(vector<int> P, vector<int> W) {
    n = (int)P.size();
    for (int i = 0; i < n; i++) {
        par[i] = P[i];
        child[par[i]].push_back(i);
    }
    for (int i = 0; i < n; i++) w[i] = W[i];
}

pll range[202020];
ll res[202020];

ll minabs(ll l, ll r) {
    if (l <= 0 && r >= 0) return 0;
    if (r < 0) return r;
    if (l > 0) return l;
}

void update(int v) {
    ll mns = 0, mxs = 0;
    for (int u : child[v]) {
        mns += range[u].first;
        mxs += range[u].second;
    }
    for (int u : child[v]) {
        range[u].first = max(range[u].first, range[v].first + range[u].second - mxs);
        range[u].second = min(range[u].second, range[v].second + range[u].first - mns);
    }
}

long long query(int L, int R) {
    ll ans = 0;
    priority_queue<pll> pq;
    for (int i = 0; i < n; i++) {
        ll m = child[i].size();
        range[i] = {L-m*R, R-m*L};
        pq.push({w[i], i});
        res[i] = 1e18;
    }

    while (!pq.empty()) {
        int v = pq.top().second;
        pq.pop();
        res[v] = minabs(range[v].first, range[v].second);
        pll temp = {range[v].second - res[v], res[v] - range[v].first};
        range[v] = {res[v], res[v]};
        ans += w[v] * abs(res[v]);
        
        update(v);
        int p = par[v];
        if (p < 0) continue;
        if (res[p] == 1e18) {
            range[p].first += temp.first;
            range[p].second -= temp.second;
        }
        update(p);
    }
    return ans;
}

Compilation message (stderr)

tree.cpp: In function 'll minabs(ll, ll)':
tree.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
   28 | }
      | ^
#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...