Submission #723688

# Submission time Handle Problem Language Result Execution time Memory
723688 2023-04-14T07:33:52 Z dxz05 Magic Tree (CEOI19_magictree) C++17
61 / 100
1849 ms 791852 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;

struct fenwick{
    vector<int> f;
    fenwick(int n){
        f.resize(n + 2);
    };
    void add(int i, int x){
        i++;
        while (i < (int)f.size()){
            f[i] = max(f[i], x);
            i += (-i) & i;
        }
    }
    int get(int i){
        i++;
        int res = 0;
        while (i > 0){
            res = max(res, f[i]);
            i -= (-i) & i;
        }
        return res;
    }
};

vector<int> g[N];

vector<vector<ll>> dp;
pair<int, int> fruit[N];

void dfs(int v){
    dp[v][fruit[v].first] = fruit[v].second;

    for (int u : g[v]){
        dfs(u);
    }

    for (int t = 1; t < dp[v].size(); t++){
        for (int u : g[v]){
            dp[v][t] += dp[u][t];
        }
    }

    for (int t = 1; t < dp[v].size(); t++) dp[v][t] = max(dp[v][t], dp[v][t - 1]);

}

int LIS(const vector<int> &a){
    int n = (int) a.size();
    fenwick f(2e5);
    int res = 0;
    for (int i = 0; i < n; i++){
        int _dp = f.get(a[i]) + 1;
        f.add(a[i], _dp);
        res = max(res, _dp);
    }
    return res;
}

void solve(){
    int n, m, k;
    cin >> n >> m >> k;

    ll sum = 0;
    bool sub3 = true;
    for (int i = 1; i < n; i++){
        int p;
        cin >> p;
        --p;
        if (p != i - 1) sub3 = false;
        g[p].push_back(i);
    }

    map<int, int> mp;

    vector<int> a(n);
    for (int i = 0; i < m; i++){
        int v, d, w;
        cin >> v >> d >> w;
        --v;
        mp[d];
        fruit[v] = MP(d, w);
        sum += w;
        a[v] = d;
        if (w != 1) sub3 = false;
    }

    if (sub3){
        vector<int> b;
        for (int x : a){
            if (x != 0) b.push_back(x);
        }
        reverse(all(b));
        cout << LIS(b) << endl;
        return;
    }

    mp[0];
    k = 0;
    for (auto &now : mp){
        now.second = ++k;
    }

    for (int i = 0; i < n; i++){
        fruit[i].first = mp[fruit[i].first];
    }

    if (k > 1000){
        cout << sum << endl;
        return;
    }

    dp.assign(n, vector<ll>(k + 1, 0));

    dfs(0);

    cout << dp[0].back() << endl;

}

int main() {
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();

    return 0;
}

Compilation message

magictree.cpp: In function 'void dfs(int)':
magictree.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int t = 1; t < dp[v].size(); t++){
      |                     ~~^~~~~~~~~~~~~~
magictree.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int t = 1; t < dp[v].size(); t++) dp[v][t] = max(dp[v][t], dp[v][t - 1]);
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2692 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 6720 KB Output is correct
2 Correct 32 ms 7500 KB Output is correct
3 Correct 84 ms 7712 KB Output is correct
4 Correct 74 ms 7532 KB Output is correct
5 Correct 82 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3540 KB Output is correct
2 Correct 2 ms 3540 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 53 ms 11720 KB Output is correct
5 Correct 64 ms 11820 KB Output is correct
6 Correct 89 ms 11864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 13024 KB Output is correct
2 Correct 72 ms 11172 KB Output is correct
3 Correct 64 ms 15412 KB Output is correct
4 Correct 44 ms 11544 KB Output is correct
5 Correct 51 ms 18948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2692 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 104 ms 26884 KB Output is correct
11 Correct 75 ms 18764 KB Output is correct
12 Correct 73 ms 29268 KB Output is correct
13 Correct 77 ms 25384 KB Output is correct
14 Correct 52 ms 8132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 128020 KB Output is correct
2 Correct 694 ms 607472 KB Output is correct
3 Correct 730 ms 610596 KB Output is correct
4 Correct 938 ms 789140 KB Output is correct
5 Correct 1849 ms 787856 KB Output is correct
6 Correct 864 ms 789792 KB Output is correct
7 Correct 853 ms 791852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2692 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 2 ms 3540 KB Output is correct
12 Correct 2 ms 3540 KB Output is correct
13 Correct 53 ms 11720 KB Output is correct
14 Correct 64 ms 11820 KB Output is correct
15 Correct 89 ms 11864 KB Output is correct
16 Correct 104 ms 26884 KB Output is correct
17 Correct 75 ms 18764 KB Output is correct
18 Correct 73 ms 29268 KB Output is correct
19 Correct 77 ms 25384 KB Output is correct
20 Correct 52 ms 8132 KB Output is correct
21 Incorrect 12 ms 3540 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2692 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 39 ms 6720 KB Output is correct
11 Correct 32 ms 7500 KB Output is correct
12 Correct 84 ms 7712 KB Output is correct
13 Correct 74 ms 7532 KB Output is correct
14 Correct 82 ms 7764 KB Output is correct
15 Correct 3 ms 3540 KB Output is correct
16 Correct 2 ms 3540 KB Output is correct
17 Correct 2 ms 3540 KB Output is correct
18 Correct 53 ms 11720 KB Output is correct
19 Correct 64 ms 11820 KB Output is correct
20 Correct 89 ms 11864 KB Output is correct
21 Correct 68 ms 13024 KB Output is correct
22 Correct 72 ms 11172 KB Output is correct
23 Correct 64 ms 15412 KB Output is correct
24 Correct 44 ms 11544 KB Output is correct
25 Correct 51 ms 18948 KB Output is correct
26 Correct 104 ms 26884 KB Output is correct
27 Correct 75 ms 18764 KB Output is correct
28 Correct 73 ms 29268 KB Output is correct
29 Correct 77 ms 25384 KB Output is correct
30 Correct 52 ms 8132 KB Output is correct
31 Correct 150 ms 128020 KB Output is correct
32 Correct 694 ms 607472 KB Output is correct
33 Correct 730 ms 610596 KB Output is correct
34 Correct 938 ms 789140 KB Output is correct
35 Correct 1849 ms 787856 KB Output is correct
36 Correct 864 ms 789792 KB Output is correct
37 Correct 853 ms 791852 KB Output is correct
38 Incorrect 12 ms 3540 KB Output isn't correct
39 Halted 0 ms 0 KB -