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

#include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v)                                                               \\
  for (auto i : v)                                                             \\
    printf("%lld ", i);                                                        \\
  puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}

const int N = 3e6 + 10;
int n, m, pw[N];
char s[N];

int cnt, son[N][3], p[N];
inline void dfs(int u, int dep, int x) {
  if (dep == n)
    return p[u] = x, void(0);
  son[u][0] = ++cnt, dfs(son[u][0], dep + 1, x);
  son[u][1] = ++cnt, dfs(son[u][1], dep + 1, x + pw[dep]);
  son[u][2] = ++cnt, dfs(son[u][2], dep + 1, x + 2 * pw[dep]);
}

int tag[N];
inline void upd(int u) { swap(son[u][1], son[u][2]), tag[u] ^= 1; }
inline void push_down(int u) {
  if (!tag[u])
    return;
  FOR(i, 0, 3) upd(son[u][i]);
  tag[u] = 0;
}
inline void dfs(int u, int dep) {
  if (dep == n)
    return;
  push_down(u);
  int tmp = son[u][2];
  son[u][2] = son[u][1], son[u][1] = son[u][0], son[u][0] = tmp;
  dfs(son[u][0], dep + 1);
}

int ans[N];
inline void print(int u, int dep, int x) {
  if (dep == n)
    return ans[p[u]] = x, void(0);
  push_down(u);
  print(son[u][0], dep + 1, x);
  print(son[u][1], dep + 1, x + pw[dep]);
  print(son[u][2], dep + 1, x + 2 * pw[dep]);
}

int main() {
  n = read(), scanf("%s", s + 1), m = strlen(s + 1);
  pw[0] = 1;
  For(i, 1, n) pw[i] = pw[i - 1] * 3;
  dfs(0, 0, 0);
  For(i, 1, m) {
    if (s[i] == 'S')
      upd(0);
    else
      dfs(0, 0);
  }
  print(0, 0, 0);
  FOR(i, 0, pw[n]) printf("%d ", ans[i]);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v)                                                               \\
  for (auto i : v)                                                             \\
    printf("%lld ", i);                                                        \\
  puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}

const int N = 550010;
int n, m, pw[N];
char s[N];

int cnt, son[N][3], p[N];
inline void dfs(int u, int dep, int x) {
  if (dep == n)
    return p[u] = x, void(0);
  son[u][0] = ++cnt, dfs(son[u][0], dep + 1, x);
  son[u][1] = ++cnt, dfs(son[u][1], dep + 1, x + pw[dep]);
  son[u][2] = ++cnt, dfs(son[u][2], dep + 1, x + 2 * pw[dep]);
}

int tag[N];
inline void upd(int u) { swap(son[u][1], son[u][2]), tag[u] ^= 1; }
inline void push_down(int u) {
  if (!tag[u])
    return;
  FOR(i, 0, 3) upd(son[u][i]);
  tag[u] = 0;
}
inline void dfs(int u, int dep) {
  if (dep == n)
    return;
  push_down(u);
  int tmp = son[u][2];
  son[u][2] = son[u][1], son[u][1] = son[u][0], son[u][0] = tmp;
  dfs(son[u][0], dep + 1);
}

int ans[N];
inline void print(int u, int dep, int x) {
  if (dep == n)
    return ans[p[u]] = x, void(0);
  push_down(u);
  print(son[u][0], dep + 1, x);
  print(son[u][1], dep + 1, x + pw[dep]);
  print(son[u][2], dep + 1, x + 2 * pw[dep]);
}

int main() {
  n = read(), scanf("%s", s + 1), m = strlen(s + 1);
  pw[0] = 1;
  For(i, 1, n) pw[i] = pw[i - 1] * 3;
  dfs(0, 0, 0);
  For(i, 1, m) {
    if (s[i] == 'S')
      upd(0);
    else
      dfs(0, 0);
  }
  print(0, 0, 0);
  FOR(i, 0, pw[n]) printf("%d ", ans[i]);
}