#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);
}
void setv (int ind, int tl, int tr, int r, long long v) { //everything in [1,r] > v gets set to v
push_down(ind,tl,tr);
if (tree[ind].mx <= v) return;
if (tl == tr) {
tree[ind].mx = v; tree[ind].slz = -1; tree[ind].alz = 0;
return;
}
int mid = (tl + tr) / 2;
if (r <= mid) {
push_down(rc,mid+1,tr);
setv(lc,tl,mid,r,v);
} else if (r < tr) {
setv(lc,tl,mid,r,v); setv(rc,mid+1,tr,r,v);
} else {
push_down(lc,tl,mid);
if (tree[lc].mx > v) {
tree[rc].slz = v; tree[rc].alz = 0;
push_down(rc,mid+1,tr);
setv(lc,tl,mid,r,v);
} else {
push_down(lc,tl,mid);
setv(rc,mid+1,tr,r,v);
}
}
tree[ind].mx = max(tree[lc].mx,tree[rc].mx);
}
long long query (int ind, int tl, int tr, int l, int r) {
push_down(ind,tl,tr);
if (tl > r || tr < l) return LLONG_MIN;
if (l <= tl && tr <= r) return tree[ind].mx;
int mid = (tl + tr) / 2;
return max(query(lc,tl,mid,l,r),query(rc,mid+1,tr,l,r));
}
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;
}
map<int,long long> dp[MN];
void dfs (int cur, bool del = true) {
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]);
//if (cur == 1) printf ("%lld\n",query(1,1,(int)xs.size(),h[cur],h[cur])-c[cur]);
setv(1,1,(int)xs.size(),h[cur],query(1,1,(int)xs.size(),h[cur],h[cur])-c[cur]);
//for (int i = 1; i <= (int)xs.size(); i++) printf ("dp[%d][%d] = %lld\n",cur,i,query(1,1,(int)xs.size(),i,i));
if (del) {
for (int j = st[cur]; j <= ed[cur]; j++) {
int v = h[at[j]];
if (!dp[cur].count(v)) dp[cur][v] = query(1,1,(int)xs.size(),v,v);
}
int v = (int)xs.size();
if (!dp[cur].count(v)) dp[cur][v] = query(1,1,(int)xs.size(),v,v);
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);
long long ret = 1e18;
for (auto [j,v] : dp[1]) ret = min(ret,v);
printf ("%lld\n",ret);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:111:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
111 | int n; scanf ("%d",&n);
| ~~~~~~^~~~~~~~~
worst_reporter2.cpp:113:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
113 | scanf ("%d %d %d",&a[i],&h[i],&c[i]);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
33116 KB |
Output is correct |
2 |
Correct |
21 ms |
33184 KB |
Output is correct |
3 |
Correct |
18 ms |
33100 KB |
Output is correct |
4 |
Correct |
16 ms |
33100 KB |
Output is correct |
5 |
Correct |
45 ms |
34844 KB |
Output is correct |
6 |
Correct |
29 ms |
34296 KB |
Output is correct |
7 |
Correct |
23 ms |
33996 KB |
Output is correct |
8 |
Correct |
47 ms |
34884 KB |
Output is correct |
9 |
Correct |
29 ms |
34312 KB |
Output is correct |
10 |
Correct |
24 ms |
34060 KB |
Output is correct |
11 |
Correct |
20 ms |
33704 KB |
Output is correct |
12 |
Correct |
28 ms |
34604 KB |
Output is correct |
13 |
Correct |
20 ms |
34380 KB |
Output is correct |
14 |
Correct |
30 ms |
34476 KB |
Output is correct |
15 |
Correct |
21 ms |
34032 KB |
Output is correct |
16 |
Correct |
56 ms |
35524 KB |
Output is correct |
17 |
Correct |
32 ms |
34612 KB |
Output is correct |
18 |
Correct |
19 ms |
33744 KB |
Output is correct |
19 |
Correct |
32 ms |
34492 KB |
Output is correct |
20 |
Correct |
24 ms |
34124 KB |
Output is correct |
21 |
Correct |
20 ms |
34124 KB |
Output is correct |
22 |
Correct |
37 ms |
34348 KB |
Output is correct |
23 |
Correct |
26 ms |
34084 KB |
Output is correct |
24 |
Correct |
32 ms |
34568 KB |
Output is correct |
25 |
Correct |
23 ms |
34264 KB |
Output is correct |
26 |
Correct |
24 ms |
34628 KB |
Output is correct |
27 |
Correct |
33 ms |
34392 KB |
Output is correct |
28 |
Correct |
32 ms |
34500 KB |
Output is correct |
29 |
Correct |
30 ms |
34552 KB |
Output is correct |
30 |
Correct |
27 ms |
34476 KB |
Output is correct |
31 |
Correct |
28 ms |
34380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
34884 KB |
Output is correct |
2 |
Execution timed out |
2074 ms |
101388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
33116 KB |
Output is correct |
2 |
Correct |
21 ms |
33184 KB |
Output is correct |
3 |
Correct |
18 ms |
33100 KB |
Output is correct |
4 |
Correct |
16 ms |
33100 KB |
Output is correct |
5 |
Correct |
45 ms |
34844 KB |
Output is correct |
6 |
Correct |
29 ms |
34296 KB |
Output is correct |
7 |
Correct |
23 ms |
33996 KB |
Output is correct |
8 |
Correct |
47 ms |
34884 KB |
Output is correct |
9 |
Correct |
29 ms |
34312 KB |
Output is correct |
10 |
Correct |
24 ms |
34060 KB |
Output is correct |
11 |
Correct |
20 ms |
33704 KB |
Output is correct |
12 |
Correct |
28 ms |
34604 KB |
Output is correct |
13 |
Correct |
20 ms |
34380 KB |
Output is correct |
14 |
Correct |
30 ms |
34476 KB |
Output is correct |
15 |
Correct |
21 ms |
34032 KB |
Output is correct |
16 |
Correct |
56 ms |
35524 KB |
Output is correct |
17 |
Correct |
32 ms |
34612 KB |
Output is correct |
18 |
Correct |
19 ms |
33744 KB |
Output is correct |
19 |
Correct |
32 ms |
34492 KB |
Output is correct |
20 |
Correct |
24 ms |
34124 KB |
Output is correct |
21 |
Correct |
20 ms |
34124 KB |
Output is correct |
22 |
Correct |
37 ms |
34348 KB |
Output is correct |
23 |
Correct |
26 ms |
34084 KB |
Output is correct |
24 |
Correct |
32 ms |
34568 KB |
Output is correct |
25 |
Correct |
23 ms |
34264 KB |
Output is correct |
26 |
Correct |
24 ms |
34628 KB |
Output is correct |
27 |
Correct |
33 ms |
34392 KB |
Output is correct |
28 |
Correct |
32 ms |
34500 KB |
Output is correct |
29 |
Correct |
30 ms |
34552 KB |
Output is correct |
30 |
Correct |
27 ms |
34476 KB |
Output is correct |
31 |
Correct |
28 ms |
34380 KB |
Output is correct |
32 |
Correct |
46 ms |
34884 KB |
Output is correct |
33 |
Execution timed out |
2074 ms |
101388 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |