Submission #703491

# Submission time Handle Problem Language Result Execution time Memory
703491 2023-02-27T13:49:06 Z Cyanmond Magic Tree (CEOI19_magictree) C++17
100 / 100
188 ms 33964 KB
#include <bits/stdc++.h>

using i64 = long long;
struct Vertice {
    bool exFruit;
    int day;
    i64 weight;
};

int main() {
    int N, M, K;
    std::cin >> N >> M >> K;
    std::vector<int> P(N);
    std::vector<std::vector<int>> tree(N);
    for (int i = 1; i < N; ++i) {
        std::cin >> P[i];
        --P[i];
        tree[P[i]].push_back(i);
    }
    std::vector<int> U(M), D(M);
    std::vector<i64> W(M);
    std::vector<Vertice> vData(N);
    for (int i = 0; i < M; ++i) {
        std::cin >> U[i] >> D[i] >> W[i];
        --U[i];
    }
    {
        // press
        auto cD = D;
        std::sort(cD.begin(), cD.end());
        cD.erase(std::unique(cD.begin(), cD.end()), cD.end());
        for (auto &e : D) {
            e = (int)(std::lower_bound(cD.begin(), cD.end(), e) - cD.begin());
        }
        K = (int)cD.size();
    }
    for (int i = 0; i < M; ++i) {
        vData[U[i]] = {true, D[i], W[i]};
    }

    auto merge = [&] (std::map<int, i64> &a, std::map<int, i64> &b) {
        if (a.size() < b.size()) {
            std::swap(a, b);
        }
        for (const auto &[k, v] : b) {
            a[k] += v;
        }
    };

    auto dfs = [&](auto &&self, const int v) -> std::map<int, i64> {
        // calc DP
        std::map<int, i64> ret;
        for (const int t : tree[v]) {
            auto res = self(self, t);
            merge(ret, res);
        }
        // cut
        if (vData[v].exFruit) {
            ret[vData[v].day] += vData[v].weight;
            auto itr = ret.upper_bound(vData[v].day);
            i64 sum = vData[v].weight;
            while (itr != ret.end() and sum != 0) {
                if (itr->second <= sum) {
                    sum -= itr->second;
                    itr = ret.erase(itr);
                } else {
                    itr->second -= sum;
                    sum = 0;
                }
            }
        }
        return ret;
    };
    const auto res = dfs(dfs, 0);
    i64 answer = 0;
    for (const auto &[k, v] : res) {
        answer += v;
    }
    std::cout << answer << std::endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 8336 KB Output is correct
2 Correct 84 ms 16596 KB Output is correct
3 Correct 183 ms 11396 KB Output is correct
4 Correct 135 ms 10020 KB Output is correct
5 Correct 164 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 122 ms 27980 KB Output is correct
5 Correct 135 ms 33964 KB Output is correct
6 Correct 139 ms 29944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 7920 KB Output is correct
2 Correct 114 ms 7856 KB Output is correct
3 Correct 110 ms 15968 KB Output is correct
4 Correct 92 ms 6792 KB Output is correct
5 Correct 133 ms 27860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 107 ms 7872 KB Output is correct
11 Correct 101 ms 7884 KB Output is correct
12 Correct 101 ms 16000 KB Output is correct
13 Correct 75 ms 6876 KB Output is correct
14 Correct 119 ms 27856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1512 KB Output is correct
2 Correct 41 ms 6180 KB Output is correct
3 Correct 42 ms 6168 KB Output is correct
4 Correct 40 ms 6276 KB Output is correct
5 Correct 17 ms 5068 KB Output is correct
6 Correct 43 ms 9692 KB Output is correct
7 Correct 43 ms 14668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 122 ms 27980 KB Output is correct
14 Correct 135 ms 33964 KB Output is correct
15 Correct 139 ms 29944 KB Output is correct
16 Correct 107 ms 7872 KB Output is correct
17 Correct 101 ms 7884 KB Output is correct
18 Correct 101 ms 16000 KB Output is correct
19 Correct 75 ms 6876 KB Output is correct
20 Correct 119 ms 27856 KB Output is correct
21 Correct 26 ms 2348 KB Output is correct
22 Correct 99 ms 8552 KB Output is correct
23 Correct 103 ms 10388 KB Output is correct
24 Correct 187 ms 12692 KB Output is correct
25 Correct 117 ms 11448 KB Output is correct
26 Correct 157 ms 16800 KB Output is correct
27 Correct 144 ms 19276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 93 ms 8336 KB Output is correct
11 Correct 84 ms 16596 KB Output is correct
12 Correct 183 ms 11396 KB Output is correct
13 Correct 135 ms 10020 KB Output is correct
14 Correct 164 ms 12368 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 122 ms 27980 KB Output is correct
19 Correct 135 ms 33964 KB Output is correct
20 Correct 139 ms 29944 KB Output is correct
21 Correct 147 ms 7920 KB Output is correct
22 Correct 114 ms 7856 KB Output is correct
23 Correct 110 ms 15968 KB Output is correct
24 Correct 92 ms 6792 KB Output is correct
25 Correct 133 ms 27860 KB Output is correct
26 Correct 107 ms 7872 KB Output is correct
27 Correct 101 ms 7884 KB Output is correct
28 Correct 101 ms 16000 KB Output is correct
29 Correct 75 ms 6876 KB Output is correct
30 Correct 119 ms 27856 KB Output is correct
31 Correct 9 ms 1512 KB Output is correct
32 Correct 41 ms 6180 KB Output is correct
33 Correct 42 ms 6168 KB Output is correct
34 Correct 40 ms 6276 KB Output is correct
35 Correct 17 ms 5068 KB Output is correct
36 Correct 43 ms 9692 KB Output is correct
37 Correct 43 ms 14668 KB Output is correct
38 Correct 26 ms 2348 KB Output is correct
39 Correct 99 ms 8552 KB Output is correct
40 Correct 103 ms 10388 KB Output is correct
41 Correct 187 ms 12692 KB Output is correct
42 Correct 117 ms 11448 KB Output is correct
43 Correct 157 ms 16800 KB Output is correct
44 Correct 144 ms 19276 KB Output is correct
45 Correct 32 ms 2380 KB Output is correct
46 Correct 108 ms 8828 KB Output is correct
47 Correct 115 ms 10588 KB Output is correct
48 Correct 188 ms 13772 KB Output is correct
49 Correct 147 ms 12176 KB Output is correct
50 Correct 185 ms 17728 KB Output is correct
51 Correct 161 ms 20208 KB Output is correct