This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)
using namespace std;
const ll N = 100009;
const ll m = 1e9+7;
ll n , q , ans;
ll p[N] , d[N];
ll path[N];
ll dp[N];
vector<ll> adj[N];
void dfs(ll x)
{
path[x]=0;
vector<ll> v;
for(auto it : adj[x])
{
dfs(it);
v.pb(path[it]+d[it]);
}
if(!v.empty())
sort(v.begin(),v.end());
if(v.size()==0)
dp[x]=0;
else if(v.size()==1)
dp[x]=v[0];
else
dp[x]=v[v.size()-1]+v[v.size()-2];
dp[x]%=m;
ans+=dp[x];
ans%=m;
if(v.size())
path[x]=v[v.size()-1];
}
int main()
{
d3
cin >> n;
p[0]=-1;
for(int i = 1 ; n>i ; i++)
{
cin >> p[i];
adj[p[i]].pb(i);
}
d[0]=0;
for(int i = 1 ; n>i ; i++)
cin >> d[i];
dfs(0);
cans
cin >> q;
while(q--)
{
ll v , ad;
cin >> v >> ad;
ans=0;
d[v]+=ad;
dfs(0);
cans
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |