Submission #491644

#TimeUsernameProblemLanguageResultExecution timeMemory
491644Sho10Arboras (RMI20_arboras)C++17
24 / 100
5039 ms24708 KiB
#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 1000000007 #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][5],nw=0,res[100005][5]; map<pair<ll,ll>,ll>d; vector<ll>g[100005]; void dfs(ll node,ll par){ multiset<pair<ll,ll>>s; for(auto it : g[node]){ if(it!=par){ dfs(it,node); s.insert(mp(dp[it][1]+d[mp(node,it)],it)); } } auto it=s.end(); if(s.size()>=1){it--; pair<ll,ll>p=*(it); dp[node][1]=p.f; res[node][1]=p.s; } if(s.size()>=1) s.erase(s.find(*(it))); if(s.size()>=1){ auto it=s.end(); it--; pair<ll,ll>p=*(it); dp[node][2]=p.f; res[node][2]=p.s; } ans+=dp[node][1]+dp[node][2]; ans%=mod; } void update(ll node){ if(node==-1){ return; } ll mx=0; ans-=dp[node][1]; ans-=dp[node][2]; //cout<<dp[node][1]<<' '<<dp[node][2]<<' '<<node<<endl; while(ans<0){ ans+=mod; } mx=dp[nw][1]+d[mp(node,nw)]; //cout<<mx<<' '<<nw<<' '<<res[node][1]<<' '<<res[node][2]<<endl; if(mx>dp[node][1]&&nw!=res[node][2]){ if(nw!=res[node][1]){ dp[node][2]=dp[node][1]; res[node][2]=res[node][1]; } dp[node][1]=mx; res[node][1]=nw; }else if(mx>dp[node][2]&&nw!=res[node][1]){ dp[node][2]=mx; res[node][2]=nw; } if(dp[node][2]>dp[node][1]){ swap(dp[node][2],dp[node][1]); swap(res[node][2],res[node][1]); } //cout<<dp[node][1]<<' '<<dp[node][2]<<' '<<node<<endl; ans+=dp[node][1]; ans+=dp[node][2]; ans%=mod; nw=node; 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=0;i<=n;i++) { res[i][1]=-1; res[i][2]=-1; } 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; nw=x; update(par[x]); cout<<ans<<endl; } } /* 5 0 0 1 1 0 0 0 0 5 1 2 2 2 3 2 4 2 4 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...