Submission #836749

#TimeUsernameProblemLanguageResultExecution timeMemory
836749JuanMagic Tree (CEOI19_magictree)C++17
3 / 100
126 ms16060 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define tii tuple<int,int,int,int> #define pii pair<int,int> #define ff first #define ss second #define all(x) x.begin(),x.end() const int maxn = 1e5 + 5; vector<int> g[maxn]; int tin[maxn], tout[maxn], pai[maxn], ans_arr[maxn], depth[maxn]; vector<tii> fruits; int n; int timer=1; void elr_tr(int u, int p){ tin[u] = timer++; depth[u] = depth[p]+1; for(int v : g[u]) if(v!=p){ elr_tr(v,u); } tout[u] = timer; } struct SEG{ int tree[4*maxn]; void fupd(int node, int tl, int tr, int id, int val){ if(tl>id || tr<id) return; if(tl==tr){tree[node] = val; return;} int mid = (tl+tr)>>1; int f1 = node*2, f2 = node*2+1; fupd(f1, tl, mid, id, val), fupd(f2, mid+1, tr, id, val); tree[node] = max(tree[f1], tree[f2]); } void upd(int id, int val){fupd(1, 1, n, id, val);} int fquery(int node, int tl, int tr, int l, int r){ if(tl>l || tr<r) return 0; if(tl>=l && tr<=r) return tree[node]; int mid = (tl+tr)>>1; int f1 = node*2, f2 = node*2+1; return max(fquery(f1, tl, mid, l, r), fquery(f2, mid+1, tr, l, r)); } int query(int l, int r){return fquery(1, 1, n, l, r);} }seg; int bit[maxn]; void upd(int x, int val){ for(; x < maxn; x+=x&-x) bit[x] += val; } int query(int l, int r){ int rt = 0; for(int i = r; i>0; i-=i&-i) rt += bit[i]; for(int i = l-1; i>0; i-=i&-i) rt -= bit[i]; return rt; } int solve(int u, int p){ int aux = 0; for(int v : g[u]) if(v!=p){ aux += solve(v,u); } return max(ans_arr[u], aux); } int32_t main(){ int m, k; cin >> n >> m >> k; for(int i = 2; i <= n; i++){ int p; cin >> p; pai[i] = p; g[i].push_back(p); g[p].push_back(i); } elr_tr(1,1); for(int i = 0; i < m; i++){ int v, d, w; cin >> v >> d >> w; fruits.push_back({d,depth[v],v,w}); } sort(all(fruits), greater<tii>()); for(auto[d,dep,u,w] : fruits){ for(int v : g[u]) if(v!=pai[u]) ans_arr[u] += seg.query(tin[v], tout[v]); ans_arr[u] = max(ans_arr[u], w); seg.upd(tin[u], ans_arr[u]); } cout << solve(1,1) << '\n'; }
#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...