#include <bits/stdc++.h>
using namespace std;
const int MN = 2e5 + 5;
#define lc ind<<1
#define rc ind<<1|1
struct Node { //range add, range set, some kind of walking
long long mx,alz,slz;
Node () {mx = alz = 0; slz = -1;}
} tree[MN << 2];
void push_down (int ind, int l, int r) {
if (tree[ind].slz >= 0) {
tree[ind].mx = tree[ind].slz;
if (l != r) {
tree[lc].slz = tree[rc].slz = tree[ind].slz;
tree[lc].alz = tree[rc].alz = 0;
}
tree[ind].slz = -1;
}
if (tree[ind].alz) {
tree[ind].mx += tree[ind].alz;
if (l != r) {
tree[lc].alz += tree[ind].alz;
tree[rc].alz += tree[ind].alz;
}
tree[ind].alz = 0;
}
}
void update (int ind, int tl, int tr, int l, int r, long long v) {
push_down(ind,tl,tr);
if (tl > r || tr < l) return;
if (l <= tl && tr <= r) {
tree[ind].alz += v;
push_down(ind,tl,tr);
return;
}
int mid = (tl + tr) / 2;
update(lc,tl,mid,l,r,v); update(rc,mid+1,tr,l,r,v);
tree[ind].mx = max(tree[lc].mx,tree[rc].mx);
}
int walk (int ind, int tl, int tr, long long v) {
push_down(ind,tl,tr);
if (tl == tr) return tree[ind].mx > v ? tl : -1;
int mid = (tl + tr) / 2;
push_down(lc,tl,mid);
if (tree[lc].mx > v) return walk(lc,tl,mid,v);
return walk(rc,mid+1,tr,v);
}
void setv (int ind, int tl, int tr, int l, int r, long long v) {
push_down(ind,tl,tr);
if (tl > r || tr < l) return;
if (l <= tl && tr <= r) {
tree[ind].slz = v; tree[ind].alz = 0;
push_down(ind,tl,tr);
return;
}
int mid = (tl + tr) / 2;
setv(lc,tl,mid,l,r,v); setv(rc,mid+1,tr,l,r,v);
tree[ind].mx = max(tree[lc].mx,tree[rc].mx);
}
long long query (int ind, int tl, int tr, int i) {
push_down(ind,tl,tr);
if (tl == tr) return tree[ind].mx;
int mid = (tl + tr) / 2;
if (i <= mid) return query(lc,tl,mid,i);
return query(rc,mid+1,tr,i);
}
int a[MN], h[MN], c[MN], sz[MN], st[MN], ed[MN], at[MN], big[MN], tt;
vector<int> adj[MN], xs;
int getx (int x) {return lower_bound(xs.begin(),xs.end(),x) - xs.begin() + 1;}
void getsz (int cur) {
sz[cur] = 1; st[cur] = ++tt; at[tt] = cur;
for (int i : adj[cur]) {
getsz(i);
sz[cur] += sz[i];
if (sz[i] > sz[big[cur]]) big[cur] = i;
}
ed[cur] = tt;
}
vector<pair<int,long long>> dp[MN];
void dfs (int cur, bool del = false) {
for (int i : adj[cur]) if (i != big[cur]) dfs(i,true);
if (big[cur]) dfs(big[cur],false);
for (int i : adj[cur]) if (i != big[cur]) {
int lst = 1;
for (auto [j,v] : dp[i]) {
if (lst <= j) update(1,1,(int)xs.size(),lst,j,v);
lst = j+1;
}
}
update(1,1,(int)xs.size(),1,(int)xs.size(),c[cur]);
long long getv = query(1,1,(int)xs.size(),h[cur])-c[cur];
int pos = walk(1,1,(int)xs.size(),getv);
if (pos <= h[cur]) setv(1,1,(int)xs.size(),pos,h[cur],getv);
if (del) {
vector<int> hs;
for (int j = st[cur]; j <= ed[cur]; j++) hs.push_back(h[at[j]]);
hs.push_back((int)xs.size());
sort(hs.begin(),hs.end());
hs.erase(unique(hs.begin(),hs.end()),hs.end());
dp[cur].resize(hs.size());
for (int i = 0; i < (int)hs.size(); i++) dp[cur][i] = {hs[i],query(1,1,(int)xs.size(),hs[i])};
tree[1].slz = tree[1].alz = 0;
}
}
int main () {
int n; scanf ("%d",&n);
for (int i = 1; i <= n; i++) {
scanf ("%d %d %d",&a[i],&h[i],&c[i]);
xs.push_back(h[i]);
if (i > 1) adj[a[i]].push_back(i);
}
sort(xs.begin(),xs.end()); xs.erase(unique(xs.begin(),xs.end()),xs.end());
for (int i = 1; i <= n; i++) h[i] = getx(h[i]);
getsz(1);
dfs(1);
printf ("%lld\n",query(1,1,(int)xs.size(),1));
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:106:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
106 | int n; scanf ("%d",&n);
| ~~~~~~^~~~~~~~~
worst_reporter2.cpp:108:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
108 | scanf ("%d %d %d",&a[i],&h[i],&c[i]);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28412 KB |
Output is correct |
3 |
Correct |
14 ms |
28444 KB |
Output is correct |
4 |
Correct |
30 ms |
28476 KB |
Output is correct |
5 |
Correct |
36 ms |
29164 KB |
Output is correct |
6 |
Correct |
29 ms |
29116 KB |
Output is correct |
7 |
Correct |
20 ms |
29040 KB |
Output is correct |
8 |
Correct |
38 ms |
29132 KB |
Output is correct |
9 |
Correct |
24 ms |
29132 KB |
Output is correct |
10 |
Correct |
20 ms |
29012 KB |
Output is correct |
11 |
Correct |
19 ms |
28868 KB |
Output is correct |
12 |
Correct |
22 ms |
29608 KB |
Output is correct |
13 |
Correct |
18 ms |
29644 KB |
Output is correct |
14 |
Correct |
25 ms |
29372 KB |
Output is correct |
15 |
Correct |
19 ms |
29252 KB |
Output is correct |
16 |
Correct |
38 ms |
29320 KB |
Output is correct |
17 |
Correct |
25 ms |
29144 KB |
Output is correct |
18 |
Correct |
18 ms |
28892 KB |
Output is correct |
19 |
Correct |
23 ms |
29316 KB |
Output is correct |
20 |
Correct |
23 ms |
29264 KB |
Output is correct |
21 |
Correct |
20 ms |
29348 KB |
Output is correct |
22 |
Correct |
27 ms |
29028 KB |
Output is correct |
23 |
Correct |
24 ms |
29052 KB |
Output is correct |
24 |
Correct |
27 ms |
29324 KB |
Output is correct |
25 |
Correct |
25 ms |
29268 KB |
Output is correct |
26 |
Correct |
22 ms |
29644 KB |
Output is correct |
27 |
Correct |
26 ms |
29268 KB |
Output is correct |
28 |
Correct |
25 ms |
29416 KB |
Output is correct |
29 |
Correct |
24 ms |
29476 KB |
Output is correct |
30 |
Correct |
23 ms |
29260 KB |
Output is correct |
31 |
Correct |
22 ms |
29388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
29164 KB |
Output is correct |
2 |
Correct |
1453 ms |
56636 KB |
Output is correct |
3 |
Correct |
772 ms |
51428 KB |
Output is correct |
4 |
Correct |
1404 ms |
55572 KB |
Output is correct |
5 |
Correct |
692 ms |
51384 KB |
Output is correct |
6 |
Correct |
298 ms |
45104 KB |
Output is correct |
7 |
Correct |
238 ms |
42684 KB |
Output is correct |
8 |
Correct |
454 ms |
70852 KB |
Output is correct |
9 |
Correct |
265 ms |
70696 KB |
Output is correct |
10 |
Correct |
156 ms |
70536 KB |
Output is correct |
11 |
Correct |
507 ms |
58928 KB |
Output is correct |
12 |
Correct |
263 ms |
57364 KB |
Output is correct |
13 |
Correct |
1947 ms |
65944 KB |
Output is correct |
14 |
Correct |
779 ms |
60660 KB |
Output is correct |
15 |
Correct |
155 ms |
46324 KB |
Output is correct |
16 |
Correct |
563 ms |
62256 KB |
Output is correct |
17 |
Correct |
319 ms |
62068 KB |
Output is correct |
18 |
Correct |
182 ms |
61720 KB |
Output is correct |
19 |
Correct |
681 ms |
49196 KB |
Output is correct |
20 |
Correct |
383 ms |
49124 KB |
Output is correct |
21 |
Correct |
538 ms |
62580 KB |
Output is correct |
22 |
Correct |
345 ms |
62568 KB |
Output is correct |
23 |
Correct |
312 ms |
75308 KB |
Output is correct |
24 |
Correct |
635 ms |
61712 KB |
Output is correct |
25 |
Correct |
515 ms |
67720 KB |
Output is correct |
26 |
Correct |
452 ms |
70240 KB |
Output is correct |
27 |
Correct |
388 ms |
62764 KB |
Output is correct |
28 |
Correct |
390 ms |
62784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28412 KB |
Output is correct |
3 |
Correct |
14 ms |
28444 KB |
Output is correct |
4 |
Correct |
30 ms |
28476 KB |
Output is correct |
5 |
Correct |
36 ms |
29164 KB |
Output is correct |
6 |
Correct |
29 ms |
29116 KB |
Output is correct |
7 |
Correct |
20 ms |
29040 KB |
Output is correct |
8 |
Correct |
38 ms |
29132 KB |
Output is correct |
9 |
Correct |
24 ms |
29132 KB |
Output is correct |
10 |
Correct |
20 ms |
29012 KB |
Output is correct |
11 |
Correct |
19 ms |
28868 KB |
Output is correct |
12 |
Correct |
22 ms |
29608 KB |
Output is correct |
13 |
Correct |
18 ms |
29644 KB |
Output is correct |
14 |
Correct |
25 ms |
29372 KB |
Output is correct |
15 |
Correct |
19 ms |
29252 KB |
Output is correct |
16 |
Correct |
38 ms |
29320 KB |
Output is correct |
17 |
Correct |
25 ms |
29144 KB |
Output is correct |
18 |
Correct |
18 ms |
28892 KB |
Output is correct |
19 |
Correct |
23 ms |
29316 KB |
Output is correct |
20 |
Correct |
23 ms |
29264 KB |
Output is correct |
21 |
Correct |
20 ms |
29348 KB |
Output is correct |
22 |
Correct |
27 ms |
29028 KB |
Output is correct |
23 |
Correct |
24 ms |
29052 KB |
Output is correct |
24 |
Correct |
27 ms |
29324 KB |
Output is correct |
25 |
Correct |
25 ms |
29268 KB |
Output is correct |
26 |
Correct |
22 ms |
29644 KB |
Output is correct |
27 |
Correct |
26 ms |
29268 KB |
Output is correct |
28 |
Correct |
25 ms |
29416 KB |
Output is correct |
29 |
Correct |
24 ms |
29476 KB |
Output is correct |
30 |
Correct |
23 ms |
29260 KB |
Output is correct |
31 |
Correct |
22 ms |
29388 KB |
Output is correct |
32 |
Correct |
33 ms |
29164 KB |
Output is correct |
33 |
Correct |
1453 ms |
56636 KB |
Output is correct |
34 |
Correct |
772 ms |
51428 KB |
Output is correct |
35 |
Correct |
1404 ms |
55572 KB |
Output is correct |
36 |
Correct |
692 ms |
51384 KB |
Output is correct |
37 |
Correct |
298 ms |
45104 KB |
Output is correct |
38 |
Correct |
238 ms |
42684 KB |
Output is correct |
39 |
Correct |
454 ms |
70852 KB |
Output is correct |
40 |
Correct |
265 ms |
70696 KB |
Output is correct |
41 |
Correct |
156 ms |
70536 KB |
Output is correct |
42 |
Correct |
507 ms |
58928 KB |
Output is correct |
43 |
Correct |
263 ms |
57364 KB |
Output is correct |
44 |
Correct |
1947 ms |
65944 KB |
Output is correct |
45 |
Correct |
779 ms |
60660 KB |
Output is correct |
46 |
Correct |
155 ms |
46324 KB |
Output is correct |
47 |
Correct |
563 ms |
62256 KB |
Output is correct |
48 |
Correct |
319 ms |
62068 KB |
Output is correct |
49 |
Correct |
182 ms |
61720 KB |
Output is correct |
50 |
Correct |
681 ms |
49196 KB |
Output is correct |
51 |
Correct |
383 ms |
49124 KB |
Output is correct |
52 |
Correct |
538 ms |
62580 KB |
Output is correct |
53 |
Correct |
345 ms |
62568 KB |
Output is correct |
54 |
Correct |
312 ms |
75308 KB |
Output is correct |
55 |
Correct |
635 ms |
61712 KB |
Output is correct |
56 |
Correct |
515 ms |
67720 KB |
Output is correct |
57 |
Correct |
452 ms |
70240 KB |
Output is correct |
58 |
Correct |
388 ms |
62764 KB |
Output is correct |
59 |
Correct |
390 ms |
62784 KB |
Output is correct |
60 |
Correct |
16 ms |
28416 KB |
Output is correct |
61 |
Incorrect |
16 ms |
28476 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |