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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

// range max, point assign
pii dat[1 << 19];
int N;
void init(int n) {
  N = 1;
  while (N < n)
    N <<= 1;
  REP(i, 2 * N - 1) dat[i] = pii(-1, -1);
}
void add(int l, int r, pii val, int a, int b, int k) {
  if (r <= a || b <= l)
    return;
  if (l <= a && b <= r) {
    dat[k] = max(dat[k], val);
    return;
  }
  add(l, r, val, a, (a + b) / 2, 2 * k + 1);
  add(l, r, val, (a + b) / 2, b, 2 * k + 2);
}
void add(int l, int r, pii val) {
  if (r <= l)
    return;
  add(l, r, val, 0, N, 0);
}
pii get(int x, int a, int b, int k) {
  pii ret = dat[k];
  if (a + 1 == b && a == x)
    return ret;
  int mid = (a + b) / 2;
  if (x < mid) {
    return max(ret, get(x, a, mid, 2 * k + 1));
  } else {
    return max(ret, get(x, mid, b, 2 * k + 2));
  }
}
pii get(int x) { return get(x, 0, N, 0); }

vi g[125252];
bool used[125252];
int lft[125252], rig[125252];
int height[125252];

void dfs(int p, int &iter, int h) {
  used[p] = true;
  height[p] = h;
  lft[p] = iter;
  iter++;
  REP(i, g[p].size()) {
    int to = g[p][i];
    if (used[to])
      continue;
    dfs(to, iter, h + 1);
  }
  used[p] = false;
  rig[p] = iter;
  iter++;
}
int main() {
  while (true) {
    int n, q;
    scanf("%d%d", &n, &q);
    if (n == 0 && q == 0)
      break;
    REP(i, n) {
      g[i].clear();
      used[i] = false;
      lft[i] = n + 1;
      rig[i] = -1;
    }
    REP(i, n - 1) {
      int src = i + 1;
      int to;
      scanf("%d", &to);
      --to;
      g[src].push_back(to);
      g[to].push_back(src);
    }
    init(2 * n);
    int iter = 0;
    dfs(0, iter, 0);
    add(lft[0], rig[0], pii(height[0], 0));
    ll ans = 0;
    while (q--) {
      char s[5];
      int x;
      scanf("%s%d", s, &x);
      --x;
      if (s[0] == 'M') {
        add(lft[x], rig[x], pii(height[x], x));
      } else {
        pii val = get(lft[x]);
        ans += val.second + 1;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

// range max, point assign
pii dat[1 << 18];
int N;
void init(int n) {
  N = 1;
  while (N < n)
    N <<= 1;
  REP(i, 2 * N - 1) dat[i] = pii(-1, -1);
}
void add(int l, int r, pii val, int a, int b, int k) {
  if (r <= a || b <= l)
    return;
  if (l <= a && b <= r) {
    dat[k] = max(dat[k], val);
    return;
  }
  add(l, r, val, a, (a + b) / 2, 2 * k + 1);
  add(l, r, val, (a + b) / 2, b, 2 * k + 2);
}
void add(int l, int r, pii val) {
  if (r <= l)
    return;
  add(l, r, val, 0, N, 0);
}
pii get(int x, int a, int b, int k) {
  pii ret = dat[k];
  if (a + 1 == b && a == x)
    return ret;
  int mid = (a + b) / 2;
  if (x < mid) {
    return max(ret, get(x, a, mid, 2 * k + 1));
  } else {
    return max(ret, get(x, mid, b, 2 * k + 2));
  }
}
pii get(int x) { return get(x, 0, N, 0); }

vi g[125252];
bool used[125252];
int lft[125252], rig[125252];
int height[125252];

void dfs(int p, int &iter, int h) {
  used[p] = true;
  height[p] = h;
  lft[p] = iter;
  iter++;
  REP(i, g[p].size()) {
    int to = g[p][i];
    if (used[to])
      continue;
    dfs(to, iter, h + 1);
  }
  used[p] = false;
  rig[p] = iter;
  iter++;
}
int main() {
  while (true) {
    int n, q;
    scanf("%d%d", &n, &q);
    if (n == 0 && q == 0)
      break;
    REP(i, n) {
      g[i].clear();
      used[i] = false;
      lft[i] = n + 1;
      rig[i] = -1;
    }
    REP(i, n - 1) {
      int src = i + 1;
      int to;
      scanf("%d", &to);
      --to;
      g[src].push_back(to);
      g[to].push_back(src);
    }
    init(n);
    int iter = 0;
    dfs(0, iter, 0);
    add(lft[0], rig[0], pii(height[0], 0));
    ll ans = 0;
    while (q--) {
      char s[5];
      int x;
      scanf("%s%d", s, &x);
      --x;
      if (s[0] == 'M') {
        add(lft[x], rig[x], pii(height[x], x));
      } else {
        pii val = get(lft[x]);
        ans += val.second + 1;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}