1 #include<bits/stdc++.h>
2 using namespace std;
3 #define MAXN 100010
4 struct node
5 {
6 int begin,end,next;
7 }edge[2*MAXN];
8 struct NODE
9 {
10 int left,right,tag,sum;
11 }tree[MAXN*5];
12 int cnt,Head[MAXN],size[MAXN],deep[MAXN],P[MAXN][17],ks[MAXN],js[MAXN],pos[MAXN],belong[MAXN],n,SIZE;
13 bool vis[MAXN];
14 void addedge(int bb,int ee)
15 {
16 edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
17 }
18 void addedge1(int bb,int ee)
19 {
20 addedge(bb,ee);addedge(ee,bb);
21 }
22 int read()
23 {
24 int s=0,fh=1;char ch=getchar();
25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();}
26 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();}
27 return s*fh;
28 }
29 void dfs1(int u)
30 {
31 int i,v;
32 size[u]=1;vis[u]=true;
33 for(i=Head[u];i!=-1;i=edge[i].next)
34 {
35 v=edge[i].end;
36 if(vis[v]==false)
37 {
38 deep[v]=deep[u]+1;
39 P[v][0]=u;
40 dfs1(v);
41 size[u]+=size[v];
42 }
43 }
44 }
45 void Ycl()
46 {
47 int i,j;
48 for(j=1;(1<<j)<=n;j++)
49 {
50 for(i=1;i<=n;i++)
51 {
52 if(P[i][j-1]!=-1)P[i][j]=P[P[i][j-1]][j-1];
53 }
54 }
55 }
56 void dfs2(int u,int chain)
57 {
58 int k=0,i,v;
59 pos[u]=++SIZE;belong[u]=chain;ks[u]=SIZE;
60 for(i=Head[u];i!=-1;i=edge[i].next)
61 {
62 v=edge[i].end;
63 if(deep[v]>deep[u]&&size[v]>size[k])k=v;
64 }
65 if(k==0){js[u]=SIZE;return;}
66 dfs2(k,chain);
67 for(i=Head[u];i!=-1;i=edge[i].next)
68 {
69 v=edge[i].end;
70 if(deep[v]>deep[u]&&v!=k)dfs2(v,v);
71 }
72 js[u]=SIZE;
73 }
74 void Pushup(int k)
75 {
76 tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
77 }
78 void Pushdown(int k)
79 {
80 int l=k*2,r=k*2+1;
81 if(tree[k].tag!=-1)
82 {
83 tree[l].tag=tree[k].tag;tree[r].tag=tree[k].tag;
84 int mid=(tree[k].left+tree[k].right)/2;
85 tree[l].sum=(mid-tree[k].left+1)*tree[k].tag;
86 tree[r].sum=(tree[k].right-mid)*tree[k].tag;
87 tree[k].tag=-1;
88 }
89 }
90 void Build(int k,int l,int r)
91 {
92 tree[k].left=l;tree[k].right=r;tree[k].tag=-1;
93 if(l==r)return;
94 int mid=(l+r)/2;
95 Build(k*2,l,mid);
96 Build(k*2+1,mid+1,r);
97 }
98 int Query_sum(int k,int ql,int qr)
99 {
100 if(ql<=tree[k].left&&tree[k].right<=qr)return tree[k].sum;
101 Pushdown(k);
102 int mid=(tree[k].left+tree[k].right)/2;
103 if(qr<=mid)return Query_sum(k*2,ql,qr);
104 else if(ql>mid)return Query_sum(k*2+1,ql,qr);
105 else return Query_sum(k*2,ql,mid)+Query_sum(k*2+1,mid+1,qr);
106 }
107 int Solve_sum(int x,int f)
108 {
109 int sum=0;
110 while(belong[x]!=belong[f])
111 {
112 sum+=Query_sum(1,pos[belong[x]],pos[x]);
113 x=P[belong[x]][0];
114 }
115 sum+=Query_sum(1,pos[f],pos[x]);
116 return sum;
117 }
118 void Change(int k,int l,int r,int C)
119 {
120 if(l<=tree[k].left&&tree[k].right<=r){tree[k].tag=C;tree[k].sum=(tree[k].right-tree[k].left+1)*C;return;}
121 Pushdown(k);
122 int mid=(tree[k].left+tree[k].right)/2;
123 if(r<=mid)Change(k*2,l,r,C);
124 else if(l>mid)Change(k*2+1,l,r,C);
125 else {Change(k*2,l,mid,C);Change(k*2+1,mid+1,r,C);}
126 Pushup(k);
127 }
128 void Solve_change(int x,int f,int k)
129 {
130 while(belong[x]!=belong[f])
131 {
132 Change(1,pos[belong[x]],pos[x],k);
133 x=P[belong[x]][0];
134 }
135 Change(1,pos[f],pos[x],k);
136 }
137 int main()
138 {
139 int bb,i,q,x;
140 char zs[12];
141 n=read();
142 memset(Head,-1,sizeof(Head));cnt=1;
143 for(i=2;i<=n;i++)
144 {
145 bb=read();addedge1(bb+1,i);
146 }
147 memset(P,-1,sizeof(P));
148 deep[1]=1;
149 dfs1(1);Ycl();
150 dfs2(1,1);
151 q=read();
152 Build(1,1,n);
153 for(i=1;i<=q;i++)
154 {
155 scanf("\n%s",zs);
156 if(zs[0]==‘i‘)
157 {
158 x=read();x++;
159 printf("%d\n",deep[x]-Solve_sum(x,1));
160 Solve_change(x,1,1);
161 }
162 else
163 {
164 x=read();x++;
165 printf("%d\n",Query_sum(1,ks[x],js[x]));
166 Change(1,ks[x],js[x],0);
167 }
168 }
169 fclose(stdin);
170 fclose(stdout);
171 return 0;
172 }