Submission #1242030

#TimeUsernameProblemLanguageResultExecution timeMemory
1242030SalihSahinTree (IOI24_tree)C++20
0 / 100
2089 ms23292 KiB
#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 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...