Submission #749900

#TimeUsernameProblemLanguageResultExecution timeMemory
749900dooweyArboras (RMI20_arboras)C++14
24 / 100
5016 ms6348 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)1e5 + 1;
const int MOD = (int)1e9 + 7;

int p[N];
ll w[N];
pii f[N];
pii g[N];

ll total = 0ll;

void inc(ll y){
    y %= MOD;
    total += y;
    if(total < 0) total += MOD;
    else if(total >= MOD) total -= MOD;
}

bool update(int id, pii nw){
    inc(-f[id].fi - g[id].fi);
    bool ret = false;
    if(nw.se == f[id].se){
        f[id] = nw;
        ret = true;
    }
    else if(nw.fi > f[id].fi){
        g[id] = f[id];
        f[id] = nw;
        ret = true;
    }
    else if(nw.fi > g[id].fi){
        g[id] = nw;
    }
    inc(f[id].fi + g[id].fi);
    return ret;
}

int main(){
    fastIO;
    //freopen("in.txt", "r", stdin);
    int n;
    cin >> n;
    for(int i = 1; i < n; i ++ ){
        cin >> p[i];
    }
    for(int i = 0 ; i < n; i ++ ){
        f[i].se = -1;
    }
    for(int i = 1; i < n; i ++ ){
        cin >> w[i];
    }
    for(int i = n - 1; i > 0 ; i -- ){
        update(p[i], mp(f[i].fi + w[i], i));
    }
    int u;
    ll ad;
    cout << total << "\n";
    int q;
    cin >> q;
    for(int i = 1; i <= q; i ++ ){
        cin >> u >> ad;
        w[u] += ad;
        while(u > 0){
            if(update(p[u], mp(f[u].fi + w[u], u))) u = p[u];
            else break;
        }
        cout << total << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...