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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 100000;
const int MAX_Q = 100000;
int par[MAX_N + 1];
bool vis[MAX_N + 1];
int N, Q;

int main() {
  while (scanf("%d %d", &N, &Q) && N > 0) {
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    par[1] = 1;
    for (int i = 2; i <= N; i++)
      scanf("%d ", &par[i]);
    long long cnt = 0;
    for (int i = 1; i <= Q; i++) {
      char ch;
      int num;
      scanf("%c ", &ch);
      scanf("%d ", &num);
      if (ch == 'M') {
        vis[num] = 1;
      } else if (ch == 'Q') {
        int cur = num;
        while (cur > 0) {
          if (vis[cur]) {
            cnt += cur;
            break;
          } else
            cur = par[cur];
        }
      }
    }
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 100000;
const int MAX_Q = 100000;
int par[MAX_N + 1];
bool vis[MAX_N + 1];
char ch[MAX_Q + 1];
int num[MAX_Q + 1];
int N, Q;

int main() {
  while (scanf("%d %d", &N, &Q) && N > 0) {
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    for (int i = 1; i <= N; i++)
      scanf("%d", &par[i]);
    getchar();
    for (int i = 1; i <= Q; i++) {
      scanf("%c ", &ch[i]);
      scanf("%d", &num[i]);
      getchar();
    }
    long long cnt = 0;
    for (int i = 1; i <= Q; i++) {
      if (ch[i] == 'M') {
        vis[num[i]] = 1;
      } else if (ch[i] == 'Q') {
        int cur = num[i];
        while (cur > 0) {
          if (vis[cur]) {
            cnt += cur;
            break;
          } else
            cur = par[cur];
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#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;
}


Error: 0

Fixed submission:

#include <cstdio>
#define M 100005
int n, q;
bool mark[M];
struct node {
  int tp, u;
} Q[M];
long long ans;
struct together {
  int fa, sum;
} A[M];
int Find(int x) {
  if (mark[A[x].sum])
    return A[x].fa;
  int f = A[x].fa;
  A[x].fa = Find(f);
  A[x].sum = A[f].sum;
  return A[x].fa;
}
int main() {
  int x, u;
  char op[5];
  while (scanf("%d %d", &n, &q), n || q) {
    A[1].fa = 1, A[1].sum = 1;
    mark[1] = 1;
    for (int i = 2; i <= n; i++) {
      scanf("%d", &x);
      A[i].fa = x;
      A[i].sum = 0;
      mark[i] = 0;
    }
    for (int i = 1; i <= q; i++) {
      scanf("%s %d", op, &Q[i].u);
      if (op[0] == 'M') {
        Q[i].tp = 1;
        mark[Q[i].u] = 1;
        A[Q[i].u].sum = Q[i].u;
      } else
        Q[i].tp = 2;
    }
    ans = 0;
    for (int i = q; i >= 1; i--) {
      // printf("id=%d\\n",i);
      if (Q[i].tp == 1) {
        mark[Q[i].u] = 0;
      } else {
        Find(Q[i].u);
        ans += A[Q[i].u].sum;
        // printf("sum=%d\\n",A[Q[i].u].sum);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
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;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
const int MAX_N = 100000 + 16;
int a[MAX_N];
int N, Q;
int main() {
  // freopen("1.txt","r",stdin);
  while (scanf("%d%d", &N, &Q) != EOF) {
    if (N == 0 && Q == 0)
      break;
    a[1] = 1;
    for (int i = 2; i <= N; i++) {
      scanf("%d", &a[i]);
    }
    long long ans = 0;
    while (N--) {
      char c[2];
      int x;
      scanf("%s %d", c, &x);
      if (c[0] == 'Q') {
        while (x != a[x]) {
          x = a[x];
        }
        ans += x;
      } else {
        a[x] = x;
      }
    }
    printf("%ld\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;

const int N = 100010;

int n, qu;
int fa[N], mark[N];

struct Node {
  int t, v;
} q[N];

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    ch = getchar(), o = -1;
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return o * x;
}

int find(int x, int ti) { return mark[x] < ti ? x : fa[x] = find(fa[x], ti); }

int main() {
  while (scanf("%d%d", &n, &qu) && n + qu) {
    ll ans = 0;
    int t = 0;
    for (int i = 2; i <= n; i++)
      fa[i] = gi(), mark[i] = qu + 1;
    for (int i = 1; i <= qu; i++) {
      char c;
      int x;
      scanf("%c", &c), x = gi();
      if (c == 'M')
        mark[x] = min(mark[x], i);
      else
        q[t].t = i, q[t++].v = x;
    }
    while (t--)
      ans += find(q[t].v, q[t].t);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
// Unite可能会用上
const int maxn = 100000 + 5;
int father[maxn];
bool idx[maxn];
int sum, flag;

int find(int a) {
  if (!flag && idx[a]) {
    sum += a;
    flag = 1;
  }

  if (a == father[a])
    return a; // 好像最后??是返回根->1
  return find(father[a]);
}

int main() {
  int N, Q;
  while (scanf("%d%d", &N, &Q) != EOF && N && Q) {
    memset(father, 0, sizeof(father));
    memset(idx, false, sizeof(idx));
    sum = 0;
    father[1] = 1;
    idx[1] = true;

    for (int i = 2; i <= N; ++i) {
      int a;
      scanf("%d", &a);
      father[i] = a;
    }
    for (int i = 1; i <= Q; ++i) {
      char ch;
      int a;
      getchar();
      scanf("%c%d", &ch, &a);
      if (ch == 'Q') {
        flag = 0;
        int temp = find(a); // 在向根?点靠近的同??找最近的被??的?点
                            //                cout<<"root == "<<temp<<endl;
      } else if (ch == 'M') {
        idx[a] = true;
      }
    }
    printf("%d\\n", sum);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

using namespace std;
const int N = 100010;

int n, q, par[N];
int find(int x) {
  if (par[x] == x)
    return x;
  return find(par[x]);
}
int main() {
  while (~scanf("%d%d", &n, &q)) {
    if (n == 0 && q == 0)
      break;
    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;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define oo 0x3f3f3f3f
using namespace std;
int n, q, pre[100100], a[100100];
int query(int x) {
  int num = 0;
  if (pre[x] != x)
    num = 1;
  else
    return num;
  while (!a[pre[x]]) {
    x = pre[x];
    num++;
  }
  return num;
}
int main() {
  while (scanf("%d%d", &n, &q), n + q) {
    int ans = 0;
    memset(a, 0, sizeof(a));
    a[1] = 1;
    for (int i = 1; i < n; i++) {
      int num;
      scanf("%d", &num);
      pre[i] = num;
    }
    while (q--) {
      int shu;
      char str[10];
      scanf("%s%d", str, &shu);
      if (str[0] == 'M')
        a[shu] = 1;
      else {
        ans += query(shu);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 4;
int par[maxn];
int x[1010], y[1010];
int find(int x) {
  if (par[x] == x)
    return x;
  else
    return find(par[x]);
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) != EOF) {
    if (n == 0 && q == 0)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &par[i]);
    }
    int ans = 0;
    while (q--) {
      char s[2];
      int x;
      scanf("%s", s);
      if (s[0] == 'Q') {
        scanf("%d", &x);
        ans += find(x);
      } else {
        scanf("%d", &x);
        par[x] = x;
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#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;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 100005;
int pre[maxn];
void init(int n) {
  for (int i = 1; i <= n; ++i) {
    pre[i] = i;
  }
}
int find(int x) {
  int r = x;
  while (r != pre[r]) {
    r = pre[r];
  }
  return r;
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; ++i) {
      int x;
      scanf("%d", &x);
      pre[i] = x;
    }
    int a, b;
    ll ans = 0;
    while (scanf("%d%d", &a, &b), a | b) {
      char s[10];
      int x;
      scanf("%s%d", s, &x);
      if (s[0] == 'Q') {
        ans += find(x);
      } else {
        pre[x] = x;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
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;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100000 + 10;
int par[MAXN], rank[MAXN], near[MAXN];
bool mark[MAXN];
int Find(int x) {
  if (x == par[x])
    return 1;
  else if (mark[par[x]])
    return par[x];
  else
    return Find(par[x]);
}
int main() {
  int n, q;
  int sum = 0;
  char ins[10];
  int v, x;

  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    par[i] = 1;
  }
  mark[1] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    par[i] = x;
  }
  for (int i = 0; i < q; i++) {
    scanf("%s%d", ins, &v);
    if (strcmp(ins, "0") == 0 && v == 0)
      break;
    if (strcmp(ins, "M") == 0) {
      mark[v] = 1;
    } else {
      sum += Find(v);
    }
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

//
//  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 <set>
#include <string>
#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;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

typedef long long lli;

const int N = 100000 + 10;

vector<int> V[N];
int scope[N][2];
int stree[N * 6];
bool marked[N];
int cnt, r;

void dfs(int u) {
  int i;
  scope[u][0] = cnt++;
  for (i = 0; i < V[u].size(); i++)
    dfs(V[u][i]);
  scope[u][1] = cnt++;
}

void mark(int index, int begin, int end, int left, int right, int x) {
  int mid;
  if (left <= begin && right >= end) {
    stree[index] = x;
    return;
  }
  mid = (begin + end) / 2;
  if (right <= mid)
    mark(index * 2, begin, mid, left, right, x);
  else if (left > mid)
    mark(index * 2 + 1, mid + 1, end, left, right, x);
  else {
    mark(index * 2, begin, mid, left, mid, x);
    mark(index * 2 + 1, mid + 1, end, mid + 1, right, x);
  }
}

int query(int index, int begin, int end, int x) {
  int mid;
  if (stree[index])
    r = stree[index];
  if (begin == end)
    return r;
  mid = (begin + end) / 2;
  if (x <= mid)
    return query(index * 2, begin, mid, x);
  else
    return query(index * 2 + 1, mid + 1, end, x);
}

int main() {
  lli res;
  int n, q, p, u, i;
  char cmd[5];
  while (scanf("%d %d", &n, &q), n > 0) {
    for (i = 1; i <= n; i++)
      marked[i] = false;
    marked[1] = true;
    memset(stree, 0, sizeof(stree));
    stree[1] = 1;
    for (i = 2; i <= n; i++) {
      scanf("%d", &p);
      V[p].push_back(i);
    }
    cnt = 0;
    dfs(1);
    res = 0;
    while (q--) {
      scanf("%s", cmd);
      if (cmd[0] == 'M') {
        scanf("%d", &u);
        if (marked[u])
          continue;
        mark(1, 0, n * 2 - 1, scope[u][0], scope[u][1], u);
      } else {
        scanf("%d", &u);
        r = 0;
        res += query(1, 0, n * 2 - 1, scope[u][0]);
      }
    }
    printf("%lld\\n", res);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#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) && (q + n) != 0) {
    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);
    long long sum = 0;
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] += 1;
    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: TLE

Fixed submission:

#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) {
  int par = tree[i];
  while (!ar[par]) {
    par = tree[par];
  }
  return par;
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) && (q + n) != 0) {
    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);
    long long sum = 0;
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] += 1;
    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;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#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:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 100005;
int tree[maxn];
bool ar[maxn];
typedef pair<char, int> P;
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] = 0;
      else
        sum += get(query[i].second);
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;

int n, q;
vector<int> root;
vector<vector<int>> edge;

void update(int p, int v) {
  if (p <= 0 || n < p)
    return;

  root[p] = v;
  for (int i = 0; i < edge[p].size(); i++) {
    if (root[edge[p][i]] == edge[p][i]) {
      continue;
    } else {
      update(edge[p][i], v);
    }
  }
}

int main() {
  while (cin >> n >> q, n || q) {
    root.clear();
    edge.clear();
    root.resize(n + 1);
    edge.resize(n + 1);
    for (int i = 2; i <= n; i++) {
      int p;
      cin >> p;
      root[i] = 1;
      edge[p].push_back(i);
    }
    root[1] = 1;
    ll sum = 0;
    rep(i, q) {
      char c;
      int v;
      cin >> c >> v;
      if (c == 'Q') {
        sum += root[v];
      } else if (c == 'M') {
        update(v, v);
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;

vector<int> root;
vector<vector<int>> edge;

void update(int p, int v) {
  root[p] = v;
  for (int i = 0; i < edge[p].size(); i++) {
    if (root[edge[p][i]] == edge[p][i]) {
      continue;
    } else {
      update(edge[p][i], v);
    }
  }
}

int main() {
  int n, q;
  while (cin >> n >> q, n || q) {
    root.resize(n + 1);
    edge.resize(n + 1);
    for (int i = 2; i <= n; i++) {
      int p;
      cin >> p;
      root[i] = 1;
      edge[p].push_back(i);
    }
    root[1] = 1;
    int sum = 0;
    rep(i, q) {
      char c;
      int v;
      cin >> c >> v;
      if (c == 'Q') {
        sum += root[v];
      } else if (c == 'M') {
        update(v, v);
      }
    }
    cout << sum << endl;
  }
  return 0;
}