답안 #1082270

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082270 2024-08-31T03:36:46 Z Zero_OP Magic Tree (CEOI19_magictree) C++14
100 / 100
88 ms 30036 KB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;

template<typename T> 
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){    
        if(a < b){
            return a = b, true;
        } return false;
    }

template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
 
template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};

const int MAX = 1e5 + 5;

int n, m, k, par[MAX], day[MAX], weight[MAX];
vector<int> adj[MAX];
map<int, ll> dp[MAX]; 
//we can prove that dp[u][t] <= dp[u][t + 1]
//notice : now the dp[u] works as the "difference array"

void dfs(int u){
    for(int v : adj[u]){
        dfs(v);

        if(sz(dp[u]) < sz(dp[v])) swap(dp[u], dp[v]);
        for(const auto& [pos, val] : dp[v]){
            dp[u][pos] += val;
        }

        dp[v].clear();
    }

    if(day[u] && weight[u]){
        ll sum = weight[u];
        while(true){
            auto iter = dp[u].upper_bound(day[u]);
            if(iter == dp[u].end()) break;

            if(iter -> second <= sum){
                sum -= iter -> second; //because of the max part
                dp[u].erase(iter); //same dp[u][t]
            } else{
                iter -> second -= sum; //can't increase
                break; 
            }
        }
        
        dp[u][day[u]] += weight[u];
     }
}

void testcase(){
    cin >> n >> m >> k;
    par[0] = -1;
    rep(i, 1, n){
        int p;
        cin >> p;
        --p;
        ::par[i] = p;
        adj[p].push_back(i);
    }

    rep(i, 0, m){
        int v, d, w;
        cin >> v >> d >> w;
        --v;
        day[v] = d;
        weight[v] = w;
    }

    dfs(0);

    ll sum = 0;
    for(auto it : dp[0]) sum += it.second;
    cout << sum << '\n';
}

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

    file("task");

    int T = 1;
    //cin >> T;
    while(T--){
        testcase();
    }

    return 0;
}

Compilation message

magictree.cpp: In function 'void dfs(int)':
magictree.cpp:56:25: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |         for(const auto& [pos, val] : dp[v]){
      |                         ^
magictree.cpp: In function 'int main()':
magictree.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:112:5: note: in expansion of macro 'file'
  112 |     file("task");
      |     ^~~~
magictree.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:112:5: note: in expansion of macro 'file'
  112 |     file("task");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7480 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7384 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 13024 KB Output is correct
2 Correct 27 ms 18164 KB Output is correct
3 Correct 88 ms 16468 KB Output is correct
4 Correct 45 ms 14808 KB Output is correct
5 Correct 63 ms 16468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7520 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 41 ms 26016 KB Output is correct
5 Correct 61 ms 30036 KB Output is correct
6 Correct 57 ms 26192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 12380 KB Output is correct
2 Correct 36 ms 12368 KB Output is correct
3 Correct 34 ms 18260 KB Output is correct
4 Correct 24 ms 10716 KB Output is correct
5 Correct 35 ms 26192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7480 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7384 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 46 ms 11776 KB Output is correct
11 Correct 38 ms 11608 KB Output is correct
12 Correct 32 ms 17500 KB Output is correct
13 Correct 23 ms 10204 KB Output is correct
14 Correct 34 ms 25732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8028 KB Output is correct
2 Correct 20 ms 10724 KB Output is correct
3 Correct 18 ms 10704 KB Output is correct
4 Correct 18 ms 10844 KB Output is correct
5 Correct 7 ms 9180 KB Output is correct
6 Correct 22 ms 13148 KB Output is correct
7 Correct 20 ms 16728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7480 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7384 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7520 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 41 ms 26016 KB Output is correct
14 Correct 61 ms 30036 KB Output is correct
15 Correct 57 ms 26192 KB Output is correct
16 Correct 46 ms 11776 KB Output is correct
17 Correct 38 ms 11608 KB Output is correct
18 Correct 32 ms 17500 KB Output is correct
19 Correct 23 ms 10204 KB Output is correct
20 Correct 34 ms 25732 KB Output is correct
21 Correct 14 ms 8540 KB Output is correct
22 Correct 39 ms 11700 KB Output is correct
23 Correct 45 ms 13680 KB Output is correct
24 Correct 71 ms 15272 KB Output is correct
25 Correct 46 ms 14036 KB Output is correct
26 Correct 60 ms 18204 KB Output is correct
27 Correct 54 ms 19284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7260 KB Output is correct
3 Correct 3 ms 7260 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7480 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7384 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 37 ms 13024 KB Output is correct
11 Correct 27 ms 18164 KB Output is correct
12 Correct 88 ms 16468 KB Output is correct
13 Correct 45 ms 14808 KB Output is correct
14 Correct 63 ms 16468 KB Output is correct
15 Correct 3 ms 7516 KB Output is correct
16 Correct 3 ms 7520 KB Output is correct
17 Correct 3 ms 7516 KB Output is correct
18 Correct 41 ms 26016 KB Output is correct
19 Correct 61 ms 30036 KB Output is correct
20 Correct 57 ms 26192 KB Output is correct
21 Correct 42 ms 12380 KB Output is correct
22 Correct 36 ms 12368 KB Output is correct
23 Correct 34 ms 18260 KB Output is correct
24 Correct 24 ms 10716 KB Output is correct
25 Correct 35 ms 26192 KB Output is correct
26 Correct 46 ms 11776 KB Output is correct
27 Correct 38 ms 11608 KB Output is correct
28 Correct 32 ms 17500 KB Output is correct
29 Correct 23 ms 10204 KB Output is correct
30 Correct 34 ms 25732 KB Output is correct
31 Correct 6 ms 8028 KB Output is correct
32 Correct 20 ms 10724 KB Output is correct
33 Correct 18 ms 10704 KB Output is correct
34 Correct 18 ms 10844 KB Output is correct
35 Correct 7 ms 9180 KB Output is correct
36 Correct 22 ms 13148 KB Output is correct
37 Correct 20 ms 16728 KB Output is correct
38 Correct 14 ms 8540 KB Output is correct
39 Correct 39 ms 11700 KB Output is correct
40 Correct 45 ms 13680 KB Output is correct
41 Correct 71 ms 15272 KB Output is correct
42 Correct 46 ms 14036 KB Output is correct
43 Correct 60 ms 18204 KB Output is correct
44 Correct 54 ms 19284 KB Output is correct
45 Correct 13 ms 8540 KB Output is correct
46 Correct 44 ms 12000 KB Output is correct
47 Correct 53 ms 13856 KB Output is correct
48 Correct 75 ms 16208 KB Output is correct
49 Correct 46 ms 14692 KB Output is correct
50 Correct 70 ms 19024 KB Output is correct
51 Correct 57 ms 20304 KB Output is correct