-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ DRTREE.cpp
More file actions
79 lines (73 loc) · 1.4 KB
/
Copy pathSPOJ DRTREE.cpp
File metadata and controls
79 lines (73 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
int N, Q, W[MAXN], par[MAXN][35], D[MAXN];
long long sum[MAXN];
vector<int> A[MAXN];
void preCompute(){
for(int i=1;i<=30;i++)
for(int j=1;j<=N;j++)
par[j][i]=par[par[j][i-1]][i-1];
}
int KthAncestor(int X, int K){
for(int i=30;i>=0;i--){
if((1<<i)<=K){
K-=(1<<i);
X=par[X][i];
}
}
return X;
}
int lca(int a, int b){
if(D[a]<D[b]) swap(a, b);
int numToClimb=D[a]-D[b];
for(int i=30;i>=0;i--){
if((1<<i)<=numToClimb){
numToClimb-=(1<<i);
a=par[a][i];
}
}
if(a==b) return a;
for(int i=30;i>=0;i--){
if(par[a][i]==par[b][i]) continue;
a=par[a][i], b=par[b][i];
}
return par[a][0];
}
long long query(int x, int root){
if(x==root) return sum[1];
else if(lca(x, root)!=x)
return sum[x];
else{
int w=KthAncestor(root, D[root]-D[x]-1);
return sum[1]-sum[w];
}
}
void dfs(int a, int d){
sum[a]=W[a];
D[a]=d;
for(auto b : A[a]){
dfs(b, d+1);
sum[a]+=sum[b];
}
}
int main(){
scanf("%d", &N);
for(int i=1;i<=N;i++)
scanf("%d", &W[i]);
for(int i=2;i<=N;i++){
scanf("%d", &par[i][0]);
A[par[i][0]].push_back(i);
}
dfs(1, 1);
preCompute();
scanf("%d", &Q);
int root=1;
for(int x, i=1;i<=Q;i++){
char option;
scanf(" %c %d\n", &option, &x);
if(option=='R') root=x;
else printf("%lld\n", query(x, root));
}
return 0;
}