답안 #423049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
423049 2021-06-10T16:39:38 Z valerikk Arboras (RMI20_arboras) C++17
0 / 100
82 ms 31472 KB
#include <bits/stdc++.h>

using namespace std;
#define int long long

const int N = 100100;
const int md = 1e9 + 7;

int n, q;
int p[N], d[N];
vector<int> g[N];
int best[N], best2[N];
int dp[N], dp2[N];
int hld[N];
int sz[N], tin[N], tout[N], T = 0;
int sum = 0;
int fen[N];
//pair<int, int> t[2 * N];
int arr[N];
int last;

bool is_par(int v, int u) {
    return tin[v] <= tin[u] && tout[v] >= tout[u];
}

int get_val(int pos) {
    /*int v = pos + n;
    pair<int, int> res = {-1, -1};
    while (v > 0) {
        res = max(res, t[v]);
        v >>= 1;
    }
    return res.second;*/
    return arr[pos];
}

void set_val(int l, int r, int val) {
    /*int vl = l + n, vr = r + n;
    while (vl < vr) {
        if (vl & 1) t[vl++] = {last++, val};
        if (vr & 1) t[--vr] = {last++, val};
        vl >>= 1, vr >>= 1;
    }*/
    for (int i = l; i < r; ++i) arr[i] = val;
}

int top(int v) {
    return get_val(tin[v]);
}

void set_path(int v, int u, int val) {
    while (u != -1 && is_par(v, u)) {
        if (is_par(v, hld[u])) {
            set_val(tin[hld[u]], tin[u] + 1, val);
        } else {
            set_val(tin[v], tin[u] + 1, val);
        }
        u = p[hld[u]];
    }
}

void add_fen(int pos, int val) {
    for (int i = pos + 1; i < N; i += i & -i) fen[i] += val;
}

void add_fen(int l, int r, int val) {
    sum += (r - l) * val;
    add_fen(l, val);
    add_fen(r, -val);
}

int get_fen(int r) {
    int res = 0;
    for (; r > 0; r -= r & -r) res += fen[r];
    return res;
}

void add_path(int v, int u, int val) {
    while (u != -1 && is_par(v, u)) {
        if (is_par(v, hld[u])) {
            add_fen(tin[hld[u]], tin[u] + 1, val);
        } else {
            add_fen(tin[v], tin[u] + 1, val);
        }
        u = p[hld[u]];
    }
}

int get_dp(int v) {
    return get_fen(tin[v] + 1);
}

void dfs(int v) {
    best[v] = best2[v] = -1;
    dp[v] = dp2[v] = 0;
    sz[v] = 1;
    for (int u : g[v]) {
        dfs(u);
        sz[v] += sz[u];
        if (dp[u] + d[u] > dp[v] || dp[v] == -1) {
            dp2[v] = dp[v], best2[v] = best[v];
            dp[v] = dp[u] + d[u], best[v] = u;
        } else if (dp[u] + d[u] > dp2[v] || dp2[v] == -1) {
            dp2[v] = dp[u] + d[u];
            best2[v] = u;
        }
    }
    sum += dp[v] + dp2[v];
}

void dfs1(int v) {
    tin[v] = T++;
    for (int &u : g[v]) {
        if (sz[u] > sz[g[v][0]]) swap(g[v][0], u);
    }
    for (int u : g[v]) {
        hld[u] = u == g[v][0] ? hld[v] : u;
        dfs1(u);
    }
    tout[v] = T;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    p[0] = -1;
    for (int i = 1; i < n; ++i) {
        cin >> p[i];
        g[p[i]].push_back(i);
    }
    for (int i = 1; i < n; ++i) {
        cin >> d[i];
    }
    dfs(0);
    dfs1(0);
    //for (int i = 0; i < 2 * n; ++i) t[i] = {-1, -1};
    for (int i = 0; i < n; ++i) arr[i] = -1;
    set_path(0, 0, 0);
    for (int i = 1; i < n; ++i) {
        int u = best[p[i]] == i ? top(p[i]) : i;
        set_path(i, i, u);
    }
    for (int i = 0; i < n; ++i) add_path(i, i, dp[i]);
    cout << sum << "\n";
    cin >> q;
    //int query_id = 0;
    while (q--) {
        int v, add;
        cin >> v >> add;
        int low = v;
        d[v] += add;
        if (v == best[p[v]]) {
            assert(top(v) == top(p[v]));
            add_path(top(v), p[v], add);
            /*cout << "query_id: " << query_id << endl;
            cout << "top: " << top(v) << endl;
            cout << "bottom: " << p[v] << endl;
            cout << "sum: " << sum << endl;*/
            v = top(v);
        }
        while (v != 0) {
            int new_dp = get_dp(v) + d[v];
            if (new_dp > get_dp(p[v])) {
                sum += get_dp(p[v]) - dp2[p[v]];
                dp2[p[v]] = get_dp(p[v]), best2[p[v]] = best[p[v]];
                best[p[v]] = v;
                add_path(top(p[v]), p[v], new_dp - get_dp(p[v]));
            } else {
                if (new_dp > dp2[p[v]]) {
                    sum += new_dp - dp2[p[v]];
                    dp2[p[v]] = new_dp, best2[p[v]] = v;
                }
                break;
            }
            v = top(p[v]);
        }
        set_path(v, low, v);
        /*cout << "top: ";
        for (int i = 0; i < n; ++i) cout << top(i) << " ";
        cout << endl;
        cout << "best: ";
        for (int i = 0; i < n; ++i) cout << best[i] << " ";
        cout << endl;*/
        cout << sum % md << "\n";
        //++query_id;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 5580 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 82 ms 28084 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 70 ms 31472 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 5580 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -