1 #include<cstdio>
  2 #include<iostream>
  3 using namespace std;
  4 
  5 const int MAXN=100005;
  6 struct node
  7 {
  8     int v;
  9     node *next;       
 10 }pool[MAXN],*h[MAXN];
 11 int cnt,tot;
 12 int fa[MAXN],dep[MAXN],size[MAXN],son[MAXN];
 13 int top[MAXN],rk[MAXN];
 14 
 15 void addedge(int u,int v)
 16 {
 17     node *p=&pool[++cnt];
 18     p->v=v;p->next=h[u];h[u]=p;     
 19 }
 20 void dfs1(int u)
 21 {
 22     int v;
 23     size[u]=1;
 24     int Bson=0,sonnum=-1;
 25     for(node *p=h[u];p;p=p->next)
 26         if(fa[u]!=(v=p->v))
 27         {
 28             fa[v]=u;
 29             dep[v]=dep[u]+1;
 30             dfs1(v);
 31             size[u]+=size[v];
 32             if(size[v]>Bson) Bson=size[v],sonnum=v;                   
 33         }
 34     son[u]=sonnum;
 35 }
 36 void dfs2(int u)
 37 {
 38     int v=son[u];
 39     if(v!=-1) 
 40     {
 41         top[v]=top[u];
 42         rk[v]=++tot;
 43         dfs2(v);
 44     }
 45     for(node *p=h[u];p;p=p->next)
 46         if(fa[v=p->v]==u && v!=son[u])
 47         {
 48             top[v]=v;
 49             rk[v]=++tot;
 50             dfs2(v);                 
 51         }
 52 }
 53 
 54 struct tree
 55 {
 56     int l,r,lazy,sum;
 57     tree *left,*right;       
 58 }t[3*MAXN],*root;
 59 int cnt1;
 60 void update(tree *p)
 61 {
 62     p->sum=0;
 63     if(p->left) p->sum+=p->left->sum;
 64     if(p->right) p->sum+=p->right->sum;     
 65 }
 66 void pushdown(tree *p)
 67 {
 68     if(p->lazy!=-1)
 69     {
 70         p->sum=p->lazy*(p->r-p->l+1);
 71         if(p->left) p->left->lazy=p->lazy,p->left->sum=p->lazy*(p->left->r-p->left->l+1);
 72         if(p->right) p->right->lazy=p->lazy,p->right->sum=p->lazy*(p->right->r-p->right->l+1);
 73         p->lazy=-1;
 74     }
 75 }
 76 void Build_Tree(tree *p,int l,int r)
 77 {
 78     p->l=l;p->r=r;p->lazy=-1;
 79     if(l==r) return;
 80     p->left=&t[++cnt1];p->right=&t[++cnt1]; 
 81     int mid=(l+r)/2;
 82     Build_Tree(p->left,l,mid);
 83     Build_Tree(p->right,mid+1,r);    
 84 }
 85 void change(tree *p,int l,int r,int c)
 86 {
 87     if(p->l==l && p->r==r)
 88     {
 89         p->lazy=c;
 90         p->sum=p->lazy*(p->r-p->l+1);
 91         return; 
 92     }
 93     pushdown(p);
 94     if(p->left->r>=r) change(p->left,l,r,c);
 95     else if(p->right->l<=l) change(p->right,l,r,c);
 96     else change(p->left,l,p->left->r,c),change(p->right,p->right->l,r,c);
 97     update(p);
 98 }
 99 int query(tree *p,int l,int r)
100 {
101     if(p->l==l && p->r==r)
102     {
103         if(p->lazy!=-1) p->sum=p->lazy*(p->r-p->l+1);
104         return p->sum;
105     }
106     pushdown(p);
107     if(p->left->r>=r) return query(p->left,l,r);
108     else if(p->right->l<=l) return query(p->right,l,r);
109     else return query(p->left,l,p->left->r)+query(p->right,p->right->l,r);
110 }
111 
112 int Sum(int x,int y)
113 {
114     int ret=0;
115     int f1=top[x],f2=top[y];    
116     while(f1!=f2)
117     {
118         if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
119         ret+=query(root,rk[f1],rk[x]);
120         change(root,rk[f1],rk[x],1);
121         x=fa[f1];f1=top[x];             
122     }
123     if(dep[x]>dep[y]) swap(x,y);
124     ret+=query(root,rk[x],rk[y]);
125     change(root,rk[x],rk[y],1);/**/
126     return ret; 
127 }
128 
129 int main()
130 {
131     int n,i,a,q;
132     char ch[11];
133     scanf("%d",&n);
134     for(i=1;i<n;i++) 
135         scanf("%d",&a),addedge(a,i);
136    
137     dfs1(0);
138     top[0]=0;rk[0]=++tot;
139     dfs2(0);
140     root=&t[++cnt1];
141     Build_Tree(root,1,n);
142     
143     scanf("%d",&q);
144     while(q --> 0)
145     {
146         scanf("%s%d",ch,&a);
147         if(ch[0]==‘i‘)
148             printf("%d\n",dep[a]+1-Sum(a,0));
149         else
150         {
151             printf("%d\n",query(root,rk[a],rk[a]+size[a]-1));
152             change(root,rk[a],rk[a]+size[a]-1,0);    
153         }
154     }
155     
156     return 0;    
157 }