stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cmath>

#include <cstdio>

#include <string>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <set>

#include <map>

#include <queue>

#include <vector>

#define rep(i, a, b) for (int i = a; i < b; ++i)

#define repr(i, a, b) for (int i = a; i <= b; ++i)

#define dep(i, a, b) for (int i = a; i > b; --i)

#define depr(i, a, b) for (int i = a; i >= b; --i)

#define getnum(n) scanf("%d", &n)

#define LL long long

#define INF 0x7f7f7f7f

#define PI acos(-1.0)

using namespace std;



const int MAXN = 1e5 + 5;

int f[MAXN], mark[MAXN], n, q, x, qt[MAXN], qv[MAXN], cnt, t;

LL ans;



inline int Find(int x) {

	return mark[x] < t ? x : f[x] = Find(f[x]);

}



inline void Init() {

	mark[1] = cnt = ans = 0;

	f[1] = 1;

	repr(i, 2, n) {

		getnum(f[i]);

		mark[i] = INF;

	}

}



inline void Solve() {

	char op[2];

	repr(i, 1, q) {

		scanf("%s%d", op, &x);

		if (op[0] == 'Q') {

			qt[cnt] = i;

			qv[cnt++] = x;

		}

		else mark[x] = min(mark[x], i);

	}

	while (cnt) {

		t = qt[--cnt];

		ans += Find(qv[cnt]);

	}

	

	printf("%lld\n", ans);

}



int main()

