Submission #840847

# Submission time Handle Problem Language Result Execution time Memory
840847 2023-08-31T18:37:13 Z qthang2k11 Magic Tree (CEOI19_magictree) C++17
100 / 100
94 ms 30412 KB
// https://codeforces.com/blog/entry/68748?#comment-530918
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e5 + 5;

int n, k, max_d, d[N], w[N];
vector<int> adj[N];

map<int, ll> dp[N];
int sz[N], id[N];

void insert(int x, int d, int w) {
  dp[x][d] += w;
  auto it = dp[x].upper_bound(d);
  while (it != dp[x].end() && it->second <= w)
    w -= it->second, it = dp[x].erase(it);
  if (it != dp[x].end()) it->second -= w;
}

void dfs(int x) {
  id[x] = x;
  sz[x] = d[x] > 0;
  int mx = -1, spec = x;
  for (int y: adj[x]) {
    dfs(y);
    if (sz[y] > mx)
      spec = y, mx = sz[y];
    sz[x] += sz[y];
  }
  id[x] = id[spec];
  for (int y: adj[x]) if (y != spec) {
    for (const auto &elem: dp[id[y]])
      dp[id[x]][elem.first] += elem.second;
    dp[id[y]].clear();
  }
  if (d[x]) insert(id[x], d[x], w[x]);
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> k >> max_d;
  for (int i = 2; i <= n; i++) {
    int pi; 
    cin >> pi;
    adj[pi].emplace_back(i);
  }
  for (int i = 1, x; i <= k; i++)
    cin >> x >> d[x] >> w[x];
  dfs(1);
  ll ans = 0;
  for (const auto &elem: dp[id[1]])
    ans += elem.second;
  cout << ans;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 13572 KB Output is correct
2 Correct 32 ms 18508 KB Output is correct
3 Correct 88 ms 17868 KB Output is correct
4 Correct 51 ms 16972 KB Output is correct
5 Correct 68 ms 16972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 3 ms 7508 KB Output is correct
3 Correct 3 ms 7508 KB Output is correct
4 Correct 38 ms 26436 KB Output is correct
5 Correct 59 ms 30412 KB Output is correct
6 Correct 62 ms 26512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 12620 KB Output is correct
2 Correct 37 ms 12620 KB Output is correct
3 Correct 37 ms 18456 KB Output is correct
4 Correct 31 ms 16716 KB Output is correct
5 Correct 34 ms 26768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 45 ms 11988 KB Output is correct
11 Correct 44 ms 11988 KB Output is correct
12 Correct 35 ms 17868 KB Output is correct
13 Correct 29 ms 16196 KB Output is correct
14 Correct 35 ms 26060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8148 KB Output is correct
2 Correct 20 ms 11148 KB Output is correct
3 Correct 20 ms 11092 KB Output is correct
4 Correct 20 ms 11092 KB Output is correct
5 Correct 10 ms 9684 KB Output is correct
6 Correct 18 ms 13416 KB Output is correct
7 Correct 19 ms 16980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 3 ms 7508 KB Output is correct
12 Correct 3 ms 7508 KB Output is correct
13 Correct 38 ms 26436 KB Output is correct
14 Correct 59 ms 30412 KB Output is correct
15 Correct 62 ms 26512 KB Output is correct
16 Correct 45 ms 11988 KB Output is correct
17 Correct 44 ms 11988 KB Output is correct
18 Correct 35 ms 17868 KB Output is correct
19 Correct 29 ms 16196 KB Output is correct
20 Correct 35 ms 26060 KB Output is correct
21 Correct 15 ms 8660 KB Output is correct
22 Correct 42 ms 12148 KB Output is correct
23 Correct 54 ms 14036 KB Output is correct
24 Correct 91 ms 16088 KB Output is correct
25 Correct 46 ms 16372 KB Output is correct
26 Correct 62 ms 18508 KB Output is correct
27 Correct 68 ms 19664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 45 ms 13572 KB Output is correct
11 Correct 32 ms 18508 KB Output is correct
12 Correct 88 ms 17868 KB Output is correct
13 Correct 51 ms 16972 KB Output is correct
14 Correct 68 ms 16972 KB Output is correct
15 Correct 4 ms 7508 KB Output is correct
16 Correct 3 ms 7508 KB Output is correct
17 Correct 3 ms 7508 KB Output is correct
18 Correct 38 ms 26436 KB Output is correct
19 Correct 59 ms 30412 KB Output is correct
20 Correct 62 ms 26512 KB Output is correct
21 Correct 45 ms 12620 KB Output is correct
22 Correct 37 ms 12620 KB Output is correct
23 Correct 37 ms 18456 KB Output is correct
24 Correct 31 ms 16716 KB Output is correct
25 Correct 34 ms 26768 KB Output is correct
26 Correct 45 ms 11988 KB Output is correct
27 Correct 44 ms 11988 KB Output is correct
28 Correct 35 ms 17868 KB Output is correct
29 Correct 29 ms 16196 KB Output is correct
30 Correct 35 ms 26060 KB Output is correct
31 Correct 7 ms 8148 KB Output is correct
32 Correct 20 ms 11148 KB Output is correct
33 Correct 20 ms 11092 KB Output is correct
34 Correct 20 ms 11092 KB Output is correct
35 Correct 10 ms 9684 KB Output is correct
36 Correct 18 ms 13416 KB Output is correct
37 Correct 19 ms 16980 KB Output is correct
38 Correct 15 ms 8660 KB Output is correct
39 Correct 42 ms 12148 KB Output is correct
40 Correct 54 ms 14036 KB Output is correct
41 Correct 91 ms 16088 KB Output is correct
42 Correct 46 ms 16372 KB Output is correct
43 Correct 62 ms 18508 KB Output is correct
44 Correct 68 ms 19664 KB Output is correct
45 Correct 16 ms 8788 KB Output is correct
46 Correct 49 ms 12492 KB Output is correct
47 Correct 60 ms 14452 KB Output is correct
48 Correct 94 ms 17188 KB Output is correct
49 Correct 51 ms 16972 KB Output is correct
50 Correct 75 ms 19468 KB Output is correct
51 Correct 59 ms 20556 KB Output is correct