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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
struct Dice {
  int top, right, front;
  Dice() : top(1), right(2), front(3){};
  Dice(int t, int r, int f) : top(t), right(r), front(f){};
  void set(int t, int r, int f) { top = t, right = r, front = f; }
  int get_top() { return top; }
  int get_bottom() { return 7 - top; }
  int get_right() { return right; }
  int get_left() { return 7 - right; }
  int get_front() { return front; }
  int get_back() { return 7 - front; }
  void to_north() {
    swap(front, top);
    top = 7 - top;
  }
  void to_south() { REP(i, 3) to_north(); }
  void to_east() {
    swap(top, right);
    top = 7 - top;
  }
  void to_west() { REP(i, 3) to_east(); }
  void to_clock() {
    swap(front, right);
    right = 7 - right;
  }
  void to_counter() { REP(i, 3) to_clock(); }
};

bool is_end(vi &v) {
  REP(i, 6) if (v[i] > 0) return false;
  return true;
}

int check(vi &v, Dice d) {
  REP(i, 6) {
    if (v[i] < 0)
      return 0;
  }
  int a = v[0] + v[5], b = v[1] + v[4], c = v[2] + v[3];
  if (a + b + c == 0)
    return 2;
  else if (a + b >= c && b + c >= a && c + a >= b)
    return 1;
  else if (a + b + 1 == c && d.get_top() != 3 && d.get_top() != 4)
    return 1;
  else if (b + c + 1 == a && d.get_top() != 1 && d.get_top() != 6)
    return 1;
  else if (c + a + 1 == b && d.get_top() != 2 && d.get_top() != 5)
    return 1;
  else
    return 0;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while (1) {
    vi t(6);
    REP(i, 6) cin >> t[i];
    if (is_end(t))
      break;
    sort(ALL(t));
    string ans("impossible");
    do {
      vi v = t;
      Dice d;
      string tmp;
      while (1) {
        Dice nd;
        vi nv;
        int st;

        nd = d;
        nv = v;
        nd.to_east();
        nv[nd.get_bottom() - 1]--;
        st = check(nv, nd);
        if (st) {
          tmp += 'E';
          if (st == 2) {
            chmin(ans, tmp);
            break;
          } else {
            d = nd;
            v = nv;
            continue;
          }
        }

        nd = d;
        nv = v;
        nd.to_north();
        nv[nd.get_bottom() - 1]--;
        st = check(nv, nd);
        if (st) {
          tmp += 'N';
          if (st == 2) {
            chmin(ans, tmp);
            break;
          } else {
            d = nd;
            v = nv;
            continue;
          }
        }

        nd = d;
        nv = v;
        nd.to_south();
        nv[nd.get_bottom() - 1]--;
        st = check(nv, nd);
        if (st) {
          tmp += 'S';
          if (st == 2) {
            chmin(ans, tmp);
            break;
          } else {
            d = nd;
            v = nv;
            continue;
          }
        }

        nd = d;
        nv = v;
        nd.to_west();
        nv[nd.get_bottom() - 1]--;
        st = check(nv, nd);
        if (st) {
          tmp += 'W';
          if (st == 2) {
            chmin(ans, tmp);
            break;
          } else {
            d = nd;
            v = nv;
            continue;
          }
        }
        break;
      }
    } while (next_permutation(ALL(t)));
    int p, q;
    cin >> p >> q;
    if (ans == "impossible")
      cout << ans << endl;
    else {
      FOR(i, p - 1, q) cout << ans[i];
      cout << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
struct Dice {
  int top, right, front;
  Dice() : top(1), right(2), front(3){};
  Dice(int t, int r, int f) : top(t), right(r), front(f){};
  void set(int t, int r, int f) { top = t, right = r, front = f; }
  int get_top() { return top; }
  int get_bottom() { return 7 - top; }
  int get_right() { return right; }
  int get_left() { return 7 - right; }
  int get_front() { return front; }
  int get_back() { return 7 - front; }
  void to_north() {
    swap(front, top);
    top = 7 - top;
  }
  void to_south() { REP(i, 3) to_north(); }
  void to_east() {
    swap(top, right);
    top = 7 - top;
  }
  void to_west() { REP(i, 3) to_east(); }
  void to_clock() {
    swap(front, right);
    right = 7 - right;
  }
  void to_counter() { REP(i, 3) to_clock(); }
};

bool is_end(vi &v) {
  REP(i, 6) if (v[i] > 0) return false;
  return true;
}

int check(vi &v, Dice d) {
  REP(i, 6) {
    if (v[i] < 0)
      return 0;
  }
  int a = v[0] + v[5], b = v[1] + v[4], c = v[2] + v[3];
  if (a + b + c == 0)
    return 2;
  else if (a + b >= c && b + c >= a && c + a >= b)
    return 1;
  else if (a + b + 1 == c && d.get_top() != 3 && d.get_top() != 4)
    return 1;
  else if (b + c + 1 == a && d.get_top() != 1 && d.get_top() != 6)
    return 1;
  else if (c + a + 1 == b && d.get_top() != 2 && d.get_top() != 5)
    return 1;
  else
    return 0;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while (1) {
    vi t(6);
    REP(i, 6) cin >> t[i];
    if (is_end(t))
      break;
    sort(ALL(t));
    string ans("impossible");
    t = {1, 2, 0, 0, 0, 0};
    // do
    //{
    vi v = t;
    Dice d;
    string tmp;
    while (1) {
      Dice nd;
      vi nv;
      int st;

      nd = d;
      nv = v;
      nd.to_east();
      nv[nd.get_bottom() - 1]--;
      st = check(nv, nd);
      if (st) {
        tmp += 'E';
        if (st == 2) {
          chmin(ans, tmp);
          break;
        } else {
          d = nd;
          v = nv;
          continue;
        }
      }

      nd = d;
      nv = v;
      nd.to_north();
      nv[nd.get_bottom() - 1]--;
      st = check(nv, nd);
      if (st) {
        tmp += 'N';
        if (st == 2) {
          chmin(ans, tmp);
          break;
        } else {
          d = nd;
          v = nv;
          continue;
        }
      }

      nd = d;
      nv = v;
      nd.to_south();
      nv[nd.get_bottom() - 1]--;
      st = check(nv, nd);
      if (st) {
        tmp += 'S';
        if (st == 2) {
          chmin(ans, tmp);
          break;
        } else {
          d = nd;
          v = nv;
          continue;
        }
      }

      nd = d;
      nv = v;
      nd.to_west();
      nv[nd.get_bottom() - 1]--;
      st = check(nv, nd);
      if (st) {
        tmp += 'W';
        if (st == 2) {
          chmin(ans, tmp);
          break;
        } else {
          d = nd;
          v = nv;
          continue;
        }
      }
      break;
    }
    //} while (next_permutation(ALL(t)));
    int p, q;
    cin >> p >> q;
    if (ans == "impossible")
      cout << ans << endl;
    else {
      FOR(i, p - 1, q) cout << ans[i];
      cout << endl;
    }
  }
  return 0;
}