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

// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 2000010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int val[N][3], ch[N][3], tot, lazy[N];
int n;
char s[N];
int ans[N], cnt;
int rev[N];
inline void build() {
  tot = 1;
  for (register int i = 1; i <= n + 1; i++)
    tot *= 3;
  tot = (tot - 1) / 2;
  for (register int i = 1; i <= tot; i++) {
    if (i * 3 <= tot) {
      ch[i][0] = i * 3 - 1;
      ch[i][1] = i * 3;
      ch[i][2] = i * 3 + 1;
      val[i][0] = 0;
      val[i][1] = 1;
      val[i][2] = 2;
    }
  }
}
inline void pushdown(int now) {
  if (lazy[now]) {
    lazy[ch[now][0]] ^= 1;
    lazy[ch[now][1]] ^= 1;
    lazy[ch[now][2]] ^= 1;
    int x = 0, y = 0;
    for (register int i = 0; i < 3; i++) {
      if (val[now][i] == 1)
        x = i;
      if (val[now][i] == 2)
        y = i;
    }
    swap(val[now][x], val[now][y]);
    lazy[now] = 0;
  }
}
void update(int now) {
  if (now * 3 > tot)
    return;
  pushdown(now);
  int pos = 0;
  for (register int i = 0; i < 3; i++) {
    val[now][i]++;
    if (val[now][i] == 3) {
      val[now][i] = 0;
      pos = ch[now][i];
    }
  }
  update(pos);
}
void dfs(int now, int v, int p) {
  if (now * 3 > tot) {
    ans[cnt++] = v;
    return;
  }
  pushdown(now);
  for (register int i = 0; i < 3; i++)
    dfs(ch[now][i], v + val[now][i] * p, p * 3);
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  scanf("%s", s + 1);
  build();
  int len = strlen(s + 1);
  for (register int i = 1; i <= len; i++) {
    if (s[i] == 'R')
      update(1);
    else
      lazy[1] ^= 1;
  }
  dfs(1, 0, 1);
  //	for(register int i=0;i<cnt;i++)cout<<ans[i]<<" ";cout<<endl;
  for (register int i = 1; i < cnt; i++)
    rev[i] = rev[i / 3] / 3 + (cnt / 3) * (i % 3);
  for (register int i = 0; i < cnt; i++)
    if (i < rev[i])
      swap(ans[i], ans[rev[i]]);
  for (register int i = 0; i < cnt; i++)
    printf("%d ", ans[i]);
  return 0;
}


Error: WA

Fixed submission:

// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int val[N][3], ch[N][3], tot, lazy[N];
int n;
char s[N];
int ans[N];
inline void build() {
  tot = 1;
  for (register int i = 1; i <= n + 1; i++)
    tot *= 3;
  tot = (tot - 1) / 2;
  for (register int i = 1; i <= tot; i++) {
    if (i * 3 <= tot) {
      ch[i][0] = i * 3 - 1;
      ch[i][1] = i * 3;
      ch[i][2] = i * 3 + 1;
      val[i][0] = 0;
      val[i][1] = 1;
      val[i][2] = 2;
    }
  }
}
inline void pushdown(int now) {
  if (lazy[now]) {
    lazy[ch[now][0]] ^= 1;
    lazy[ch[now][1]] ^= 1;
    lazy[ch[now][2]] ^= 1;
    int x = 0, y = 0;
    for (register int i = 0; i < 3; i++) {
      if (val[now][i] == 1)
        x = ch[now][i];
      if (val[now][i] == 2)
        y = ch[now][i];
    }
    swap(val[now][x], val[now][y]);
  }
}
void update(int now) {
  pushdown(now);
  if (now * 3 > tot)
    return;
  for (register int i = 0; i < 3; i++) {
    val[now][i]++;
    if (val[now][i] == 3) {
      val[now][i] = 0;
      update(ch[now][i]);
    }
  }
}
void dfs(int now, int v, int p) {
  if (now * 3 > tot) {
    ans[++ans[0]] = v;
    return;
  }
  for (register int i = 0; i < 3; i++)
    dfs(ch[now][i], v + val[now][i] * p, p * 3);
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  scanf("%s", s + 1);
  build();
  int len = strlen(s + 1);
  for (register int i = 1; i <= len; i++) {
    if (s[i] == 'S')
      update(1);
    else
      lazy[1] ^= 1;
  }
  dfs(1, 0, 1);
  for (register int i = 1; i <= ans[0]; i++)
    printf("%d ", ans[i]);
  return 0;
}