# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
547243 | pure_mem | Magic Tree (CEOI19_magictree) | C++14 | 123 ms | 18860 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 1e5 + 2;
const int INF = 1e9;
typedef map<int, ll> DP;
void merge(DP& oth, DP& cur) {
if(cur.size() < oth.size())
cur.swap(oth);
for(auto [pos, val]: oth) {
cur[pos] += val;
}
oth.clear();
}
void insert(DP& cur, int pos, ll val) {
cur[pos] += val;
DP::iterator it = cur.upper_bound(pos);
ll sum = 0;
while(it != cur.end()) {
if(it->second + sum > val) {
it->second += sum - val;
break;
}
else {
sum += it->second;
it = cur.erase(it);
}
}
}
int n, m, k;
DP dp[N];
vector<int> g[N];
pair<int, int> fruit[N];
void dfs(int v) {
for(int to: g[v]) {
dfs(to);
merge(dp[to], dp[v]);
}
insert(dp[v], fruit[v].X, fruit[v].Y);
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i = 2, v;i <= n;i++) {
cin >> v;
g[v].push_back(i);
}
for(int i = 1;i <= m;i++) {
int v, d, w;
cin >> v >> d >> w;
fruit[v] = {d, w};
}
dfs(1);
ll res = 0;
for(auto [pos, val]: dp[1])
res += val;
cout << res;
}
Compilation message (stderr)
# | 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... |