1 //It is made by Awson on 2017.10.13
2 #include <set>
3 #include <map>
4 #include <cmath>
5 #include <ctime>
6 #include <stack>
7 #include <queue>
8 #include <string>
9 #include <cstdio>
10 #include <vector>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 #define Lr(r) (r<<1)
19 #define Rr(r) (r<<1|1)
20 using namespace std;
21 const int N = 1e5;
22
23 char ch[20];
24 int n, u;
25 struct tt {
26 int to, next;
27 }edge[N+5];
28 int path[N+5], TOP;
29 int size[N+5], son[N+5], fa[N+5], dep[N+5];
30 int pos[N+5], top[N+5], tot;
31 int sgm[(N<<2)+5], lazy[(N<<2)+5];
32
33 void add(int u, int v) {
34 edge[++TOP].to = v;
35 edge[TOP].next = path[u];
36 path[u]= TOP;
37 }
38 void dfs1(int u, int depth, int father) {
39 dep[u] = depth, size[u] = 1, son[u] = N+2, fa[u] = father;
40 for (int i = path[u]; i; i = edge[i].next) {
41 dfs1(edge[i].to, depth+1, u);
42 size[u] += size[edge[i].to];
43 if (size[edge[i].to] > size[son[u]]) son[u] = edge[i].to;
44 }
45 }
46 void dfs2(int u, int tp) {
47 top[u] = tp, pos[u] = ++tot;
48 if (son[u] != N+2) dfs2(son[u], tp);
49 for (int i = path[u]; i; i = edge[i].next)
50 if (edge[i].to != son[u])
51 dfs2(edge[i].to, edge[i].to);
52 }
53 void pushdown(int o, int l, int r) {
54 if (lazy[o] == 1) {
55 int mid = (l+r)>>1;
56 sgm[Lr(o)] = mid-l+1;
57 mid++;
58 sgm[Rr(o)] = r-mid+1;
59 lazy[Lr(o)] = lazy[Rr(o)] = 1;
60 lazy[o] = 0;
61 }else if (lazy[o] == -1) {
62 sgm[Lr(o)] = sgm[Rr(o)] = 0;
63 lazy[Lr(o)] = lazy[Rr(o)] = -1;
64 lazy[o] = 0;
65 }
66 }
67 void update(int o, int l, int r, int a, int b, int key) {
68 if (a <= l && r <= b) {
69 lazy[o] = key;
70 if (key == 1) sgm[o] = r-l+1;
71 else sgm[o] = 0;
72 return;
73 }
74 pushdown(o, l, r);
75 int mid = (l+r)>>1;
76 if (mid >= a) update(Lr(o), l, mid, a, b, key);
77 if (mid < b) update(Rr(o), mid+1, r, a, b, key);
78 sgm[o] = sgm[Lr(o)]+sgm[Rr(o)];
79 }
80 int query(int o, int l, int r, int a, int b) {
81 if (a <= l && r <= b) return sgm[o];
82 pushdown(o, l, r);
83 int mid = (l+r)>>1;
84 int ans = 0;
85 if (mid >= a) ans += query(Lr(o), l, mid, a, b);
86 if (mid < b) ans += query(Rr(o), mid+1, r, a, b);
87 return ans;
88 }
89 int lca(int u, int v) {
90 int sum = 0, ans = 0;
91 while (top[u] != top[v]) {
92 if (dep[top[u]] < dep[top[v]]) swap(u, v);
93 sum += pos[u]-pos[top[u]]+1;
94 ans += query(1, 1, n, pos[top[u]], pos[u]);
95 update(1, 1, n, pos[top[u]], pos[u], 1);
96 u = fa[top[u]];
97 }
98 if (dep[u] < dep[v]) swap(u, v);
99 sum += pos[u]-pos[v]+1;
100 ans += query(1, 1, n, pos[v], pos[u]);
101 update(1, 1, n, pos[v], pos[u], 1);
102 return sum-ans;
103 }
104 void work() {
105 scanf("%d", &n);
106 for (int i = 1; i < n; i++) {
107 scanf("%d", &u);
108 add(u, i);
109 }
110 dfs1(0, 1, 0);
111 dfs2(0, 0);
112 int q; scanf("%d", &q);
113 while(q--) {
114 scanf("%s%d", ch, &u);
115 if (ch[0] == ‘u‘) {
116 printf("%d\n",query(1, 1, n, pos[u], pos[u]+size[u]-1));
117 update(1, 1, n, pos[u], pos[u]+size[u]-1, -1);
118 }else printf("%d\n", lca(0, u));
119 }
120 }
121 int main() {
122 work();
123 return 0;
124 }