Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 531500
void Read(int &p) {
  p = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    p = p * 10 + c - '0';
}
int n, m, rot, cnt, val[N], ans[N];
char T[N];
struct Node {
  int ch[3], rv, tg;
} S[N];
void Build(int &q, int d, int s) {
  q = ++cnt;
  if (d == n) {
    S[q].tg = s;
    return;
  }
  Build(S[q].ch[0], d + 1, s);
  Build(S[q].ch[1], d + 1, s + val[d]);
  Build(S[q].ch[2], d + 1, s + val[d] * 2);
}
void Pushdown(int q) {
  if (S[q].rv) {
    swap(S[q].ch[1], S[q].ch[2]);
    S[S[q].ch[0]].rv ^= 1;
    S[S[q].ch[1]].rv ^= 1;
    S[S[q].ch[2]].rv ^= 1;
    S[q].rv = 0;
  }
}
void Insert(int q, int d) {
  if (d == n)
    return;
  Pushdown(q);
  int a = S[q].ch[0];
  int b = S[q].ch[1];
  int c = S[q].ch[2];
  S[q].ch[1] = a;
  S[q].ch[2] = b;
  S[q].ch[0] = c;
  Insert(S[q].ch[0], d + 1);
}
void Getans(int q, int d, int s) {
  if (d == n) {
    ans[S[q].tg] = s;
    return;
  }
  Pushdown(q);
  Getans(S[q].ch[0], d + 1, s);
  Getans(S[q].ch[1], d + 1, s + val[d]);
  Getans(S[q].ch[2], d + 1, s + val[d] * 2);
}
int main() {
  Read(n);
  val[0] = 1;
  for (int i = 1; i <= n; i++)
    val[i] = val[i - 1] * 3;
  Build(rot, 0, 0);
  scanf("%s", T + 1);
  m = strlen(T + 1);
  for (int i = 1; i <= m; i++)
    if (T[i] == 'S')
      S[rot].rv ^= 1;
    else
      Insert(rot, 0);
  Getans(rot, 0, 0);
  for (int i = 0; i < val[n]; i++)
    printf("%d%c", ans[i], i == val[n] - 1 ? 10 : 32);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 5000005
void Read(int &p) {
  p = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    p = p * 10 + c - '0';
}
int n, m, rot, cnt, val[N], ans[N];
char T[N];
struct Node {
  int ch[3], rv, tg;
} S[N];
void Build(int &q, int d, int s) {
  q = ++cnt;
  if (d == n) {
    S[q].tg = s;
    return;
  }
  Build(S[q].ch[0], d + 1, s);
  Build(S[q].ch[1], d + 1, s + val[d]);
  Build(S[q].ch[2], d + 1, s + val[d] * 2);
}
void Pushdown(int q) {
  if (S[q].rv) {
    swap(S[q].ch[1], S[q].ch[2]);
    S[S[q].ch[0]].rv ^= 1;
    S[S[q].ch[1]].rv ^= 1;
    S[S[q].ch[2]].rv ^= 1;
    S[q].rv = 0;
  }
}
void Insert(int q, int d) {
  if (d == n)
    return;
  Pushdown(q);
  int a = S[q].ch[0];
  int b = S[q].ch[1];
  int c = S[q].ch[2];
  S[q].ch[1] = a;
  S[q].ch[2] = b;
  S[q].ch[0] = c;
  Insert(S[q].ch[0], d + 1);
}
void Getans(int q, int d, int s) {
  if (d == n) {
    ans[S[q].tg] = s;
    return;
  }
  Pushdown(q);
  Getans(S[q].ch[0], d + 1, s);
  Getans(S[q].ch[1], d + 1, s + val[d]);
  Getans(S[q].ch[2], d + 1, s + val[d] * 2);
}
int main() {
  Read(n);
  val[0] = 1;
  for (int i = 1; i <= n; i++)
    val[i] = val[i - 1] * 3;
  Build(rot, 0, 0);
  scanf("%s", T + 1);
  m = strlen(T + 1);
  for (int i = 1; i <= m; i++)
    if (T[i] == 'S')
      S[rot].rv ^= 1;
    else
      Insert(rot, 0);
  Getans(rot, 0, 0);
  for (int i = 0; i < val[n]; i++)
    printf("%d%c", ans[i], i == val[n] - 1 ? 10 : 32);
}