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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rng(i, l, r) for (int i = int(l); i < int(r); i++)
#define rep(i, r) rng(i, 0, r)
#define rrng(i, l, r) for (int i = int(r) - 1; i >= int(l); i--)
#define rrep(i, r) rrng(i, 0, r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x, -1, sizeof(x))
#define zero(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define si(x) int(x.size())
#define inf INT_MAX / 2 - 100
#define infl LLONG_MAX / 3
#ifdef LOCAL
#define dmp(x) cerr << __LINE__ << ' ' << #x << ' ' << x << endl
#else
#define dmp(x) void(0)
#endif

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

template <class t> using vc = vector<t>;
template <class t> using vvc = vector<vector<t>>;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;

ll readl(void) {
  ll x;
  cin >> x;
  return x;
}
int readi(void) {
  int x;
  cin >> x;
  return x;
}

string readstr() {
  string s;
  cin >> s;
  return s;
}
vi readvi(int n, int off = 0) {
  vi v(n);
  rep(i, n) v[i] = readi(), v[i] += off;
  return v;
}

vl readvl(int n, int off = 0) {
  vl v(n);
  rep(i, n) v[i] = readl(), v[i] += off;
  return v;
}

template <class t> void print(t x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << "\\n";
  if (suc == 2)
    cout << " ";
}

template <class t> void print(const vc<t> &v, int suc = 1) {
  rep(i, si(v)) print(v[i], i == int(si(v)) - 1 ? 1 : suc);
}

template <class t> bool inc(t a, t b, t c) { return !(c < b || b < a); }

template <class t> void compress(vc<t> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.ed);
}

template <class t> int lwb(const vc<t> &v, const t &a) {
  return lower_bound(all(v), a) - v.bg;
}

template <class t> struct Compress {
  vc<t> v;
  Compress() = default;
  Compress(const vc<t> &x) { add(x); }
  Compress(const initializer_list<vc<t>> &x) {
    for (auto &p : x)
      add(p);
  }
  void add(const t &x) { v.eb(x); }
  void add(const vc<t> &x) { copy(all(x), back_inserter(v)); }
  void build() { compress(v); }
  int get(const t &x) const { return lwb(v, x); }
  vc<t> get(const vc<t> &x) const {
    vc<t> res(x);
    for (auto &p : res)
      p = get(p);
    return res;
  }
  const t &operator[](int x) const { return v[x]; }
};
void Yes(bool ex = true) {
  cout << "Yes\\n";
  if (ex)
    exit(0);
}
void YES(bool ex = true) {
  cout << "YES\\n";
  if (ex)
    exit(0);
}
void No(bool ex = true) {
  cout << "No\\n";
  if (ex)
    exit(0);
}
void NO(bool ex = true) {
  cout << "NO\\n";
  if (ex)
    exit(0);
}
void orYes(bool x, bool ex = true) {
  if (x)
    Yes(ex);
  else
    No(ex);
}
void orYES(bool x, bool ex = true) {
  if (x)
    YES(ex);
  else
    NO(ex);
}
void Possible(bool ex = true) {
  cout << "Possible\\n";
  if (ex)
    exit(0);
}
void POSSIBLE(bool ex = true) {
  cout << "POSSIBLE\\n";
  if (ex)
    exit(0);
}
void Impossible(bool ex = true) {
  cout << "Impossible\\n";
  if (ex)
    exit(0);
}
void IMPOSSIBLE(bool ex = true) {
  cout << "IMPOSSIBLE\\n";
  if (ex)
    exit(0);
}
void orPossible(bool x, bool ex = true) {
  if (x)
    Possible(ex);
  else
    Impossible(ex);
}
void orPOSSIBLE(bool x, bool ex = true) {
  if (x)
    POSSIBLE(ex);
  else
    IMPOSSIBLE(ex);
}

int n, ru[13], ch[13][600000];
vc<char> p[13][600000];
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n;
  ru[0] = 1;
  rng(i, 1, n + 1) ru[i] = ru[i - 1] * 3;
  string s;
  cin >> s;
  rep(i, si(s)) { p[0][0].pb(s[i]); }
  rng(i, 1, n + 1) {
    rep(j, ru[i]) {
      int x = j / ru[i - 1];
      for (auto &k : p[i - 1][j % ru[i - 1]]) {
        if (k == 'S') {
          if (x == 1)
            x = 2;
          else if (x == 2)
            x = 1;
          if (si(p[i][j]) > 0 && p[i][j].back() == 'S')
            p[i][j].pop_back();
          else
            p[i][j].pb('S');
        } else {
          if (x == 2)
            p[i][j].pb('R');
          x = (x + 1) % 3;
        }
      }
      ch[i][j] = x;
    }
  }
  rep(i, ru[n]) {
    int ans = 0;
    rep(j, n) { ans += ch[j + 1][i % ru[j + 1]] * ru[j]; }
    cout << ans;
    if (i == ru[n] - 1)
      cout << endl;
    else
      cout << ' ';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rng(i, l, r) for (int i = int(l); i < int(r); i++)
#define rep(i, r) rng(i, 0, r)
#define rrng(i, l, r) for (int i = int(r) - 1; i >= int(l); i--)
#define rrep(i, r) rrng(i, 0, r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x, -1, sizeof(x))
#define zero(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define si(x) int(x.size())
#define inf INT_MAX / 2 - 100
#define infl LLONG_MAX / 3
#ifdef LOCAL
#define dmp(x) cerr << __LINE__ << ' ' << #x << ' ' << x << endl
#else
#define dmp(x) void(0)
#endif

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
}
template <class t, class u> void chmin(t &a, u b) {
  if (b < a)
    a = b;
}

