제출 #972716

#제출 시각아이디문제언어결과실행 시간메모리
972716huutuanWorst Reporter 4 (JOI21_worst_reporter4)C++14
100 / 100
351 ms123716 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int N=2e5+10; map<int, int> mp[N]; int n, a[N], h[N], c[N], sz[N]; vector<int> g[N]; int vis[N]; void dfs_sz(int u){ vis[u]=1; sz[u]=1; for (int &v:g[u]){ dfs_sz(v); sz[u]+=sz[v]; if (sz[v]>sz[g[u][0]]) swap(v, g[u][0]); } } void dfs_merge(int u){ if (g[u].empty()){ mp[u][1]=c[u]; mp[u][h[u]+1]=-c[u]; return; } for (int &v:g[u]){ dfs_merge(v); if (v==g[u][0]) mp[u].swap(mp[v]); else{ for (auto &i:mp[v]){ mp[u][i.first]+=i.second; } } } mp[u][h[u]+1]-=c[u]; auto it=mp[u].find(h[u]+1); int cur=c[u]; while (it!=mp[u].begin()){ --it; if (it->second<0 && cur>=-it->second){ cur-=-it->second; it=mp[u].erase(it); }else{ it->second+=cur; cur=0; break; } } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<int> vals{-1}; for (int i=1; i<=n; ++i){ cin >> a[i] >> h[i] >> c[i]; vals.push_back(h[i]); if (a[i]!=i) g[a[i]].push_back(i); } sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin()); for (int i=1; i<=n; ++i) h[i]=lower_bound(vals.begin(), vals.end(), h[i])-vals.begin(); int ans=0; for (int i=1; i<=n; ++i) if (!vis[i]){ int u=i; vector<int> tmp; while (!vis[u]){ tmp.push_back(u); vis[u]=1; u=a[u]; } vector<int> cycle(find(tmp.begin(), tmp.end(), u), tmp.end()); for (int j:cycle) vis[j]=2; vector<int> child; for (int j:cycle) for (int k:g[j]) if (vis[k]!=2) child.push_back(k); for (int &v:child){ dfs_sz(v); if (sz[v]>sz[child[0]]) swap(v, child[0]); } map<int, int> mpp; for (int &v:child){ dfs_merge(v); if (v==child[0]){ mpp.swap(mp[v]); }else{ for (auto &j:mp[v]) mpp[j.first]+=j.second; } } int cur=mpp.empty()?0:mpp.begin()->second; map<int, int> cc; for (int v:cycle) cc[h[v]]+=c[v]; vector<pair<int, int>> vv; vv.emplace_back(0, 0); for (auto &j:mpp) vv.emplace_back(j.first, vv.back().second+j.second); for (auto &j:cc){ auto it=lower_bound(vv.begin(), vv.end(), make_pair(j.first+1, (int)-1e18))-1; cur=max(cur, it->second+j.second); } ans+=cur; } cout << accumulate(c+1, c+n+1, 0ll)-ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...