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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define forall(it, c)                                                          \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c & 15);
  if (!f)
    x = -x;
}

const int maxn = 1 << 20;
int K, n;
int pw[15];
int ans[maxn];
char s[2 << 17];

struct tree {
  static const int maxn = 1 << 20;
  int go[maxn][3];
  int lz[maxn], id[maxn];
  int tot;
  void push(int x) {
    swap(go[x][1], go[x][2]);
    lz[x] ^= 1;
  }
  void pd(int x) {
    if (lz[x]) {
      push(go[x][0]);
      push(go[x][1]);
      push(go[x][2]);
      lz[x] = 0;
    }
  }
  void build(int &x, int lvl, int num) {
    x = ++tot;
    if (K == lvl) {
      id[x] = num;
      return;
    }
    build(go[x][0], lvl + 1, num);
    build(go[x][1], lvl + 1, num + pw[lvl]);
    build(go[x][2], lvl + 1, num + 2 * pw[lvl]);
  }
  void add(int x, int lvl) {
    if (K == lvl)
      return;
    pd(x);
    add(go[x][2], lvl + 1);
    swap(go[x][1], go[x][2]);
    swap(go[x][0], go[x][1]);
  }
  void prt(int x, int lvl, int num) {
    if (K == lvl) {
      ans[id[x]] = num;
      return;
    }
    pd(x);
    prt(go[x][0], lvl + 1, num);
    prt(go[x][1], lvl + 1, num + pw[lvl]);
    prt(go[x][2], lvl + 1, num + 2 * pw[lvl]);
  }
} tr;

int main() {
  read(K);
  rep(i, K + 1) pw[i] = i ? 3 * pw[i - 1] : 1;
  n = pw[K];
  int rt = 0;
  tr.build(rt, 0, 0);
  scanf("%s", s);
  int m = strlen(s);
  rep(i, m) {
    if (s[i] == 'S')
      tr.push(rt);
    else
      tr.add(rt, 0);
  }
  tr.prt(rt, 0, 0);
  rep(i, n) printf("%d ", ans[i]);
  puts("");
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define forall(it, c)                                                          \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c & 15);
  if (!f)
    x = -x;
}

const int maxn = 531441;
int K, n;
int pw[15];
int ans[maxn];
char s[2 << 17];

struct tree {
  static const int maxn = 531441;
  int go[maxn][3];
  int lz[maxn], id[maxn];
  int tot;
  void push(int x) {
    swap(go[x][1], go[x][2]);
    lz[x] ^= 1;
  }
  void pd(int x) {
    if (lz[x]) {
      push(go[x][0]);
      push(go[x][1]);
      push(go[x][2]);
      lz[x] = 0;
    }
  }
  void build(int &x, int lvl, int num) {
    x = ++tot;
    if (K == lvl) {
      id[x] = num;
      return;
    }
    build(go[x][0], lvl + 1, num);
    build(go[x][1], lvl + 1, num + pw[lvl]);
    build(go[x][2], lvl + 1, num + 2 * pw[lvl]);
  }
  void add(int x, int lvl) {
    if (K == lvl)
      return;
    pd(x);
    add(go[x][2], lvl + 1);
    swap(go[x][1], go[x][2]);
    swap(go[x][0], go[x][1]);
  }
  void prt(int x, int lvl, int num) {
    if (K == lvl) {
      ans[id[x]] = num;
      return;
    }
    pd(x);
    prt(go[x][0], lvl + 1, num);
    prt(go[x][1], lvl + 1, num + pw[lvl]);
    prt(go[x][2], lvl + 1, num + 2 * pw[lvl]);
  }
} tr;

int main() {
  read(K);
  rep(i, K + 1) pw[i] = i ? 3 * pw[i - 1] : 1;
  n = pw[K];
  int rt = 0;
  tr.build(rt, 0, 0);
  scanf("%s", s);
  int m = strlen(s);
  rep(i, m) {
    if (s[i] == 'S')
      tr.push(rt);
    else
      tr.add(rt, 0);
  }
  tr.prt(rt, 0, 0);
  rep(i, n) printf("%d ", ans[i]);
  puts("");
  return 0;
}