Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read() {
  LL f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
const int MAXN = 550005;
const int MAXV = 11;
int bin[15];
int n, m, ans[MAXN];
struct Trie {
  int son[MAXN][3], tot, root;
  int val[MAXN];
  bool rev[MAXN];
  void insert(int v) {
    int p = root;
    for (int i = 0; i <= n - 1; i++) {
      int c = v / bin[i];
      c %= 3;
      if (!son[p][c])
        son[p][c] = ++tot;
      p = son[p][c];
    }
    val[p] = v;
  }
  void down(int p) {
    if (!rev[p])
      return;
    swap(son[p][1], son[p][2]);
    rev[son[p][0]] ^= 1;
    rev[son[p][1]] ^= 1;
    rev[son[p][2]] ^= 1;
    rev[p] = 0;
  }
  void addone(int p, int dep) {
    if (dep == 0)
      return;
    down(p);
    int a = son[p][0], b = son[p][1], c = son[p][2];
    son[p][0] = c;
    son[p][1] = a;
    son[p][2] = b;
    addone(son[p][0], dep - 1);
  }
  void getans(int p, int dep, int nowp) {
    if (dep == n) {
      ans[val[p]] = nowp;
      return;
    }
    down(p);
    getans(son[p][0], dep + 1, nowp);
    getans(son[p][1], dep + 1, nowp + bin[dep]);
    getans(son[p][2], dep + 1, nowp + 2 * bin[dep]);
  }
} tr;
char ch[MAXN];
int main() {
#ifdef Rose
  double BeginJudgeTime = clock();
#endif
  bin[0] = 1;
  for (int i = 1; i < 15; i++)
    bin[i] = bin[i - 1] * 3;
  n = read();
  scanf("%s", ch + 1);
  m = strlen(ch + 1);
  tr.tot = tr.root = 1;
  for (int i = 0; i < bin[n]; i++)
    tr.insert(i);
  for (int i = 1; i <= m; i++) {
    if (ch[i] == 'S')
      tr.rev[1] ^= 1;
    else
      tr.addone(1, n);
  }
  tr.getans(1, 0, 0);
  for (int i = 0; i < bin[n]; i++)
    pr1(ans[i]);
  puts("");
#ifdef Rose
  double EndJudgeTime = clock();
  cerr << "JudgeTime is"
       << " ";
  cerr << (EndJudgeTime - BeginJudgeTime) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read() {
  LL f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
const int MAXN = 2e6;
const int MAXV = 13;
int bin[15];
int n, m, ans[MAXN];
struct Trie {
  int son[MAXN][3], tot, root;
  int val[MAXN];
  bool rev[MAXN];
  void insert(int v) {
    int p = root;
    for (int i = 0; i <= n - 1; i++) {
      int c = v / bin[i];
      c %= 3;
      if (!son[p][c])
        son[p][c] = ++tot;
      p = son[p][c];
    }
    val[p] = v;
  }
  void down(int p) {
    if (!rev[p])
      return;
    swap(son[p][1], son[p][2]);
    rev[son[p][0]] ^= 1;
    rev[son[p][1]] ^= 1;
    rev[son[p][2]] ^= 1;
    rev[p] = 0;
  }
  void addone(int p, int dep) {
    if (dep == 0)
      return;
    down(p);
    int a = son[p][0], b = son[p][1], c = son[p][2];
    son[p][0] = c;
    son[p][1] = a;
    son[p][2] = b;
    addone(son[p][0], dep - 1);
  }
  void getans(int p, int dep, int nowp) {
    if (dep == n) {
      ans[val[p]] = nowp;
      return;
    }
    down(p);
    getans(son[p][0], dep + 1, nowp);
    getans(son[p][1], dep + 1, nowp + bin[dep]);
    getans(son[p][2], dep + 1, nowp + 2 * bin[dep]);
  }
} tr;
char ch[MAXN];
int main() {
#ifdef Rose
  double BeginJudgeTime = clock();
#endif
  bin[0] = 1;
  for (int i = 1; i < 15; i++)
    bin[i] = bin[i - 1] * 3;
  n = read();
  scanf("%s", ch + 1);
  m = strlen(ch + 1);
  tr.tot = tr.root = 1;
  for (int i = 0; i < bin[n]; i++)
    tr.insert(i);
  for (int i = 1; i <= m; i++) {
    if (ch[i] == 'S')
      tr.rev[1] ^= 1;
    else
      tr.addone(1, n);
  }
  tr.getans(1, 0, 0);
  for (int i = 0; i < bin[n]; i++)
    pr1(ans[i]);
  puts("");
#ifdef Rose
  double EndJudgeTime = clock();
  cerr << "JudgeTime is"
       << " ";
  cerr << (EndJudgeTime - BeginJudgeTime) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}