1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 #define rep(i,l,r) for(int i=l; i<=r; i++)
6 #define clr(x,y) memset(x,y,sizeof(x))
7 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
8 using namespace std;
9 const int INF = 0x3f3f3f3f;
10 const int maxn = 100010;
11 inline int read(){
12 int ans = 0, f = 1;
13 char c = getchar();
14 for(; !isdigit(c); c = getchar())
15 if (c == ‘-‘) f = -1;
16 for(; isdigit(c); c = getchar())
17 ans = ans * 10 + c - ‘0‘;
18 return ans * f;
19 }
20 struct Edge{
21 Edge *pre; int to;
22 }edge[maxn<<1];
23 Edge *last[maxn], *pt = edge;
24 struct Node{
25 int l,r,v,t;
26 }t[maxn<<2];
27 int n,m,x,y,q,segnum=0,dep[maxn],top[maxn],size[maxn],fa[maxn],pos[maxn];
28 char ch[10];
29 inline void addedge(int x,int y){
30 pt->pre = last[x]; pt->to = y; last[x] = pt++;
31 }
32 void dfs(int x){
33 size[x] = 1;
34 travel(x){
35 dep[p->to] = dep[x] + 1;
36 fa[p->to] = x;
37 dfs(p->to);
38 size[x] += size[p->to];
39 }
40 }
41 void dfs(int x,int chain){
42 pos[x] = ++segnum; top[x] = chain;
43 int k = 0;
44 travel(x){
45 if (size[p->to] > size[k]) k = p->to;
46 }
47 if (!k) return;
48 dfs(k,chain);
49 travel(x){
50 if (p->to != k) dfs(p->to,p->to);
51 }
52 }
53 void build(int u,int v,int w){
54 t[w].l = u; t[w].r = v; t[w].v = 0; t[w].t = -1;
55 if (u == v) return;
56 int mid = (u + v) >> 1;
57 build(u,mid,w<<1); build(mid+1,v,w<<1|1);
58 }
59 inline void pushdown(int w){
60 if (t[w].l == t[w].r || t[w].t == -1) return;
61 t[w<<1].t = t[w<<1|1].t = t[w].t;
62 t[w<<1].v = (t[w<<1].r - t[w<<1].l + 1) * t[w].t;
63 t[w<<1|1].v = (t[w<<1|1].r - t[w<<1|1].l + 1) * t[w].t;
64 t[w].t = -1;
65 }
66 inline void maintain(int w){
67 t[w].v = t[w<<1].v + t[w<<1|1].v;
68 }
69 void modify(int u,int v,int w,int x){
70 pushdown(w);
71 if (t[w].l == u && t[w].r == v){
72 t[w].v = x * (v - u + 1);
73 t[w].t = x; return;
74 }
75 int mid = (t[w].l + t[w].r) >> 1;
76 if (v <= mid) modify(u,v,w<<1,x);
77 else if (u > mid) modify(u,v,w<<1|1,x);
78 else{
79 modify(u,mid,w<<1,x);
80 modify(mid+1,v,w<<1|1,x);
81 }
82 maintain(w);
83 }
84 int query(int u,int v,int w){
85 pushdown(w);
86 if (t[w].l == u && t[w].r == v) return t[w].v;
87 int mid = (t[w].l + t[w].r) >> 1;
88 if (v <= mid) return query(u,v,w<<1);
89 else if (u > mid) return query(u,v,w<<1|1);
90 else return query(u,mid,w<<1) + query(mid+1,v,w<<1|1);
91 }
92 int install(int x,int y){
93 int ret = 0;
94 while (top[x] != top[y]){
95 if (dep[top[x]] < dep[top[y]]) swap(x,y);
96 ret += query(pos[top[x]],pos[x],1);
97 modify(pos[top[x]],pos[x],1,1);
98 x = fa[top[x]];
99 }
100 if (dep[x] < dep[y]) swap(x,y);
101 ret += query(pos[y],pos[x],1);
102 modify(pos[y],pos[x],1,1);
103 return ret;
104 }
105 int uninstall(int x){
106 int ret = query(pos[x],pos[x]+size[x]-1,1);
107 modify(pos[x],pos[x]+size[x]-1,1,0);
108 return ret;
109 }
110 int main(){
111 n = read(); clr(last,0);
112 rep(i,1,n-1){
113 x = read();
114 addedge(x+1,i+1);
115 }
116 dep[1] = 0; dfs(1); dfs(1,1); build(1,n,1);
117 q = read();
118 rep(i,1,q){
119 scanf("%s",ch); x = read() + 1;
120 if (ch[0] == ‘i‘) printf("%d\n",dep[x] + 1 - install(1,x));
121 else printf("%d\n",uninstall(x));
122 }
123 return 0;
124 }