template <class t> using vc = vector<t>;
template <class t> using vvc = vector<vector<t>>;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;

ll readl(void) {
  ll x;
  cin >> x;
  return x;
}
int readi(void) {
  int x;
  cin >> x;
  return x;
}

string readstr() {
  string s;
  cin >> s;
  return s;
}
vi readvi(int n, int off = 0) {
  vi v(n);
  rep(i, n) v[i] = readi(), v[i] += off;
  return v;
}

vl readvl(int n, int off = 0) {
  vl v(n);
  rep(i, n) v[i] = readl(), v[i] += off;
  return v;
}

template <class t> void print(t x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << "\\n";
  if (suc == 2)
    cout << " ";
}

template <class t> void print(const vc<t> &v, int suc = 1) {
  rep(i, si(v)) print(v[i], i == int(si(v)) - 1 ? 1 : suc);
}

template <class t> bool inc(t a, t b, t c) { return !(c < b || b < a); }

template <class t> void compress(vc<t> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.ed);
}

template <class t> int lwb(const vc<t> &v, const t &a) {
  return lower_bound(all(v), a) - v.bg;
}

template <class t> struct Compress {
  vc<t> v;
  Compress() = default;
  Compress(const vc<t> &x) { add(x); }
  Compress(const initializer_list<vc<t>> &x) {
    for (auto &p : x)
      add(p);
  }
  void add(const t &x) { v.eb(x); }
  void add(const vc<t> &x) { copy(all(x), back_inserter(v)); }
  void build() { compress(v); }
  int get(const t &x) const { return lwb(v, x); }
  vc<t> get(const vc<t> &x) const {
    vc<t> res(x);
    for (auto &p : res)
      p = get(p);
    return res;
  }
  const t &operator[](int x) const { return v[x]; }
};
void Yes(bool ex = true) {
  cout << "Yes\\n";
  if (ex)
    exit(0);
}
void YES(bool ex = true) {
  cout << "YES\\n";
  if (ex)
    exit(0);
}
void No(bool ex = true) {
  cout << "No\\n";
  if (ex)
    exit(0);
}
void NO(bool ex = true) {
  cout << "NO\\n";
  if (ex)
    exit(0);
}
void orYes(bool x, bool ex = true) {
  if (x)
    Yes(ex);
  else
    No(ex);
}
void orYES(bool x, bool ex = true) {
  if (x)
    YES(ex);
  else
    NO(ex);
}
void Possible(bool ex = true) {
  cout << "Possible\\n";
  if (ex)
    exit(0);
}
void POSSIBLE(bool ex = true) {
  cout << "POSSIBLE\\n";
  if (ex)
    exit(0);
}
void Impossible(bool ex = true) {
  cout << "Impossible\\n";
  if (ex)
    exit(0);
}
void IMPOSSIBLE(bool ex = true) {
  cout << "IMPOSSIBLE\\n";
  if (ex)
    exit(0);
}
void orPossible(bool x, bool ex = true) {
  if (x)
    Possible(ex);
  else
    Impossible(ex);
}
void orPOSSIBLE(bool x, bool ex = true) {
  if (x)
    POSSIBLE(ex);
  else
    IMPOSSIBLE(ex);
}
string s;
int ans[200005], pw[15], m;
int pr[200005], nx[200005], ch[200005];
int z(int x) { return (x + m) % m; }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  cin >> s;
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  for (int i = 0; i < n; i++) {
    m = pw[i + 1];
    rep(j, m) {
      nx[j] = z(j + 1);
      pr[j] = z(j - 1);
    }
    int a = 0, b = m / 3, c = b * 2;
    rep(j, s.size()) {
      if (s[j] == 'R') {
        a = pr[a];
        b = pr[b];
        c = pr[c];
      } else {
        int pra = pr[a];
        int prb = pr[b];
        int prc = pr[c];
        pr[a] = prc;
        pr[b] = pra;
        pr[c] = prb;
        swap(b, c);
      }
    }
    rep(j, m) ch[j] = 0;
    while (1) {
      a = pr[a];
      ch[a] = pw[i] * 2;
      if (a == c)
        break;
    }
    while (1) {
      c = pr[c];
      ch[c] = pw[i];
      if (b == c)
        break;
    }
    rep(j, pw[n]) { ans[j] += ch[z(j)]; }
  }
  rep(i, pw[n]) {
    cout << ans[i];
    if (i == pw[n] - 1)
      cout << endl;
    else
      cout << ' ';
  }
}