alexjercan/bugnet huggingface dataset:
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

#include <utility>

using namespace std;

int N, Q;
pair<char, int> S[100000];
int parent[100001];
int mark[100001];
int set[100001];

int findRoot(int p) {
  int root = p;
  while (set[root] != -1) {
    root = set[root];
  }
  while (p != root) {
    int t = set[p];
    set[p] = root;
    p = t;
  }
  return root;
}

void unionSet(int p, int q) {
  int root1 = findRoot(p);
  int root2 = findRoot(q);
  if (root1 != root2) {
    set[root1] = root2;
  }
}

int main() {
  // freopen("MarkedAncestor.in", "r", stdin);
  // freopen("2170-input.txt", "r", stdin);
  while (scanf("%d%d", &N, &Q) == 2 && !(N == 0 && Q == 0)) {
    parent[1] = 1;
    for (int i = 2; i <= N; ++i) {
      scanf("%d", &parent[i]);
    }
    set[1] = -1;
    for (int i = 2; i <= N; ++i) {
      set[i] = parent[i];
    }
    memset(mark, 0, sizeof(mark));
    mark[1] = 1;
    for (int i = 0; i < Q; ++i) {
      char buf[10];
      scanf("%s%d", buf, &S[i].second);
      S[i].first = buf[0];
      if (S[i].first == 'M') {
        set[S[i].second] = -1;
        mark[S[i].second]++;
      }
    }
    long long sum = 0;
    // From back to front.
    for (int i = Q - 1; i >= 0; --i) {
      if (S[i].first == 'M') {
        if (--mark[S[i].second] == 0) {
          unionSet(S[i].second, parent[S[i].second]);
        }
      } else if (S[i].first == 'Q') {
        int v = findRoot(S[i].second);
        // printf("findRoot(%d) = %d\\n", S[i].second, v);
        sum += v;
      }
    }
    printf("%lld\\n", sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

#include <utility>

using namespace std;

int N, Q;
pair<char, int> S[100000];
int parent[100001];
int set[100001];

int findRoot(int p) {
  int root = p;
  while (set[root] != -1) {
    root = set[root];
  }
  while (p != root) {
    int t = set[p];
    set[p] = root;
    p = t;
  }
  return root;
}

void unionSet(int p, int q) {
  int root1 = findRoot(p);
  int root2 = findRoot(q);
  if (root1 != root2) {
    set[root1] = root2;
  }
}

int main() {
  // freopen("MarkedAncestor.in", "r", stdin);
  while (scanf("%d%d", &N, &Q) == 2 && !(N == 0 && Q == 0)) {
    for (int i = 2; i <= N; ++i) {
      scanf("%d", &parent[i]);
    }
    set[1] = -1;
    for (int i = 2; i <= N; ++i) {
      set[i] = parent[i];
    }
    for (int i = 0; i < Q; ++i) {
      char buf[10];
      scanf("%s%d", buf, &S[i].second);
      S[i].first = buf[0];
      if (S[i].first == 'M') {
        set[S[i].second] = -1;
      }
    }
    long long sum = 0;
    // From back to front.
    for (int i = Q - 1; i >= 0; --i) {
      if (S[i].first == 'M') {
        unionSet(S[i].second, parent[S[i].second]);
      } else if (S[i].first == 'Q') {
        int v = findRoot(S[i].second);
        // printf("findRoot(%d) = %d\\n", S[i].second, v);
        sum += v;
      }
    }
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

// C++14 (Clang 3.8.0)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef long long ll;

int n, m;
const int MAX_N = 100005;

int used[MAX_N];
int mark[MAX_N];
vector<int> child[100010];
void dfs(int start, int root) {
  // cout<<used[start]<<start<<endl;
  if (used[start] == 1)
    return;
  mark[start] = root;
  for (int i = 0; i < child[start].size(); i++)
    dfs(child[start][i], root);
}
int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 and m == 0)
      break;
    // map<int,vector<int> > child;
    // int used[MAX_N];
    // int mark[MAX_N];

    for (int i = 0; i < MAX_N; i++) {
      used[i] = 0;
      mark[i] = 0;
      child[i].clear();
    }

    for (int i = 0; i < n - 1; i++) {
      int par;
      scanf("%d", &par);
      par--;
      child[par].push_back(i + 1);
    }
    ll ans = 0;
    used[0] = 1;

    for (int i = 0; i < m; i++) {
      char a;
      int b;
      cin >> a >> b;
      b--;
      if (a == 'Q') {
        ans += (ll)(mark[b] + 1);
      } else {
        dfs(b, b);
        used[b] = 1;
        // for(int i=0;i<10;i++) cout<<mark[i]<<endl;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// C++14 (Clang 3.8.0)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> p;

int n, m;
const int MAX_N = 100005;

int used[MAX_N];
int mark[MAX_N];
map<int, vector<int>> child;

void dfs(int start, int root) {
  // cout<<used[start]<<start<<endl;
  if (used[start] == 1)
    return;
  mark[start] = root;
  for (int i = 0; i < child[start].size(); i++)
    dfs(child[start][i], root);
}
int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 and m == 0)
      break;
    // map<int,vector<int> > child;
    // int used[MAX_N];
    // int mark[MAX_N];

    for (int i = 0; i < MAX_N; i++) {
      used[i] = 0;
      mark[i] = 0;
      child[i].clear();
    }

    for (int i = 0; i < n - 1; i++) {
      int par;
      scanf("%d", &par);
      par--;
      child[par].push_back(i + 1);
    }
    int ans = 0;
    used[0] = 1;

    for (int i = 0; i < m; i++) {
      char a;
      int b;
      cin >> a >> b;
      b--;
      if (a == 'Q') {
        ans += (mark[b] + 1);
      } else {
        dfs(b, b);
        used[b] = 1;
        // for(int i=0;i<10;i++) cout<<mark[i]<<endl;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) repi(i, 0, a)
#define pb push_back
const int inf = 1e9;
int N, Q;
vector<int> ch[100010];
int marked[100010];

void dfs(int v, int a) {
  if (marked[v] == v or marked[v] == a)
    return;
  marked[v] = a;
  rep(i, ch[v].size()) dfs(ch[v][i], a);
}

int solve() {
  memset(marked, 0, sizeof(marked));
  int ans = 0;
  while (Q--) {
    char c;
    int v;
    cin >> c >> v;
    v--;
    if (c == 'Q')
      ans += marked[v] + 1;
    else
      dfs(v, v);
  }
  return ans;
}

bool input() {
  cin >> N >> Q;
  if (!N and !Q)
    return 0;
  rep(i, N) ch[i].clear();
  rep(i, N - 1) {
    int a;
    cin >> a;
    ch[a - 1].pb(i + 1);
  }
  return 1;
}
signed main() {
  while (input())
    cout << solve() << endl;
  return 0;
}

Error: TLE

Fixed submission:

// C++14 (Clang 3.8.0)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> p;

int n, m;
const int MAX_N = 100005;

int used[MAX_N];
int mark[MAX_N];
map<int, vector<int>> child;

void dfs(int start, int root) {
  // cout<<used[start]<<start<<endl;
  if (used[start] == 1)
    return;
  mark[start] = root;
  for (int i = 0; i < child[start].size(); i++)
    dfs(child[start][i], root);
}
int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 and m == 0)
      break;
    // map<int,vector<int> > child;
    // int used[MAX_N];
    // int mark[MAX_N];

    for (int i = 0; i < MAX_N; i++) {
      used[i] = 0;
      mark[i] = 0;
      child[i].clear();
    }

    for (int i = 0; i < n - 1; i++) {
      int par;
      scanf("%d", &par);
      par--;
      child[par].push_back(i + 1);
    }
    int ans = 0;
    used[0] = 1;

    for (int i = 0; i < m; i++) {
      char a;
      int b;
      cin >> a >> b;
      b--;
      if (a == 'Q') {
        ans += (mark[b] + 1);
      } else {
        dfs(b, b);
        used[b] = 1;
        // for(int i=0;i<10;i++) cout<<mark[i]<<endl;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <queue>
#define MAX 100001

using namespace std;

int mark[MAX], t[MAX];

int query(int v) {
  if (mark[v])
    return v;
  else {
    int a = query(t[v]);
    t[v] = a;
    return a;
  }
}

int main() {
  int n, q;
  while (scanf("%d %d", &n, &q), n) {
    long long int ans = 0;
    deque<pair<char, int>> qs;

    for (int i = 1; i <= n; i++)
      mark[i] = 0;
    mark[1] = 1;

    for (int i = 2; i <= n; i++) {
      int p;
      scanf("%d", &p);
      t[i] = p;
    }

    for (int i = 0; i < q; i++) {
      char order;
      int v;
      scanf(" %c %d", &order, &v);
      if (order == 'M')
        mark[v]++;
      qs.push_front(make_pair(order, v));
    }

    for (int i = 0; i < qs.size(); i++)
      if (qs[i].first == 'Q')
        ans += query(qs[i].second);
      else
        mark[qs[i].second]--;

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

Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
#define MAX 100001

using namespace std;

int mark[MAX], t[MAX];

int query(int v) {
  if (mark[v])
    return v;
  else {
    int a = query(t[v]);
    t[v] = a;
    return a;
  }
}

int main() {
  int n, q;
  while (scanf("%d %d", &n, &q), n) {
    int ans = 0;
    deque<pair<char, int>> qs;

    for (int i = 1; i <= n; i++)
      mark[i] = 0;
    mark[1] = 1;

    for (int i = 2; i <= n; i++) {
      int p;
      scanf("%d", &p);
      t[i] = p;
    }

    for (int i = 0; i < q; i++) {
      char order;
      int v;
      scanf(" %c %d", &order, &v);
      if (order == 'M')
        mark[v] = 1;
      qs.push_front(make_pair(order, v));
    }

    // printf("asdf %d\\n",(int)qs.size());
    // for(int i=0; i<qs.size(); i++){
    //    printf("%c %d\\n",qs[i].first,qs[i].second);
    // }

    for (int i = 0; i < qs.size(); i++)
      if (qs[i].first == 'Q')
        ans += query(qs[i].second);
      else
        mark[qs[i].second] = 0;

    printf("%d\\n", ans);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#define MAX 100010
#define X first
#define Y second
using namespace std;

int n, q, z;
int m[MAX];
vector<int> c[MAX];
stack<pair<char, int>> Q;

int u[MAX];

void DFS(int x, int g) {
  u[x] = g;
  if (m[x])
    g = x;
  for (int i = 0; i < c[x].size(); i++) {
    DFS(c[x][i], g);
  }
}

int FIND(int x) {
  if (m[x])
    return x;
  int ret = FIND(u[x]);
  u[x] = ret;
  return ret;
}

int main() {

  while (cin >> n >> q && n) {

    for (int i = 0; i < n; i++) {
      c[i].clear();
      u[i] = 0, m[i] = 0;
    }

    for (int i = 1; i < n; i++) {
      cin >> z;
      z--;
      c[z].push_back(i);
    }

    m[0]++;
    for (int i = 0; i < q; i++) {
      pair<char, int> r;
      cin >> r.X >> r.Y;
      r.Y--;
      if (r.X == 'M')
        m[r.Y]++;
      Q.push(r);
    }

    DFS(0, 0);

    long long ans = 0;

    while (!Q.empty()) {
      if (Q.top().X == 'M') {
        m[Q.top().Y]--;
      } else {
        ans += FIND(Q.top().Y) + 1;
      }
      Q.pop();
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX 100010
#define INF 1 << 20
using namespace std;

vector<int> lis[MAX];

int u[MAX], r[MAX];

bool vis[MAX];

void DFS(int x, int len, int nod) {
  vis[x] = true;
  r[x] = len, u[x] = nod;
  for (int i = 0; i < lis[x].size(); i++) {
    if (!vis[lis[x][i]] && r[lis[x][i]] > len + 1) {
      DFS(lis[x][i], len + 1, nod);
    }
  }
  vis[x] = false;
}

int main() {
  int n, q, z;
  char w[2];

  while (cin >> n >> q && n) {

    for (int i = 1; i < n; i++) {
      scanf("%d", &z);
      z--;
      lis[i].push_back(z);
      lis[z].push_back(i);
      r[i] = INF;
    }
    long long ans = 0;

    DFS(0, 0, 0);
    while (q--) {
      scanf("%s%d", w, &z);
      z--;

      if (w[0] == 'M') {
        DFS(z, 0, z);
      } else {
        ans += u[z] + 1;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <stdio.h>
#include <utility>

#define dprintf(s, ...) printf("%s:%d" s, __func__, __LINE__, __VA_ARGS__)
using namespace std;

typedef pair<int, int> P;
typedef long long int ll;

// Marked Ancestor
// AOJ 2170

int N, Q;

int par[100001], marked[100001], mpar[100001];

void init() {

  fill(par, par + N + 1, -1);
  fill(marked, marked + N + 1, 0);
  fill(mpar, mpar + N + 1, 1);

  marked[1] = 1;
  par[1] = 1;
}

int ans(int v) {

  // if(marked[par[v]] == 1) return mpar[v] = par[v];
  // return mpar[v] = ans(par[v]);

  while (marked[v] == 0)
    v = par[v];

  return v;
}

int main() {

  while (1) {

    ll res = 0;

    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;

    init();

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

    for (int i = 0; i < Q; i++) {
      char op;
      int v;
      scanf("%s %d", &op, &v);

      if (op == 'Q')
        res += ans(v);
      if (op == 'M')
        marked[v] = 1;
    }
    printf("%lld\\n", res);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <stdio.h>
#include <utility>

#define dprintf(s, ...) printf("%s:%d" s, __func__, __LINE__, __VA_ARGS__)
using namespace std;

typedef pair<int, int> P;
typedef long long int ll;

// Marked Ancestor
// AOJ 2170

int N, Q;

int par[100001], marked[100001], mpar[100001];

void init() {

  fill(par, par + N + 1, -1);
  fill(marked, marked + N + 1, 0);
  fill(mpar, mpar + N + 1, 1);

  marked[1] = 1;
  par[1] = 0;
  mpar[1] = 0;
}

int ans(int v) {

  if (marked[par[v]] == 1)
    return par[v];
  return mpar[v] = ans(par[v]);
}

int main() {

  ios_base::sync_with_stdio(false);

  while (1) {

    int res = 0;

    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;

    init();

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

    for (int i = 0; i < Q; i++) {
      char op;
      int v;
      scanf("%s %d", &op, &v);

      if (op == 'Q')
        res += ans(v);
      if (op == 'M')
        marked[v] = 1;
    }
    printf("%d\\n", res);
  }

  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;
const int maxn = 1e5 + 5;
std::vector<int> v[maxn];
int p[maxn], r[maxn];

int find(int x) {
  int t = x;
  while (t != p[t]) {
    t = p[t];
  }
  return t;
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  p[1] = 1;
  while (cin >> n >> q && n) {
    LL ans = 0;
    for (int i = 2; i <= n; i++) {
      int x;
      cin >> x;
      p[i] = x;
    }
    while (q--) {
      char op[2];
      int x;
      cin >> op >> x;
      if (op[0] == 'M')
        p[x] = x;
      else
        ans += find(x);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;
const int maxn = 1e5 + 5;
std::vector<int> v[maxn];
int p[maxn], r[maxn];

int find(int x) { return p[x] == x ? x : find(p[x]); }

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  p[1] = 1;
  while (cin >> n >> q && n) {
    LL ans = 0;
    for (int i = 2; i <= n; i++) {
      int x;
      cin >> x;
      p[i] = x;
    }
    while (q--) {
      char op[2];
      int x;
      cin >> op >> x;
      if (op[0] == 'M')
        p[x] = x;
      else
        ans += find(x);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

const int MAX_N = 100001;

int N, Q;
long long int ans;
char Ope[MAX_N];
int Openum[MAX_N];
int par[MAX_N];
bool marked[MAX_N];

void init() {
  for (int i = 0; i <= N; i++) {
    par[i] = i;
  }
}

int search(int x) {
  // int a = par[x];
  if (marked[x])
    return x;
  else
    return search(par[x]);
}

// int find(int x) {
//   if (par[x] == x) return x;
//   else return par[x] = find(par[x]);
// }

// void unite(int x, int y) {
//   par[find(x)] = find(y);
// }

// bool same(int x, int y) {
//   return find(x) == find(y);
// }

void solve() {
  ans = 0;
  for (int i = 0; i < Q; i++) {
    char ope = Ope[i];
    int num = Openum[i];

    if (ope == 'M') {
      marked[num] = true;
    }

    else if (ope == 'Q') {
      ans += search(num);
    }
  }
  printf("%lld\\n", ans);
}

int main() {
  marked[1] = true;
  int num;
  while (1) {
    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    init();

    for (int i = 2; i <= N; i++) {
      scanf("%d", &num);
      // unite(num,i);
      par[i] = num;
      marked[i] = false;
    }
    for (int i = 0; i < Q; i++) {
      scanf("  %c", &Ope[i]);
      scanf("%d", &Openum[i]);
    }
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100000;

int N, Q;
char Ope[MAX_N];
int Openum[MAX_N], Marked[MAX_N];
vector<int> child[MAX_N];

void mark(int x, int num) {
  if (Marked[x] == x || Marked[x] == num)
    return;
  Marked[x] = num;
  for (int i = 0; i < child[x].size(); i++) {
    mark(child[x][i], num);
  }
}

void solve() {
  int ans = 0;
  for (int i = 0; i < MAX_N; i++)
    Marked[i] = 1;
  for (int i = 0; i < Q; i++) {
    char ope = Ope[i];
    int num = Openum[i];
    if (ope == 'M')
      mark(num, num);
    else if (ope == 'Q')
      ans += Marked[num];
  }
  printf("%d\\n", ans);
}

int main() {
  while (1) {
    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    for (int i = 0; i <= N; i++)
      child[i].clear();
    for (int i = 2; i <= N; i++) {
      int par;
      scanf("%d", &par);
      child[par].push_back(i);
    }
    for (int i = 0; i < Q; i++) {
      scanf("  %c", &Ope[i]);
      scanf("%d", &Openum[i]);
    }
    solve();
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long lli;

struct Tree {
  vector<int> par;
  vector<int> mark;
  Tree(int n) : par(n, -1), mark(n, 0) {}

  void unmark(int k) { mark[k]--; }

  int query(int k) {
    if (mark[k])
      return k;
    return par[k] = query(par[k]);
  }
};

int main() {
  for (int N, Q; cin >> N >> Q, N + Q > 0;) {
    Tree tree(N);
    tree.mark[0]++;
    for (int i = 0; i < N - 1; i++) {
      int p;
      cin >> p;
      p--;
      tree.par[i + 1] = p;
    }
    vector<int> type;
    vector<int> node;
    for (int i = 0; i < Q; i++) {
      char t;
      int n;
      cin >> t >> n;
      n--;
      type.push_back(t == 'Q');
      node.push_back(n);
      if (t == 'M')
        tree.mark[n]++;
    }
    reverse(type.begin(), type.end());
    reverse(node.begin(), node.end());
    lli res = 0;
    for (int i = 0; i < Q; i++) {
      if (type[i]) {
        res += tree.query(node[i]) + 1;
      } else {
        tree.unmark(node[i]);
      }
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long lli;

struct Tree {
  vector<int> par;
  vector<char> mark;
  Tree(int n) : par(n, -1), mark(n, false) {}

  void unmark(int k) { mark[k] = false; }

  int query(int k) {
    if (mark[k])
      return k;
    return par[k] = query(par[k]);
  }
};

int main() {
  for (int N, Q; cin >> N >> Q, N + Q > 0;) {
    Tree tree(N);
    tree.mark[0] = true;
    for (int i = 0; i < N - 1; i++) {
      int p;
      cin >> p;
      p--;
      tree.par[i + 1] = p;
    }
    vector<int> type;
    vector<int> node;
    for (int i = 0; i < Q; i++) {
      char t;
      int n;
      cin >> t >> n;
      n--;
      type.push_back(t == 'Q');
      node.push_back(n);
      if (t == 'M')
        tree.mark[n] = true;
    }
    reverse(type.begin(), type.end());
    reverse(node.begin(), node.end());
    lli res = 0;
    for (int i = 0; i < Q; i++) {
      if (type[i]) {
        res += tree.query(node[i]) + 1;
      } else {
        tree.unmark(node[i]);
      }
    }
    cout << res << endl;
  }
}