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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int maxn = 888888;
#define MP make_pair
#define PB push_back
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 0;
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, m, q, rt, cnt, ch[maxn][3], id[maxn], ans[maxn];
bool rev[maxn];
char s[maxn];
inline void setrev(int x) {
  rev[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
inline void pushdown(int x) {
  if (rev[x]) {
    if (ch[x][0])
      setrev(ch[x][0]);
    if (ch[x][1])
      setrev(ch[x][1]);
    if (ch[x][2])
      setrev(ch[x][2]);
    rev[x] = false;
  }
}
void build(int &x, int dep, int cur, int pr) {
  x = ++cnt;
  if (dep == n) {
    id[x] = cur;
    return;
  }
  build(ch[x][0], dep + 1, cur, pr * 3);
  build(ch[x][1], dep + 1, cur + pr, pr * 3);
  build(ch[x][2], dep + 1, cur + pr * 2, pr * 3);
}
void add(int x, int dep) {
  pushdown(x);
  if (dep == n)
    return;
  swap(ch[x][1], ch[x][2]);
  swap(ch[x][0], ch[x][1]);
  add(ch[x][0], dep + 1);
}
void dfs(int x, int dep, int cur, int pr) {
  pushdown(x);
  if (dep == n) {
    ans[id[x]] = cur;
    return;
  }
  dfs(ch[x][0], dep + 1, cur, pr * 3);
  dfs(ch[x][1], dep + 1, cur + pr, pr * 3);
  dfs(ch[x][2], dep + 1, cur + pr * 2, pr * 3);
}
int main() {
  n = read();
  build(rt, 0, 0, 1);
  scanf("%s", s + 1);
  q = strlen(s + 1);
  FOR(i, 1, q) if (s[i] == 'S') setrev(rt);
  else add(rt, 0);
  dfs(rt, 0, 0, 1);
  m = 1;
  FOR(i, 1, n) m *= 3;
  FOR(i, 0, m - 1) printf("%d ", ans[i]);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int maxn = 555555;
#define MP make_pair
#define PB push_back
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 0;
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, m, q, rt, cnt, ch[maxn][3], id[maxn], ans[maxn];
bool rev[maxn];
char s[maxn];
inline void setrev(int x) {
  rev[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
inline void pushdown(int x) {
  if (rev[x]) {
    if (ch[x][0])
      setrev(ch[x][0]);
    if (ch[x][1])
      setrev(ch[x][1]);
    if (ch[x][2])
      setrev(ch[x][2]);
    rev[x] = false;
  }
}
void build(int &x, int dep, int cur, int pr) {
  x = ++cnt;
  if (dep == n) {
    id[x] = cur;
    return;
  }
  build(ch[x][0], dep + 1, cur, pr * 3);
  build(ch[x][1], dep + 1, cur + pr, pr * 3);
  build(ch[x][2], dep + 1, cur + pr * 2, pr * 3);
}
void add(int x, int dep) {
  pushdown(x);
  if (dep == n)
    return;
  swap(ch[x][1], ch[x][2]);
  swap(ch[x][0], ch[x][1]);
  add(ch[x][0], dep + 1);
}
void dfs(int x, int dep, int cur, int pr) {
  pushdown(x);
  if (dep == n) {
    ans[id[x]] = cur;
    return;
  }
  dfs(ch[x][0], dep + 1, cur, pr * 3);
  dfs(ch[x][1], dep + 1, cur + pr, pr * 3);
  dfs(ch[x][2], dep + 1, cur + pr * 2, pr * 3);
}
int main() {
  n = read();
  build(rt, 0, 0, 1);
  scanf("%s", s + 1);
  q = strlen(s + 1);
  FOR(i, 1, q) if (s[i] == 'S') setrev(rt);
  else add(rt, 0);
  dfs(rt, 0, 0, 1);
  m = 1;
  FOR(i, 1, n) m *= 3;
  FOR(i, 0, m - 1) printf("%d ", ans[i]);
}