Submission #1051467

# Submission time Handle Problem Language Result Execution time Memory
1051467 2024-08-10T07:12:48 Z otarius Magic Tree (CEOI19_magictree) C++17
100 / 100
242 ms 34508 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()

// #define int long long
// #define int unsigned long long

// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

void open_file(string filename) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}

// const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 1e5 + 25;

vector<int> g[maxN];
map<int, ll> dp[maxN];
int n, m, k, d[maxN], w[maxN];
void dfs(int v, int par) {
    for (int u : g[v]) {
        if (u == par) continue;
        dfs(u, v);
        if (dp[v].size() < dp[u].size())
            swap(dp[v], dp[u]);
        for (auto x : dp[u]) dp[v][x.ff] += x.sc;
        dp[u].clear();
    } auto it = dp[v].upper_bound(d[v]); ll cnt = w[v];
    for ( ; it != dp[v].end(); ++it) {
        if (it -> sc < cnt) {
            cnt -= it -> sc; it -> sc = 0;
        } else {
            it -> sc -= cnt; break;
        }
    } dp[v][d[v]] += w[v];
}
void solve() {
    cin >> n >> m >> k;
    for (int i = 2; i <= n; i++) {
        int x; cin >> x; g[x].pb(i);
    } for (int i = 1; i <= m; i++) {
        int _v, _d, _w;
        cin >> _v >> _d >> _w;
        d[_v] = _d; w[_v] = _w;
    } dfs(1, -1); ll ans = 0;
    for (auto x : dp[1]) ans += x.sc;
    cout << ans;
}
int32_t main() { 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}

Compilation message

magictree.cpp: In function 'void open_file(std::string)':
magictree.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8028 KB Output is correct
2 Correct 1 ms 8028 KB Output is correct
3 Correct 2 ms 8108 KB Output is correct
4 Correct 1 ms 8028 KB Output is correct
5 Correct 1 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8116 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 1 ms 8120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 12628 KB Output is correct
2 Correct 26 ms 20052 KB Output is correct
3 Correct 82 ms 16136 KB Output is correct
4 Correct 47 ms 14432 KB Output is correct
5 Correct 51 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8284 KB Output is correct
3 Correct 1 ms 8124 KB Output is correct
4 Correct 53 ms 34508 KB Output is correct
5 Correct 52 ms 34420 KB Output is correct
6 Correct 242 ms 34388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 11864 KB Output is correct
2 Correct 27 ms 11892 KB Output is correct
3 Correct 30 ms 19604 KB Output is correct
4 Correct 19 ms 10460 KB Output is correct
5 Correct 29 ms 30548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8028 KB Output is correct
2 Correct 1 ms 8028 KB Output is correct
3 Correct 2 ms 8108 KB Output is correct
4 Correct 1 ms 8028 KB Output is correct
5 Correct 1 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8116 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 1 ms 8120 KB Output is correct
10 Correct 30 ms 11200 KB Output is correct
11 Correct 29 ms 11096 KB Output is correct
12 Correct 26 ms 18976 KB Output is correct
13 Correct 18 ms 9692 KB Output is correct
14 Correct 30 ms 30032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 16 ms 10332 KB Output is correct
3 Correct 16 ms 10332 KB Output is correct
4 Correct 17 ms 10492 KB Output is correct
5 Correct 8 ms 8928 KB Output is correct
6 Correct 24 ms 13704 KB Output is correct
7 Correct 15 ms 18268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8028 KB Output is correct
2 Correct 1 ms 8028 KB Output is correct
3 Correct 2 ms 8108 KB Output is correct
4 Correct 1 ms 8028 KB Output is correct
5 Correct 1 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8116 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 1 ms 8120 KB Output is correct
10 Correct 2 ms 8280 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 1 ms 8124 KB Output is correct
13 Correct 53 ms 34508 KB Output is correct
14 Correct 52 ms 34420 KB Output is correct
15 Correct 242 ms 34388 KB Output is correct
16 Correct 30 ms 11200 KB Output is correct
17 Correct 29 ms 11096 KB Output is correct
18 Correct 26 ms 18976 KB Output is correct
19 Correct 18 ms 9692 KB Output is correct
20 Correct 30 ms 30032 KB Output is correct
21 Correct 10 ms 9052 KB Output is correct
22 Correct 40 ms 11352 KB Output is correct
23 Correct 41 ms 13904 KB Output is correct
24 Correct 79 ms 15952 KB Output is correct
25 Correct 37 ms 13788 KB Output is correct
26 Correct 48 ms 20756 KB Output is correct
27 Correct 67 ms 25188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8028 KB Output is correct
2 Correct 1 ms 8028 KB Output is correct
3 Correct 2 ms 8108 KB Output is correct
4 Correct 1 ms 8028 KB Output is correct
5 Correct 1 ms 8028 KB Output is correct
6 Correct 2 ms 8028 KB Output is correct
7 Correct 2 ms 8116 KB Output is correct
8 Correct 2 ms 8028 KB Output is correct
9 Correct 1 ms 8120 KB Output is correct
10 Correct 37 ms 12628 KB Output is correct
11 Correct 26 ms 20052 KB Output is correct
12 Correct 82 ms 16136 KB Output is correct
13 Correct 47 ms 14432 KB Output is correct
14 Correct 51 ms 16724 KB Output is correct
15 Correct 2 ms 8280 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 1 ms 8124 KB Output is correct
18 Correct 53 ms 34508 KB Output is correct
19 Correct 52 ms 34420 KB Output is correct
20 Correct 242 ms 34388 KB Output is correct
21 Correct 30 ms 11864 KB Output is correct
22 Correct 27 ms 11892 KB Output is correct
23 Correct 30 ms 19604 KB Output is correct
24 Correct 19 ms 10460 KB Output is correct
25 Correct 29 ms 30548 KB Output is correct
26 Correct 30 ms 11200 KB Output is correct
27 Correct 29 ms 11096 KB Output is correct
28 Correct 26 ms 18976 KB Output is correct
29 Correct 18 ms 9692 KB Output is correct
30 Correct 30 ms 30032 KB Output is correct
31 Correct 4 ms 8536 KB Output is correct
32 Correct 16 ms 10332 KB Output is correct
33 Correct 16 ms 10332 KB Output is correct
34 Correct 17 ms 10492 KB Output is correct
35 Correct 8 ms 8928 KB Output is correct
36 Correct 24 ms 13704 KB Output is correct
37 Correct 15 ms 18268 KB Output is correct
38 Correct 10 ms 9052 KB Output is correct
39 Correct 40 ms 11352 KB Output is correct
40 Correct 41 ms 13904 KB Output is correct
41 Correct 79 ms 15952 KB Output is correct
42 Correct 37 ms 13788 KB Output is correct
43 Correct 48 ms 20756 KB Output is correct
44 Correct 67 ms 25188 KB Output is correct
45 Correct 11 ms 9052 KB Output is correct
46 Correct 34 ms 11612 KB Output is correct
47 Correct 54 ms 13780 KB Output is correct
48 Correct 121 ms 16724 KB Output is correct
49 Correct 40 ms 14288 KB Output is correct
50 Correct 71 ms 21332 KB Output is correct
51 Correct 76 ms 26032 KB Output is correct