Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 12;
const int maxN = 600000;
const int maxt = 200002;

struct node {
  node *ch[3];
  int lazy, id;
};

node *build(int d, int cur);
void pd(node *p);
void change(node *p, int d);
void cal(node *p, int d, int cur);

int n, t;
char str[maxt];
int powd[maxn];
int ans[maxN];

int main() {
  scanf("%d", &n);
  scanf("%s", str + 1);
  t = strlen(str + 1);
  powd[0] = 1;
  for (int i = 1; i < n; i++) {
    powd[i] = 3 * powd[i - 1];
  }
  node *rt = build(0, 0);
  for (int i = 1; i <= t; i++) {
    if (str[i] == 'S')
      rt->lazy ^= 1;
    else
      change(rt, 0);
  }
  cal(rt, 0, 0);
  int N = 1;
  for (int i = 1; i <= n; i++)
    N *= 3;
  for (int i = 0; i < N; i++) {
    printf("%d ", ans[i]);
  }
  return 0;
}

node *create() {
  static node pool[maxN];
  static node *p = pool;
  p->ch[0] = p->ch[1] = p->ch[2] = NULL;
  p->lazy = p->id = 0;
  return p++;
}

node *build(int d, int cur) {
  node *p = create();
  if (d == n) {
    p->id = cur;
    return p;
  }
  p->ch[0] = build(d + 1, cur);
  p->ch[1] = build(d + 1, cur + powd[d]);
  p->ch[2] = build(d + 1, cur + powd[d] + powd[d]);
  return p;
}

void pd(node *p) {
  if (!p->lazy)
    return;
  swap(p->ch[1], p->ch[2]);
  p->ch[0]->lazy ^= 1;
  p->ch[1]->lazy ^= 1;
  p->ch[2]->lazy ^= 1;
  p->lazy = 0;
}

void change(node *p, int d) {
  if (d == n)
    return;
  pd(p);
  swap(p->ch[0], p->ch[1]);
  swap(p->ch[0], p->ch[2]);
  change(p->ch[0], d + 1);
}

void cal(node *p, int d, int cur) {
  if (d == n) {
    ans[p->id] = cur;
    return;
  }
  pd(p);
  cal(p->ch[0], d + 1, cur);
  cal(p->ch[1], d + 1, cur + powd[d]);
  cal(p->ch[2], d + 1, cur + powd[d] + powd[d]);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 20;
const int maxN = 1000000;
const int maxt = 2000002;

struct node {
  node *ch[3];
  int lazy, id;
};

node *build(int d, int cur);
void pd(node *p);
void change(node *p, int d);
void cal(node *p, int d, int cur);

int n, t;
char str[maxt];
int powd[maxn];
int ans[maxN];

int main() {
  scanf("%d", &n);
  scanf("%s", str + 1);
  t = strlen(str + 1);
  powd[0] = 1;
  for (int i = 1; i < n; i++) {
    powd[i] = 3 * powd[i - 1];
  }
  node *rt = build(0, 0);
  for (int i = 1; i <= t; i++) {
    if (str[i] == 'S')
      rt->lazy ^= 1;
    else
      change(rt, 0);
  }
  cal(rt, 0, 0);
  int N = 1;
  for (int i = 1; i <= n; i++)
    N *= 3;
  for (int i = 0; i < N; i++) {
    printf("%d ", ans[i]);
  }
  return 0;
}

node *create() {
  static node pool[maxN];
  static node *p = pool;
  p->ch[0] = p->ch[1] = p->ch[2] = NULL;
  p->lazy = p->id = 0;
  return p++;
}

node *build(int d, int cur) {
  node *p = create();
  if (d == n) {
    p->id = cur;
    return p;
  }
  p->ch[0] = build(d + 1, cur);
  p->ch[1] = build(d + 1, cur + powd[d]);
  p->ch[2] = build(d + 1, cur + powd[d] + powd[d]);
  return p;
}

void pd(node *p) {
  if (!p->lazy)
    return;
  swap(p->ch[1], p->ch[2]);
  p->ch[0]->lazy ^= 1;
  p->ch[1]->lazy ^= 1;
  p->ch[2]->lazy ^= 1;
  p->lazy = 0;
}

void change(node *p, int d) {
  if (d == n)
    return;
  pd(p);
  swap(p->ch[0], p->ch[1]);
  swap(p->ch[0], p->ch[2]);
  change(p->ch[0], d + 1);
}

void cal(node *p, int d, int cur) {
  if (d == n) {
    ans[p->id] = cur;
    return;
  }
  pd(p);
  cal(p->ch[0], d + 1, cur);
  cal(p->ch[1], d + 1, cur + powd[d]);
  cal(p->ch[2], d + 1, cur + powd[d] + powd[d]);
}