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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 13;
const int maxp = 2e6 + 5;
const int maxm = 2e5 + 5;

int n, m;
int pw[maxn], ans[maxp];
char t[maxm];

class Trie {
public:
  int tot;
  int p[maxp], ch[maxp][3], tag[maxp];
  Trie() {
    tot = 1;
    memset(ch, -1, sizeof(ch));
    memset(p, -1, sizeof(p));
    return;
  }
  inline void insert(int x) {
    int u = 0, y = x;
    for (int i = 0; i < n; ++i, x /= 3) {
      if (!~ch[u][x % 3])
        ch[u][x % 3] = tot++;
      u = ch[u][x % 3];
    }
    //		printf("u = %d y = %d\\n", u, y);
    p[u] = y;
    return;
  }
  inline void inv(int u) {
    tag[u] ^= 1;
    swap(ch[u][1], ch[u][2]);
    return;
  }
  inline void pushDown(int u) {
    if (tag[u]) {
      FOR(i, 0, 3)
      inv(ch[u][i]);
      tag[u] = false;
    }
    return;
  }
  inline void update(int dep, int u) {
    //		printf("dep = %d u = %d\\n", dep, u);
    if (dep > n || !~u)
      return;
    pushDown(u);
    int tmp = ch[u][2];
    ch[u][2] = ch[u][1];
    ch[u][1] = ch[u][0];
    ch[u][0] = tmp;
    update(dep + 1, ch[u][0]);
    return;
  }
  inline void dfs(int dep, int u, int x) {
    if (dep > n || !~u)
      return;
    //		printf("dep = %d u = %d x = %d\\n", dep, u, x);
    pushDown(u);
    FOR(i, 0, 3)
    dfs(dep + 1, ch[u][i], x + i * pw[dep]);
    if (~p[u])
      ans[p[u]] = x;
    return;
  }
} trie;

inline void precalc() {
  pw[0] = 1;
  FOR(i, 1, maxn)
  pw[i] = pw[i - 1] * 3;
  return;
}

int main() {
  scanf("%d", &n);
  precalc();
  scanf("%s", t);
  m = strlen(t);
  FOR(i, 0, pw[n])
  trie.insert(i);
  FOR(i, 0, m) {
    if (t[i] == 'S')
      trie.inv(0);
    else
      trie.update(0, 0);
  }
  trie.dfs(0, 0, 0);
  FOR(i, 0, pw[n])
  printf("%d ", ans[i]);
  puts("");
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 13;
const int maxp = 2e6 + 5;
const int maxm = 2e5 + 5;

int n, m;
int pw[maxn], ans[maxn];
char t[maxm];

class Trie {
public:
  int tot;
  int p[maxp], ch[maxp][3], tag[maxp];
  Trie() {
    tot = 1;
    memset(ch, -1, sizeof(ch));
    memset(p, -1, sizeof(p));
    return;
  }
  inline void insert(int x) {
    int u = 0, y = x;
    for (int i = 0; i < n; ++i, x /= 3) {
      if (!~ch[u][x % 3])
        ch[u][x % 3] = tot++;
      u = ch[u][x % 3];
    }
    //		printf("u = %d y = %d\\n", u, y);
    p[u] = y;
    return;
  }
  inline void inv(int u) {
    tag[u] ^= 1;
    swap(ch[u][1], ch[u][2]);
    return;
  }
  inline void pushDown(int u) {
    if (tag[u]) {
      FOR(i, 0, 3)
      inv(ch[u][i]);
      tag[u] = false;
    }
    return;
  }
  inline void update(int dep, int u) {
    //		printf("dep = %d u = %d\\n", dep, u);
    if (dep > n || !~u)
      return;
    pushDown(u);
    int tmp = ch[u][2];
    ch[u][2] = ch[u][1];
    ch[u][1] = ch[u][0];
    ch[u][0] = tmp;
    update(dep + 1, ch[u][0]);
    return;
  }
  inline void dfs(int dep, int u, int x) {
    if (dep > n || !~u)
      return;
    //		printf("dep = %d u = %d x = %d\\n", dep, u, x);
    pushDown(u);
    FOR(i, 0, 3)
    dfs(dep + 1, ch[u][i], x + i * pw[dep]);
    if (~p[u])
      ans[p[u]] = x;
    return;
  }
} trie;

inline void precalc() {
  pw[0] = 1;
  FOR(i, 1, maxn)
  pw[i] = pw[i - 1] * 3;
  return;
}

int main() {
  scanf("%d", &n);
  precalc();
  scanf("%s", t);
  m = strlen(t);
  FOR(i, 0, pw[n])
  trie.insert(i);
  FOR(i, 0, m) {
    if (t[i] == 'S')
      trie.inv(0);
    else
      trie.update(0, 0);
  }
  trie.dfs(0, 0, 0);
  FOR(i, 0, pw[n])
  printf("%d ", ans[i]);
  puts("");
  return 0;
}