Submission #921756

# Submission time Handle Problem Language Result Execution time Memory
921756 2024-02-04T09:52:37 Z sleepntsheep Magic Tree (CEOI19_magictree) C++17
3 / 100
2000 ms 235840 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>

using u32 = unsigned;
using i32 = int;
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;

#define int long long
using namespace std;
using pt = complex<f80>;
#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 100005

set<pair<int, i64>> s[N];


int n, m, k; 
i64 dp[N];
basic_string<int> g[N];
deque<pair<int, i64>> pre[N];

void dfs(int u)
{
    i64 nocur = 0, cur = 0, fruit = s[u].size(), d, w;
    if (fruit) tie(d, cur) = *s[u].begin();

    for (auto v : g[u])
    {
        dfs(v);
        nocur += dp[v];
        if (s[v].size() > s[u].size()) s[v].swap(s[u]);
        for (auto &[x, x2] : s[v])
        {
            auto it = s[u].lower_bound(make_pair(x, 0));
            if (it != s[u].end())
            {
                int nw = it->second + x2;
                s[u].erase(it);
                s[u].emplace(x, nw);
            }
            else s[u].emplace(x, x2);
        }
    }

    {
        pre[u].emplace_back(k+1, 0);
        for (auto &[x, x2] : s[u]) pre[u].emplace_back(x, x2);
        //for (int i = (int)pre[u].size() - 2; i >= 0; --i) pre[u][i].second += pre[u][i+1].second;
        for (int i = 1; i < (int)pre[u].size(); ++i)
            pre[u][i].second += pre[u][i-1].second;
    }

    if (!fruit) return void(dp[u] = nocur);

    for (auto v : g[u])
    {
        dfs(v);
        int l = 0, r = (int)pre[v].size() - 1, z = -1;
        while (l <= r)
        {
            int m = (l+r)/2;
            if (pre[v][m].first <= d) l = m + 1, z = m;
            else r = m - 1;
        }
        if (z != -1) cur += pre[v][z].second;
    }

    dp[u] = max(cur, nocur);
}

signed main()
{
    ShinLena;
    cin >> n >> m >> k;
    for (int i = 2, p; i <= n; ++i) cin >> p, g[p].push_back(i);
    for (int v, d, w, i = 0; i < m; ++i)
    {
        cin >> v >> d >> w;
        s[v].emplace(d, w);
    }
    dfs(1);
    cout << dp[1];

    return 0;
}


Compilation message

magictree.cpp: In function 'void dfs(long long int)':
magictree.cpp:39:53: warning: unused variable 'w' [-Wunused-variable]
   39 |     i64 nocur = 0, cur = 0, fruit = s[u].size(), d, w;
      |                                                     ^
# Verdict Execution time Memory Grader output
1 Correct 38 ms 76300 KB Output is correct
2 Incorrect 39 ms 76380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 98 ms 79792 KB Output is correct
2 Correct 257 ms 235840 KB Output is correct
3 Correct 118 ms 84680 KB Output is correct
4 Correct 100 ms 82840 KB Output is correct
5 Correct 210 ms 148348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2029 ms 79976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2044 ms 92916 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 76300 KB Output is correct
2 Incorrect 39 ms 76380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 49 ms 76624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 76300 KB Output is correct
2 Incorrect 39 ms 76380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 76300 KB Output is correct
2 Incorrect 39 ms 76380 KB Output isn't correct
3 Halted 0 ms 0 KB -