#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;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
5528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1677 ms |
5612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |