Language: C++

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

class LCADist{
	int n;
	vector< vector<int> > parentI;
	vector< vector<ll > > parentL;
	vector<int> depth;
	ll ans;
	int maxdepth;
	
	void initdfs(vector< vector<pii> > &g, int r, int p, int d){
		depth[r] = d;
		REP(i, g[r].size()){
			if(g[r][i].first != p){
				parentI[0][g[r][i].first] = r;
				parentL[0][g[r][i].first] = g[r][i].second;
				initdfs(g, g[r][i].first, r, d+1);
			}
		}
	}
public:
	LCADist(vector< vector<pii> > &g, int root) {
		n=g.size();
		maxdepth = 0;
		while(n>>maxdepth) maxdepth ++;
		
		parentI = vector< vector<int> >(maxdepth+1, vector<int>(n));
		parentL = vector< vector<ll > >(maxdepth+1, vector<ll>(n));
		depth = vector<int>(n);
		
		parentI[0][0] = -1;
		initdfs(g, root, -1, 0);
		REP(k, maxdepth){
			REP(i, n){
				if(parentI[k][i] < 0) parentI[k+1][i] = -1;
				else{
					parentI[k+1][i] = parentI[k][parentI[k][i]];
					parentL[k+1][i] = parentL[k][parentI[k][i]] + parentL[k][i];
				}
			}
		}
	}
	
	ll lca(int u, int v){
		if(depth[u] > depth[v]) swap(u, v);
		ll ans = 0;
		if(u==v) return ans;
		REP(k, maxdepth+1){
			if((depth[v]-depth[u])>>k&1){
				ans += parentL[k][v];
				v = parentI[k][v];
			}
		}
		if(u==v) return ans;
		RREP(k, maxdepth+1){
			if(parentI[k][u] != parentI[k][v]){
				ans += parentL[k][u] + parentL[k][v];
				u = parentI[k][u];
				v = parentI[k][v];
			}
		}
		return ans + parentL[0][u] + parentL[0][v];
	}
	
	int getDepth(int v){
		return depth[v];
	}
};
int n, q;
const int BLOCK = 255
main(){
	ios::sync_with_stdio(false);
	while(cin >> n >> q, n){
		vector< vector<pii> > g(n);
		REP(i, n-1){
			int p; cin >> p; p--;
			g[p].emplace_back(i+1, 1);
		}
		LCADist lca(g, 0);
		set<int> marked;
		vector<pii> res(n);
		REP(i, n) res[i] = pii(lca.getDepth(i), 0);
		ll sum = 0;
		REP(i, q){
			char c;
			int v;
			cin >> c >> v; v--;
			if(c == 'Q'){
				pii ans = res[v];
				FOR(u, marked){
					int d = lca.lca(*u, v);
					if(lca.getDepth(v) - lca.getDepth(*u) == d)
						ans = min(ans, pii(d, *u));
				}
				sum += ans.second+1;
			}else{
				marked.insert(v);
			}
			if((i & BLOCK) == BLOCK){
				queue<pair<pii, int>> que;
				FOR(u, marked) que.emplace(pii(0, *u), *u);
				marked.clear();
				while(!que.empty()){
					pii c = que.front().first;
					const int v = que.front().second;
					que.pop();
					if(res[v] < c) continue;
					res[v] = c;
					c.first ++;
					FOR(e, g[v]){
						if(chmin(res[e->first], c))
							que.emplace(c, e->first);
					}
				}
			}
		}
		cout << sum << endl;
	}
	return 0;
}

Compiler output:
a.cc:123:1: error: expected ',' or ';' before 'main'
  123 | main(){
      | ^~~~