This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 100000;
const int K_MAX = 100000;
const int BITS = 17;
int N, M, K;
int parent[N_MAX + 2];
vector <int> sons[N_MAX + 2];
int ripeTime[N_MAX + 2];
int juice[N_MAX + 2];
struct Fenwick {
map <int, ll> mp;
multiset <pair <int, int>> upds;
void update (int pos, int val) {
upds.insert(make_pair(pos, val));
for (int i = pos; i <= K; i += i & -i) {
mp[i] += val;
}
}
ll query (int pos) {
ll val = 0;
for (int i = pos; i >= 1; i -= i & -i) {
if (mp.find(i) != mp.end()) {
val += mp[i];
}
}
return val;
}
void maxify (int pos, int val) {
update(pos, val);
multiset <pair <int, int>> :: iterator it = upds.lower_bound(make_pair(pos + 1, 0));
while (val > 0 && it != upds.end()) {
int itpos = it->first, itval = it->second;
if (itval <= val) {
update(itpos, -itval);
upds.erase(upds.find(make_pair(itpos, -itval)));
val -= itval;
it++;
upds.erase(upds.find(make_pair(itpos, itval)));
} else {
update(itpos, -val);
val = 0;
}
}
}
};
Fenwick Fen[N_MAX + 2];
void dfs (int u) {
int heavy = -1;
for (int v : sons[u]) {
dfs(v);
if (heavy == -1 || (int) Fen[heavy].upds.size() < (int) Fen[v].upds.size()) {
heavy = v;
}
}
if (heavy != -1) {
swap(Fen[u], Fen[heavy]);
}
for (int v : sons[u]) {
if (v != heavy) {
for (pair <int, int> upd : Fen[v].upds) {
Fen[u].update(upd.first, upd.second);
}
Fen[v].upds.clear();
Fen[v].mp.clear();
}
}
if (juice[u] != 0) {
Fen[u].maxify(ripeTime[u], juice[u]);
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M >> K;
for (int u = 2; u <= N; u++) {
cin >> parent[u];
sons[parent[u]].push_back(u);
}
for (int i = 1; i <= M; i++) {
int u;
cin >> u;
cin >> ripeTime[u] >> juice[u];
}
dfs(1);
cout << Fen[1].query(K) << "\n";
return 0;
}
# | 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... |