//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
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 time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15180 KB |
Output is correct |
2 |
Correct |
9 ms |
15144 KB |
Output is correct |
3 |
Correct |
9 ms |
15180 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
15 ms |
15676 KB |
Output is correct |
6 |
Correct |
14 ms |
15504 KB |
Output is correct |
7 |
Correct |
13 ms |
15476 KB |
Output is correct |
8 |
Correct |
16 ms |
15740 KB |
Output is correct |
9 |
Correct |
14 ms |
15492 KB |
Output is correct |
10 |
Correct |
13 ms |
15436 KB |
Output is correct |
11 |
Correct |
12 ms |
15436 KB |
Output is correct |
12 |
Correct |
13 ms |
16504 KB |
Output is correct |
13 |
Correct |
13 ms |
16460 KB |
Output is correct |
14 |
Correct |
13 ms |
15996 KB |
Output is correct |
15 |
Correct |
12 ms |
15948 KB |
Output is correct |
16 |
Correct |
17 ms |
15820 KB |
Output is correct |
17 |
Correct |
14 ms |
15500 KB |
Output is correct |
18 |
Correct |
11 ms |
15360 KB |
Output is correct |
19 |
Correct |
14 ms |
16076 KB |
Output is correct |
20 |
Correct |
12 ms |
15880 KB |
Output is correct |
21 |
Correct |
11 ms |
15948 KB |
Output is correct |
22 |
Correct |
13 ms |
15612 KB |
Output is correct |
23 |
Correct |
12 ms |
15420 KB |
Output is correct |
24 |
Correct |
14 ms |
16176 KB |
Output is correct |
25 |
Correct |
12 ms |
15964 KB |
Output is correct |
26 |
Correct |
12 ms |
16504 KB |
Output is correct |
27 |
Correct |
13 ms |
16076 KB |
Output is correct |
28 |
Correct |
15 ms |
16332 KB |
Output is correct |
29 |
Correct |
13 ms |
16460 KB |
Output is correct |
30 |
Correct |
14 ms |
16632 KB |
Output is correct |
31 |
Correct |
14 ms |
16588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
15684 KB |
Output is correct |
2 |
Correct |
535 ms |
36928 KB |
Output is correct |
3 |
Correct |
321 ms |
22212 KB |
Output is correct |
4 |
Correct |
541 ms |
32916 KB |
Output is correct |
5 |
Correct |
315 ms |
22104 KB |
Output is correct |
6 |
Correct |
204 ms |
21904 KB |
Output is correct |
7 |
Correct |
188 ms |
21700 KB |
Output is correct |
8 |
Correct |
197 ms |
65572 KB |
Output is correct |
9 |
Correct |
180 ms |
65476 KB |
Output is correct |
10 |
Correct |
131 ms |
65412 KB |
Output is correct |
11 |
Correct |
190 ms |
45260 KB |
Output is correct |
12 |
Correct |
171 ms |
45224 KB |
Output is correct |
13 |
Correct |
497 ms |
37192 KB |
Output is correct |
14 |
Correct |
286 ms |
22084 KB |
Output is correct |
15 |
Correct |
114 ms |
21668 KB |
Output is correct |
16 |
Correct |
282 ms |
49080 KB |
Output is correct |
17 |
Correct |
155 ms |
42076 KB |
Output is correct |
18 |
Correct |
127 ms |
41988 KB |
Output is correct |
19 |
Correct |
334 ms |
31836 KB |
Output is correct |
20 |
Correct |
130 ms |
19348 KB |
Output is correct |
21 |
Correct |
304 ms |
49600 KB |
Output is correct |
22 |
Correct |
152 ms |
42556 KB |
Output is correct |
23 |
Correct |
145 ms |
65404 KB |
Output is correct |
24 |
Correct |
278 ms |
51356 KB |
Output is correct |
25 |
Correct |
243 ms |
63580 KB |
Output is correct |
26 |
Correct |
224 ms |
69060 KB |
Output is correct |
27 |
Correct |
289 ms |
70124 KB |
Output is correct |
28 |
Correct |
299 ms |
70096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15180 KB |
Output is correct |
2 |
Correct |
9 ms |
15144 KB |
Output is correct |
3 |
Correct |
9 ms |
15180 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
15 ms |
15676 KB |
Output is correct |
6 |
Correct |
14 ms |
15504 KB |
Output is correct |
7 |
Correct |
13 ms |
15476 KB |
Output is correct |
8 |
Correct |
16 ms |
15740 KB |
Output is correct |
9 |
Correct |
14 ms |
15492 KB |
Output is correct |
10 |
Correct |
13 ms |
15436 KB |
Output is correct |
11 |
Correct |
12 ms |
15436 KB |
Output is correct |
12 |
Correct |
13 ms |
16504 KB |
Output is correct |
13 |
Correct |
13 ms |
16460 KB |
Output is correct |
14 |
Correct |
13 ms |
15996 KB |
Output is correct |
15 |
Correct |
12 ms |
15948 KB |
Output is correct |
16 |
Correct |
17 ms |
15820 KB |
Output is correct |
17 |
Correct |
14 ms |
15500 KB |
Output is correct |
18 |
Correct |
11 ms |
15360 KB |
Output is correct |
19 |
Correct |
14 ms |
16076 KB |
Output is correct |
20 |
Correct |
12 ms |
15880 KB |
Output is correct |
21 |
Correct |
11 ms |
15948 KB |
Output is correct |
22 |
Correct |
13 ms |
15612 KB |
Output is correct |
23 |
Correct |
12 ms |
15420 KB |
Output is correct |
24 |
Correct |
14 ms |
16176 KB |
Output is correct |
25 |
Correct |
12 ms |
15964 KB |
Output is correct |
26 |
Correct |
12 ms |
16504 KB |
Output is correct |
27 |
Correct |
13 ms |
16076 KB |
Output is correct |
28 |
Correct |
15 ms |
16332 KB |
Output is correct |
29 |
Correct |
13 ms |
16460 KB |
Output is correct |
30 |
Correct |
14 ms |
16632 KB |
Output is correct |
31 |
Correct |
14 ms |
16588 KB |
Output is correct |
32 |
Correct |
16 ms |
15684 KB |
Output is correct |
33 |
Correct |
535 ms |
36928 KB |
Output is correct |
34 |
Correct |
321 ms |
22212 KB |
Output is correct |
35 |
Correct |
541 ms |
32916 KB |
Output is correct |
36 |
Correct |
315 ms |
22104 KB |
Output is correct |
37 |
Correct |
204 ms |
21904 KB |
Output is correct |
38 |
Correct |
188 ms |
21700 KB |
Output is correct |
39 |
Correct |
197 ms |
65572 KB |
Output is correct |
40 |
Correct |
180 ms |
65476 KB |
Output is correct |
41 |
Correct |
131 ms |
65412 KB |
Output is correct |
42 |
Correct |
190 ms |
45260 KB |
Output is correct |
43 |
Correct |
171 ms |
45224 KB |
Output is correct |
44 |
Correct |
497 ms |
37192 KB |
Output is correct |
45 |
Correct |
286 ms |
22084 KB |
Output is correct |
46 |
Correct |
114 ms |
21668 KB |
Output is correct |
47 |
Correct |
282 ms |
49080 KB |
Output is correct |
48 |
Correct |
155 ms |
42076 KB |
Output is correct |
49 |
Correct |
127 ms |
41988 KB |
Output is correct |
50 |
Correct |
334 ms |
31836 KB |
Output is correct |
51 |
Correct |
130 ms |
19348 KB |
Output is correct |
52 |
Correct |
304 ms |
49600 KB |
Output is correct |
53 |
Correct |
152 ms |
42556 KB |
Output is correct |
54 |
Correct |
145 ms |
65404 KB |
Output is correct |
55 |
Correct |
278 ms |
51356 KB |
Output is correct |
56 |
Correct |
243 ms |
63580 KB |
Output is correct |
57 |
Correct |
224 ms |
69060 KB |
Output is correct |
58 |
Correct |
289 ms |
70124 KB |
Output is correct |
59 |
Correct |
299 ms |
70096 KB |
Output is correct |
60 |
Correct |
10 ms |
15180 KB |
Output is correct |
61 |
Correct |
10 ms |
15212 KB |
Output is correct |
62 |
Correct |
9 ms |
15212 KB |
Output is correct |
63 |
Correct |
9 ms |
15196 KB |
Output is correct |
64 |
Correct |
458 ms |
32196 KB |
Output is correct |
65 |
Correct |
345 ms |
23236 KB |
Output is correct |
66 |
Correct |
469 ms |
34928 KB |
Output is correct |
67 |
Correct |
326 ms |
23736 KB |
Output is correct |
68 |
Correct |
229 ms |
22644 KB |
Output is correct |
69 |
Correct |
241 ms |
22340 KB |
Output is correct |
70 |
Correct |
434 ms |
50024 KB |
Output is correct |
71 |
Correct |
268 ms |
34500 KB |
Output is correct |
72 |
Correct |
491 ms |
55888 KB |
Output is correct |
73 |
Correct |
288 ms |
43332 KB |
Output is correct |
74 |
Correct |
408 ms |
43316 KB |
Output is correct |
75 |
Correct |
298 ms |
31048 KB |
Output is correct |
76 |
Correct |
204 ms |
30784 KB |
Output is correct |
77 |
Correct |
347 ms |
43740 KB |
Output is correct |
78 |
Correct |
233 ms |
31292 KB |
Output is correct |
79 |
Correct |
479 ms |
46272 KB |
Output is correct |
80 |
Correct |
384 ms |
32984 KB |
Output is correct |
81 |
Correct |
221 ms |
32344 KB |
Output is correct |
82 |
Correct |
387 ms |
56012 KB |
Output is correct |
83 |
Correct |
141 ms |
51984 KB |
Output is correct |
84 |
Correct |
409 ms |
45636 KB |
Output is correct |
85 |
Correct |
378 ms |
45840 KB |
Output is correct |
86 |
Correct |
394 ms |
46132 KB |
Output is correct |
87 |
Correct |
391 ms |
45720 KB |
Output is correct |
88 |
Correct |
409 ms |
45636 KB |
Output is correct |