답안 #949483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949483 2024-03-19T09:30:59 Z berr Magic Tree (CEOI19_magictree) C++17
100 / 100
110 ms 37800 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long


signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m, k; cin >> n >> m >> k;

    vector<vector<int>> g(n);
    array<int, 2> base = {-1, -1};
    vector<array<int, 2>> val(n, base);
    
    for(int i=1; i<n; i++){
        int x; cin >> x; x--;
        g[x].push_back(i);
    }

    for(int i=0; i<m; i++){
        int v, d, w; cin >> v >> d >> w;
        v--;
        val[v] = {d, w};
    }

    auto dfs=[&](int v, auto&&dfs)->set<array<int, 2>>{
        set<array<int, 2>> res;
        res.insert({0, 0});

        for(auto i: g[v]){
            auto x = dfs(i, dfs);
            if(x.size() > res.size()) swap(res, x);
        
            for(auto i: x){
                auto a = res.lower_bound({i[0], 0});

                if(a==res.end()||(*a)[0] > i[0]) a=prev(a);
                if((*a)[0] == i[0]){
                    i[1]+=(*a)[1];
                    res.erase(a);
                    res.insert(i); 
                 
                }
                else{
                    
                    res.insert(i);
            
                }
            }
        }

        
        if(val[v][0]!=-1){
            auto a = res.lower_bound({val[v][0], 0});
            auto i=val[v];

            if(a==res.end()||(*a)[0] > i[0]) a=prev(a);
            if((*a)[0] == i[0]){
                i[1]+=(*a)[1];
                res.erase(a);
                res.insert(i);
            
            }
            else{
                
                res.insert(i);
            }

            while(1){
                auto it = res.upper_bound({val[v][0], (int)1e18});
                if(it==res.end()) break;
                else{
                    if((*it)[1]>val[v][1]){
                        array<int, 2> x=*it;
                        res.erase(it);
                        x[1]-=val[v][1];
                        if(x[1]) res.insert(x);
                        break;
                    } 
                    else{
                        val[v][1]-=(*it)[1];
                        res.erase(it);
                    }
                }
            }
        }

        return res;
    };

    auto x = dfs(0, dfs);
    int ans=0;
    for(auto i: x)ans+=i[1];

    cout<<ans;

}   











# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 8964 KB Output is correct
2 Correct 46 ms 19832 KB Output is correct
3 Correct 110 ms 12624 KB Output is correct
4 Correct 74 ms 10892 KB Output is correct
5 Correct 83 ms 13700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 744 KB Output is correct
4 Correct 65 ms 37596 KB Output is correct
5 Correct 88 ms 37600 KB Output is correct
6 Correct 101 ms 37592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 8276 KB Output is correct
2 Correct 44 ms 8160 KB Output is correct
3 Correct 66 ms 20220 KB Output is correct
4 Correct 36 ms 6868 KB Output is correct
5 Correct 56 ms 37800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 55 ms 7516 KB Output is correct
11 Correct 52 ms 7512 KB Output is correct
12 Correct 64 ms 19548 KB Output is correct
13 Correct 38 ms 6400 KB Output is correct
14 Correct 64 ms 36948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1628 KB Output is correct
2 Correct 24 ms 6840 KB Output is correct
3 Correct 23 ms 6744 KB Output is correct
4 Correct 23 ms 7000 KB Output is correct
5 Correct 15 ms 5596 KB Output is correct
6 Correct 26 ms 12120 KB Output is correct
7 Correct 31 ms 18780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 744 KB Output is correct
13 Correct 65 ms 37596 KB Output is correct
14 Correct 88 ms 37600 KB Output is correct
15 Correct 101 ms 37592 KB Output is correct
16 Correct 55 ms 7516 KB Output is correct
17 Correct 52 ms 7512 KB Output is correct
18 Correct 64 ms 19548 KB Output is correct
19 Correct 38 ms 6400 KB Output is correct
20 Correct 64 ms 36948 KB Output is correct
21 Correct 18 ms 2136 KB Output is correct
22 Correct 59 ms 7772 KB Output is correct
23 Correct 62 ms 9552 KB Output is correct
24 Correct 97 ms 11120 KB Output is correct
25 Correct 65 ms 10192 KB Output is correct
26 Correct 106 ms 16208 KB Output is correct
27 Correct 102 ms 20308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 57 ms 8964 KB Output is correct
11 Correct 46 ms 19832 KB Output is correct
12 Correct 110 ms 12624 KB Output is correct
13 Correct 74 ms 10892 KB Output is correct
14 Correct 83 ms 13700 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 744 KB Output is correct
18 Correct 65 ms 37596 KB Output is correct
19 Correct 88 ms 37600 KB Output is correct
20 Correct 101 ms 37592 KB Output is correct
21 Correct 46 ms 8276 KB Output is correct
22 Correct 44 ms 8160 KB Output is correct
23 Correct 66 ms 20220 KB Output is correct
24 Correct 36 ms 6868 KB Output is correct
25 Correct 56 ms 37800 KB Output is correct
26 Correct 55 ms 7516 KB Output is correct
27 Correct 52 ms 7512 KB Output is correct
28 Correct 64 ms 19548 KB Output is correct
29 Correct 38 ms 6400 KB Output is correct
30 Correct 64 ms 36948 KB Output is correct
31 Correct 5 ms 1628 KB Output is correct
32 Correct 24 ms 6840 KB Output is correct
33 Correct 23 ms 6744 KB Output is correct
34 Correct 23 ms 7000 KB Output is correct
35 Correct 15 ms 5596 KB Output is correct
36 Correct 26 ms 12120 KB Output is correct
37 Correct 31 ms 18780 KB Output is correct
38 Correct 18 ms 2136 KB Output is correct
39 Correct 59 ms 7772 KB Output is correct
40 Correct 62 ms 9552 KB Output is correct
41 Correct 97 ms 11120 KB Output is correct
42 Correct 65 ms 10192 KB Output is correct
43 Correct 106 ms 16208 KB Output is correct
44 Correct 102 ms 20308 KB Output is correct
45 Correct 17 ms 2136 KB Output is correct
46 Correct 70 ms 8272 KB Output is correct
47 Correct 70 ms 9808 KB Output is correct
48 Correct 105 ms 12368 KB Output is correct
49 Correct 72 ms 10788 KB Output is correct
50 Correct 92 ms 16976 KB Output is correct
51 Correct 105 ms 21328 KB Output is correct