#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int mxn=2e5+5;
const int inf=1e9;
ll c[mxn];
int h[mxn];
vector<int> adj[mxn];
map<int,ll> mp[mxn];
int sz[mxn];
int in[mxn],out[mxn],inv[mxn];
int T = 0;
int n;
void dfs0(int u) {
sz[u] = 1;
in[u] = ++T;
inv[T] = u;
for(int v : adj[u]) {
dfs0(v);
sz[u] += sz[v];
}
out[u] = T;
}
struct segtree {
ll tree[4*mxn];
ll lazy[4*mxn];
void init() {
memset(tree, 0, sizeof tree);
memset(lazy, -1, sizeof lazy);
}
void propogate(int node, int left,int right) {
if(lazy[node] != -1) {
lazy[left] = lazy[node];
lazy[right] = lazy[node];
tree[left] = 0;
tree[right] = 0;
lazy[node] = -1;
}
if(tree[node] != 0) {
tree[left] += tree[node];
tree[right] += tree[node];
tree[node] = 0;
}
}
void update(int node,int b,int e,int l,int r,ll v) {
if(e < l || b > r) return;
if(b >= l && e <= r) {
tree[node] += v;
return;
}
int mid = b + e >> 1;
int left = node << 1;
int right = left | 1;
propogate(node,left,right);
update(left, b, mid, l , r, v);
update(right, mid + 1, e, l, r, v);
}
void Set(int node, int b,int e, int l, int r, ll v) {
if(e < l || b > r) return;
if(b >= l && e <= r) {
lazy[node] = v;
tree[node] = 0;
return;
}
int mid = b + e >> 1;
int left = node << 1;
int right = left | 1;
propogate(node, left, right);
Set(left, b, mid, l, r, v);
Set(right, mid + 1, e, l, r, v);
}
ll query(int node, int b,int e,int p) {
if(b == e) return tree[node] + max(0ll,lazy[node]);
int mid = b + e >> 1;
int left = node << 1;
int right = left | 1;
if(lazy[node] != -1) return tree[node] + lazy[node];
if(p <= mid) return tree[node] + query(left, b, mid, p);
return tree[node] + query(right, mid + 1, e, p);
}
void add(int l,int r,ll v) {
update(1,1,n,l,r,v);
}
ll query(int p) {
return query(1,1,n,p);
}
} st;
void dfs(int u, bool keep = false) {
int mx = -1, bigChild = -1;
// cout<<u<<" "<<keep<<endl;
for(int v : adj[u]) {
if(sz[v] > mx) {
mx = sz[v];
bigChild = v;
}
}
for(int v : adj[u]) if(v != bigChild) dfs(v);
if(bigChild != -1) dfs(bigChild,true);
vector<int> ind;
for(int v : adj[u]) {
if(v == bigChild) continue;
int last = 0;
ll mv = mp[v][h[v]];
for(auto x : mp[v]) {
ll td = x.s;
if(x.f < h[v]) td = min(td, mv);
st.add(last + 1, x.f, td);
last = x.f;
ind.push_back(x.f);
}
}
st.add(1,h[u] - 1, c[u]);
st.add(h[u] + 1, n, c[u]);
ind.push_back(h[u]);
ind.push_back(1);
ind.push_back(n);
ll nc = st.query(h[u]);
int lo = 1;
int hi = h[u] - 1;
int pos = -1;
ll rmv = -1;
while(lo <= hi) {
int mid = lo + hi >> 1;
ll v = st.query(mid);
if(v > nc) {
pos = mid;
rmv = v;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
if(pos != -1) {
st.Set(1,1,n, pos, h[u] - 1, nc);
}
// cout<<"Starting "<<u<<" "<<h[u]<<" "<<c[u]<<endl;
// for(int i = 1; i <= n; i++) {
// cout<<"DP "<<u<<" "<<i<<" = "<<st.query(i)<<endl;
// }
// cout<<endl<<endl;
if(!keep) {
for(int i = in[bigChild]; i <= out[bigChild]; i++) {
ind.push_back(h[inv[i]]);
}
for(int i : ind) {
mp[u][i] = st.query(i);
}
st.Set(1,1,n,1,n,0);
}
}
int main() {
cin >> n;
st.init();
vector<int> v;
for(int i = 1; i <= n; i++) {
int bap;
scanf("%d %d %lld",&bap,&h[i],&c[i]);
if(bap != i) adj[bap].push_back(i);
v.push_back(h[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()), v.end());
for(int i = 1; i <= n; i++) {
h[i] = lower_bound(v.begin(),v.end(),h[i]) - v.begin() + 1;
}
dfs0(1);
dfs(1, true);
cout<<st.query(1)<<endl;
}
Compilation message
worst_reporter2.cpp: In member function 'void segtree::update(int, int, int, int, int, long long int)':
worst_reporter2.cpp:72:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid = b + e >> 1;
| ~~^~~
worst_reporter2.cpp: In member function 'void segtree::Set(int, int, int, int, int, long long int)':
worst_reporter2.cpp:92:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int mid = b + e >> 1;
| ~~^~~
worst_reporter2.cpp: In member function 'long long int segtree::query(int, int, int, int)':
worst_reporter2.cpp:105:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
105 | int mid = b + e >> 1;
| ~~^~~
worst_reporter2.cpp: In function 'void dfs(int, bool)':
worst_reporter2.cpp:179:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
179 | int mid = lo + hi >> 1;
| ~~~^~~~
worst_reporter2.cpp:176:8: warning: variable 'rmv' set but not used [-Wunused-but-set-variable]
176 | ll rmv = -1;
| ^~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:227:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
227 | scanf("%d %d %lld",&bap,&h[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26828 KB |
Output is correct |
2 |
Correct |
14 ms |
26828 KB |
Output is correct |
3 |
Correct |
12 ms |
26924 KB |
Output is correct |
4 |
Correct |
12 ms |
26860 KB |
Output is correct |
5 |
Correct |
30 ms |
28376 KB |
Output is correct |
6 |
Correct |
25 ms |
28232 KB |
Output is correct |
7 |
Correct |
27 ms |
27984 KB |
Output is correct |
8 |
Correct |
33 ms |
28640 KB |
Output is correct |
9 |
Correct |
24 ms |
28312 KB |
Output is correct |
10 |
Correct |
22 ms |
27976 KB |
Output is correct |
11 |
Correct |
18 ms |
27740 KB |
Output is correct |
12 |
Correct |
26 ms |
28492 KB |
Output is correct |
13 |
Correct |
17 ms |
28444 KB |
Output is correct |
14 |
Correct |
25 ms |
28108 KB |
Output is correct |
15 |
Correct |
19 ms |
27948 KB |
Output is correct |
16 |
Correct |
36 ms |
29204 KB |
Output is correct |
17 |
Correct |
27 ms |
28592 KB |
Output is correct |
18 |
Correct |
22 ms |
27740 KB |
Output is correct |
19 |
Correct |
24 ms |
28492 KB |
Output is correct |
20 |
Correct |
19 ms |
28492 KB |
Output is correct |
21 |
Correct |
19 ms |
28364 KB |
Output is correct |
22 |
Correct |
23 ms |
28588 KB |
Output is correct |
23 |
Correct |
22 ms |
28616 KB |
Output is correct |
24 |
Correct |
23 ms |
28552 KB |
Output is correct |
25 |
Correct |
20 ms |
28488 KB |
Output is correct |
26 |
Correct |
19 ms |
28552 KB |
Output is correct |
27 |
Correct |
23 ms |
28416 KB |
Output is correct |
28 |
Correct |
27 ms |
28376 KB |
Output is correct |
29 |
Correct |
21 ms |
28512 KB |
Output is correct |
30 |
Correct |
22 ms |
28044 KB |
Output is correct |
31 |
Correct |
24 ms |
28032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26828 KB |
Output is correct |
2 |
Correct |
14 ms |
26828 KB |
Output is correct |
3 |
Correct |
12 ms |
26924 KB |
Output is correct |
4 |
Correct |
12 ms |
26860 KB |
Output is correct |
5 |
Correct |
30 ms |
28376 KB |
Output is correct |
6 |
Correct |
25 ms |
28232 KB |
Output is correct |
7 |
Correct |
27 ms |
27984 KB |
Output is correct |
8 |
Correct |
33 ms |
28640 KB |
Output is correct |
9 |
Correct |
24 ms |
28312 KB |
Output is correct |
10 |
Correct |
22 ms |
27976 KB |
Output is correct |
11 |
Correct |
18 ms |
27740 KB |
Output is correct |
12 |
Correct |
26 ms |
28492 KB |
Output is correct |
13 |
Correct |
17 ms |
28444 KB |
Output is correct |
14 |
Correct |
25 ms |
28108 KB |
Output is correct |
15 |
Correct |
19 ms |
27948 KB |
Output is correct |
16 |
Correct |
36 ms |
29204 KB |
Output is correct |
17 |
Correct |
27 ms |
28592 KB |
Output is correct |
18 |
Correct |
22 ms |
27740 KB |
Output is correct |
19 |
Correct |
24 ms |
28492 KB |
Output is correct |
20 |
Correct |
19 ms |
28492 KB |
Output is correct |
21 |
Correct |
19 ms |
28364 KB |
Output is correct |
22 |
Correct |
23 ms |
28588 KB |
Output is correct |
23 |
Correct |
22 ms |
28616 KB |
Output is correct |
24 |
Correct |
23 ms |
28552 KB |
Output is correct |
25 |
Correct |
20 ms |
28488 KB |
Output is correct |
26 |
Correct |
19 ms |
28552 KB |
Output is correct |
27 |
Correct |
23 ms |
28416 KB |
Output is correct |
28 |
Correct |
27 ms |
28376 KB |
Output is correct |
29 |
Correct |
21 ms |
28512 KB |
Output is correct |
30 |
Correct |
22 ms |
28044 KB |
Output is correct |
31 |
Correct |
24 ms |
28032 KB |
Output is correct |
32 |
Correct |
32 ms |
28612 KB |
Output is correct |
33 |
Correct |
1301 ms |
114948 KB |
Output is correct |
34 |
Correct |
849 ms |
93496 KB |
Output is correct |
35 |
Correct |
1281 ms |
110892 KB |
Output is correct |
36 |
Correct |
879 ms |
93448 KB |
Output is correct |
37 |
Correct |
453 ms |
67928 KB |
Output is correct |
38 |
Correct |
340 ms |
58424 KB |
Output is correct |
39 |
Correct |
684 ms |
91572 KB |
Output is correct |
40 |
Correct |
416 ms |
91704 KB |
Output is correct |
41 |
Correct |
218 ms |
91484 KB |
Output is correct |
42 |
Correct |
754 ms |
75320 KB |
Output is correct |
43 |
Correct |
438 ms |
68920 KB |
Output is correct |
44 |
Correct |
1657 ms |
156748 KB |
Output is correct |
45 |
Correct |
1004 ms |
116968 KB |
Output is correct |
46 |
Correct |
275 ms |
60028 KB |
Output is correct |
47 |
Correct |
747 ms |
89856 KB |
Output is correct |
48 |
Correct |
458 ms |
89752 KB |
Output is correct |
49 |
Correct |
307 ms |
88628 KB |
Output is correct |
50 |
Correct |
621 ms |
92688 KB |
Output is correct |
51 |
Correct |
494 ms |
92532 KB |
Output is correct |
52 |
Correct |
687 ms |
92416 KB |
Output is correct |
53 |
Correct |
449 ms |
92544 KB |
Output is correct |
54 |
Correct |
441 ms |
91748 KB |
Output is correct |
55 |
Correct |
663 ms |
89436 KB |
Output is correct |
56 |
Correct |
556 ms |
89776 KB |
Output is correct |
57 |
Correct |
504 ms |
90140 KB |
Output is correct |
58 |
Correct |
562 ms |
74536 KB |
Output is correct |
59 |
Correct |
572 ms |
74500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26828 KB |
Output is correct |
2 |
Correct |
14 ms |
26828 KB |
Output is correct |
3 |
Correct |
12 ms |
26924 KB |
Output is correct |
4 |
Correct |
12 ms |
26860 KB |
Output is correct |
5 |
Correct |
30 ms |
28376 KB |
Output is correct |
6 |
Correct |
25 ms |
28232 KB |
Output is correct |
7 |
Correct |
27 ms |
27984 KB |
Output is correct |
8 |
Correct |
33 ms |
28640 KB |
Output is correct |
9 |
Correct |
24 ms |
28312 KB |
Output is correct |
10 |
Correct |
22 ms |
27976 KB |
Output is correct |
11 |
Correct |
18 ms |
27740 KB |
Output is correct |
12 |
Correct |
26 ms |
28492 KB |
Output is correct |
13 |
Correct |
17 ms |
28444 KB |
Output is correct |
14 |
Correct |
25 ms |
28108 KB |
Output is correct |
15 |
Correct |
19 ms |
27948 KB |
Output is correct |
16 |
Correct |
36 ms |
29204 KB |
Output is correct |
17 |
Correct |
27 ms |
28592 KB |
Output is correct |
18 |
Correct |
22 ms |
27740 KB |
Output is correct |
19 |
Correct |
24 ms |
28492 KB |
Output is correct |
20 |
Correct |
19 ms |
28492 KB |
Output is correct |
21 |
Correct |
19 ms |
28364 KB |
Output is correct |
22 |
Correct |
23 ms |
28588 KB |
Output is correct |
23 |
Correct |
22 ms |
28616 KB |
Output is correct |
24 |
Correct |
23 ms |
28552 KB |
Output is correct |
25 |
Correct |
20 ms |
28488 KB |
Output is correct |
26 |
Correct |
19 ms |
28552 KB |
Output is correct |
27 |
Correct |
23 ms |
28416 KB |
Output is correct |
28 |
Correct |
27 ms |
28376 KB |
Output is correct |
29 |
Correct |
21 ms |
28512 KB |
Output is correct |
30 |
Correct |
22 ms |
28044 KB |
Output is correct |
31 |
Correct |
24 ms |
28032 KB |
Output is correct |
32 |
Correct |
32 ms |
28612 KB |
Output is correct |
33 |
Correct |
1301 ms |
114948 KB |
Output is correct |
34 |
Correct |
849 ms |
93496 KB |
Output is correct |
35 |
Correct |
1281 ms |
110892 KB |
Output is correct |
36 |
Correct |
879 ms |
93448 KB |
Output is correct |
37 |
Correct |
453 ms |
67928 KB |
Output is correct |
38 |
Correct |
340 ms |
58424 KB |
Output is correct |
39 |
Correct |
684 ms |
91572 KB |
Output is correct |
40 |
Correct |
416 ms |
91704 KB |
Output is correct |
41 |
Correct |
218 ms |
91484 KB |
Output is correct |
42 |
Correct |
754 ms |
75320 KB |
Output is correct |
43 |
Correct |
438 ms |
68920 KB |
Output is correct |
44 |
Correct |
1657 ms |
156748 KB |
Output is correct |
45 |
Correct |
1004 ms |
116968 KB |
Output is correct |
46 |
Correct |
275 ms |
60028 KB |
Output is correct |
47 |
Correct |
747 ms |
89856 KB |
Output is correct |
48 |
Correct |
458 ms |
89752 KB |
Output is correct |
49 |
Correct |
307 ms |
88628 KB |
Output is correct |
50 |
Correct |
621 ms |
92688 KB |
Output is correct |
51 |
Correct |
494 ms |
92532 KB |
Output is correct |
52 |
Correct |
687 ms |
92416 KB |
Output is correct |
53 |
Correct |
449 ms |
92544 KB |
Output is correct |
54 |
Correct |
441 ms |
91748 KB |
Output is correct |
55 |
Correct |
663 ms |
89436 KB |
Output is correct |
56 |
Correct |
556 ms |
89776 KB |
Output is correct |
57 |
Correct |
504 ms |
90140 KB |
Output is correct |
58 |
Correct |
562 ms |
74536 KB |
Output is correct |
59 |
Correct |
572 ms |
74500 KB |
Output is correct |
60 |
Runtime error |
249 ms |
365388 KB |
Execution killed with signal 11 |
61 |
Halted |
0 ms |
0 KB |
- |