#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 7;
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 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;
}
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;
}
}
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]) {
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] = dp[u] + d[u];
best2[v] = u;
}
}
if (best[v] == -1 && !g[v].empty()) best[v] = g[v][0];
if (best2[v] == -1 && g[v].size() >= 2) best2[v] = g[v][1];
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};
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 kek = v;
d[v] += add;
if (v == best[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) {
if (get_dp(v) + d[v] > 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], get_dp(v) + d[v] - get_dp(p[v]));
} else {
if (get_dp(v) + d[v] > dp2[p[v]]) {
sum += get_dp(v) + d[v] - dp2[p[v]];
dp2[p[v]] = get_dp(v) + d[v], best2[p[v]] = v;
}
break;
}
v = top(p[v]);
}
set_path(v, kek, 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;
}
Compilation message
arboras.cpp: In function 'int main()':
arboras.cpp:148:9: warning: unused variable 'query_id' [-Wunused-variable]
148 | int query_id = 0;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
185 ms |
17176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
128 ms |
19148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |