Submission #477987

# Submission time Handle Problem Language Result Execution time Memory
477987 2021-10-05T00:56:42 Z zerver Magic Tree (CEOI19_magictree) C++14
100 / 100
156 ms 33416 KB
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(nullptr)
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define sz(v) int(v.size())
#define ss second
#define ff first
#define endl '\n'
using namespace std;
typedef long long Long;
typedef pair<int, int> ii;
const Long INF = 1e13;
const int N = 1e5 + 7;
vector<int> adj[N];
multiset<ii> dif[N];
int dia[N], peso[N], pt[N];

void dfs(int from){
  int big = 0;
  for(int to: adj[from]){
    dfs(to);
    if(big == 0 || sz(dif[pt[to]]) > sz(dif[pt[big]])) big = to;
  }
  if(big == 0){
    if(dia[from] == 0) return;
    dif[pt[from]].insert({dia[from], peso[from]});
    return;
  }
  pt[from] = pt[big];
  for(int to: adj[from]){
    if(to == big) continue;
    for(auto p: dif[pt[to]]) dif[pt[from]].insert(p);
  }
  if(dia[from] != 0){
    ii par = {dia[from] + 1, 0};
    auto it = dif[pt[from]].lower_bound(par);
    ii ins = {dia[from], peso[from]};
    ii gua = ins;
    for(; it != dif[pt[from]].end(); ){
      ii npar = *(it);
      if(npar.ss <= ins.ss){
        ins.ss -= npar.ss;
        it = dif[pt[from]].erase(it);
      }
      else{
        npar.ss -= ins.ss;
        dif[pt[from]].erase(it);
        dif[pt[from]].insert(npar);
        break;
      }
    }
    dif[pt[from]].insert(gua);
  }
}

void solve(){
  int n, m, k; cin >> n >> m >> k;
  for(int i = 2; i <= n; i++){
    int p; cin >> p;
    adj[p].push_back(i);
  }
  for(int i = 0; i < m; i++){
    int v; cin >> v;
    cin >> dia[v] >> peso[v];
  }
  for(int i = 1; i <= n; i++) pt[i] = i;
  dfs(1);
  Long ans = 0;
  for(auto p: dif[pt[1]]) ans += p.ss;
  cout << ans << endl;
}

