#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<ll, 3>
const ll inf = 1e15;
const int N = 2e5;
struct node{
ll x;
int l, r, s;
node(){
l = r = x = s = 0;
}
};
vector<node> all = {node()};
int CC = 0;
int nw(){
all.pb(node());
return ++CC;
}
struct DS{
vector<pair<int, ll>> vv;
int rt;
void init(){
rt = nw();
}
int size(){
return all[rt].s;
}
void fn(int v, int tl, int tr){
if (!all[v].s) return;
if (tl == tr){
vv.pb({tl, all[v].x});
return;
}
int tm = (tl + tr) / 2;
if (all[v].l) fn(all[v].l, tl, tm);
if (all[v].r) fn(all[v].r, tm + 1, tr);
}
void fn(){
vv.clear();
fn(rt, 1, N);
}
void recalc(int v){
all[v].x = all[v].s = 0;
if (all[v].l){
all[v].x += all[all[v].l].x;
all[v].s += all[all[v].l].s;
}
if (all[v].r){
all[v].x += all[all[v].r].x;
all[v].s += all[all[v].r].s;
}
}
void add(int v, int tl, int tr, int& p, ll& x){
if (tl == tr){
all[v].x += x;
all[v].s = 1;
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!all[v].l) all[v].l = nw();
add(all[v].l, tl, tm, p, x);
}
else {
if (!all[v].r) all[v].r = nw();
add(all[v].r, tm + 1, tr, p, x);
}
recalc(v);
}
void add(int l, int r, ll x){
add(rt, 1, N, l, x);
r++; x = -x;
if (r <= N) add(rt, 1, N, r, x);
}
void add1(int& p, ll& x){
add(rt, 1, N, p, x);
}
void clear(int& v, int tl, int tr, int l, int r){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
all[v] = node();
return;
}
int tm = (tl + tr) / 2;
if (all[v].l) clear(all[v].l, tl, tm, l, r);
if (all[v].r) clear(all[v].r, tm + 1, tr, l, r);
recalc(v);
}
void upd(int v, int tl, int tr, int& p, ll& x){
if (tl == tr){
all[v].x = x;
all[v].s = 1;
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!all[v].l) all[v].l = nw();
upd(all[v].l, tl, tm, p, x);
}
else {
if (!all[v].r) all[v].r = nw();
upd(all[v].r, tm + 1, tr, p, x);
}
recalc(v);
}
void upd(int p, ll x){
upd(rt, 1, N, p, x);
}
void ch(int l, int r, ll x){
ll S1 = 0, S2 = get(l - 1);
if (r < N) S1 = get(r + 1);
if (l < r) clear(rt, 1, N, l + 1, r);
upd(l, x - S2);
if (r < N) upd(r + 1, S1 - x);
}
ll get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return all[v].x;
int tm = (tl + tr) / 2;
ll out = 0;
if (all[v].l) out += get(all[v].l, tl, tm, l, r);
if (all[v].r) out += get(all[v].r, tm + 1, tr, l, r);
return out;
}
ll get(int p){
if (!p) return 0;
int l = 1;
return get(rt, 1, N, l, p);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1), h(n + 1), c(n + 1), g[n + 1];
for (int i = 1; i <= n; i++){
cin>>a[i]>>h[i]>>c[i];
if (a[i] != i) g[a[i]].pb(i);
}
vector<pii> all;
for (int i = 1; i <= n; i++) all.pb({h[i], i});
sort(all.begin(), all.end());
int i = 0, cc = 0;
while (i < all.size()){
int j = i;
while (j < all.size() && all[i] == all[j]){
j++;
}
cc++;
while (i < j){
h[all[i].ss] = cc;
i++;
}
}
DS T[n + 1];
function<void(int)> solve = [&](int v){
for (int i: g[v]) solve(i);
T[v].init();
for (int i: g[v]){
if (T[v].size() < T[i].size()) swap(T[v], T[i]);
T[i].fn();
for (auto [p, x]: T[i].vv){
T[v].add1(p, x);
}
}
if (h[v] > 1) T[v].add(1, h[v] - 1, c[v]);
if (h[v] < cc) T[v].add(h[v] + 1, cc, c[v]);
ll S1 = T[v].get(h[v]), S2 = (h[v] == cc) ? inf : T[v].get(h[v] + 1);
if (S1 < S2 && h[v] > 1 && T[v].get(h[v] - 1) > S1){
int l = 1, r = h[v] - 1;
while (l + 1 < r){
int m = (l + r) / 2;
ll k = T[v].get(m);
if (k <= S1){
l = m + 1;
}
else {
r = m;
}
}
if (T[v].get(l) > S1) r = l;
T[v].ch(r, h[v] - 1, S1);
}
};
solve(1);
ll out = 1e15;
for (int x = 1; x <= cc; x++) out = min(out, T[1].get(x));
cout<<out<<"\n";
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:158:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | while (i < all.size()){
| ~~^~~~~~~~~~~~
worst_reporter2.cpp:160:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | while (j < all.size() && all[i] == all[j]){
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
23 ms |
7392 KB |
Output is correct |
6 |
Correct |
28 ms |
7528 KB |
Output is correct |
7 |
Correct |
23 ms |
7432 KB |
Output is correct |
8 |
Correct |
23 ms |
7428 KB |
Output is correct |
9 |
Correct |
23 ms |
7424 KB |
Output is correct |
10 |
Correct |
24 ms |
7536 KB |
Output is correct |
11 |
Correct |
25 ms |
7420 KB |
Output is correct |
12 |
Correct |
16 ms |
3588 KB |
Output is correct |
13 |
Correct |
16 ms |
2700 KB |
Output is correct |
14 |
Correct |
16 ms |
3080 KB |
Output is correct |
15 |
Correct |
16 ms |
3076 KB |
Output is correct |
16 |
Correct |
29 ms |
14072 KB |
Output is correct |
17 |
Correct |
30 ms |
14072 KB |
Output is correct |
18 |
Correct |
22 ms |
7684 KB |
Output is correct |
19 |
Correct |
19 ms |
4612 KB |
Output is correct |
20 |
Correct |
19 ms |
4584 KB |
Output is correct |
21 |
Correct |
16 ms |
4608 KB |
Output is correct |
22 |
Correct |
21 ms |
7164 KB |
Output is correct |
23 |
Correct |
20 ms |
7248 KB |
Output is correct |
24 |
Correct |
17 ms |
4608 KB |
Output is correct |
25 |
Correct |
19 ms |
4608 KB |
Output is correct |
26 |
Correct |
6 ms |
2440 KB |
Output is correct |
27 |
Correct |
19 ms |
7656 KB |
Output is correct |
28 |
Correct |
17 ms |
4632 KB |
Output is correct |
29 |
Correct |
15 ms |
4868 KB |
Output is correct |
30 |
Correct |
7 ms |
2440 KB |
Output is correct |
31 |
Correct |
7 ms |
2404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
23 ms |
7392 KB |
Output is correct |
6 |
Correct |
28 ms |
7528 KB |
Output is correct |
7 |
Correct |
23 ms |
7432 KB |
Output is correct |
8 |
Correct |
23 ms |
7428 KB |
Output is correct |
9 |
Correct |
23 ms |
7424 KB |
Output is correct |
10 |
Correct |
24 ms |
7536 KB |
Output is correct |
11 |
Correct |
25 ms |
7420 KB |
Output is correct |
12 |
Correct |
16 ms |
3588 KB |
Output is correct |
13 |
Correct |
16 ms |
2700 KB |
Output is correct |
14 |
Correct |
16 ms |
3080 KB |
Output is correct |
15 |
Correct |
16 ms |
3076 KB |
Output is correct |
16 |
Correct |
29 ms |
14072 KB |
Output is correct |
17 |
Correct |
30 ms |
14072 KB |
Output is correct |
18 |
Correct |
22 ms |
7684 KB |
Output is correct |
19 |
Correct |
19 ms |
4612 KB |
Output is correct |
20 |
Correct |
19 ms |
4584 KB |
Output is correct |
21 |
Correct |
16 ms |
4608 KB |
Output is correct |
22 |
Correct |
21 ms |
7164 KB |
Output is correct |
23 |
Correct |
20 ms |
7248 KB |
Output is correct |
24 |
Correct |
17 ms |
4608 KB |
Output is correct |
25 |
Correct |
19 ms |
4608 KB |
Output is correct |
26 |
Correct |
6 ms |
2440 KB |
Output is correct |
27 |
Correct |
19 ms |
7656 KB |
Output is correct |
28 |
Correct |
17 ms |
4632 KB |
Output is correct |
29 |
Correct |
15 ms |
4868 KB |
Output is correct |
30 |
Correct |
7 ms |
2440 KB |
Output is correct |
31 |
Correct |
7 ms |
2404 KB |
Output is correct |
32 |
Correct |
27 ms |
7424 KB |
Output is correct |
33 |
Correct |
1452 ms |
435508 KB |
Output is correct |
34 |
Correct |
1335 ms |
435252 KB |
Output is correct |
35 |
Correct |
1281 ms |
438104 KB |
Output is correct |
36 |
Correct |
1268 ms |
438064 KB |
Output is correct |
37 |
Correct |
1272 ms |
439456 KB |
Output is correct |
38 |
Correct |
1270 ms |
438576 KB |
Output is correct |
39 |
Correct |
864 ms |
161112 KB |
Output is correct |
40 |
Correct |
792 ms |
112176 KB |
Output is correct |
41 |
Correct |
486 ms |
87208 KB |
Output is correct |
42 |
Correct |
819 ms |
143792 KB |
Output is correct |
43 |
Correct |
772 ms |
94600 KB |
Output is correct |
44 |
Correct |
1361 ms |
463276 KB |
Output is correct |
45 |
Correct |
1357 ms |
440748 KB |
Output is correct |
46 |
Correct |
960 ms |
456360 KB |
Output is correct |
47 |
Correct |
1060 ms |
244980 KB |
Output is correct |
48 |
Correct |
1041 ms |
245492 KB |
Output is correct |
49 |
Correct |
714 ms |
245232 KB |
Output is correct |
50 |
Correct |
827 ms |
222372 KB |
Output is correct |
51 |
Correct |
815 ms |
222388 KB |
Output is correct |
52 |
Correct |
882 ms |
245884 KB |
Output is correct |
53 |
Correct |
932 ms |
245992 KB |
Output is correct |
54 |
Correct |
222 ms |
87464 KB |
Output is correct |
55 |
Correct |
790 ms |
243192 KB |
Output is correct |
56 |
Correct |
637 ms |
151016 KB |
Output is correct |
57 |
Correct |
561 ms |
105068 KB |
Output is correct |
58 |
Correct |
300 ms |
69292 KB |
Output is correct |
59 |
Correct |
261 ms |
69240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
23 ms |
7392 KB |
Output is correct |
6 |
Correct |
28 ms |
7528 KB |
Output is correct |
7 |
Correct |
23 ms |
7432 KB |
Output is correct |
8 |
Correct |
23 ms |
7428 KB |
Output is correct |
9 |
Correct |
23 ms |
7424 KB |
Output is correct |
10 |
Correct |
24 ms |
7536 KB |
Output is correct |
11 |
Correct |
25 ms |
7420 KB |
Output is correct |
12 |
Correct |
16 ms |
3588 KB |
Output is correct |
13 |
Correct |
16 ms |
2700 KB |
Output is correct |
14 |
Correct |
16 ms |
3080 KB |
Output is correct |
15 |
Correct |
16 ms |
3076 KB |
Output is correct |
16 |
Correct |
29 ms |
14072 KB |
Output is correct |
17 |
Correct |
30 ms |
14072 KB |
Output is correct |
18 |
Correct |
22 ms |
7684 KB |
Output is correct |
19 |
Correct |
19 ms |
4612 KB |
Output is correct |
20 |
Correct |
19 ms |
4584 KB |
Output is correct |
21 |
Correct |
16 ms |
4608 KB |
Output is correct |
22 |
Correct |
21 ms |
7164 KB |
Output is correct |
23 |
Correct |
20 ms |
7248 KB |
Output is correct |
24 |
Correct |
17 ms |
4608 KB |
Output is correct |
25 |
Correct |
19 ms |
4608 KB |
Output is correct |
26 |
Correct |
6 ms |
2440 KB |
Output is correct |
27 |
Correct |
19 ms |
7656 KB |
Output is correct |
28 |
Correct |
17 ms |
4632 KB |
Output is correct |
29 |
Correct |
15 ms |
4868 KB |
Output is correct |
30 |
Correct |
7 ms |
2440 KB |
Output is correct |
31 |
Correct |
7 ms |
2404 KB |
Output is correct |
32 |
Correct |
27 ms |
7424 KB |
Output is correct |
33 |
Correct |
1452 ms |
435508 KB |
Output is correct |
34 |
Correct |
1335 ms |
435252 KB |
Output is correct |
35 |
Correct |
1281 ms |
438104 KB |
Output is correct |
36 |
Correct |
1268 ms |
438064 KB |
Output is correct |
37 |
Correct |
1272 ms |
439456 KB |
Output is correct |
38 |
Correct |
1270 ms |
438576 KB |
Output is correct |
39 |
Correct |
864 ms |
161112 KB |
Output is correct |
40 |
Correct |
792 ms |
112176 KB |
Output is correct |
41 |
Correct |
486 ms |
87208 KB |
Output is correct |
42 |
Correct |
819 ms |
143792 KB |
Output is correct |
43 |
Correct |
772 ms |
94600 KB |
Output is correct |
44 |
Correct |
1361 ms |
463276 KB |
Output is correct |
45 |
Correct |
1357 ms |
440748 KB |
Output is correct |
46 |
Correct |
960 ms |
456360 KB |
Output is correct |
47 |
Correct |
1060 ms |
244980 KB |
Output is correct |
48 |
Correct |
1041 ms |
245492 KB |
Output is correct |
49 |
Correct |
714 ms |
245232 KB |
Output is correct |
50 |
Correct |
827 ms |
222372 KB |
Output is correct |
51 |
Correct |
815 ms |
222388 KB |
Output is correct |
52 |
Correct |
882 ms |
245884 KB |
Output is correct |
53 |
Correct |
932 ms |
245992 KB |
Output is correct |
54 |
Correct |
222 ms |
87464 KB |
Output is correct |
55 |
Correct |
790 ms |
243192 KB |
Output is correct |
56 |
Correct |
637 ms |
151016 KB |
Output is correct |
57 |
Correct |
561 ms |
105068 KB |
Output is correct |
58 |
Correct |
300 ms |
69292 KB |
Output is correct |
59 |
Correct |
261 ms |
69240 KB |
Output is correct |
60 |
Runtime error |
281 ms |
524288 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |