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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 1001000;
int n, pw[20], pos[N];
char s[N];
struct node {
  node *s[3];
  int lab;
  bool rev;
  void setr() {
    rev ^= 1;
    swap(s[1], s[2]);
  }
  void push() {
    if (rev) {
      rep(j, 0, 3) if (s[j]) s[j]->setr();
      rev = 0;
    }
  }
} pool[N], *cur = pool, *rt;

int main() {
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  rt = cur++;
  rep(i, 0, pw[n]) {
    node *p = rt;
    rep(j, 0, n) {
      int w = i / pw[j] % 3;
      if (!p->s[w])
        p->s[w] = cur++;
      p = p->s[w];
    }
    p->lab = i;
  }
  scanf("%s", s);
  int m = strlen(s);
  rep(i, 0, m) if (s[i] == 'S') { rt->setr(); }
  else {
    node *p = rt;
    rep(j, 0, n) {
      p->push();
      node *t = p->s[2];
      p->s[2] = p->s[1];
      p->s[1] = p->s[0];
      p->s[0] = t;
      p = p->s[0];
    }
  }
  rep(i, 0, pw[n]) {
    node *p = rt;
    rep(j, 0, n) {
      int w = i / pw[j] % 3;
      p->push();
      p = p->s[w];
    }
    // printf("zz %d\\n",p-cur);
    pos[p->lab] = i;
    // printf("%d ",p->lab);
  }
  rep(i, 0, pw[n]) printf("%d%c", pos[i], " \\n"[i == pw[n] - 1]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 601000;
int n, pw[20], pos[N];
char s[N];
struct node {
  node *s[3];
  int lab;
  bool rev;
  void setr() {
    rev ^= 1;
    swap(s[1], s[2]);
  }
  void push() {
    if (rev) {
      rep(j, 0, 3) if (s[j]) s[j]->setr();
      rev = 0;
    }
  }
} pool[N], *cur = pool, *rt;

int main() {
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  rt = cur++;
  rep(i, 0, pw[n]) {
    node *p = rt;
    rep(j, 0, n) {
      int w = i / pw[j] % 3;
      if (!p->s[w])
        p->s[w] = cur++;
      p = p->s[w];
    }
    p->lab = i;
  }
  scanf("%s", s);
  int m = strlen(s);
  rep(i, 0, m) if (s[i] == 'S') { rt->setr(); }
  else {
    node *p = rt;
    rep(j, 0, n) {
      p->push();
      node *t = p->s[2];
      p->s[2] = p->s[1];
      p->s[1] = p->s[0];
      p->s[0] = t;
      p = p->s[0];
    }
  }
  rep(i, 0, pw[n]) {
    node *p = rt;
    rep(j, 0, n) {
      int w = i / pw[j] % 3;
      p->push();
      p = p->s[w];
    }
    // printf("zz %d\\n",p-cur);
    pos[p->lab] = i;
    // printf("%d ",p->lab);
  }
  rep(i, 0, pw[n]) printf("%d%c", pos[i], " \\n"[i == pw[n] - 1]);
}