Problem p01288 (C++) — buggy submission, verdict: Runtime Error

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

#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));

#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);

#define rep(i, n) for (int i = 0; i < n; i++)

map<int, int> near;

int parent[100010];
int marked[100010];
int uf[100010];
int get(int x) { return marked[x] ? x : parent[x] = get(parent[x]); }
int main() {
  int N, Qq;
  while (cin >> N >> Qq && N) {
    memset(marked, 0, sizeof(marked));
    vector<pair<int, int>> Q;
    for (int i = 1; i < N; i++) {
      cin >> parent[i];
      parent[i]--;
    }
    marked[0] = true;
    rep(i, Qq) {
      char c;
      int v;
      cin >> c >> v;
      --v;
      if (c == 'M') {
        if (marked[v])
          continue;
        marked[v] = true;
      }
      Q.push_back({c == 'M', v});
    }
    reverse(Q.begin(), Q.end());
    long long ans = 0;
    for (auto q : Q) {
      if (q.first) {
        marked[q.second] = false;
      } else {
        ans += get(q.second) + 1;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));

#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);

#define rep(i, n) for (int i = 0; i < n; i++)

map<int, int> near;

vector<int> M[100010];
vector<int> Q[100010];
vector<int> ch[100010];
int chk[100010];
long long ans;

long long esp_org, esp_new;

vector<int> memo;
int dfs(int x) {
  stack<int> S;
  S.push(x);
  while (S.size()) {
    x = S.top();
    S.pop();
    if (chk[x] == 0) {
      memo.push_back(x);
      S.push(x);
      for (auto c : ch[x])
        S.push(c);
      chk[x] = true;
    } else {
      memo.push_back(x);
      chk[x] = false;
    }
  }
}
int dfs2(int x) {
  for (auto x : memo) {
    if (chk[x]) {
      for (int i : M[x])
        near.erase(i);
    } else {
      for (int i : Q[x]) {
        int a = (*(--near.lower_bound(i))).second;
        ans += a + 1;
      }
      for (int i : M[x])
        near[i] = x;
      chk[x] = true;
    }
  }
}
int main() {
  int N, Qq;
  while (cin >> N >> Qq && N) {
    ans = 0;
    memo.clear();
    for (int i = 0; i < 100010; i++)
      M[i].clear(), Q[i].clear(), ch[i].clear(), chk[i] = 0;
    rep(i, N - 1) {
      int x;
      cin >> x;
      ch[--x].push_back(i + 1);
    }
    M[0].push_back(-1);
    rep(i, Qq) {
      char c;
      int v;

      cin >> c >> v;
      --v;
      if (c == 'Q')
        Q[v].push_back(i);
      if (c == 'M')
        M[v].push_back(i);
    }
    dfs(0);
    // for( auto i : memo ) cout << i << endl;
    dfs2(0);
    cout << ans << endl;
  }
}