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

#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  register char ch = getchar();
  x = 0;
  for (; ch == ' ' || ch == '\\n'; ch = getchar())
    ;
  for (; ch != ' ' && ch != '\\n'; x = x * 10 + ch - '0', ch = getchar())
    ;
}
void reads(string &s) {
  register char ch = getchar();
  s = "";
  for (; ch == ' ' || ch == '\\n'; ch = getchar())
    ;
  for (; ch != ' ' && ch != '\\n'; s += ch, ch = getchar())
    ;
}
void write(int x) {
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int K, pw[13], ans[800000], dat[800000], go[800000][3];
bool lz[800000];
string typ;
void built_trie(int id, int lv, int now) {
  if (lv == K) {
    dat[id] = now;
    return;
  }
  for (int i = 0; i < 3; i++) {
    go[id][i] = id * 3 + i + 1;
    built_trie(go[id][i], lv + 1, now + i * pw[lv]);
  }
}
void push_down(int id) {
  swap(go[id][1], go[id][2]);
  for (int i = 0; i < 3; i++)
    lz[go[id][i]] ^= 1;
  lz[id] = 0;
}
void update_trie(int id, int lv) {
  if (lv == K)
    return;
  if (lz[id])
    push_down(id);
  update_trie(go[id][2], lv + 1);
  swap(go[id][1], go[id][2]), swap(go[id][0], go[id][1]);
}
void make_ans(int id, int lv, int now) {
  if (lv == K) {
    ans[dat[id]] = now;
    return;
  }
  if (lz[id])
    push_down(id);
  for (int i = 0; i < 3; i++)
    make_ans(go[id][i], lv + 1, now + i * pw[lv]);
}
int main() {
  pw[0] = 1, read(K), reads(typ);
  for (int i = 1; i <= K; i++)
    pw[i] = pw[i - 1] * 3;
  built_trie(0, 0, 0);
  for (int i = 0; i < typ.size(); i++) {
    if (typ[i] == 'S')
      lz[0] ^= 1;
    else
      update_trie(0, 0);
  }
  make_ans(0, 0, 0);
  for (int i = 0; i < pw[K]; i++)
    write(ans[i]), putchar(' ');
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  register char ch = getchar();
  x = 0;
  for (; ch == ' ' || ch == '\\n'; ch = getchar())
    ;
  for (; ch != ' ' && ch != '\\n'; x = x * 10 + ch - '0', ch = getchar())
    ;
}
void reads(string &s) {
  register char ch = getchar();
  s = "";
  for (; ch == ' ' || ch == '\\n'; ch = getchar())
    ;
  for (; ch != ' ' && ch != '\\n'; s += ch, ch = getchar())
    ;
}
void write(int x) {
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int K, pw[13], ans[532000], dat[532000], go[532000][3];
bool lz[532000];
string typ;
void built_trie(int id, int lv, int now) {
  if (lv == K) {
    dat[id] = now;
    return;
  }
  for (int i = 0; i < 3; i++) {
    go[id][i] = id * 3 + i + 1;
    built_trie(go[id][i], lv + 1, now + i * pw[lv]);
  }
}
void push_down(int id) {
  swap(go[id][1], go[id][2]);
  for (int i = 0; i < 3; i++)
    lz[go[id][i]] ^= 1;
  lz[id] = 0;
}
void update_trie(int id, int lv) {
  if (lv == K)
    return;
  if (lz[id])
    push_down(id);
  update_trie(go[id][2], lv + 1);
  swap(go[id][1], go[id][2]), swap(go[id][0], go[id][1]);
}
void make_ans(int id, int lv, int now) {
  if (lv == K) {
    ans[dat[id]] = now;
    return;
  }
  if (lz[id])
    push_down(id);
  for (int i = 0; i < 3; i++)
    make_ans(go[id][i], lv + 1, now + i * pw[lv]);
}
int main() {
  pw[0] = 1, read(K), reads(typ);
  for (int i = 1; i <= K; i++)
    pw[i] = pw[i - 1] * 3;
  built_trie(0, 0, 0);
  for (int i = 0; i < typ.size(); i++) {
    if (typ[i] == 'S')
      lz[0] ^= 1;
    else
      update_trie(0, 0);
  }
  make_ans(0, 0, 0);
  for (int i = 0; i < pw[K]; i++)
    write(ans[i]), putchar(' ');
}