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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

int main() {
  int N;
  vector<int64_t> var(3);
  cin >> N >> var[0] >> var[1] >> var[2];

  vector<int> op;
  rep(i, N) {
    string s;
    cin >> s;

    if (s == "AB") {
      op.push_back(0);
    } else if (s == "BC") {
      op.push_back(1);
    } else {
      op.push_back(2);
    }
  }

  int64_t S = var[0] + var[1] + var[2];
  vector<int> result;
  if (S == 0) {
    cout << "No\\n";
    return 0;
  } else if (S == 1) {
    rep(i, N) {
      int j = op[i];
      if (var[j] == 0 && var[(j + 1) % 3] == 0) {
        cout << "No\\n";
        return 0;
      } else {
        result.push_back((var[j] < var[(j + 1) % 3] ? j : (j + 1) % 3));
        swap(var[j], var[(j + 1) % 3]);
      }
    }
  } else if (S == 2) {
    vector<vector<bool>> reach(N + 1, vector<bool>(27, false));

    rirep(i, N) rep(x, 3) rep(y, 3) rep(z, 3) {
      vector<bool>::reference curr = reach[i][x * 9 + y * 3 + z];
      if (i == N) {
        curr = (x + y + z == 2);
      } else {
        const int d[] = {1, -1, 0};
        int j = op[i];
        int dx = d[(3 - j) % 3];
        int dy = d[(4 - j) % 3];
        int dz = d[(5 - j) % 3];

        curr = ((x + dx >= 0 && y + dy >= 0 && z + dz >= 0 &&
                 reach[i + 1][(x + dx) * 9 + (y + dy) * 3 + z + dz]) ||
                (x - dx >= 0 && y - dy >= 0 && z - dz >= 0 &&
                 reach[i + 1][(x - dx) * 9 + (y - dy) * 3 + z - dz]));
      }
    }

    int x = var[0], y = var[1], z = var[2];
    rep(i, 3) rep(j, 3) rep(k, 3) {
      cerr << i << " " << j << " " << k << " : " << reach[0][x * 9 + y * 3 + z]
           << "\\n";
    }
    if (!reach[0][x * 9 + y * 3 + z]) {
      cout << "No\\n";
      return 0;
    }

    rep(i, N) {
      const int d[] = {1, -1, 0};
      int j = op[i];
      int dx = d[(3 - j) % 3];
      int dy = d[(4 - j) % 3];
      int dz = d[(5 - j) % 3];

      if (x + dx >= 0 && y + dy >= 0 && z + dz >= 0 &&
          reach[i + 1][(x + dx) * 9 + (y + dy) * 3 + z + dz]) {
        x += dx;
        y += dy;
        z += dz;
        result.push_back(j);
      } else {
        x -= dx;
        y -= dy;
        z -= dz;
        result.push_back((j + 1) % 3);
      }
    }

  } else {
    rep(i, N) {
      int j = op[i];
      if (var[j] == 0 && var[(j + 1) % 3] == 0) {
        cout << "No\\n";
        return 0;
      } else {
        if (var[j] < var[(j + 1) % 3]) {
          result.push_back(j);
          var[j]++;
          var[(j + 1) % 3]--;
        } else {
          result.push_back((j + 1) % 3);
          var[j]--;
          var[(j + 1) % 3]++;
        }
      }
    }
  }

  cout << "Yes\\n";
  rep(i, N) { cout << (char)("ABC"[result[i]]) << "\\n"; }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

int main() {
  int N;
  vector<int64_t> var(3);
  cin >> N >> var[0] >> var[1] >> var[2];

  vector<int> op;
  rep(i, N) {
    string s;
    cin >> s;

    if (s == "AB") {
      op.push_back(0);
    } else if (s == "BC") {
      op.push_back(1);
    } else {
      op.push_back(2);
    }
  }

  int64_t S = var[0] + var[1] + var[2];
  vector<int> result;
  if (S == 0) {
    cout << "No\\n";
    return 0;
  } else if (S == 1) {
    rep(i, N) {
      int j = op[i];
      if (var[j] == 0 && var[(j + 1) % 3] == 0) {
        cout << "No\\n";
        return 0;
      } else {
        result.push_back((var[j] < var[(j + 1) % 3] ? j : (j + 1) % 3));
        swap(var[j], var[(j + 1) % 3]);
      }
    }
  } else if (S == 2) {
    vector<vector<bool>> reach(N + 1, vector<bool>(27, false));

    rirep(i, N) rep(x, 3) rep(y, 3) rep(z, 3) {
      vector<bool>::reference curr = reach[i][x * 9 + y * 3 + z];
      if (i == N) {
        curr = (x + y + z == 2);
      } else {
        const int d[] = {1, -1, 0};
        int j = op[i];
        int dx = d[(3 - j) % 3];
        int dy = d[(4 - j) % 3];
        int dz = d[(5 - j) % 3];

        curr = (x + dx >= 0 && x + dx <= 2 && y + dy >= 0 && y + dy <= 2 &&
                z + dz >= 0 && z + dz <= 2 &&
                reach[i + 1][(x + dx) * 9 + (y + dy) * 3 + z + dz]) ||
               (x - dx >= 0 && x - dx <= 2 && y - dy >= 0 && y - dy <= 2 &&
                z - dz >= 0 && z - dz <= 2 &&
                reach[i + 1][(x - dx) * 9 + (y - dy) * 3 + z - dz]);
      }
    }

    int x = var[0], y = var[1], z = var[2];
    if (reach[0][x * 9 + y * 3 + z]) {
      cout << "No\\n";
      return 0;
    }

    rep(i, N) {
      const int d[] = {1, -1, 0};
      int j = op[i];
      int dx = d[(3 - j) % 3];
      int dy = d[(4 - j) % 3];
      int dz = d[(5 - j) % 3];

      if (x + dx >= 0 && x + dx <= 2 && y + dy >= 0 && y + dy <= 2 &&
          z + dz >= 0 && z + dz <= 2 &&
          reach[i + 1][(x + dx) * 9 + (y + dy) * 3 + z + dz]) {
        x += dx;
        y += dy;
        z += dz;
        result.push_back(j);
      } else {
        x -= dx;
        y -= dy;
        z -= dz;
        result.push_back((j + 1) % 3);
      }
    }
    throw string("hoge");

  } else {
    rep(i, N) {
      int j = op[i];
      if (var[j] == 0 && var[(j + 1) % 3] == 0) {
        cout << "No\\n";
        return 0;
      } else {
        if (var[j] < var[(j + 1) % 3]) {
          result.push_back(j);
          var[j]++;
          var[(j + 1) % 3]--;
        } else {
          result.push_back((j + 1) % 3);
          var[j]--;
          var[(j + 1) % 3]++;
        }
      }
    }
  }

  cout << "Yes\\n";
  rep(i, N) { cout << (char)("ABC"[result[i]]) << "\\n"; }
  return 0;
}