#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//slope trick
int n,m,k,d[100001],w[100001];
vector<int> adj[100001];
map<int,ll> mp[100001]; //(x,y) : add y to position x.
void dfs(int x) {
if (adj[x].size()) {
int mnode=-1;
//merge
for (auto s : adj[x]) {
dfs(s);
if (mnode==-1) mnode=s;
else {
if (mp[mnode].size()<mp[s].size()) swap(s,mnode);
for (auto it=mp[s].begin(); it!=mp[s].end(); ++it) mp[mnode][(*it).first]+=(*it).second; //add s to mnode
}
}
/*
if (x==3) {
cout<<x<<" -> ";
for (auto it=mp[mnode].begin(); it!=mp[mnode].end(); ++it) cout<<"("<<(*it).first<<","<<(*it).second<<") ";
cout<<"\n";
} */
auto it=mp[mnode].upper_bound(d[x]);
ll sum=0;
while (sum+(*it).second<=w[x] && it!=mp[mnode].end()) {
auto h=it++;
mp[mnode].erase(h);
}
if (it!=mp[mnode].end()) (*it).second=(*it).second+sum-w[x];
mp[mnode][d[x]]+=w[x];
/*
if (x==6 || x==7) {
cout<<x<<" -> ";
for (auto it=mp[mnode].begin(); it!=mp[mnode].end(); ++it) cout<<"("<<(*it).first<<","<<(*it).second<<") ";
cout<<"\n";
} */
swap(mp[x],mp[mnode]);
/*
if (x==3) {
ll ans=0;
for (auto it=mp[x].begin(); it!=mp[x].end(); ++it) ans+=(*it).second;
cout<<ans;
} */
} else {
mp[x][d[x]]=w[x];
}
/*
ll ans=0;
for (auto it=mp[x].begin(); it!=mp[x].end(); ++it) ans+=(*it).second;
cout<<x<<" -> "<<ans<<"\n"; */
/*
if (x==6 || x==7) {
cout<<x<<" -> ";
for (auto it=mp[x].begin(); it!=mp[x].end(); ++it) cout<<"("<<(*it).first<<","<<(*it).second<<") ";
cout<<"\n";
} */
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m>>k;
for (int i=2; i<=n; ++i) {
int x; cin>>x;
adj[x].push_back(i);
}
for (int i=0; i<m; ++i) {
int x; cin>>x;
cin>>d[x]>>w[x];
}
dfs(1);
ll ans=0;
for (auto it=mp[1].begin(); it!=mp[1].end(); ++it) ans+=(*it).second;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
21068 KB |
Output is correct |
2 |
Correct |
55 ms |
19600 KB |
Output is correct |
3 |
Correct |
134 ms |
37284 KB |
Output is correct |
4 |
Correct |
78 ms |
20568 KB |
Output is correct |
5 |
Correct |
114 ms |
21920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
16260 KB |
Output is correct |
2 |
Correct |
68 ms |
15368 KB |
Output is correct |
3 |
Correct |
56 ms |
19616 KB |
Output is correct |
4 |
Correct |
53 ms |
16468 KB |
Output is correct |
5 |
Correct |
52 ms |
27468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
8660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |