답안 #491560

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491560 2021-12-03T07:38:04 Z Sho10 Arboras (RMI20_arboras) C++17
11 / 100
5000 ms 19504 KB
#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];
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){
        auto it=s.end();
it--;
dp[node][2]=*(it);
}
ans+=dp[node][1]+dp[node][2];
ans%=mod;
}
void update(ll node){
if(node==-1){
    return;
}
ll nw=0;
multiset<ll>s;
ans-=dp[node][1];
ans-=dp[node][2];
while(ans<0){
    ans+=mod;
}
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];
ans%=mod;
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:49:4: warning: unused variable 'nw' [-Wunused-variable]
   49 | ll nw=0;
      |    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 2860 KB Output is correct
2 Correct 78 ms 2920 KB Output is correct
3 Correct 68 ms 2844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1153 ms 16456 KB Output is correct
2 Execution timed out 5069 ms 16060 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5061 ms 19504 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 2860 KB Output is correct
2 Correct 78 ms 2920 KB Output is correct
3 Correct 68 ms 2844 KB Output is correct
4 Correct 1153 ms 16456 KB Output is correct
5 Execution timed out 5069 ms 16060 KB Time limit exceeded
6 Halted 0 ms 0 KB -