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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int NN = 100 * 1000 + 1;
int parent[2][NN];
// int orig[NN];
vector<int> child[20][NN];
int level[NN];
int N, Q;
P nearest[NN];
// bool marked[NN];
void setLevel(int i, int n) {
  level[i] = n;
  for (int j = 0; j < child[0][i].size(); j++) {
    setLevel(child[0][i][j], n + 1);
  }
}
int setNearest(int p, int cur, P n) {
  if (nearest[p].first < n.first) {
    nearest[p] = n;
    for (int i = 0; i < child[cur][p].size(); i++) {
      setNearest(child[cur][p][i], cur, n);
    }
  }
}
void exec() {
  if (!scanf("%d%d", &N, &Q)) {
    exit(0);
  }
  if (N == 0 && Q == 0) {
    exit(0);
  }
  for (int i = 1; i <= N; i++) {
    child[0][i].clear();
    child[1][i].clear();
    parent[0][i] = 0;
    parent[1][i] = 0;
    level[i] = 0;
    nearest[i] = P(0, 0);
  }
  for (int i = 2; i <= N; i++) {
    scanf("%d", parent[0] + i);
    int p = parent[0][i];
    // orig[i]=p;
    // marked[i]=false;
    child[0][p].push_back(i);
  }
  // marked[1]=true;
  setLevel(1, 0);
  int step = 2;
  int pre = 1;
  int cur = 0;
  /*
  cout << "step 1" << endl;
  for(int i=2;i<=N;i++){
                  cout << i << ", " << parent[cur][i] << ": ";
                  for(int j=0;j<child[cur][i].size();j++){
                          cout << child[cur][i][j] << ", ";
                  }
                  cout << endl;
          }
//*/
  while (step < N) {
    swap(cur, pre);
    for (int i = 2; i <= N; i++) {
      if ((level[i] % step) == 0) {
        int p = parent[pre][i];
        parent[cur][i] = parent[pre][p];
        child[cur][i].clear();
        for (int j = 0; j < child[pre][i].size(); j++) {
          int ch = child[pre][i][j];
          for (int k = 0; k < child[pre][ch].size(); k++) {
            child[cur][i].push_back(child[pre][ch][k]);
          }
        }
      }
    }
    for (int i = 2; i <= N; i++) {
      if (level[i] % step) {
        child[cur][i].swap(child[pre][i]);
        parent[cur][i] = parent[pre][i];
      }
    }
    /*
    cout << "step " << step << endl;
    for(int i=2;i<=N;i++){
            cout << i << ", " << parent[cur][i] << ": ";
            for(int j=0;j<child[cur][i].size();j++){
                    cout << child[cur][i][j] << ", ";
            }
            cout << endl;
    }//*/
    step *= 2;
  }

  /*
  for(int i=2;i<=N;i++){
          cout << i << ", " << parent[cur][i] << ": ";
          for(int j=0;j<child[cur][i].size();j++){
                  cout << child[cur][i][j] << ", ";
          }
          cout << endl;
  }//*/

  nearest[1] = P(0, 1);
  char c[2];
  int v;
  LL sum = 0;
  for (int i = 0; i < Q; i++) {
    scanf("%1s %d", c, &v);
    // cout << "zzz " << endl;
    // int ov=v;
    if (c[0] == 'Q') {
      /*
      int vv=v;
      while(!marked[vv]){
              vv=orig[vv];
      }*/
      while (nearest[v].second == 0) {
        v = parent[cur][v];
      }
      // assert(vv==nearest[v].second);
      /*
      if(vv!=nearest[v].second)
      {
              cout << endl;
              cout << i << ", " << vv << ", " << nearest[v].second << ", " << ov
      << endl; while (!marked[ov]){ cout << ov << ", " << level[ov] << endl;
                      ov=orig[ov];
              }
              cout << ov << level[ov] << endl;
              return;
      }*/
      sum += nearest[v].second;
    } else {
      // marked[v]=true;
      P n = P(level[v], v);
      setNearest(v, cur, n);
    }
  }
  printf("%lld\\n", sum);
}
int main() {
  while (true)
    exec();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int NN = 100 * 1000 + 1;
int parent[2][NN];
vector<int> child[2][NN];
int level[NN];
int N, Q;
P nearest[NN];
void setLevel(int i, int n) {
  nearest[i] = P(0, 0);
  level[i] = n;
  for (int j = 0; j < child[0][i].size(); j++) {
    setLevel(child[0][i][j], n + 1);
  }
}
int setNearest(int p, int cur, P n) {
  if (nearest[p].first < n.first) {
    // cout << "nearest " << p << ", " << n.second << endl;
    nearest[p] = n;
    for (int i = 0; i < child[cur][p].size(); i++) {
      setNearest(child[cur][p][i], cur, n);
    }
  }
}
void exec() {
  scanf("%d%d", &N, &Q);
  if (N == 0 && Q == 0) {
    exit(0);
  }
  for (int i = 2; i <= N; i++) {
    scanf("%d", parent[0] + i);
    int p = parent[0][i];
    child[0][p].push_back(i);
  }
  setLevel(1, 0);
  int step = 2;
  int pre = 1;
  int cur = 0;
  while (step < N) {
    swap(cur, pre);
    for (int i = 2; i <= N; i++) {
      if (level[i] % step == 0) {
        int p = parent[pre][i];
        parent[cur][i] = parent[pre][p];
        child[cur][i].clear();
        for (int j = 0; j < child[pre][i].size(); j++) {
          for (int k = 0; k < child[pre][j].size(); k++) {
            child[cur][i].push_back(child[pre][j][k]);
          }
        }
      } else {
        child[cur][i].swap(child[pre][i]);
        parent[cur][i] = parent[pre][i];
      }
    }
    step *= 2;
  }

  /*
  for(int i=2;i<=N;i++){
          cout << i << ", " << level[i] << ": ";
          for(int j=0;j<child[cur][i].size();j++){
                  cout << j << ", ";
          }
          cout << endl;
  }*/

  nearest[1] = P(0, 1);
  char c[2];
  int v;
  int sum = 0;
  for (int i = 0; i < Q; i++) {
    scanf("%1s %d", c, &v);
    if (c[0] == 'Q') {
      while (nearest[v].second == 0) {
        v = parent[cur][v];
      }
      sum += nearest[v].second;
    } else {
      P n = P(level[v], v);
      setNearest(v, cur, n);
    }
  }
  printf("%d\\n", sum);
}
int main() {
  while (true)
    exec();
}