Submission #928272

#TimeUsernameProblemLanguageResultExecution timeMemory
928272vjudge1Magic Tree (CEOI19_magictree)C++17
3 / 100
74 ms11308 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ent "\n" const int maxn = 1e5 + 100; const ll INF = (ll)1e18 + 100; const int MOD = 998244353; int n, m, k; vector<int> g[maxn]; vector<pii> q[maxn]; int f[maxn]; ll d[maxn * 4]; void upd(int i, ll x, int v = 1, int tl = 0, int tr = k){ if(tl == tr) d[v] = x; else{ int mid = (tl + tr) >> 1; if(i <= mid) upd(i, x, v<<1, tl, mid); else upd(i, x, v<<1|1, mid+1, tr); d[v] = max(d[v<<1], d[v<<1|1]); } } ll get(int l, int r, int v = 1, int tl = 0, int tr = k){ if(tr < l || tl > r) return -INF; if(l <= tl && tr <= r) return d[v]; int mid = (tl + tr) >> 1; return max(get(l, r, v<<1, tl, mid) , get(l, r, v<<1|1, mid+1, tr)); } void calc(int v, int i){ if(f[v]) return; f[v] = i; for(int to: g[v]){ calc(to, i); } } void test(){ cin >> n >> m >> k; for(int i = 2; i <= n; i++){ int p; cin >> p; g[p].push_back(i); } for(int i = 1; i <= m; i++){ int v, d, w; cin >> v >> d >> w; q[d].push_back({v, w}); } for(int i = 1; i <= k; i++){ vector<pii> s = {{0, 0}}; for(auto [v, d]: q[i]){ calc(v, i); s.push_back({f[v], d}); } ll mx = 0, sum = 0; s.push_back({i, 0}); sort(s.begin(), s.end()); for(int i = s.size() - 1; i > 0; i--){ sum += s[i].second; if(s[i-1].first < s[i].first){ mx = max(mx, get(s[i-1].first, s[i].first - 1) + sum); } } upd(i, mx); } cout << get(k, k); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t; t = 1; while(t--) test(); }
#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...