답안 #646588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
646588 2022-09-30T08:50:52 Z Vanilla Arboras (RMI20_arboras) C++17
11 / 100
5000 ms 13704 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int64 mod = 1e9 + 7;
const int maxn = 1e5 + 2;
int64 dp[maxn], down[maxn];
map <pair <int, int>, int64> c;
int p[maxn];
vector <int> ad [maxn];
int n;

void upd (int u) {
    vector <int64> d;
    down[u] = 0;
    dp[u] = 0;
    for (int v: ad[u]) {
        down[u] = max(down[u], down[v] + c[{u,v}]);
        d.push_back(down[v] + c[{u,v}]);
    }
    sort(d.rbegin(), d.rend());
    for (int i = 0; i < min((int) d.size(), 2); i++) dp[u]+=d[i];
}

void dfs (int u) {
    for (int v: ad[u]) {
        dfs(v);
    }
    upd(u);
}

int64 calc () {
    for (int i = 0; i < n; i++) dp[i] = 0;
    dfs(0);
    int64 rs = 0;
    for (int i = 0; i < n; i++){
        rs+=dp[i];
    }
    return rs % mod;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n - 1; i++){
        int x;
        cin >> x;
        ad[x].push_back(i);
        p[i] = x;
    }
    for (int i = 1; i <= n - 1; i++){
        int x;
        cin >> x;
        c[{p[i], i}] = x;
    }
    int q;
    cin >> q;
    if (n <= 1000 && q <= 1000) {
        cout << calc() << "\n";
        while (q--) {
            int64 x,y;
            cin >> x >> y;
            c[{p[x], x}] += y;
            cout << calc() << "\n";
        }
    }
    else {
        dfs(0);
        p[0] = -1;
        int64 rs = 0;
        for (int i = 0; i < n; i++){
            rs = (rs + dp[i]) % mod;
        }
        cout << rs << "\n";
        while (q--) {
            int64 x,y;
            cin >> x >> y;
            c[{p[x], x}]+=y;
            x = p[x];
            while (x != -1) {
                // cout << q << " " << x << " " << dp[x] << "\n";
                rs = (rs - dp[x] + mod) % mod;
                upd(x);
                rs = (rs + dp[x] + mod) % mod;
                x = p[x];
            }
            cout << rs << "\n";
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 2772 KB Output is correct
2 Correct 128 ms 2784 KB Output is correct
3 Correct 139 ms 2748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1561 ms 13704 KB Output is correct
2 Execution timed out 5043 ms 13576 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5043 ms 13436 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 2772 KB Output is correct
2 Correct 128 ms 2784 KB Output is correct
3 Correct 139 ms 2748 KB Output is correct
4 Correct 1561 ms 13704 KB Output is correct
5 Execution timed out 5043 ms 13576 KB Time limit exceeded
6 Halted 0 ms 0 KB -