Submission #448411

#TimeUsernameProblemLanguageResultExecution timeMemory
448411flappybirdMagic Tree (CEOI19_magictree)C++14
0 / 100
2069 ms28944 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,fma") using namespace std; typedef long long ll; typedef pair<ll, ll> pll; #define MAX 101010 #define MAXS 20 #define ln '\n' struct segtree { ll N, s; vector<ll> tree, l, r; void init(ll x = 1) { if (x >= s) { l[x] = r[x] = x - s + 1; return; } init(x * 2); init(x * 2 + 1); l[x] = l[x * 2]; r[x] = r[x * 2 + 1]; } segtree(ll n) { N = n; s = 1LL << (ll)ceil(log2(N)); tree.resize(2 * s + 1); l.resize(2 * s + 1); r.resize(2 * s + 1); init(); } void update(ll x, ll a) { x += s - 1; tree[x] = max(tree[x], a); x /= 2; while (x) { tree[x] = max(tree[x * 2], tree[x * 2 + 1]); x /= 2; } } ll query(ll low, ll high, ll loc = 1) { if (low > high) return 0; if (l[loc] == low && r[loc] == high) return tree[loc]; if (r[loc * 2] >= high) return query(low, high, loc * 2); if (l[loc * 2 + 1] <= low) return query(low, high, loc * 2 + 1); return max(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1)); } segtree() {} }; struct Fruit { ll v; ll w; }; ll sp[MAX][MAXS]; ll cnt = 0; ll ord[MAX]; ll dp[MAX]; pll range[MAX]; vector<ll> adj[MAX]; vector<Fruit> fruit[MAX]; ll dfs(ll x = 1, ll p = 0) { ord[x] = ++cnt; ll mx = ord[x]; for (auto v : adj[x]) { if (v == p) continue; mx = max(mx, dfs(v, x)); } range[x] = { ord[x], mx }; return mx; } signed main() { ios::sync_with_stdio(false), cin.tie(0); ll N, M, K; cin >> N >> M >> K; ll i, j; for (i = 2; i <= N; i++) { cin >> sp[i][0]; adj[sp[i][0]].push_back(i); adj[i].push_back(sp[i][0]); } for (i = 1; i <= N; i++) { for (j = 1; j < MAXS; j++) sp[i][j] = sp[sp[i][j - 1]][j - 1]; } ll a; Fruit f; for (i = 1; i <= M; i++) { cin >> f.v >> a >> f.w; fruit[a].push_back(f); } /* dfs(); segtree rmq; rmq = segtree(N); for (i = 1; i <= K; i++) { for (auto f : fruit[i]) { ll v = f.v; ll w = f.w; ll sum = 0; for (auto x : adj[v]) { if (x == sp[v][0]) continue; sum += rmq.query(range[x].first, range[x].second); } rmq.update(ord[v], sum + w); } } cout << rmq.query(2, N) << ln; */ for (i = K; i >= 1; i--) { for (auto f : fruit[i]) { ll v = f.v; ll w = f.w; ll d = w - dp[v]; d = max(d, 0LL); while (v) { dp[v] += d; v = sp[v][0]; } } } cout << dp[1] << ln; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...