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

#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T> bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}

int readint() {
  int x = 0, f = 1;
  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 n, m, cnt;
int pw[15], ch[800000][3], tag[800000], fl[800000], ans[800000];
char s[200005];

void insert(int x, int t) {
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (!ch[p][x % 3])
      ch[p][x % 3] = ++cnt;
    p = ch[p][x % 3];
    x /= 3;
  }
  fl[p] = t;
}

void add() {
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (tag[p]) {
      swap(ch[p][1], ch[p][2]);
      for (int i = 0; i < 3; i++)
        tag[ch[p][i]] ^= 1;
      tag[p] = 0;
    }
    swap(ch[p][0], ch[p][1]);
    swap(ch[p][0], ch[p][2]);
    p = ch[p][0];
  }
}

void getans(int id, int num, int dep) {
  if (fl[id])
    return (void)(ans[fl[id] - 1] = num);
  if (tag[id]) {
    swap(ch[id][1], ch[id][2]);
    for (int i = 0; i < 3; i++)
      tag[ch[id][i]] ^= 1;
    tag[id] = 0;
  }
  getans(ch[id][0], num, dep + 1);
  getans(ch[id][1], num + pw[dep], dep + 1);
  getans(ch[id][2], num + 2 * pw[dep], dep + 1);
}

int main() {
  n = readint();
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  for (int i = 0; i < pw[n]; i++)
    insert(i, i + 1);
  scanf("%s", s + 1);
  m = strlen(s + 1);
  for (int i = 1; i <= m; i++) {
    if (s[i] == 'S')
      tag[0] ^= 1;
    else
      add();
  }
  getans(0, 0, 0);
  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  printf("\\n");
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T> bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}

int readint() {
  int x = 0, f = 1;
  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 n, m, cnt;
int pw[15], ch[600000][3], tag[600000], fl[600000], ans[600000];
char s[200005];

void insert(int x, int t) {
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (!ch[p][x % 3])
      ch[p][x % 3] = ++cnt;
    p = ch[p][x % 3];
    x /= 3;
  }
  fl[p] = t;
}

void add() {
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (tag[p]) {
      swap(ch[p][1], ch[p][2]);
      for (int i = 0; i < 3; i++)
        tag[ch[p][i]] ^= 1;
      tag[p] = 0;
    }
    swap(ch[p][0], ch[p][1]);
    swap(ch[p][0], ch[p][2]);
    p = ch[p][0];
  }
}

void getans(int id, int num, int dep) {
  if (fl[id])
    return (void)(ans[fl[id] - 1] = num);
  if (tag[id]) {
    swap(ch[id][1], ch[id][2]);
    for (int i = 0; i < 3; i++)
      tag[ch[id][i]] ^= 1;
    tag[id] = 0;
  }
  getans(ch[id][0], num, dep + 1);
  getans(ch[id][1], num + pw[dep], dep + 1);
  getans(ch[id][2], num + 2 * pw[dep], dep + 1);
}

int main() {
  n = readint();
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  scanf("%s", s + 1);
  m = strlen(s + 1);
  for (int i = 0; i < pw[n]; i++)
    insert(i, i + 1);
  for (int i = 1; i <= m; i++) {
    if (s[i] == 'S')
      tag[0] ^= 1;
    else
      add();
  }
  getans(0, 0, 0);
  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  printf("\\n");
  return 0;
}