{

	while (~scanf("%d%d", &n, &q) && (n || q)) {

		Init();

		Solve();

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.155681116582829x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
/*

 ━━━━━?

 ┓┏┓┏┓┃μ'sic foever!!

 ┛┗┛┗┛┃＼○／

 ┓┏┓┏┓┃ /

 ┛┗┛┗┛┃ノ)

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┃┃┃┃┃┃

 ┻┻┻┻┻┻

 */

#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <map>

#include <stack>

#include <vector>

using namespace std;

const int maxn=1e5+10;

bool vis[maxn];

int pre[maxn];

long long res;

bool flag;

int find(int x){

    int r = x;

    while(pre[r]!=r){

        if(!flag&&vis[pre[r]]){

            res += pre[r];

            flag = true;

            return -1;

        }

        r = pre[r];

    }

    return r;

}

void join(int a,int b){

    pre[b]=a;

}

int main(){

    int n,q,i,j,a,b;

    char op;

    while(~scanf("%d%d",&n,&q)&&n&&q){

        res = 0;

        memset(vis, false, sizeof(vis));

        for(i=1;i<=n;i++){

            pre[i] = i;

        }

        for(i=2;i<=n;i++){

            scanf("%d",&a);

            join(a,i);

        }

        getchar();

        for(i=1;i<=q;i++){

            scanf("%c %d",&op,&a);

            if(op=='Q'){

                if(!vis[a]){

                    flag = false;

                    if(find(a)==-1){

                        

                    }else{

                        res += 1;

                    }

                }else{

                    res += a;

                }

            }else{

                vis[a] = true;

            }

            getchar();

        }

        printf("%lld\n",res);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.672547439822098x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

#define MAXN 100005

int Set[MAXN];

int Root(int x)

{return Set[x]==x?x:Set[x]=Root(Set[x]);}

int N,Q;

int fa[MAXN];

int Que[MAXN][2];

int marked[MAXN];

int main()

{

	char op[3];

	int id;

	long long ans;

	while(~scanf("%d%d",&N,&Q)&&N&&Q)

	{

		memset(Que,0,sizeof Que);

		memset(fa,0,sizeof fa);

		memset(Set,0,sizeof Set);

		memset(marked,0,sizeof marked);

		ans=0;

		Set[1]=1;fa[1]=1;

		for(int i=2;i<=N;i++)

		{

			scanf("%d",fa+i);

			Set[i]=fa[i];

		}

		for(int i=1;i<=Q;i++)

		{

			scanf("%s%d",op,&id);

			if(op[0]=='Q')Que[i][0]=1;

			else {Que[i][0]=2;Set[id]=id;marked[id]++;}

			Que[i][1]=id;

		}

		for(int i=Q;i>=1;i--)

		{

			if(Que[i][0]==1)

				ans+=1LL*Root(Que[i][1]);

			else

			{

				marked[Que[i][1]]--;

				if(!marked[Que[i][1]])

					Set[Que[i][1]]=fa[Que[i][1]];

			}

		}

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 11.327737930760076x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn = 2e5 + 5;

#define bug(x) printf("  %s = %d \n",#x,x);

#define mem(arr,x) memset(arr,x,sizeof arr);

int par[maxn];



int dfs(int pos){

	if(par[pos] != pos)

		return dfs(par[pos]);

	else

	 	return pos;

}



int main() {

#ifdef LOCAL

	freopen("in.txt", "r", stdin);

#endif

	ios_base::sync_with_stdio(0);

	int N,Q;

	while(scanf("%d%d",&N,&Q) == 2){

		if(N == 0 && Q == 0) break;

		par[1] = 1;

		for(int i = 2; i <=  N; i++){

			scanf("%d",par + i);

		}

		char op;

		int  pos;

		long long  Sum = 0;

		getchar();

		for(int i = 1; i <= Q;i++){

			scanf("%c %d",&op,&pos);

//			bug(op);

			if(op == 'Q'){

				Sum += dfs(pos);

			}

			else{

				par[pos] = pos;

			}

			getchar();

		}

		printf("%lld\n",Sum);

	}

}

// Faster C++ solution for the same problem (measured speedup: 5.223318759882736x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

using namespace std;



const int N = 100000;



int n, p;

int pre[N+1];



int find(int x)

{

    while (x != pre[x])

        x = pre[x];

    return x;

}



int main(void)

{

    while (scanf("%d%d", &n, &p) != EOF) {

        if (!n && !p) break;

        pre[1] = 1;

        for (int i = 2; i <= n; i ++)

            scanf("%d", &pre[i]);

        long long sum = 0;

        for (int i = 1; i <= p; i ++) {

            char s[2];

            int x;

            scanf("%s%d", s, &x);

            if (s[0] == 'M')

                pre[x] = x;

            else

                sum += find(x);

        }

        printf("%lld\n", sum);

    }



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.154316995426958x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<cstring>

const int MAX_N=100000+16;

int a[MAX_N*2];

bool mark[MAX_N*2];

int N,Q;

int main(){

    //freopen("1.txt","r",stdin);

    while(scanf("%d%d",&N,&Q)!=EOF){

        if(N==0&&Q==0)break;

        memset(mark,false,sizeof(bool)*(N+10));

        a[1]=0;

        mark[1]=1;

        for(int i=2;i<=N;i++){

            scanf("%d",&a[i]);

        }

        long ans=0;

        while(Q--){

            char c[2];

            int x;

            scanf("%s %d",c,&x);

            if(c[0]=='Q'){

                while(!mark[x]){

                    x=a[x];

                }

                ans+=x;

            }

            else {

                mark[x]=true;

            }

        }

        printf("%ld\n",ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.113544005317414x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long ll;

typedef pair<int,int>P;

const int MAXN=100010;



int f[MAXN], mark[MAXN];

P Q[MAXN];



int getf(int k, int id)

{

	return mark[k] < id ? k : f[k] = getf(f[k], id);

	/*如果k?点被??的??早于????，就返回k。否?????k的父?点，

	且因???路径上有一些?点未被??或者???期，可以路径??,直接?被

	???点到最近???点路径上的所有?点都指向最近???点*/

}



int main()

{

	int n, q;

	while(~scanf("%d %d", &n, &q),n + q)

	{

		for(int i = 2; i <= n; i++)

		{

			scanf("%d", &f[i]);

			mark[i] = inf;

		}

		int cnt = 0;

		char c;

		int x;

		for(int i = 1; i <= q; i++)

		{

            getchar();

			scanf("%c %d", &c, &x);

			if(c == 'Q')

			Q[cnt++] = P(i, x);//P(i,x)表示在i?刻??x的最近??点

			else

			mark[x] = min(mark[x], i);//mark[i]表示i?点第一次被??的??，inf表示未被??

		}

		ll ans = 0;

		for(int i = cnt - 1; i >= 0; i--)

		ans += getf(Q[i].second, Q[i].first);

		printf("%lld\n", ans);

	}

 	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.152230693348907x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

using namespace std;



#define FOR(i,a,b) for(int i=(a),i##_end___=(b);i<i##_end___;i++)

#define DFOR(i,a,b) for(int i=(a)-1,i##_end___=(b);i>=i##_end___;i--)

typedef long long LL;

typedef double DB;

const int V=1e5+5;



int n,m;

int Fa[V];

int Mark[V];



struct Ask{

	char op[4];

	int v;

}Que[V];



int dfs(int v,int tim){return Mark[v]<tim?v:Fa[v]=dfs(Fa[v],tim);}



int main(){

	while(~scanf("%d%d",&n,&m)){

		if(!n&&!m)break;

		

		FOR(i,2,n+1)Mark[i]=1e9,scanf("%d",&Fa[i]);

		FOR(i,1,m+1){

			scanf("%s%d",Que[i].op,&Que[i].v);

			if(Que[i].op[0]=='M')Mark[Que[i].v]=min(Mark[Que[i].v],i);

		}

		

		LL ans=0;

		

		DFOR(i,m+1,1)if(Que[i].op[0]=='Q')ans+=dfs(Que[i].v,i);

		

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<algorithm>

#include<iostream>

#include<string.h>

#include<sstream>

#include<stdio.h>

#include<math.h>

#include<vector>

#include<string>

#include<queue>

#include<set>

#include<map>

//#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;

const int INF=0x3f3f3f3f;

const double eps=1e-8;

const double PI=acos(-1.0);

typedef long long ll;

const int maxn=100010;

int tree[maxn];

int Find(int x)

{

    if(tree[x]==x)

        return x;

    return Find(tree[x]);

}

int main()

{

    int n,m,id,i;

    char com[10];

    ll ans;



    while(scanf("%d%d",&n,&m),n||m)

    {

        tree[1]=1;

        ans=0;

        for(i=2;i<=n;i++)

            scanf("%d",&tree[i]);

        for(i=0;i<m;i++)

        {

            scanf("%s%d",com,&id);

            if(com[0]=='M')

                tree[id]=id;

            else

                ans+=Find(id);

        }

        printf("%lld\n",ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1687483831261565x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
//

//  AOJ 2170 Marked Ancestor

//

//  Created by TaoSama on 2015-03-16

//  Copyright (c) 2015 TaoSama. All rights reserved.

//

#include <algorithm>

#include <cctype>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iomanip>

#include <iostream>

#include <map>

#include <queue>

#include <string>

#include <set>

#include <vector>

#define CLR(x,y) memset(x, y, sizeof(x))



using namespace std;

const int INF = 0x3f3f3f3f;

const int MOD = 1e9 + 7;

const int N = 1e5 + 10;



int n, q, par[N];



int find(int x) {

	if(par[x] == x) return x;

	return find(par[x]);

}



int main() {

#ifdef LOCAL

	freopen("in.txt", "r", stdin);

//	freopen("out.txt","w",stdout);

#endif

	ios_base::sync_with_stdio(0);



	while(~scanf("%d%d", &n, &q) && n && q) {

		par[1] = 1;

		for(int i = 2; i <= n; ++i) scanf("%d", &par[i]);



		long long ans = 0;

		for(int i = 1; i <= q; ++i) {

			char op[2]; int x;

			scanf("%s%d", op, &x);

			if(op[0] == 'Q') ans += find(x);

			else par[x] = x;

		}

		printf("%lld\n", ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.195491875172023x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
//支持分?操作的并?集

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N=1e5+10;

int par[MAX_N];

int find(int x){

  int tmp=x;

  while(par[tmp]!=tmp){

    tmp=par[tmp];

  }

  return tmp;

}

int main(){

  //freopen("in", "r", stdin);

  char op[2];

  int N, Q, x;

  ll ans;

  par[1]=1;

  while(scanf("%d%d", &N, &Q), N){

    ans=0;

    for(int i=2; i<=N; i++){

      scanf("%d", par+i);

    }

    while(Q--){

      scanf("%s%d", op, &x);

      if(*op=='M'){

        par[x]=x;

      }

      else ans+=(ll)find(x);

    }

    printf("%lld\n", ans);

  }

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1663706630866075x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<string>

#include<cstring>

#include<queue>

#include<vector>

#include<stack>

#include<vector>

#include<map>

#include<set>

#include<algorithm>

//#include<bits/stdc++.h>

using namespace std;



const int maxn = 1e5+5;

int pa[maxn];

int Mrk[maxn];



int qt[maxn],qv[maxn];

int Qt;

int fdst(int x)

{

    return Mrk[x] < Qt ? x :pa[x] = fdst(pa[x]);

}



//#define LOCAL

int main()

{

#ifdef LOCAL

    freopen("in.txt","r",stdin);

#endif

    int N, Q;

    while(scanf("%d%d",&N,&Q),N){

        for(int i = 2; i <= N; i++){

            scanf("%d",pa+i);

            Mrk[i] = Q+1;

        }

        int c = 0;

        for(int i = 1; i <= Q; i++){

            char op[2];

            int v;

            scanf("%s%d",op,&v);

            if(*op == 'Q'){

                qt[c] = i;

                qv[c++] = v;

            }else {

                Mrk[v] = min(Mrk[v],i);//取最早生效的??

            }

        }

        long long ans = 0;

        while(c--){

            Qt = qt[c];

            ans += fdst(qv[c]);

        }

        printf("%lld\n",ans);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.130921331630718x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<cstdlib>

#include<cstring>

using namespace std;

const int MAXN = 100000 + 10;

int par[MAXN];

int Find(int x){

	if (x == par[x])

		return par[x];

	return Find(par[x]);

}

int main(){

	int n, q;

	char ins[10];

	int v;



	while (~scanf("%d%d", &n, &q) && n && q){

		long long sum = 0;

		par[1] = 1;

		for (int i = 2; i <= n; i++){

			scanf("%d", &par[i]);

		}

		for (int i = 0; i < q; i++){

			scanf("%s%d", ins, &v);

			if (strcmp(ins, "M") == 0){

				par[v] = v;

			}

			else{

				sum += Find(v);

			}

		}

		printf("%lld\n", sum);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.125863381100266x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}
// Slower C++ solution:
#include <cmath>

#include <cstdio>

#include <string>

#include <cstring>

#include <iostream>

#include <algorithm>

#include <set>

#include <map>

#include <queue>

#include <vector>

#define rep(i, a, b) for (int i = a; i < b; ++i)

#define repr(i, a, b) for (int i = a; i <= b; ++i)

#define dep(i, a, b) for (int i = a; i > b; --i)

#define depr(i, a, b) for (int i = a; i >= b; --i)

#define getnum(n) scanf("%d", &n)

#define LL long long

#define INF 0x7f7f7f7f

#define PI acos(-1.0)

using namespace std;



const int MAXN = 1e5 + 5;

int f[MAXN], mark[MAXN], n, q, x, qt[MAXN], qv[MAXN], cnt, t;

LL ans;



inline int Find(int x) {

	return mark[x] < t ? x : f[x] = Find(f[x]);

}



inline void Init() {

	mark[1] = cnt = ans = 0;

	f[1] = 1;

	repr(i, 2, n) {

		getnum(f[i]);

		mark[i] = INF;

	}

}



inline void Solve() {

	char op[2];

	repr(i, 1, q) {

		scanf("%s%d", op, &x);

		if (op[0] == 'Q') {

			qt[cnt] = i;

			qv[cnt++] = x;

		}

		else mark[x] = min(mark[x], i);

	}

	while (cnt) {

		t = qt[--cnt];

		ans += Find(qv[cnt]);

	}

	

	printf("%lld\n", ans);

}



int main()

{

	while (~scanf("%d%d", &n, &q) && (n || q)) {

		Init();

		Solve();

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.1697581392986285x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

		scanf("%d%d",&n,&q);

		if(!(n||q))

			break;

		a[1]=1;

		for(int i=2;i<=n;i++)

			scanf("%d",a+i);

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		printf("%lld\n",ans);

	}

	return 0;

}