Submission #388088

#TimeUsernameProblemLanguageResultExecution timeMemory
388088myrcellaWorst Reporter 4 (JOI21_worst_reporter4)C++17
100 / 100
541 ms70124 KiB
//by szh #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define debug(x) cerr<<#x<<"="<<x<<endl #define pq priority_queue #define inf 0x3f #define rep(i,a,b) for (int i=a;i<(b);i++) #define MP make_pair #define SZ(x) (int(x.size())) #define ll long long #define big 1000000000 #define ALL(x) x.begin(),x.end() int lowbit(int x) {return x&(-x);} const int maxn=2e5+10; int n; vector <int> edge[maxn]; int h[maxn]; int c[maxn]; map <int,ll> ans[maxn]; void combine(map<int,ll> &a,map<int,ll> b) { auto cur=a.begin(); ll tmp=0; for (auto it=b.begin();it!=b.end();it++) { tmp+=it->se; cur->se+=tmp; cur=a.upper_bound(it->fi); if (cur!=a.end()) cur->se-=tmp; if (a.find(it->fi)==a.end()) { if (cur==a.end()) break; a[it->fi]=cur->se+tmp; cur->se=-tmp; } } while (cur!=a.end()) cur=a.erase(cur); return; } map<int,ll> solve(int u) { map<int,ll> ret; if (edge[u].empty()) { ret[big]=c[u]; ret[h[u]]=0; return ret; } ret=solve(edge[u][0]); rep(i,1,SZ(edge[u])) { auto cur=solve(edge[u][i]); if (SZ(ret)<SZ(cur)) swap(cur,ret); combine(ret,cur); } // debug(ret.begin()->fi);debug(ret.begin()->se); auto it=ret.lower_bound(h[u]); if ((it->fi)!=h[u]) { ret[h[u]]=(it->se); it->se=0; } it=ret.find(h[u]); while (it!=ret.begin()&&((it->se)<=c[u])) { auto tmp=it;tmp--; it->se+=tmp->se; it=ret.erase(tmp); } it->se-=(ll)c[u]; ret.begin()->se+=(ll)c[u]; it=ret.upper_bound(h[u]); it->se+=(ll)c[u]; // debug(u); // for (auto it=ret.begin();it!=ret.end();it++) cout<<(it->fi)<<" "<<(it->se)<<endl; return ret; } int vis[maxn]; int cycle[maxn]; ll sum[maxn]; vector <int> sta; void dfs(int u) { vis[u]=1; for (int i:edge[u]) { if (vis[i]==2) continue; if (vis[i]==1) cycle[u]=i,sta.pb(i); else { dfs(i); if (cycle[i]!=-1) cycle[u]=cycle[i]; } } vis[u]=2; if (cycle[u]!=-1) { if (ans[cycle[u]].find(h[u])!=ans[cycle[u]].end()) ans[cycle[u]][h[u]]-=c[u]; else ans[cycle[u]][h[u]]=-c[u]; sum[cycle[u]]+=c[u]; } } map <int,ll> son; int main() { // freopen("input.txt","r",stdin); std::ios::sync_with_stdio(false);cin.tie(); memset(cycle,-1,sizeof(cycle)); cin>>n; rep(i,1,n+1) { int x; cin>>x>>h[i]>>c[i]; edge[x].pb(i); } rep(i,1,n+1) if(vis[i]!=2) dfs(i); ll res=0; for (int i:sta) { for (auto &it:ans[i]) it.se+=sum[i]; ans[i][0]=sum[i]; int u=i; son.clear(); son[big]=0; do { int tmp; for (int v:edge[u]) { if (cycle[v]==-1) { auto cur=solve(v); if (SZ(son)<SZ(cur)) swap(son,cur); combine(son,cur); } else tmp=v; } u=tmp; }while (u!=i); // for (auto it:son) debug(it.fi),debug(it.se); ll ress=1e18,sum=0; auto cur=ans[i].begin(); for (auto it:son) { sum+=it.se; while (cur!=ans[i].end()&&cur->fi<=it.fi) { ress=min(ress,cur->se+sum); cur++; } } res+=ress; } cout<<res; return 0; }

Compilation message (stderr)

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:135:12: warning: 'tmp' may be used uninitialized in this function [-Wmaybe-uninitialized]
  135 |   }while (u!=i);
      |           ~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...