Submission #1034640

#TimeUsernameProblemLanguageResultExecution timeMemory
1034640underwaterkillerwhaleWorst Reporter 4 (JOI21_worst_reporter4)C++17
100 / 100
329 ms81844 KiB
#include <bits/stdc++.h> #define se second #define fs first #define mp make_pair #define pb push_back #define ll long long #define ii pair<ll,ll> #define ld long double #define SZ(v) (int)v.size() #define ALL(v) v.begin(), v.end() #define bit(msk, i) ((msk >> i) & 1) #define iter(id, v) for(auto id : v) #define rep(i,m,n) for(int i=(m); i<=(n); i++) #define reb(i,m,n) for(int i=(m); i>=(n); i--) using namespace std; mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count()); ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); } const int N = 2e5 + 7; const int Mod = 1e9 + 7; const int szBL = 320; const int INF = 1e9; const int BASE = 137; struct Data { int A, H, C; }; int n; Data a[N]; int szC[N]; vector<int> ke[N]; map<int, ll> dp[N];///dp[u]: decreasing function int dd[N], par[N]; void dfs (int u, vector<int> &Val) { // cout<< u <<"\n"; if (!dd[u]) dd[u] = 1; Val.push_back(a[u].H); szC[u] = 1; int mxV = -1; iter (&v, ke[u]) { dfs(v, Val); szC[u] += szC[v]; if (mxV == -1 || szC[mxV] < szC[v]) mxV = v; } if (mxV != -1) swap(dp[u], dp[mxV]); iter (&v, ke[u]) { if (v == mxV) continue; iter (&id, dp[v]) { dp[u][id.fs] += id.se; } map<int, ll> ().swap(dp[v]); } dp[u][a[u].H] += a[u].C; ll val = a[u].C; auto it = dp[u].lower_bound(a[u].H); while (it != dp[u].begin()) { --it; if (val >= it->se) { val -= it->se; it = dp[u].erase(it); } else { it->se -= val; break; } } // cout << u<<": "; // iter (&id, dp[u]) cout <<id.fs<<","<<id.se<<" "; // cout<<"\n"; } void solution () { cin >> n; ll tot = 0; rep (i, 1, n) { cin >> a[i].A >> a[i].H >> a[i].C; tot += a[i].C; ke[a[i].A].push_back(i); par[i] = a[i].A; // cout << a[i].A <<" "<<i<<" h\n"; } ll res = 0; rep (u, 1, n) { if (dd[u]) continue; static vector<int> ver; ver.clear(); int cur = u; while (!dd[cur]) { dd[cur] = 1; ver.push_back(cur); cur = par[cur]; } vector<int> cycle (find(ALL(ver), cur), ver.end()); iter (&v, cycle) dd[v] = 2; static map<int, ll> dpr, cnt; dpr.clear(); cnt.clear(); static vector<int> Val; Val.clear(); iter (&v, cycle) { // cout << v<<"\n"; cnt[a[v].H] += a[v].C; Val.push_back(a[v].H); iter (&k, ke[v]) { if (dd[k] == 2) continue; dfs(k, Val); if (SZ(dp[k]) > SZ(dpr)) swap(dpr, dp[k]); iter (&id, dp[k]) { dpr[id.fs] += id.se; } } } dpr[INF] += 0; for (auto it = dpr.rbegin(); ; ++it) { if (next(it) == dpr.rend()) break; next(it)->se += it->se; // cout << it->fs <<" "<<it->se <<"\n"; } ll delta = 0; iter (&id, Val) { delta = max(delta, cnt[id] + dpr.lower_bound(id)->se); } res += delta; // cout<<"\n"; } cout << tot - res <<"\n"; } #define file(name) freopen(name".inp", "r", stdin); \ freopen(name".out", "w", stdout); int main () { // file("c"); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll num_Test = 1; // cin >> num_Test; while(num_Test--) solution(); } /* no bug challenge */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...