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

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 1e6 + 5;
int ch[N][3], n, m, t, cnt, ans[N];
bool tg[N];
char s[N];

void build(int x, int v, int fl, int pw) {
  if (fl == n)
    return;
  for (int i = 0; i < 3; i++)
    ch[x][i] = fl + 1 < n ? ++cnt : v + i * pw,
    build(fl + 1 < n ? cnt : v + i * pw, v + i * pw, fl + 1, pw * 3);
}

void dfs(int x, int v, int fl, int pw) {
  if (fl == n) {
    ans[x] = v;
    return;
  }
  if (tg[x])
    swap(ch[x][1], ch[x][2]);
  for (int i = 0; i < 3; i++)
    tg[ch[x][i]] ^= tg[x];
  tg[x] = 0;
  for (int i = 0; i < 3; i++)
    dfs(ch[x][i], v + i * pw, fl + 1, pw * 3);
}

void upr(int x, int fl) {
  if (fl == n)
    return;
  if (tg[x])
    swap(ch[x][1], ch[x][2]);
  for (int i = 0; i < 3; i++)
    tg[ch[x][i]] ^= tg[x];
  tg[x] = 0;
  swap(ch[x][1], ch[x][2]);
  swap(ch[x][0], ch[x][1]);
  upr(ch[x][0], fl + 1);
}

void ups() { tg[m] ^= 1; }

signed main() {
  read(n);
  scanf("%s", s + 1);
  t = strlen(s + 1);
  m = 1;
  for (int i = 1; i <= n; i++)
    m *= 3;
  build(cnt = m, 0, 0, 1);
  for (int i = 1; i <= t; i++)
    if (s[i] == 'R')
      upr(m, 0);
    else
      ups();
  dfs(m, 0, 0, 1);
  for (int i = 0; i < m; i++)
    write(ans[i]), putchar(' ');
}

Error: WA

Fixed submission:

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 3e5 + 5;
int ch[N][3], n, m, t, cnt, ans[N];
bool tg[N];
char s[N];

void build(int x, int v, int fl, int pw) {
  if (fl == n)
    return;
  for (int i = 0; i < 3; i++)
    ch[x][i] = fl + 1 < n ? ++cnt : v + i * pw,
    build(fl + 1 < n ? cnt : v + i * pw, v + i * pw, fl + 1, pw * 3);
}

void dfs(int x, int v, int fl, int pw) {
  if (tg[x])
    swap(ch[x][1], ch[x][2]);
  if (fl == n) {
    ans[x] = v;
    return;
  }
  for (int i = 0; i < 3; i++)
    tg[ch[x][i]] ^= tg[x], dfs(ch[x][i], v + i * pw, fl + 1, pw * 3);
  tg[x] = 0;
}

void upr(int x, int fl) {
  if (tg[x])
    swap(ch[x][1], ch[x][2]);
  if (fl == n)
    return;
  for (int i = 0; i < 3; i++)
    tg[ch[x][i]] ^= tg[x];
  tg[x] = 0;
  swap(ch[x][1], ch[x][2]);
  swap(ch[x][0], ch[x][1]);
  upr(ch[x][0], fl + 1);
}

void ups() { tg[m] ^= 1; }

signed main() {
  read(n);
  scanf("%s", s + 1);
  t = strlen(s + 1);
  m = 1;
  for (int i = 1; i <= n; i++)
    m *= 3;
  build(cnt = m, 0, 0, 1);
  for (int i = 1; i <= t; i++)
    if (s[i] == 'R')
      upr(m, 0);
    else
      ups();
  dfs(m, 0, 0, 1);
  for (int i = 0; i < m; i++)
    write(ans[i]), putchar(' ');
}