#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using pil = pair<int, ll>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
const int N = 2e5 + 5;
const int lg = 18;
vi g[N];
int n;
int A[N], H[N];
ll C[N];
vector<pil> mem[N];
int sub[N];
namespace bit {
int edits[4*lg*N], ptr = 0;
ll ft[N];
void clear() {
while(ptr)
ft[edits[--ptr]] = 0;
}
void update(int l, int r, ll x) {
for(; l <= n; l += l & -l) {
ft[l] += x;
edits[ptr++] = l;
}
for(r++; r <= n; r += r & -r) {
ft[r] -= x;
edits[ptr++] = r;
}
}
ll query(int pos) {
ll ret = 0;
for(; pos > 0; pos -= pos & -pos)
ret += ft[pos];
return ret;
}
int func(int r, ll x) { // max l < r such that query(l) < x
int idx = 0; ll sm = 0;
for(int pw = 1 << (lg-1); pw; pw >>= 1) {
if((idx|pw) < r and sm + ft[idx|pw] < x) {
idx |= pw;
sm += ft[idx];
}
}
return idx;
}
vector<pil> grab() {
std::vector<pil> ret;
int l = 1;
while(l <= n) {
ll x = query(l);
int r = func(n+1, x+1);
ret.emplace_back(l, x);
l = r+1;
}
ret.emplace_back(n+1, 69); // hehe
return ret;
}
}
void solve(int u, bool keep) {
ii big;
for(int v : g[u])
big = max(big, ii(sub[v], v));
for(int v : g[u])
if(v != big.se) solve(v, 0);
if(big.se) solve(big.se, 1);
for(int v : g[u]) if(v != big.se) {
vector<pil>& f = mem[v];
for(int i = 0; i < sz(f)-1; ++i) {
bit::update(f[i].fi, f[i+1].fi-1, f[i].se);
}
}
ll val = bit::query(H[u]);
bit::update(1, n, C[u]);
int r = H[u];
for(ll val_r = bit::query(r); val_r > val; val_r = bit::query(r)) {
int l = bit::func(r, val_r);
ll x = val_r - val;
bit::update(l+1, r, -x);
r = l;
}
if(!keep) {
mem[u] = bit::grab();
bit::clear();
}
}
int vis[N], onstk[N], par[N];
set<ii> edges;
vector<vi> cyc;
void dfs(int u) {
vis[u] = onstk[u] = 1;
for(int v : {A[u]}) {
if(vis[v]) {
if(onstk[v]) {
cyc.push_back({});
vi &bk = cyc.back();
int uu = u;
bk.emplace_back(uu);
while(uu != v) {
uu = par[uu];
bk.emplace_back(uu);
}
// reverse(all(bk));
}
} else {
par[v] = u;
dfs(v);
}
}
onstk[u] = 0;
}
void dfs_init(int u) {
sub[u] = 1;
for(int v : g[u]) {
dfs_init(v);
sub[u] += sub[v];
}
}
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d %d %lld", &A[i], &H[i], &C[i]);
}
{ // compress
vi tmp(H+1, H+1+n);
sort(all(tmp));
tmp.erase(unique(all(tmp)), tmp.end());
for(int i = 1; i <= n; ++i)
H[i] = int(lower_bound(all(tmp), H[i]) - tmp.begin()) + 1;
}
for(int i = 1; i <= n; ++i) {
if(!vis[i]) {
dfs(i);
}
}
for(vi& c : cyc) for(int u : c) A[u] = -1;
for(int i = 1; i <= n; ++i)
if(~A[i]) g[A[i]].emplace_back(i);
ll ans = 0;
vector<ll> sm(n+1);
for(vi &c : cyc) {
vi ls;
ll tot = 0;
for(int u : c) {
ls.insert(ls.end(), all(g[u]));
tot += C[u];
sm[H[u]] += C[u];
}
for(int v : ls) {
dfs_init(v);
solve(v, 0);
}
for(int v : ls) {
vector<pil>& f = mem[v];
for(int i = 0; i < sz(f)-1; ++i) {
bit::update(f[i].fi, f[i+1].fi-1, f[i].se);
}
}
ll now = LLONG_MAX;
for(int u : c) {
int x = H[u];
now = min(now, tot - sm[x] + bit::query(x));
}
now = min(now, tot - sm[1] + bit::query(1));
ans += now;
bit::clear();
for(int u : c) sm[H[u]] -= C[u];
}
printf("%lld\n", ans);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main(int, const char**)':
worst_reporter2.cpp:146:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
146 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
worst_reporter2.cpp:148:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
148 | scanf("%d %d %lld", &A[i], &H[i], &C[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
16 ms |
10860 KB |
Output is correct |
6 |
Correct |
16 ms |
10604 KB |
Output is correct |
7 |
Correct |
13 ms |
10476 KB |
Output is correct |
8 |
Correct |
19 ms |
10860 KB |
Output is correct |
9 |
Correct |
14 ms |
10500 KB |
Output is correct |
10 |
Correct |
13 ms |
10476 KB |
Output is correct |
11 |
Correct |
12 ms |
10348 KB |
Output is correct |
12 |
Correct |
13 ms |
11628 KB |
Output is correct |
13 |
Correct |
11 ms |
11628 KB |
Output is correct |
14 |
Correct |
13 ms |
10988 KB |
Output is correct |
15 |
Correct |
12 ms |
10988 KB |
Output is correct |
16 |
Correct |
17 ms |
10988 KB |
Output is correct |
17 |
Correct |
14 ms |
10604 KB |
Output is correct |
18 |
Correct |
12 ms |
10348 KB |
Output is correct |
19 |
Correct |
14 ms |
11372 KB |
Output is correct |
20 |
Correct |
13 ms |
11244 KB |
Output is correct |
21 |
Correct |
12 ms |
11372 KB |
Output is correct |
22 |
Correct |
13 ms |
10988 KB |
Output is correct |
23 |
Correct |
13 ms |
11116 KB |
Output is correct |
24 |
Correct |
14 ms |
11372 KB |
Output is correct |
25 |
Correct |
13 ms |
11244 KB |
Output is correct |
26 |
Correct |
12 ms |
11372 KB |
Output is correct |
27 |
Correct |
14 ms |
11372 KB |
Output is correct |
28 |
Correct |
15 ms |
11500 KB |
Output is correct |
29 |
Correct |
13 ms |
11500 KB |
Output is correct |
30 |
Correct |
13 ms |
10988 KB |
Output is correct |
31 |
Correct |
13 ms |
11116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
10860 KB |
Output is correct |
2 |
Correct |
608 ms |
58840 KB |
Output is correct |
3 |
Correct |
490 ms |
42612 KB |
Output is correct |
4 |
Correct |
602 ms |
58016 KB |
Output is correct |
5 |
Correct |
478 ms |
42460 KB |
Output is correct |
6 |
Correct |
363 ms |
34140 KB |
Output is correct |
7 |
Correct |
318 ms |
32860 KB |
Output is correct |
8 |
Correct |
366 ms |
98184 KB |
Output is correct |
9 |
Correct |
271 ms |
100256 KB |
Output is correct |
10 |
Correct |
219 ms |
93788 KB |
Output is correct |
11 |
Correct |
317 ms |
63836 KB |
Output is correct |
12 |
Correct |
265 ms |
65116 KB |
Output is correct |
13 |
Correct |
643 ms |
67544 KB |
Output is correct |
14 |
Correct |
474 ms |
47068 KB |
Output is correct |
15 |
Correct |
236 ms |
32860 KB |
Output is correct |
16 |
Correct |
368 ms |
85140 KB |
Output is correct |
17 |
Correct |
286 ms |
80092 KB |
Output is correct |
18 |
Correct |
269 ms |
85596 KB |
Output is correct |
19 |
Correct |
367 ms |
68572 KB |
Output is correct |
20 |
Correct |
314 ms |
72924 KB |
Output is correct |
21 |
Correct |
358 ms |
85212 KB |
Output is correct |
22 |
Correct |
288 ms |
80420 KB |
Output is correct |
23 |
Correct |
217 ms |
83548 KB |
Output is correct |
24 |
Correct |
403 ms |
85080 KB |
Output is correct |
25 |
Correct |
360 ms |
88920 KB |
Output is correct |
26 |
Correct |
331 ms |
89048 KB |
Output is correct |
27 |
Correct |
280 ms |
61016 KB |
Output is correct |
28 |
Correct |
276 ms |
61016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
16 ms |
10860 KB |
Output is correct |
6 |
Correct |
16 ms |
10604 KB |
Output is correct |
7 |
Correct |
13 ms |
10476 KB |
Output is correct |
8 |
Correct |
19 ms |
10860 KB |
Output is correct |
9 |
Correct |
14 ms |
10500 KB |
Output is correct |
10 |
Correct |
13 ms |
10476 KB |
Output is correct |
11 |
Correct |
12 ms |
10348 KB |
Output is correct |
12 |
Correct |
13 ms |
11628 KB |
Output is correct |
13 |
Correct |
11 ms |
11628 KB |
Output is correct |
14 |
Correct |
13 ms |
10988 KB |
Output is correct |
15 |
Correct |
12 ms |
10988 KB |
Output is correct |
16 |
Correct |
17 ms |
10988 KB |
Output is correct |
17 |
Correct |
14 ms |
10604 KB |
Output is correct |
18 |
Correct |
12 ms |
10348 KB |
Output is correct |
19 |
Correct |
14 ms |
11372 KB |
Output is correct |
20 |
Correct |
13 ms |
11244 KB |
Output is correct |
21 |
Correct |
12 ms |
11372 KB |
Output is correct |
22 |
Correct |
13 ms |
10988 KB |
Output is correct |
23 |
Correct |
13 ms |
11116 KB |
Output is correct |
24 |
Correct |
14 ms |
11372 KB |
Output is correct |
25 |
Correct |
13 ms |
11244 KB |
Output is correct |
26 |
Correct |
12 ms |
11372 KB |
Output is correct |
27 |
Correct |
14 ms |
11372 KB |
Output is correct |
28 |
Correct |
15 ms |
11500 KB |
Output is correct |
29 |
Correct |
13 ms |
11500 KB |
Output is correct |
30 |
Correct |
13 ms |
10988 KB |
Output is correct |
31 |
Correct |
13 ms |
11116 KB |
Output is correct |
32 |
Correct |
16 ms |
10860 KB |
Output is correct |
33 |
Correct |
608 ms |
58840 KB |
Output is correct |
34 |
Correct |
490 ms |
42612 KB |
Output is correct |
35 |
Correct |
602 ms |
58016 KB |
Output is correct |
36 |
Correct |
478 ms |
42460 KB |
Output is correct |
37 |
Correct |
363 ms |
34140 KB |
Output is correct |
38 |
Correct |
318 ms |
32860 KB |
Output is correct |
39 |
Correct |
366 ms |
98184 KB |
Output is correct |
40 |
Correct |
271 ms |
100256 KB |
Output is correct |
41 |
Correct |
219 ms |
93788 KB |
Output is correct |
42 |
Correct |
317 ms |
63836 KB |
Output is correct |
43 |
Correct |
265 ms |
65116 KB |
Output is correct |
44 |
Correct |
643 ms |
67544 KB |
Output is correct |
45 |
Correct |
474 ms |
47068 KB |
Output is correct |
46 |
Correct |
236 ms |
32860 KB |
Output is correct |
47 |
Correct |
368 ms |
85140 KB |
Output is correct |
48 |
Correct |
286 ms |
80092 KB |
Output is correct |
49 |
Correct |
269 ms |
85596 KB |
Output is correct |
50 |
Correct |
367 ms |
68572 KB |
Output is correct |
51 |
Correct |
314 ms |
72924 KB |
Output is correct |
52 |
Correct |
358 ms |
85212 KB |
Output is correct |
53 |
Correct |
288 ms |
80420 KB |
Output is correct |
54 |
Correct |
217 ms |
83548 KB |
Output is correct |
55 |
Correct |
403 ms |
85080 KB |
Output is correct |
56 |
Correct |
360 ms |
88920 KB |
Output is correct |
57 |
Correct |
331 ms |
89048 KB |
Output is correct |
58 |
Correct |
280 ms |
61016 KB |
Output is correct |
59 |
Correct |
276 ms |
61016 KB |
Output is correct |
60 |
Correct |
7 ms |
9708 KB |
Output is correct |
61 |
Correct |
7 ms |
9708 KB |
Output is correct |
62 |
Correct |
8 ms |
9708 KB |
Output is correct |
63 |
Correct |
8 ms |
9708 KB |
Output is correct |
64 |
Correct |
603 ms |
53848 KB |
Output is correct |
65 |
Correct |
459 ms |
40284 KB |
Output is correct |
66 |
Correct |
580 ms |
50708 KB |
Output is correct |
67 |
Correct |
457 ms |
42408 KB |
Output is correct |
68 |
Correct |
361 ms |
34204 KB |
Output is correct |
69 |
Correct |
330 ms |
32736 KB |
Output is correct |
70 |
Correct |
182 ms |
36700 KB |
Output is correct |
71 |
Correct |
155 ms |
33124 KB |
Output is correct |
72 |
Correct |
186 ms |
42460 KB |
Output is correct |
73 |
Correct |
160 ms |
42460 KB |
Output is correct |
74 |
Correct |
348 ms |
59740 KB |
Output is correct |
75 |
Correct |
306 ms |
61276 KB |
Output is correct |
76 |
Correct |
290 ms |
63964 KB |
Output is correct |
77 |
Correct |
276 ms |
57008 KB |
Output is correct |
78 |
Correct |
247 ms |
58580 KB |
Output is correct |
79 |
Correct |
390 ms |
51848 KB |
Output is correct |
80 |
Correct |
317 ms |
43740 KB |
Output is correct |
81 |
Correct |
257 ms |
38748 KB |
Output is correct |
82 |
Correct |
183 ms |
42716 KB |
Output is correct |
83 |
Correct |
193 ms |
32160 KB |
Output is correct |
84 |
Correct |
352 ms |
56156 KB |
Output is correct |
85 |
Correct |
366 ms |
55988 KB |
Output is correct |
86 |
Correct |
352 ms |
56308 KB |
Output is correct |
87 |
Correct |
343 ms |
57052 KB |
Output is correct |
88 |
Correct |
387 ms |
59220 KB |
Output is correct |