Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define calc(x) swap((x)->son[1], (x)->son[2])
using namespace std;

char s[200010];

struct Node {
  int x, rev;
  Node *son[3];
  Node() { x = -1, son[0] = son[1] = son[2] = NULL, rev = 0; }
  void pushdown() {
    if (!rev)
      return;
    son[0]->rev ^= 1, son[1]->rev ^= 1, son[2]->rev ^= 1;
    calc(son[0]), calc(son[1]), calc(son[2]);
    rev = 0;
  }
} pool[540000], *CUR = pool;

Node *Build(int l, int r, int dep, int x) {
  if (l == r) {
    CUR->x = x;
    return CUR++;
  }
  Node *now = CUR++;
  int m1 = l + (r - l + 1) / 3 - 1;
  int m2 = l + (r - l + 1) / 3 * 2 - 1;
  now->son[0] = Build(l, m1, dep * 3, x);
  now->son[1] = Build(m1 + 1, m2, dep * 3, x + dep);
  now->son[2] = Build(m2 + 1, r, dep * 3, x + dep * 2);
  return now;
}

void Update(Node *T) {
  if (T->x != -1)
    return;
  T->pushdown();
  swap(T->son[0], T->son[2]), swap(T->son[1], T->son[2]);
  Update(T->son[0]);
}

int ans[540000];

void print(Node *T, int dep, int x) {
  if (T->x != -1) {
    ans[T->x] = x;
    return;
  }
  T->pushdown();
  print(T->son[0], dep * 3, x);
  print(T->son[1], dep * 3, x + dep);
  print(T->son[2], dep * 3, x + dep * 2);
}

int main() {
  int n;
  scanf("%d", &n);
  int N = 1;
  for (int i = 0; i < n; i++) {
    N *= 3;
  }
  N--;
  Node *T = Build(0, N, 1, 0);
  scanf("%s", s);
  int m = strlen(s);
  for (int i = 0; i < m; i++) {
    if (s[i] == 'R')
      Update(T);
    else {
      T->rev ^= 1;
      calc(T);
    }
  }
  print(T, 1, 0);
  for (int i = 0; i <= N; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define calc(x) swap((x)->son[1], (x)->son[2])
using namespace std;

char s[200010];

struct Node {
  int x, rev;
  Node *son[3];
  Node() { x = -1, son[0] = son[1] = son[2] = NULL, rev = 0; }
  void pushdown() {
    if (!rev)
      return;
    son[0]->rev ^= 1, son[1]->rev ^= 1, son[2]->rev ^= 1;
    calc(son[0]), calc(son[1]), calc(son[2]);
    rev = 0;
  }
} pool[1500000], *CUR = pool;

Node *Build(int l, int r, int dep, int x) {
  if (l == r) {
    CUR->x = x;
    return CUR++;
  }
  Node *now = CUR++;
  int m1 = l + (r - l + 1) / 3 - 1;
  int m2 = l + (r - l + 1) / 3 * 2 - 1;
  now->son[0] = Build(l, m1, dep * 3, x);
  now->son[1] = Build(m1 + 1, m2, dep * 3, x + dep);
  now->son[2] = Build(m2 + 1, r, dep * 3, x + dep * 2);
  return now;
}

void Update(Node *T) {
  if (T->x != -1)
    return;
  T->pushdown();
  swap(T->son[0], T->son[2]), swap(T->son[1], T->son[2]);
  Update(T->son[0]);
}

int ans[540000];

void print(Node *T, int dep, int x) {
  if (T->x != -1) {
    ans[T->x] = x;
    return;
  }
  T->pushdown();
  print(T->son[0], dep * 3, x);
  print(T->son[1], dep * 3, x + dep);
  print(T->son[2], dep * 3, x + dep * 2);
}

int main() {
  int n;
  scanf("%d", &n);
  int N = 1;
  for (int i = 0; i < n; i++) {
    N *= 3;
  }
  N--;
  Node *T = Build(0, N, 1, 0);
  scanf("%s", s);
  int m = strlen(s);
  for (int i = 0; i < m; i++) {
    if (s[i] == 'R')
      Update(T);
    else {
      T->rev ^= 1;
      calc(T);
    }
  }
  print(T, 1, 0);
  for (int i = 0; i <= N; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}