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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 1e6 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
// inline char gc() {return
// pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc() { return getchar(); }
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int t[MAXN], s[3][MAXN], p[MAXN], d[MAXN], Fpow[20], N, len, n, tot = 1,
                                                                ans[MAXN];
char ch[MAXN];
struct Node {
  int id, x, d;
};
inline void Build() {
  queue<Node> q;
  q.push((Node){1, 0, 0});
  while (!q.empty()) {
    int x = q.front().id;
    d[x] = q.front().d, p[x] = q.front().x, q.pop();
    if (d[x] < n)
      Rep(i, 0, 2) s[i][x] = ++tot,
                   q.push((Node){s[i][x], p[x] + Fpow[d[x]] * i, d[x] + 1});
  }
}
inline void pushr(int x) { swap(s[1][x], s[2][x]), t[x] ^= 1; }
inline void pushdown(int x) {
  if (t[x])
    pushr(s[0][x]), pushr(s[1][x]), pushr(s[2][x]), t[x] = 0;
}
inline void Add(int x) {
  pushdown(x);
  if (d[x] == n)
    return;
  int s0 = s[0][x], s1 = s[1][x], s2 = s[2][x];
  s[1][x] = s0, s[2][x] = s1, s[0][x] = s2, Add(s[0][x]);
}
void dfs(int x, int val, int d) {
  if (x != 1)
    ans[p[x]] = val;
  pushdown(x);
  if (d == n)
    return;
  Rep(i, 0, 2) dfs(s[i][x], val + Fpow[d] * i, d + 1);
}
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  n = read(), scanf("%s", ch + 1), len = strlen(ch + 1);
  Fpow[0] = 1;
  Rep(i, 1, n) Fpow[i] = Fpow[i - 1] * 3;
  N = Fpow[n];
  Build();
  Rep(i, 1, len) {
    if (ch[i] == 'S')
      pushr(1);
    else if (ch[i] == 'R')
      Add(1);
  }
  dfs(1, 0, 0);
  Rep(i, 0, N - 1) cout << ans[i] << ' ';
  cout << "\\n";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 6e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
// inline char gc() {return
// pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
inline char gc() { return getchar(); }
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int t[MAXN], s[3][MAXN], p[MAXN], d[MAXN], Fpow[20], N, len, n, tot = 1,
                                                                ans[MAXN];
char ch[MAXN];
struct Node {
  int id, x, d;
};
inline void Build() {
  queue<Node> q;
  q.push((Node){1, 0, 0});
  while (!q.empty()) {
    int x = q.front().id;
    d[x] = q.front().d, p[x] = q.front().x, q.pop();
    if (d[x] < n)
      Rep(i, 0, 2) s[i][x] = ++tot,
                   q.push((Node){s[i][x], p[x] + Fpow[d[x]] * i, d[x] + 1});
  }
}
inline void pushr(int x) { swap(s[1][x], s[2][x]), t[x] ^= 1; }
inline void pushdown(int x) {
  if (t[x])
    pushr(s[0][x]), pushr(s[1][x]), pushr(s[2][x]), t[x] = 0;
}
inline void Add(int x) {
  pushdown(x);
  if (d[x] == n)
    return;
  int s0 = s[0][x], s1 = s[1][x], s2 = s[2][x];
  s[1][x] = s0, s[2][x] = s1, s[0][x] = s2, Add(s[0][x]);
}
void dfs(int x, int val, int d) {
  if (x != 1)
    ans[p[x]] = val;
  pushdown(x);
  if (d == n)
    return;
  Rep(i, 0, 2) dfs(s[i][x], val + Fpow[d] * i, d + 1);
}
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  n = read(), scanf("%s", ch + 1), len = strlen(ch + 1);
  Fpow[0] = 1;
  Rep(i, 1, n) Fpow[i] = Fpow[i - 1] * 3;
  N = Fpow[n];
  Build();
  dfs(1, 0, 0);
  Rep(i, 0, N - 1) cout << ans[i] << " ";
  cout << '\\n';
  Rep(i, 1, len) {
    if (ch[i] == 'S')
      pushr(1);
    else if (ch[i] == 'R')
      Add(1);
  }
  dfs(1, 0, 0);
  Rep(i, 0, N - 1) cout << ans[i] << ' ';
  cout << "\\n";
  return 0;
}