Submission #199821

# Submission time Handle Problem Language Result Execution time Memory
199821 2020-02-03T14:29:51 Z quocnguyen1012 Magic Tree (CEOI19_magictree) C++14
100 / 100
195 ms 37112 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 5;

vector<int> adj[maxn];
map<int, ll> f[maxn];
int N, W[maxn], D[maxn], M, K;

void dfs(int u)
{
  for (int v : adj[u]){
    dfs(v);
    if (f[v].size() > f[u].size()) swap(f[u], f[v]);
    for (auto & all : f[v])
      f[u][all.fi] += all.se;
  }
  if (D[u]){
    vector<int> vi;
    ll sum = 0;
    auto it = f[u].upper_bound(D[u]);
    for (; it != f[u].end(); ++it){
      sum += it->se;
      if (W[u] < sum) break;
      vi.pb(it->fi);
    }
    if (it != f[u].end()) it->se = sum - W[u];
    for (auto & i : vi) f[u].erase(i);
    f[u][D[u]] += W[u];
  }
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> M >> K;
  for (int i = 2; i <= N; ++i){
    int p; cin >> p;
    adj[p].pb(i);
  }
  for (int i = 1; i <= M; ++i){
    int u; cin >> u;
    cin >> D[u] >> W[u];
  }
  dfs(1);
  ll ret = 0;
  for (auto it : f[1]) ret += it.se;
  cout << ret;
}

Compilation message

magictree.cpp: In function 'int main()':
magictree.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
magictree.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7288 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7288 KB Output is correct
6 Correct 10 ms 7416 KB Output is correct
7 Correct 11 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 10 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 18424 KB Output is correct
2 Correct 60 ms 19704 KB Output is correct
3 Correct 195 ms 37112 KB Output is correct
4 Correct 109 ms 19956 KB Output is correct
5 Correct 153 ms 22008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7544 KB Output is correct
2 Correct 10 ms 7544 KB Output is correct
3 Correct 14 ms 7672 KB Output is correct
4 Correct 75 ms 27384 KB Output is correct
5 Correct 98 ms 31352 KB Output is correct
6 Correct 96 ms 27388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 15612 KB Output is correct
2 Correct 93 ms 14840 KB Output is correct
3 Correct 75 ms 19576 KB Output is correct
4 Correct 56 ms 15984 KB Output is correct
5 Correct 68 ms 27512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7288 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7288 KB Output is correct
6 Correct 10 ms 7416 KB Output is correct
7 Correct 11 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 10 ms 7416 KB Output is correct
10 Correct 110 ms 17912 KB Output is correct
11 Correct 101 ms 16912 KB Output is correct
12 Correct 70 ms 18936 KB Output is correct
13 Correct 55 ms 15348 KB Output is correct
14 Correct 65 ms 26872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8184 KB Output is correct
2 Correct 42 ms 10488 KB Output is correct
3 Correct 43 ms 10488 KB Output is correct
4 Correct 42 ms 10616 KB Output is correct
5 Correct 19 ms 8948 KB Output is correct
6 Correct 38 ms 13052 KB Output is correct
7 Correct 41 ms 16888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7288 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7288 KB Output is correct
6 Correct 10 ms 7416 KB Output is correct
7 Correct 11 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 10 ms 7416 KB Output is correct
10 Correct 10 ms 7544 KB Output is correct
11 Correct 10 ms 7544 KB Output is correct
12 Correct 14 ms 7672 KB Output is correct
13 Correct 75 ms 27384 KB Output is correct
14 Correct 98 ms 31352 KB Output is correct
15 Correct 96 ms 27388 KB Output is correct
16 Correct 110 ms 17912 KB Output is correct
17 Correct 101 ms 16912 KB Output is correct
18 Correct 70 ms 18936 KB Output is correct
19 Correct 55 ms 15348 KB Output is correct
20 Correct 65 ms 26872 KB Output is correct
21 Correct 29 ms 10744 KB Output is correct
22 Correct 100 ms 18808 KB Output is correct
23 Correct 108 ms 22008 KB Output is correct
24 Correct 162 ms 30332 KB Output is correct
25 Correct 100 ms 19312 KB Output is correct
26 Correct 130 ms 21540 KB Output is correct
27 Correct 111 ms 20728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7288 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7288 KB Output is correct
6 Correct 10 ms 7416 KB Output is correct
7 Correct 11 ms 7416 KB Output is correct
8 Correct 10 ms 7416 KB Output is correct
9 Correct 10 ms 7416 KB Output is correct
10 Correct 82 ms 18424 KB Output is correct
11 Correct 60 ms 19704 KB Output is correct
12 Correct 195 ms 37112 KB Output is correct
13 Correct 109 ms 19956 KB Output is correct
14 Correct 153 ms 22008 KB Output is correct
15 Correct 10 ms 7544 KB Output is correct
16 Correct 10 ms 7544 KB Output is correct
17 Correct 14 ms 7672 KB Output is correct
18 Correct 75 ms 27384 KB Output is correct
19 Correct 98 ms 31352 KB Output is correct
20 Correct 96 ms 27388 KB Output is correct
21 Correct 101 ms 15612 KB Output is correct
22 Correct 93 ms 14840 KB Output is correct
23 Correct 75 ms 19576 KB Output is correct
24 Correct 56 ms 15984 KB Output is correct
25 Correct 68 ms 27512 KB Output is correct
26 Correct 110 ms 17912 KB Output is correct
27 Correct 101 ms 16912 KB Output is correct
28 Correct 70 ms 18936 KB Output is correct
29 Correct 55 ms 15348 KB Output is correct
30 Correct 65 ms 26872 KB Output is correct
31 Correct 14 ms 8184 KB Output is correct
32 Correct 42 ms 10488 KB Output is correct
33 Correct 43 ms 10488 KB Output is correct
34 Correct 42 ms 10616 KB Output is correct
35 Correct 19 ms 8948 KB Output is correct
36 Correct 38 ms 13052 KB Output is correct
37 Correct 41 ms 16888 KB Output is correct
38 Correct 29 ms 10744 KB Output is correct
39 Correct 100 ms 18808 KB Output is correct
40 Correct 108 ms 22008 KB Output is correct
41 Correct 162 ms 30332 KB Output is correct
42 Correct 100 ms 19312 KB Output is correct
43 Correct 130 ms 21540 KB Output is correct
44 Correct 111 ms 20728 KB Output is correct
45 Correct 33 ms 11256 KB Output is correct
46 Correct 103 ms 19832 KB Output is correct
47 Correct 110 ms 23428 KB Output is correct
48 Correct 179 ms 33400 KB Output is correct
49 Correct 103 ms 19956 KB Output is correct
50 Correct 141 ms 22392 KB Output is correct
51 Correct 120 ms 21624 KB Output is correct