alexjercan/bugnet huggingface dataset:
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> G[100000], euler;
int dbl[100000][18], ff[100000], ss[100000], bit[200001], n, q;
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) {
  while (i <= 2 * n) {
    bit[i] += x;
    i += i & -i;
  }
}
void dfs(int v) {
  ff[v] = euler.size();
  euler.push_back(v);
  rep(i, G[v].size()) dfs(G[v][i]);
  ss[v] = euler.size();
  euler.push_back(v);
}
int main() {
  while (true) {
    cin >> n >> q;
    if (n == 0)
      break;
    euler.clear();
    rep(i, n) G[i].clear();
    rep(i, n) rep(j, 18) dbl[i][j] = -1;
    rep(i, 2 * n + 1) bit[i] = 0;
    rep(i, n - 1) {
      int p;
      cin >> p;
      G[p - 1].push_back(i + 1);
      dbl[i + 1][0] = p - 1;
    }
    rep(i, 17) {
      rep(j, n) {
        if (dbl[j][i] == -1)
          dbl[j][i + 1] = -1;
        else
          dbl[j][i + 1] = dbl[dbl[j][i]][i];
      }
    }
    dfs(0);
    add(ff[0] + 1, 1);
    add(ss[0] + 1, -1);
    long long ans = 0;
    rep(i, q) {
      char c;
      int v;
      cin >> c >> v;
      v--;
      if (c == 'M') {
        add(ff[v] + 1, 1);
        add(ss[v] + 1, -1);
      } else {
        int x = sum(ff[v] + 1);
        for (int j = 17; j >= 0; j--) {
          if (dbl[v][j] != -1 && sum(ff[dbl[v][j]] + 1) >= x)
            v = dbl[v][j];
        }
        ans += (v + 1);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> G[200000], euler;
int dbl[200000][30], ff[200000], ss[200000], bit[400001], n, q;
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) {
  while (i <= 2 * n) {
    bit[i] += x;
    i += i & -i;
  }
}
void dfs(int v) {
  ff[v] = euler.size();
  euler.push_back(v);
  rep(i, G[v].size()) dfs(G[v][i]);
  ss[v] = euler.size();
  euler.push_back(v);
}
int main() {
  while (true) {
    cin >> n >> q;
    if (n == 0)
      break;
    euler.clear();
    rep(i, n) G[i].clear();
    rep(i, n) rep(j, 18) dbl[i][j] = -1;
    rep(i, 2 * n + 1) bit[i] = 0;
    rep(i, n - 1) {
      int p;
      cin >> p;
      G[p - 1].push_back(i + 1);
      dbl[i + 1][0] = p - 1;
    }
    rep(i, 17) {
      rep(j, n) {
        if (dbl[j][i] == -1)
          dbl[j][i + 1] = -1;
        else
          dbl[j][i + 1] = dbl[dbl[j][i]][i];
      }
    }
    dfs(0);
    add(ff[0] + 1, 1);
    add(ss[0] + 1, -1);
    int ans = 0;
    rep(i, q) {
      char c;
      int v;
      cin >> c >> v;
      v--;
      assert(v != 0);
      if (c == 'M') {
        add(ff[v] + 1, 1);
        add(ss[v] + 1, -1);
      } else {
        v = dbl[v][0];
        int x = sum(ff[v] + 1);
        for (int j = 17; j >= 0; j--) {
          if (dbl[v][j] != -1 && sum(ff[dbl[v][j]] + 1) >= x)
            v = dbl[v][j];
        }
        ans += (v + 1);
      }
    }
    cout << ans << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define ll long long
int q, n, v;
char c;
int p[100005];
int main() {
  while ((~scanf("%d %d", &n, &q)) && q && n) {
    for (int i = 2; i <= n; ++i)
      scanf("%d", &p[i]);
    p[1] = 1;
    ll ans = 0;
    for (int i = 0; i < q; ++i) {
      scanf("\\n%c %d", &c, &v);
      if (c == 'M')
        p[v] = v;
      else {
        while (p[v] != v)
          v = p[v];
        ans += v;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define ll long long
int q, n, v;
char c;
int p[100005];
// bool m[100005];
int main() {
  while ((~scanf("%d %d", &n, &q)) && q && n) {
    for (int i = 1; i <= n; ++i)
      scanf("%d", &p[i]);
    // memset(m, 0, sizeof m);
    // m[1] = true;
    ll ans = 0;
    for (int i = 0; i < q; ++i) {
      scanf("\\n%c %d", &c, &v);
      if (c == 'M')
        p[v] = v;
      else {
        while (p[v] != v)
          v = p[v];
        ans += v;
        // printf("%d**\\n", v);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define int long long
using namespace std;
static const int MAX_N = 100000;
static const int inf = 1ll << 60;
typedef pair<int, int> pii;

int N, M;
int res;
int ans;
vector<int> V[MAX_N + 5];
bool Mark[MAX_N + 5];

void dfs(int n) {
  if (res != inf)
    return;
  if (Mark[n] == true)
    res = n;
  else {
    for (int i = 0; i < V[n].size(); ++i) {
      dfs(V[n][i]);
    }
  }
}

signed main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i <= N; ++i)
      V[i].clear();
    memset(Mark, 0, sizeof(Mark));
    Mark[1] = true;
    for (int i = 2; i <= N; ++i) {
      int v;
      cin >> v;
      V[i].PB(v);
    }
    ans = 0;
    for (int i = 0; i < M; ++i) {
      char que;
      int k;
      cin >> que;
      cin >> k;
      if (que == 'M')
        Mark[k] = true;
      else {
        res = inf;
        dfs(k);
        if (res != inf)
          ans += res;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define int long long
using namespace std;
static const int MAX_N = 100000;
static const int inf = 1ll << 60;
typedef pair<int, int> pii;

int N, M;
int res;
int ans;
vector<int> V[MAX_N];
bool Mark[MAX_N];

void dfs(int n) {
  if (res != inf)
    return;
  if (Mark[n] == true)
    res = n;
  else {
    for (int i = 0; i < V[n].size(); ++i) {
      dfs(V[n][i]);
    }
  }
}

signed main() {
  cin >> N >> M;
  Mark[1] = true;
  for (int i = 2; i <= N; ++i) {
    int v;
    cin >> v;
    V[i].PB(v);
  }
  ans = 0;
  for (int i = 0; i < M; ++i) {
    char que;
    int k;
    cin >> que;
    cin >> k;
    if (que == 'M')
      Mark[k] = true;
    else {
      res = inf;
      dfs(k);
      if (res != inf)
        ans += res;
    }
  }
  cout << ans << endl;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, q, root[105000];
long long res;
int marked[105000];
pair<char, int> query[105000];

int r(int x) {
  if (marked[x])
    return x;
  return root[x] = r(root[x]);
}

int main() {
  while (true) {
    scanf("%d%d", &n, &q);
    //	cin >> n >> q;
    if (n == 0 && q == 0)
      return 0;
    res = 0;
    fill(marked, marked + n + 2, 0);
    marked[0] = true;
    for (int i = 1; i < n; i++) {
      cin >> root[i];
      root[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> query[i].first >> query[i].second;
      query[i].second--;
      marked[query[i].second] += (query[i].first == 'M');
    }
    for (int i = q; i-- > 0;) {
      if (query[i].first == 'M') {
        marked[query[i].second]--;
      } else {
        res += r(query[i].second) + 1;
      }
    }
    printf("%lld\\n", res);
    //	cout << res << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, q, root[105000];
long long res;
int marked[105000];
pair<char, int> query[105000];

int r(int x) {
  if (marked[x])
    return x;
  return root[x] = r(root[x]);
}

int main() {
  while (true) {
    scanf("%d%d", &n, &q);
    //	cin >> n >> q;
    if (n == 0 && q == 0)
      return 0;
    res = 0;
    fill(marked, marked + n + 2, 0);
    marked[1] = true;
    for (int i = 1; i < n; i++) {
      cin >> root[i];
      root[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> query[i].first >> query[i].second;
      query[i].second--;
      marked[query[i].second] += (query[i].first == 'M');
    }
    for (int i = q; i-- > 0;) {
      if (query[i].first == 'M') {
        marked[query[i].second]--;
      } else {
        res += r(query[i].second) + 1;
      }
    }
    printf("%lld\\n", res);
    //	cout << res << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

/* vim: set st=2 sts=2 : */
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef pair<char, int> P;

int N, Q;
int mark[100001];
int tree[100001];
P query[100001];

int get(int i) {
  if (mark[i])
    return i;
  return tree[i] = get(tree[i]);
}

int main() {
  while (scanf("%d%d", &N, &Q), N | Q) {
    memset(mark, 0, sizeof(mark));
    mark[0] = 1;
    for (int i = 1; i < N; i++) {
      scanf("%d", tree + i);
      tree[i]--;
    }
    for (int i = 0; i < Q; i++) {
      scanf(" %c%d", &query[i].first, &query[i].second);
      query[i].second--;
    }
    for (int i = 0; i < Q; i++) {
      if (query[i].first == 'M') {
        mark[query[i].second] += 1;
      }
    }

    long long c = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (query[i].first == 'M') {
        mark[query[i].second] -= 1;
      } else {
        c += get(query[i].second) + 1;
      }
    }
    printf("%lld\\n", c);
  }
}

Error: 0

Fixed submission:

/* vim: set st=2 sts=2 : */
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef pair<char, int> P;

int N, Q;
bool mark[100000];
int tree[100000];
P query[100000];

int get(int i) {
  if (mark[i])
    return i;
  return tree[i] = get(tree[i]);
}

int main() {
  while (scanf("%d%d", &N, &Q), N | Q) {
    memset(mark, 0, sizeof(mark));
    mark[0] = 1;
    tree[0] = 0;
    for (int i = 1; i < N; i++) {
      scanf("%d", tree + i);
      tree[i]--;
    }
    for (int i = 0; i < Q; i++) {
      scanf(" %c%d", &query[i].first, &query[i].second);
      query[i].second--;
    }
    for (int i = 0; i < Q; i++) {
      if (query[i].first == 'M') {
        mark[query[i].second] += 1;
      }
    }

    long long c = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (query[i].first == 'M') {
        mark[query[i].second] -= 1;
      } else {
        c += get(query[i].second) + 1;
      }
    }
    printf("%lld\\n", c);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef pair<char, int> P;
const int maxn = 100005;
int tree[maxn];
int ar[maxn];
P query[maxn];
int get(int i) {
  if (ar[i])
    return i;
  return tree[i] = get(tree[i]);
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q), n | q) {
    memset(ar, 0, sizeof(ar));
    ar[1] = true;
    for (int i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (int i = 0; i < q; i++)
      scanf(" %c%d", &query[i].first, &query[i].second);
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] += 1;
    long long sum = 0;
    for (int i = q - 1; i >= 0; i--)
      if (query[i].first == 'M')
        ar[query[i].second] -= 1;
      else
        sum += get(query[i].second);
    printf("%lld\\n", sum);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef pair<char, int> P;
const int maxn = 100005;
int tree[maxn];
bool ar[maxn];
P query[maxn];
int get(int i) {
  if (ar[i])
    return i;
  return tree[i] = get(tree[i]);
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q), n | q) {
    memset(ar, 0, sizeof(ar));
    ar[1] = true;
    for (int i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (int i = 0; i < q; i++)
      scanf(" %c%d", &query[i].first, &query[i].second);
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] = true;
    long long sum = 0;
    for (int i = q - 1; i >= 0; i--)
      if (query[i].first == 'M')
        ar[query[i].second] = false;
      else
        sum += get(query[i].second);
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define Q 100005
using namespace std;
typedef long long ll;
int n, q, u, t[Q], p[Q];
bool marked[Q];
char c[Q];
ll ans;

int find(int x) {
  if (marked[x])
    return x;
  return p[x] = find(p[x]);
}

int main() {
  while (1) {
    cin >> n >> q;
    if (!n && !q)
      break;
    for (int i = 1; i <= n; i++)
      marked[i] = false;
    for (int i = 2; i <= n; i++)
      cin >> u, p[i] = u;
    marked[1] = true;
    for (int i = 0; i < q; i++) {
      cin >> c[i] >> t[i];
      if (c[i] == 'M') {
        if (marked[t[i]])
          c[i] = 'E';
        marked[t[i]] = true;
      }
    }
    ans = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (c[i] == 'E')
        continue;
      if (c[i] == 'M' && t[i] != 1)
        marked[t[i]] = false;
      else
        ans += find(t[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#define Q 100005
using namespace std;
typedef long long ll;
int n, q, u, t[Q], p[Q];
bool marked[Q];
char c[Q];
ll ans;
set<int> s;

int find(int x) {
  if (marked[p[x]])
    return p[x];
  return p[x] = find(p[x]);
}

int main() {
  while (1) {
    cin >> n >> q;
    if (!n && !q)
      break;
    for (int i = 1; i <= n; i++)
      marked[i] = false;
    for (int i = 2; i <= n; i++)
      cin >> u, p[i] = u;
    for (int i = 0; i < q; i++) {
      cin >> c[i] >> t[i];
      if (c[i] == 'M') {
        marked[t[i]] = true;
        if (s.find(t[i]) == s.end())
          s.insert(t[i]);
        else
          c[i] = 'E';
      }
    }
    marked[1] = true;
    ans = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (c[i] == 'E')
        continue;
      if (c[i] == 'M' && t[i] != 1)
        marked[t[i]] = false;
      else
        ans += find(t[i]);
    }
    cout << ans << endl;
    s.clear();
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, q;
vector<int> v[MAX];
int marked[MAX] = {};

void dfs(int now, int a) {
  if (marked[now] == a || marked[now] == now)
    return;
  marked[now] = a;
  for (int i = 0; i < v[now].size(); i++) {
    dfs(v[now][i], a);
  }
}

int main() {
  char c;
  int a;

  while (1) {
    cin >> n >> q;
    if (n + q == 0)
      break;
    for (int i = 0; i < MAX; i++) {
      marked[i] = 0;
      v[i].clear();
    }
    for (int i = 1; i < n; i++) {
      cin >> a;
      v[a - 1].push_back(i);
    }
    long long ans = 0;
    for (int i = 0; i < q; i++) {
      cin >> c >> a;
      if (c == 'Q') {
        ans += marked[a - 1] + 1;
      } else {
        dfs(a - 1, a - 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

class DisjointSet {
public:
  vector<int> rank, p;
  vector<bool> mark;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    mark.resize(size, false);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

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

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }

  void MarkNode(int x) { mark[x] = true; }

  int findmark(int x) {
    if (mark[x])
      return x + 1;
    return findmark(p[x]);
  }
};

int main() {
  int n, q, a, ans = 0;
  char Q;

  while (1) {
    cin >> n >> q;
    if (n + q == 0)
      break;

    DisjointSet ds = DisjointSet(n);

    ds.MarkNode(0);

    for (int i = 1; i < n; i++) {
      cin >> a;
      ds.link(i, a - 1);
    }
    for (int i = 0; i < q; i++) {
      cin >> Q >> a;
      if (Q == 'Q')
        ans += ds.findmark(a - 1);
      else {
        ds.MarkNode(a - 1);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
using ll = long long;
const int maxn = 100004;
const int INF = 0x3f3f3f3f;

int pre[maxn];
bool vis[maxn];

int find(int x) {
  while (!vis[x] && pre[x] != x) {
    x = pre[x];
  }
  return x;
}

int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m) && (n || m)) {
    vis[1] = true;
    pre[1] = 1;
    for (int i = 2; i <= n; ++i)
      scanf("%d", &pre[i]);
    ll ans = 0;
    for (int i = 0; i < m; ++i) {
      getchar();
      char c = getchar();
      int a;
      scanf("%d", &a);
      switch (c) {
      case 'M':
        vis[a] = true;
        break;
      case 'Q':
        ans += find(a);
      }
    }
    printf("%lld\\n", ans);
    memset(vis, 0, sizeof(vis));
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

char s[10];

int u, n, q, mark[100005], query[100005], id[100005], a[100005], tot,
    fa[100005];

int fin(int cur, int id) {
  return mark[cur] < id ? cur : (fa[cur] = fin(fa[cur], id));
}

int main() {
  while (~scanf("%d %d", &n, &q) && n) {
    memset(mark, 0x3f, sizeof(mark));
    for (int i = 2; i <= n; i++)
      scanf("%d", &fa[i]);
    fa[1] = 1;
    mark[1] = 0;
    tot = 0;

    for (int i = 1; i <= q; i++) {
      scanf("%s%d", s + 1, &u);
      if (s[1] == 'M')
        mark[u] = i;
      else
        query[++tot] = u, id[tot] = i;
    }
    long long ans = 0;
    for (int i = tot; i >= 1; i--) {
      ans += fin(query[i], id[i]);
    }
    printf("%lld\\n", ans);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
const int maxn = 5e5 + 5;
int n, m;
int far[maxn];
int Rank[maxn];
int find(int x) {
  if (far[x] == x)
    return x;
  else
    return find(far[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y])
    far[x] = y;
  else {
    far[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}
bool check(int x, int y) { return find(x) == find(y); }
void init(int n) {
  for (int i = 0; i <= n; i++) {
    far[i] = i;
    // Rank[i] = 0;
  }
}

int main() {
  while (~scanf("%d %d", &n, &m) && (m || n)) {
    init(n);
    int a;
    long long ans = 0;
    for (int i = 2; i <= n; i++) {
      scanf("%d", &far[i]);
    }
    char c[3];
    for (int i = 0; i < m; i++) {
      scanf("%s %d", c, &a);
      if (c[0] == 'M')
        far[a] = a;
      else {
        ans += find(a);
      }
    }
    printf("%lld\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
const int maxn = 5e5 + 5;
int n, m;
int far[maxn];
int Rank[maxn];
int Mark[maxn];
int find(int x) {
  if (far[x] == x || Mark[x])
    return x;
  else
    return find(far[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y])
    far[x] = y;
  else {
    far[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}
bool check(int x, int y) { return find(x) == find(y); }
void init(int n) {
  for (int i = 1; i <= n; i++) {
    far[i] = i;
    Rank[i] = 0;
    Mark[i] = 0;
  }
}

int main() {
  while (scanf("%d %d", &n, &m) && (m || n)) {
    init(n);
    int a;
    int ans = 0;
    for (int i = 1; i < n; i++) {
      scanf("%d", &a);
      far[i] = a;
      if (a == i)
        Mark[i] = 1;
    }
    char c[3];
    for (int i = 0; i < m; i++) {
      scanf("%s %d", c, &a);
      if (c[0] == 'M')
        Mark[a] = 1;
      else {
        ans += find(a);
      }
    }
    printf("%d\\n", ans);
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>

#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;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define INF 0x7f7f7f7f
int n, q, askl;
long long father[110001] = {}, ask[110001][2], Time[110001];
long long getF(long long t) {
  if (Time[t] < ask[askl][1])
    return t;
  return father[t] = getF(father[t]);
}
int main() {
  char c;
  int x, i, t;
  long long ans;
  while (cin >> n >> q && n && q) {
    askl = ans = 0;
    t = 0;
    memset(Time, INF, sizeof(Time));
    for (i = 2; i <= n; ++i)
      cin >> father[i];
    Time[1] = 0;
    while (q > 0) {
      getchar();
      cin >> c >> x;
      ++t;
      if (c == 'M')
        Time[x] = t;
      else {
        ++askl;
        ask[askl][0] = x;
        ask[askl][1] = t;
      }
      --q;
    }
    while (askl > 0) {
      ans += getF(ask[askl][0]);
      --askl;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Fi first
#define Se second
#define bug(x) cerr << #x << '=' << x << ' '
#define debug(x) cerr << #x << '=' << x << endl
#define FOR(a, b, c) for (int a = (b), a##_end = (c); a <= a##_end; ++a)
#define ROF(a, b, c) for (int a = (b), a##_end = (c); a >= a##_end; --a)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f, N = 1e5 + 10;
template <class T> inline bool chkmin(T &A, T B) {
  return B < A ? A = B, 1 : 0;
}
template <class T> inline bool chkmax(T &A, T B) {
  return A < B ? A = B, 1 : 0;
}
int n, m, op[N], u[N], fa[N], vis[N];
int par[N];
int Find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = Find(par[x]);
}
int main() {
  char s[10];
  while (scanf("%d%d", &n, &m), n || m) {
    ll ans = 0;
    memset(vis, 0, sizeof(vis));
    memset(par, 0, sizeof(par));
    fa[1] = 1;
    FOR(i, 2, n) scanf("%d", &fa[i]);
    FOR(i, 1, m) {
      scanf("%s%d", s + 1, &u[i]);
      if (s[1] == 'M')
        op[i] = 1, par[u[i]] = u[i], ++vis[u[i]];
      else
        op[i] = 2;
    }
    FOR(i, 1, n) if (!par[i]) par[i] = fa[i];
    ROF(i, m, 1)
    if (op[i] == 1) {
      if (!(--vis[u[i]]))
        par[u[i]] = Find(fa[u[i]]);
    } else
      ans += Find(u[i]);
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  x = 0;
  char c = getchar();
  for (; c < 48; c = getchar())
    ;
  for (; c > 47; c = getchar())
    x = (x << 3) + (x << 1) + (c ^ 48);
}
#define M 200005
int op[M], fa[M], x[M], n, q;
bool mark[M];
void in() {
  int i;
  memset(mark, 0, sizeof(mark));
  mark[1] = 1;
  fa[1] = 1;
  for (i = 2; i <= n; i++)
    read(fa[i]);
  for (i = 1; i <= q; i++) {
    read(op[i]);
    read(x[i]);
    if (op[i] != 97)
      mark[x[i]] = 1;
  }
}
int getf(int x) {
  if (mark[x])
    return x;
  return fa[x] = getf(fa[x]);
}
void solve() {
  int fx, i;
  long long res = 0;
  for (i = q; i; i--) {
    //		printf("op[%d]=%d %d\\n",i,op[i],x[i]);
    if (op[i] == 97) {
      fx = getf(fa[x[i]]);
      res = res + fx;
    } else {
      mark[x[i]] = 0;
    }
  }
  printf("%lld\\n", res);
}

int main() {
  //	freopen("1.txt","r",stdin);
  for (;;) {
    read(n);
    read(q);
    if (n == 0 && q == 0)
      return 0;
    in();
    solve();
  }
  return 0;
}