#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 998244353
#define PI 3.14159265359
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,q,par[100005],mx[100005],ans=0,dp[100005][3];
map<pair<ll,ll>,ll>d;
vector<ll>g[100005];
void dfs(ll node,ll par){
multiset<ll>s;
for(auto it : g[node]){
if(it!=par){
dfs(it,node);
s.insert(dp[it][1]+d[mp(node,it)]);
}
}
auto it=s.end();
if(s.size()>=1){it--;
dp[node][1]=*(it);
}
if(s.size()>=1)
s.erase(s.find(*(it)));
if(s.size()>=1)
dp[node][2]=*(it);
ans+=dp[node][1]+dp[node][2];
}
void update(ll node){
if(node==-1){
return;
}
ll nw=0;
multiset<ll>s;
ans-=dp[node][1];
ans-=dp[node][2];
for(auto it : g[node]){
if(it!=par[node]){
s.insert(dp[it][1]+d[mp(node,it)]);
}
}
dp[node][1]=0;
dp[node][2]=0;
auto it=s.end();
if(s.size()>=1){it--;
dp[node][1]=*(it);
s.erase(s.find(dp[node][1]));
}
if(s.size()>=1){
it=s.end();
it--;
dp[node][2]=*(it);
}
ans+=dp[node][1]+dp[node][2];
update(par[node]);
}
int32_t main(){
CODE_START;
cin>>n;
for(ll i=1;i<n;i++)
{
ll x;
cin>>x;
par[i]=x;
g[x].pb(i);
}
for(ll i=1;i<n;i++)
{
ll dist;
cin>>dist;
d[mp(par[i],i)]=dist;
}
par[0]=-1;
dfs(0,-1);
cout<<ans<<endl;
cin>>q;
while(q--){
ll x,y;
cin>>x>>y;
d[mp(par[x],x)]+=y;
update(par[x]);
cout<<ans<<endl;
}
}
Compilation message
arboras.cpp: In function 'void update(long long int)':
arboras.cpp:45:4: warning: unused variable 'nw' [-Wunused-variable]
45 | ll nw=0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
2860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1230 ms |
16988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5091 ms |
19832 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
2860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |