Problem:
Problem F: Marked Ancestor

You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:

M v: (Mark) Mark node v.

Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself.

Your job is to write a program that performs a sequence of these operations on a given tree and calculates
the value that each Q operation will print. To avoid too large output file, your program is requested to
print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to
calculate every output of query operations in a given sequence.

Input

The input consists of multiple datasets. Each dataset has the following format:

The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree
T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.

The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.

The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, print the sum of the outputs of all query operations in one line.

Sample Input

6 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0

Output for the Sample Input

4

Solution in C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; 
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;

template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args> 
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }
#define ldout fixed << setprecision(40) 

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9+7;

// ????¶?????????????????????!!!!!!!!!!!!!
template <class T>
struct fenwick_tree {
    /**********************/
    // ?????????????????
    /**********************/
    T T0 = 0;
    T op(T a, T b) { return a + b; }
    /**********************/
    // ??????????????????
    /**********************/

    // x????\?????????????????????????op=+, invop=1, T=int, T0=0???????????
    // i??????????±????????????????????????????s[i]????????????????x[i] = s[i]?????????????????? (??????: i=5, s[i]="45", x[i]=9)
    // 0123456789ABCDEF
    // 01234567--------
    // 0123----89AB----
    // 01--23--89--CD--
    // 0-2-4-6-8-A-C-E-
    int n_org;
    vector<T> x;
    fenwick_tree(int n_)  { 
        n_org = n_;
        int n = 1; while (n <= n_) n *= 2;
        x = vector<T>(n, T0);
    }
    // O(log n)
    T query(int j) {
        T S = T0;
        for (j; j >= 0; j = (j & (j + 1)) - 1)  // j????????C->B->7??????????????????????£0????????????C????\?\????????????????????????????????????????????????????????????
            S = op(S, x[j]); 
        return S;
    }
    // O(log n)
    void update(int k, T a) {
        for (; k < x.size(); k |= k+1) // k????????C->D->F??????????????????????£C????\?\?????????????????????????????????????????????????
            x[k] = op(x[k], a); 
    }
    // O(1)
    // ?????????x[i]????\?????????????????????????????????
    T access(int k) {
        return query(k) - (k ? query(k-1) : 0);
    }
    void print(void) {
        for (int i = 0; i < n_org; i++) 
            cout << access(i) << " ";
        cout << endl;
    }
    void print_raw(void) {
        for (int i = 0; i < x.size(); i++) 
            cout << x[i] << " ";
        cout << endl;
    }
};

// ??????????????
//
// ????????????O(n): ??????????????????????\?????????, ??????????, ????????????????\??????
//
// LCA O(log n)
// ??????????????????????? O(log n)
// ????????????????¬????????\?\????????????????¬???????? O(log n)
class Tree {
public:
    static const int MAXLOGV = 25;
    vector<vector<int> > m_edges; // m_edges[i][j]??????????????: i->j??????????????????????
    int vn; // ?????????????, vn<2^MAXLOGV
    int root; // ??????????????????????????????

    vector<vector<int>> parent; // parent[i][j]: j??????i^2??????????????????j=0?????????????????????
    vector<int> depth; // depth[i]: ????????i???????????????????????????????, ????????????0

    vector<int> euler; // ????????????????????????\?????????????????\???????????????????????vn*2
    vector<int> f; // f[i] = euler??????i??????????????????????1????????????°?????????, ????????vn
    vector<int> s; // f[i] = euler??????i??????????????????????2????????????°?????????, ????????vn

    /*********/
    // ????????????
    /*********/
    Tree(int vn, int root) : vn(vn), root(root) {
        parent.resize(MAXLOGV);
        m_edges.resize(vn);
        for (int i = 0; i < MAXLOGV; i++) 
            parent[i].resize(vn);
        depth.resize(vn);
    }

    // ????????????????????????
    void unite(int u, int v) {
        m_edges[u].push_back(v);
        m_edges[v].push_back(u);
    }

    // root?????????????????????????????????????§??????
    // unite????????????????????????????????????????????????????????????????
    void init() {
        euler.clear(); euler.reserve(2 * vn);
        f.clear(); f.resize(vn);
        s.clear(); s.resize(vn);
        dfs(root, -1, 0);
        for (int k = 0; k+1 < MAXLOGV; k++) // 2^k?????????????????????×????
            for (int v = 0; v < vn; v++) 
                if (parent[k][v] < 0) 
                    parent[k+1][v] = -1; // 2^k??????????????????????????¶??????????????????¢2^(k+1)????????????????????????¬?????¶????????????????????
                else 
                    parent[k+1][v] = parent[k][parent[k][v]]; // 2^(k+1)???????????????2^k?????????????2^k?????????????
    }

    // 1??????????????????????\???????????????????\?????????????????????
    void dfs(int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        f[v]=euler.size();
        euler.push_back(v);
        for (int next : m_edges[v]) 
            if (next != p)
                dfs(next, v, d+1);
        s[v]=euler.size();
        euler.push_back(v);
    }

    /*********/
    // ??????????????????
    /*********/
    // ????????u, v??????????????????????????????????
    //
    // O(log n)
    int lca(int u, int v) const {
        if (depth[u] > depth[v]) swap(u, v); // u????????????????????????????????????
        for (int k = 0; k < MAXLOGV; k++)    // v??????u???????±??????????????????????????????????
            if ((depth[v] - depth[u])>>k & 1) 
                v = parent[k][v];
        if (u == v) return u;
        for (int k = MAXLOGV-1; k >= 0; k--) { // ????????????????????????????????\??????????????????
            if (parent[k][u] == parent[k][v]) // ??????????????
                continue; 
            u = parent[k][u];
            v = parent[k][v];
        }
        return parent[0][u];
    }

    // u??????v?????????\???????????????
    // ???????\????\???????\?????????????????????????????????????????????????????
    //
    // O(log n)
    int dist(int u, int v) const {
        int p = lca(u, v);
        return (depth[u]-depth[p]) + (depth[v]-depth[p]);
    }

    // [root, v]?????????????????f???????????????????????????????¬??????\?????????????????????
    // 1??????????????????????????????????????????-1????????????????????
    //
    // O(log n)
    int binary_search(int v, function<bool(int)> f) const {
        for(int j = MAXLOGV - 1; j >= 0;j--) 
            if(parent[j][v] != -1 && f(parent[j][v]))
                v = parent[j][v];
        return f(v) ? v : -1;
    }

    // ?????????????????????
    void print_dfs(int v, int p) const {
        for (int i = 0; i < depth[v]; i++)
            cout << " ";
        cout << v << endl;
        for (int next : m_edges[v]) if (next != p) 
            print_dfs(next, v);
    }
    void print(void) const {
        print_dfs(root, -1);
    }
};

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);

    ll n, q; 
    while (cin >> n >> q && n && q) {
        Tree t(n, 0);
        rep(i, n-1) {
            ll tmp; cin >> tmp;
            t.unite(i+1, tmp-1); 
        }
        t.init();

        fenwick_tree<ll> ft(n*2);
        ft.update(t.f[0], 1);
        ft.update(t.s[0], -1);

        ll ret = 0;
        rep(_, q) {
            char c; ll v; cin >> c >> v; v--;
            if (c == 'M') {
                if (ft.access(t.f[v]))
                    continue;
                ft.update(t.f[v], 1);
                ft.update(t.s[v], -1);
            } else {
                ll marked_num = ft.query(t.f[v]);
                ret += t.binary_search(v, [&](ll x){ return ft.query(t.f[x]) == marked_num; }) + 1; 
            }
        }
        cout << ret << endl;
    }
    return 0;
}