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

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, p3[13], L, tot;
string t;
int son[797170][3], laz[797170], leaf[797170], p[797170], lid[797170];
void push_down(int x) {
  if (leaf[x]) {
    laz[x] = 0;
    return;
  }
  if (laz[x]) {
    laz[x] = 0;
    laz[son[x][0]] ^= 1;
    laz[son[x][1]] ^= 1;
    laz[son[x][2]] ^= 1;
    swap(son[x][1], son[x][2]);
  }
  return;
}
void update(int x) {
  if (leaf[x])
    return;
  push_down(x);
  update(son[x][2]);
  int tmp = son[x][0];
  son[x][0] = son[x][2];
  son[x][2] = son[x][1];
  son[x][1] = tmp;
  return;
}
void build(int x, int cu, int level) {
  if (level == n) {
    leaf[x] = 1;
    lid[x] = cu;
    return;
  }
  son[x][0] = ++tot;
  build(son[x][0], cu + p3[level] * 0, level + 1);
  son[x][1] = ++tot;
  build(son[x][1], cu + p3[level] * 1, level + 1);
  son[x][2] = ++tot;
  build(son[x][2], cu + p3[level] * 2, level + 1);
  return;
}
void dfs(int x, int cu, int level) {
  if (leaf[x]) {
    p[lid[x]] = cu;
    return;
  }
  push_down(x);
  dfs(son[x][0], cu + p3[level] * 0, level + 1);
  dfs(son[x][1], cu + p3[level] * 1, level + 1);
  dfs(son[x][2], cu + p3[level] * 2, level + 1);
  return;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> t;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  tot = 1;
  build(1, 0, 0);
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == 'R')
      update(1);
    else
      laz[1] ^= 1;
  }
  dfs(1, 0, 0);
  for (int i = 0; i < p3[n]; i++)
    cout << p[i] << ' ';
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, p3[13], tot;
string t;
int son[797170][3], laz[797170], leaf[797170], p[797170];
void push_down(int x) {
  if (leaf[x]) {
    laz[x] = 0;
    return;
  }
  if (laz[x]) {
    laz[x] = 0;
    laz[son[x][0]] ^= 1;
    laz[son[x][1]] ^= 1;
    laz[son[x][2]] ^= 1;
    swap(son[x][1], son[x][2]);
  }
  return;
}
void update(int x) {
  if (leaf[x])
    return;
  push_down(x);
  update(son[x][2]);
  int tmp = son[x][0];
  son[x][0] = son[x][2];
  son[x][2] = son[x][1];
  son[x][1] = tmp;
  return;
}
void build(int x, int level) {
  if (level == n) {
    leaf[x] = 1;
    return;
  }
  son[x][0] = ++tot;
  build(son[x][0], level + 1);
  son[x][1] = ++tot;
  build(son[x][1], level + 1);
  son[x][2] = ++tot;
  build(son[x][2], level + 1);
  return;
}
void dfs(int x, int cu, int level) {
  if (leaf[x]) {
    p[x - (p3[n] - 1) / 2] = cu;
    return;
  }
  push_down(x);
  dfs(son[x][0], cu + p3[level] * 0, level + 1);
  dfs(son[x][1], cu + p3[level] * 1, level + 1);
  dfs(son[x][2], cu + p3[level] * 2, level + 1);
  return;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> t;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  tot = 1;
  build(1, 0);
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == 'R')
      update(1);
    else
      laz[1] ^= 1;
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= p3[n]; i++)
    cout << p[i] << ' ';
  cout << endl;
  return 0;
}