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

#include <cstdio>
#include <stack>
#include <vector>

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

using namespace std;

typedef long long ll;

template <class T> class segment_tree {
  static const int N_MAX = 1 << 17;
  int n;
  T dat[2 * N_MAX];

  void update_min(int l, int r, int a, int b, int u, T v) {
    if (l <= a && b <= r) {
      dat[u] = min(dat[u], v);
      return;
    }

    int c = (a + b + 1) / 2;
    if (l < c && a < r)
      update_min(l, r, a, c, 2 * u, v);
    if (l < b && c < r)
      update_min(l, r, c, b, 2 * u + 1, v);
  }

public:
  void build(int N, T val) {
    for (n = 1; n < N; n *= 2)
      ;
    rep(u, 2 * n) dat[u] = val;
  }

  void update_min(int l, int r, T v) { update_min(l, r, 0, n, 1, v); }

  T query(int u) {
    u += n;
    T res = dat[u];
    for (u /= 2; u >= 1; u /= 2)
      res = min(res, dat[u]);
    return res;
  }
};

int main() {
  for (int n, q; scanf("%d%d", &n, &q), n;) {
    static vector<int> T[100000]; // tree
    rep(u, n) T[u].clear();
    rep(i, n - 1) {
      int u, v = i + 1;
      scanf("%d", &u);
      u--;
      T[u].push_back(v);
    }

    // euler tour technique
    static int start[100000], end[100000];
    int idx = 0;
    stack<pair<int, int>> D;
    D.push(make_pair(0, 0));
    while (!D.empty()) {
      int u = D.top().first, type = D.top().second;
      D.pop();
      if (type == 0) { // in
        start[u] = idx++;
        D.push(make_pair(u, 1));
        rep(i, T[u].size()) D.push(make_pair(T[u][i], 0));
      } else { // out
        end[u] = idx;
      }
    }

    static int f[100000]; // f : start[u] -> u
    rep(u, n) f[start[u]] = u;

    static segment_tree<int> S;
    S.build(n, 0);

    ll ans = 0;
    while (q--) {
      char c;
      int u;
      scanf(" %c%d", &c, &u);
      u--;
      if (c == 'Q') {
        ans += f[-S.query(start[u])] + 1;
      } else {
        S.update_min(start[u], end[u], -start[u]);
      }
    }
    printf("%lld\\n", ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <stack>
#include <vector>

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

using namespace std;

typedef long long ll;

template <class T> class segment_tree_RMQ {
  static const int N_MAX = 100000;
  int n;
  T dat[2 * N_MAX];

  void update_min(int l, int r, int a, int b, int u, T v) {
    if (l <= a && b <= r) {
      dat[u] = min(dat[u], v);
      return;
    }

    int c = (a + b + 1) / 2;
    if (l < c && a < r)
      update_min(l, r, a, c, 2 * u, v);
    if (l < b && c < r)
      update_min(l, r, c, b, 2 * u + 1, v);
  }

public:
  void build(int N, T val) {
    for (n = 1; n < N; n *= 2)
      ;
    rep(u, 2 * n) dat[u] = val;
  }

  void update_min(int l, int r, T v) { update_min(l, r, 0, n, 1, v); }

  T query(int u) {
    u += n;
    T res = dat[u];
    for (u /= 2; u >= 1; u /= 2)
      res = min(res, dat[u]);
    return res;
  }
};

int main() {
  for (int n, q; scanf("%d%d", &n, &q), n;) {
    static vector<int> T[100000]; // tree
    rep(u, n) T[u].clear();
    rep(i, n - 1) {
      int u, v = i + 1;
      scanf("%d", &u);
      u--;
      T[u].push_back(v);
    }

    // euler tour technique ( 再帰でやったらスタックあふれた... )
    static int start[100000], end[100000];
    int idx = 0;
    stack<pair<int, int>> D;
    D.push(make_pair(0, 0));
    while (!D.empty()) {
      int u = D.top().first, type = D.top().second;
      D.pop();
      if (type == 0) { // in
        start[u] = idx++;
        D.push(make_pair(u, 1));
        rep(i, T[u].size()) D.push(make_pair(T[u][i], 0));
      } else { // out
        end[u] = idx;
      }
    }

    static segment_tree_RMQ<int> S;
    S.build(n, 0);

    ll ans = 0;
    while (q--) {
      char c;
      int u;
      scanf(" %c%d", &c, &u);
      u--;
      if (c == 'Q') {
        ans += -S.query(start[u]) + 1;
      } else {
        S.update_min(start[u], end[u], -u);
      }
    }
    printf("%lld\\n", ans);
  }

  return 0;
}