int main(){
  FAST_IO;
  int tt = 1; 
  // cin >> tt;
  while(tt--) solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 7 ms 7360 KB Output is correct
3 Correct 5 ms 7364 KB Output is correct
4 Correct 5 ms 7312 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7244 KB Output is correct
7 Correct 6 ms 7372 KB Output is correct
8 Correct 5 ms 7244 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 15864 KB Output is correct
2 Correct 46 ms 17672 KB Output is correct
3 Correct 149 ms 29216 KB Output is correct
4 Correct 72 ms 17344 KB Output is correct
5 Correct 118 ms 18880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7500 KB Output is correct
2 Correct 5 ms 7500 KB Output is correct
3 Correct 5 ms 7500 KB Output is correct
4 Correct 61 ms 26084 KB Output is correct
5 Correct 84 ms 30916 KB Output is correct
6 Correct 84 ms 26120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 27552 KB Output is correct
2 Correct 156 ms 33416 KB Output is correct
3 Correct 94 ms 22460 KB Output is correct
4 Correct 80 ms 19128 KB Output is correct
5 Correct 77 ms 28392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 7 ms 7360 KB Output is correct
3 Correct 5 ms 7364 KB Output is correct
4 Correct 5 ms 7312 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7244 KB Output is correct
7 Correct 6 ms 7372 KB Output is correct
8 Correct 5 ms 7244 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 118 ms 26516 KB Output is correct
11 Correct 119 ms 26888 KB Output is correct
12 Correct 89 ms 19768 KB Output is correct
13 Correct 60 ms 18368 KB Output is correct
14 Correct 77 ms 25792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7960 KB Output is correct
2 Correct 29 ms 10260 KB Output is correct
3 Correct 29 ms 10248 KB Output is correct
4 Correct 31 ms 10204 KB Output is correct
5 Correct 14 ms 9004 KB Output is correct
6 Correct 31 ms 12364 KB Output is correct
7 Correct 33 ms 15944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 7 ms 7360 KB Output is correct
3 Correct 5 ms 7364 KB Output is correct
4 Correct 5 ms 7312 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7244 KB Output is correct
7 Correct 6 ms 7372 KB Output is correct
8 Correct 5 ms 7244 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 6 ms 7500 KB Output is correct
11 Correct 5 ms 7500 KB Output is correct
12 Correct 5 ms 7500 KB Output is correct
13 Correct 61 ms 26084 KB Output is correct
14 Correct 84 ms 30916 KB Output is correct
15 Correct 84 ms 26120 KB Output is correct
16 Correct 118 ms 26516 KB Output is correct
17 Correct 119 ms 26888 KB Output is correct
18 Correct 89 ms 19768 KB Output is correct
19 Correct 60 ms 18368 KB Output is correct
20 Correct 77 ms 25792 KB Output is correct
21 Correct 24 ms 10788 KB Output is correct
22 Correct 84 ms 19752 KB Output is correct
23 Correct 83 ms 19744 KB Output is correct
24 Correct 117 ms 26684 KB Output is correct
25 Correct 71 ms 18744 KB Output is correct
26 Correct 114 ms 20344 KB Output is correct
27 Correct 86 ms 19908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 7 ms 7360 KB Output is correct
3 Correct 5 ms 7364 KB Output is correct
4 Correct 5 ms 7312 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7244 KB Output is correct
7 Correct 6 ms 7372 KB Output is correct
8 Correct 5 ms 7244 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 66 ms 15864 KB Output is correct
11 Correct 46 ms 17672 KB Output is correct
12 Correct 149 ms 29216 KB Output is correct
13 Correct 72 ms 17344 KB Output is correct
14 Correct 118 ms 18880 KB Output is correct
15 Correct 6 ms 7500 KB Output is correct
16 Correct 5 ms 7500 KB Output is correct
17 Correct 5 ms 7500 KB Output is correct
18 Correct 61 ms 26084 KB Output is correct
19 Correct 84 ms 30916 KB Output is correct
20 Correct 84 ms 26120 KB Output is correct
21 Correct 130 ms 27552 KB Output is correct
22 Correct 156 ms 33416 KB Output is correct
23 Correct 94 ms 22460 KB Output is correct
24 Correct 80 ms 19128 KB Output is correct
25 Correct 77 ms 28392 KB Output is correct
26 Correct 118 ms 26516 KB Output is correct
27 Correct 119 ms 26888 KB Output is correct
28 Correct 89 ms 19768 KB Output is correct
29 Correct 60 ms 18368 KB Output is correct
30 Correct 77 ms 25792 KB Output is correct
31 Correct 9 ms 7960 KB Output is correct
32 Correct 29 ms 10260 KB Output is correct
33 Correct 29 ms 10248 KB Output is correct
34 Correct 31 ms 10204 KB Output is correct
35 Correct 14 ms 9004 KB Output is correct
36 Correct 31 ms 12364 KB Output is correct
37 Correct 33 ms 15944 KB Output is correct
38 Correct 24 ms 10788 KB Output is correct
39 Correct 84 ms 19752 KB Output is correct
40 Correct 83 ms 19744 KB Output is correct
41 Correct 117 ms 26684 KB Output is correct
42 Correct 71 ms 18744 KB Output is correct
43 Correct 114 ms 20344 KB Output is correct
44 Correct 86 ms 19908 KB Output is correct
45 Correct 28 ms 11240 KB Output is correct
46 Correct 93 ms 20980 KB Output is correct
47 Correct 87 ms 20816 KB Output is correct
48 Correct 138 ms 29428 KB Output is correct
49 Correct 81 ms 19348 KB Output is correct
50 Correct 112 ms 21316 KB Output is correct
51 Correct 104 ms 20724 KB Output is correct