alexjercan/bugnet huggingface dataset:
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize "-O3"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pip pair<int, pii>
#define F first
#define S second
#define pb push_back
#define pp pop_back

const int P = 1000000007;
const int N = 505;

using namespace std;

int n, p[N * N];
int d[N][N], o[N][N];

stack<pip> q;
void upd() {
  while (!q.empty()) {
    pip c = q.top();
    q.pop();
    c.F = -c.F;
    int i = c.S.F, j = c.S.S;
    if (d[i][j] < c.F)
      continue;
    d[i][j] = c.F;
    if (d[i][j] + o[i - 1][j] < d[i - 1][j])
      q.push({-d[i][j] - o[i - 1][j], {i - 1, j}});
    if (d[i][j] + o[i + 1][j] < d[i + 1][j])
      q.push({-d[i][j] - o[i + 1][j], {i + 1, j}});
    if (d[i][j] + o[i][j - 1] < d[i][j - 1])
      q.push({-d[i][j] - o[i][j - 1], {i, j - 1}});
    if (d[i][j] + o[i][j + 1] < d[i][j + 1])
      q.push({-d[i][j] - o[i][j + 1], {i, j + 1}});
  }
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> n;
  for (int i = 0; i < n * n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      o[i][j] = 1;
      d[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
    }
  }

  int ans = 0;

  for (int k = 0; k < n * n; k++) {
    int i = 1 + (p[k] - 1) / n, j = 1 + (p[k] - 1) % n;
    ans += d[i][j] - 1;
    o[i][j] = 0;
    q.push({-(d[i][j] - 1), {i, j}});
    upd();
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pip pair<int, pii>
#define F first
#define S second
#define pb push_back
#define pp pop_back

const int P = 1000000007;
const int N = 505;

using namespace std;

int n, p[N * N];
int d[N][N], o[N][N];

priority_queue<pip> q;
void upd() {
  while (!q.empty()) {
    pip c = q.top();
    q.pop();
    c.F = -c.F;
    int i = c.S.F, j = c.S.S;
    if (d[i][j] < c.F)
      continue;
    d[i][j] = c.F;
    if (d[i][j] + o[i - 1][j] < d[i - 1][j])
      q.push({-d[i][j] - o[i - 1][j], {i - 1, j}});
    if (d[i][j] + o[i + 1][j] < d[i + 1][j])
      q.push({-d[i][j] - o[i + 1][j], {i + 1, j}});
    if (d[i][j] + o[i][j - 1] < d[i][j - 1])
      q.push({-d[i][j] - o[i][j - 1], {i, j - 1}});
    if (d[i][j] + o[i][j + 1] < d[i][j + 1])
      q.push({-d[i][j] - o[i][j + 1], {i, j + 1}});
  }
}

int main() {

  cin >> n;
  for (int i = 0; i < n * n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      o[i][j] = 1;
      d[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
    }
  }

  int ans = 0;

  for (int k = 0; k < n * n; k++) {
    int i = 1 + (p[k] - 1) / n, j = 1 + (p[k] - 1) % n;
    ans += d[i][j] - 1;
    o[i][j] = 0;
    q.push({-(d[i][j] - 1), {i, j}});
    upd();
  }

  cout << ans << endl;

  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<bool>> occupied(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
    }
  }
  vector<int> bfs;
  bfs.reserve(n * n);
  const int mx[4] = {0, 1, 0, -1};
  const int my[4] = {1, 0, -1, 0};
  for (int _ = 0; _ < n * n; _++) {
    int cur;
    cin >> cur;
    cur--;
    int sx = cur / n, sy = cur % n;
    occupied[sx][sy] = false;
    dist[sx][sy]--;
    ans += dist[sx][sy];
    bfs.clear();
    bfs.push_back(cur);
    for (int s = 0; s < int(bfs.size()); s++) {
      int v = bfs[s];
      int x = v / n, y = v % n;
      for (int d = 0; d < 4; d++) {
        int nx = x + mx[d], ny = y + my[d];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int nd = occupied[nx][ny] + dist[x][y];
          if (nd < dist[nx][ny]) {
            dist[nx][ny] = nd;
            bfs.push_back(nx * n + ny);
          }
        }
      }
    }
  }
  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<bool>> occupied(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
    }
  }
  vector<int> bfs;
  bfs.reserve(n * n);
  const int mx[4] = {0, 1, 0, -1};
  const int my[4] = {1, 0, -1, 0};
  for (int _ = 0; _ < n * n; _++) {
    int cur;
    cin >> cur;
    cur--;
    int sx = cur / n, sy = cur % n;
    occupied[sx][sy] = false;
    dist[sx][sy]--;
    ans += dist[sx][sy];
    bfs.push_back(cur);
    for (int s = 0; s < int(bfs.size()); s++) {
      int v = bfs[s];
      int x = v / n, y = v % n;
      for (int d = 0; d < 4; d++) {
        int nx = x + mx[d], ny = y + my[d];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int nd = occupied[nx][ny] + dist[x][y];
          if (nd < dist[nx][ny]) {
            dist[nx][ny] = nd;
            bfs.push_back(nx * n + ny);
          }
        }
      }
    }
  }
  cout << ans << '\\n';
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;

const int N = 509;
int n, p[N * N];

int grid[N][N];
int here[N][N];

int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};

void dfs(int x, int y) {
  int cur = grid[x][y] + here[x][y];
  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i], yy = y + dy[i];
    if (grid[xx][yy] > cur) {
      grid[xx][yy] = cur;
      dfs(xx, yy);
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int l = i, r = n - 1 - i, u = j, d = n - 1 - j;
      grid[i][j] = min(l, min(r, min(u, d)));
      here[i][j] = 1;
    }
  }

  int ans = 0;

  for (int i = 0; i < n * n; ++i) {
    int cur = p[i] - 1, x = cur / n, y = cur % n;
    ans += grid[x][y];

    here[x][y] = 0;
    dfs(x, y);
  }

  cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;

const int N = 509;
int n, m;

struct fen {
  int bit[N * 2];

  int sum(int r) {
    int ret = 0;
    for (++r; r > 0; r -= r & -r) {
      ret += bit[r];
    }
    return ret;
  }

  void add(int i, int d) {
    for (++i; i < m; i += i & -i) {
      bit[i] += d;
    }
  }
};

int p[N];
fen cols[N], rows[N];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  m = 1;
  while (m <= n) {
    m *= 2;
  }

  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cols[i].add(j, 1);
      rows[i].add(j, 1);
    }
  }

  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int cur = p[i] - 1, x = cur / n, y = cur % n;
    int l = rows[x].sum(y) - 1, r = rows[x].sum(n - 1) - l - 1;
    int u = cols[y].sum(x) - 1, d = cols[y].sum(n - 1) - u - 1;
    ans += min(l, min(r, min(u, d)));
    rows[x].add(y, -1);
    cols[y].add(x, -1);
  }

  cout << ans << endl;

  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int ans[600010], A, B, n, f[600010], g[600010], len, nw, lenn, nww;
char s[200010], ss[200010];

int main() {
  scanf("%d%s", &n, s), len = strlen(s), A = B = 1;
  for (int i = 1; i <= (n + 1) / 2; i++)
    A *= 3;
  for (int i = (n + 1) / 2; i < n; i++)
    B *= 3;
  for (int i = 0; i < A * B; i++)
    f[i] = f[i / 3] * 3 + (i * 2 % 3), g[i] = (i + 1) % (A * B);
  for (int i = 0; i < A; i++) {
    nw = i, lenn = 0, ss[0] = 'P';
    for (int j = 0; j < len; j++)
      if (s[j] == 'R') {
        nw = g[nw];
        if (nw % A == 0)
          ss[++lenn] = 'R';
      } else {
        nw = f[nw];
        if (ss[lenn] == 'S')
          lenn--;
        else
          ss[++lenn] = 'S';
      }
    ans[i] = nw, nw %= A;
    for (int j = 1; j < B; j++) {
      nww = j;
      for (int k = 1; k <= lenn; k++)
        if (ss[k] == 'R')
          nww = g[nww];
        else
          nww = f[nww];
      ans[j * A + i] = nww % B * A + nw;
    }
  }
  for (int i = 0; i < A * B; i++)
    printf("%d ", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

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

bool bo, boo;
int ans[600010], A, B, n, f[600010], g[600010], len, nw;
char s[200010];

int main() {
  scanf("%d%s", &n, s), len = strlen(s), A = B = 1;
  for (int i = 1; i <= (n + 1) / 2; i++)
    A *= 3;
  for (int i = (n + 1) / 2; i < n; i++)
    B *= 3;
  for (int i = 0; i < A * B; i++)
    f[i] = f[i / 3] * 3 + (i * 2 % 3), g[i] = (i + 1) % (A * B);
  for (int i = 0; i < A; i++) {
    nw = i, bo = 0, boo = 0;
    for (int j = 0; j < len; j++)
      if (s[j] == 'R')
        nw = g[nw], boo |= (nw >= A);
      else
        nw = f[nw], bo ^= 1;
    if (!boo) {
      for (int j = 0; j < B; j++)
        ans[j * A + i] = (bo ? f[j] : j) * A + nw;
    } else {
      ans[i] = nw;
      for (int j = 1; j < B; j++) {
        nw = j * A + i;
        for (int k = 0; k < len; k++)
          if (s[k] == 'R')
            nw = g[nw];
          else
            nw = f[nw];
        ans[j * A + i] = nw;
      }
    }
  }
  for (int i = 0; i < A * B; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, rt, tot, sum[N], ans[N], t[N][3], lz[N];
char s[N];
void build(int &now, int n, int b, int c) {
  now = ++tot;
  if (n == 0) {
    sum[now] = c;
    return;
  }
  build(t[now][0], n - 1, b * 3, c);
  build(t[now][1], n - 1, b * 3, c + b);
  build(t[now][2], n - 1, b * 3, c + b + b);
}
void updata(int now, int n) {
  swap(t[now][1], t[now][2]);
  lz[t[now][0]] ^= 1;
  lz[t[now][1]] ^= 1;
  lz[t[now][2]] ^= 1;
  lz[now] = 0;
}
void solve(int now, int n) {
  if (n == 0)
    return;
  if (lz[now])
    updata(now, n);
  int a = t[now][0], b = t[now][1], c = t[now][2];
  t[now][1] = a;
  t[now][2] = b;
  t[now][0] = c;
  solve(t[now][0], n - 1);
}
void getans(int now, int n, int b, int c) {
  if (n == 0) {
    ans[sum[now]] = c;
    return;
  }
  if (lz[now])
    updata(now, n);
  getans(t[now][0], n - 1, b * 3, c);
  getans(t[now][1], n - 1, b * 3, c + b);
  getans(t[now][2], n - 1, b * 3, c + b + b);
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  build(rt, n, 1, 0);
  for (int i = 1; s[i]; i++) {
    if (s[i] == 'S')
      lz[rt] ^= 1;
    else
      solve(rt, n);
  }
  getans(rt, n, 1, 0);
  int sum = 1;
  for (int i = 1; i <= n; i++)
    sum *= 3;
  for (int i = 0; i < sum; i++)
    printf(i == sum - 1 ? "%d\\n" : "%d ", ans[i]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, rt, tot, sum[N], ans[N], t[N][3], lz[N];
char s[N];
void build(int &now, int n, int b, int c) {
  now = ++tot;
  if (n == 0) {
    sum[now] = c;
    return;
  }
  build(t[now][0], n - 1, b * 3, c);
  build(t[now][1], n - 1, b * 3, c + b);
  build(t[now][2], n - 1, b * 3, c + b + b);
}
void updata(int now, int n) {
  swap(t[now][1], t[now][2]);
  lz[t[now][0]] ^= 1;
  lz[t[now][1]] ^= 1;
  lz[t[now][2]] ^= 1;
  lz[now] = 0;
}
void solve(int now, int n) {
  if (n == 0)
    return;
  if (lz[now])
    updata(now, n);
  int a = t[now][0], b = t[now][1], c = t[now][2];
  t[now][1] = a;
  t[now][2] = b;
  t[now][0] = c;
  solve(t[now][0], n - 1);
}
void getans(int now, int n, int b, int c) {
  if (n == 0) {
    ans[sum[now]] = c;
    return;
  }
  if (lz[now])
    updata(now, n);
  getans(t[now][0], n - 1, b * 3, c);
  getans(t[now][1], n - 1, b * 3, c + b);
  getans(t[now][2], n - 1, b * 3, c + b + b);
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  build(rt, n, 1, 0);
  for (int i = 1; s[i]; i++) {
    if (s[i] == 'S')
      lz[rt] ^= 1;
    else
      solve(rt, n);
  }
  getans(rt, n, 1, 0);
  int sum = 1;
  for (int i = 1; i <= n; i++)
    sum *= 3;
  for (int i = 0; i < sum; i++)
    printf(i == sum - 1 ? "%d\\n" : "%d ", ans[i]);
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline int Pow(int a, int b) {
  return b ? Pow(a * a, b >> 1) * (b & 1 ? a : 1) : 1;
}
int main() {
  int N, now, len = 0, A[531441], B[531441];
  char T[200001], S[200001];
  now = scanf("%d %s", &N, T);
  len = strlen(T);
  fflush(stdin);
  int pw3 = Pow(3, N), x = Pow(3, N >> 1), y = Pow(3, N - (N >> 1));
  rep(i, pw3) B[i] = B[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, x) {
    int temp = i;
    now = 0;
    rep(j, len) {
      if (T[j] == 'S') {
        temp = B[temp];
        if (S[now] == 'S')
          --now;
        else
          S[++now] = 'S';
      } else {
        ++temp;
        if (temp == x)
          S[++now] = 'R', temp = 0;
      }
    }
    rep(j, y) {
      int keep = j;
      rep(k, now) {
        if (S[k + 1] == 'S')
          keep = B[keep];
        else if (S[k + 1] == 'R')
          if ((++keep) == y)
            keep = 0;
      }
      A[i + j * x] = temp + keep * x;
    }
  }
  rep(i, pw3) printf("%d%c", A[i], i + 1 == pw3 ? '\\n' : ' ');
  return fflush(stdout), 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline int Pow(int a, int b) {
  return b ? Pow(a * a, b >> 1) * (b & 1 ? a : 1) : 1;
}
int main() {
  int N, now, len = 0, A[200001], B[531441];
  char T[200001], S[200001];
  now = scanf("%d %s", &N, T);
  len = strlen(T);
  fflush(stdin);
  int pw3 = Pow(3, N), x = Pow(3, N >> 1), y = Pow(3, N - (N >> 1));
  rep(i, pw3) B[i] = B[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, x) {
    int temp = i;
    now = 0;
    rep(j, len) {
      if (T[j] == 'S') {
        temp = B[temp];
        if (S[now] == 'S')
          --now;
        else
          S[++now] = 'S';
      } else {
        ++temp;
        if (temp == x)
          S[++now] = 'R', temp = 0;
      }
    }
    rep(j, y) {
      int keep = j;
      rep(k, now) {
        if (S[k + 1] == 'S')
          keep = B[keep];
        else if (S[k + 1] == 'R')
          if ((++keep) == y)
            keep = 0;
      }
      A[i + j * x] = temp + keep * x;
    }
  }
  rep(i, pw3) printf("%d%c", A[i], i + 1 == pw3 ? '\\n' : ' ');
  return fflush(stdout), 0;
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
int N, x, y, z, sw, pw3[13] = {1}, A[531441] = {0}, trit[13][531441] = {0};
char T[200001];
void OUT(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
char *replace(char *s, const char *before, const char *after) {
  assert(s != (char *)NULL);
  assert(before != (char *)NULL);
  assert(after != (char *)NULL);
  const size_t szb = strlen(before), sza = strlen(after);
  char *pt = s;
  if (szb == 0)
    return s;
  for (;;) {
    pt = strstr(pt, before);
    if (pt == (char *)NULL)
      break;
    const char *remain = pt + szb;
    memmove(pt + sza, remain, strlen(remain) + 1);
    memcpy(pt, after, sza);
    pt += sza;
  }
  return s;
}
signed main(void) {
  x = scanf("%d %s", &N, T + 1);
  replace(T + 1, "SS", "");
  if (T[0] == '\\n')
    return 0;
  rep(i, N) pw3[i + 1] = 3 * pw3[i];
  rep(i, strlen(T + 1)) {
    if (T[i + 1] == 'S') {
      sw ^= 1;
      continue;
    }
    x = 0;
    rep(j, N) {
      if (sw)
        z = 1;
      else
        z = 2;
      y = x + pw3[j] * ((trit[j][x] + z) % 3);
      trit[j][x] = (trit[j][x] + z) % 3;
      x = y;
    }
  }
  rep(i, N) {
    rep(j, pw3[N]) {
      x = ((j / pw3[i]) % 3 - trit[i][j % pw3[i]] + 3) % 3;
      if (sw)
        x = (3 - x) % 3;
      A[j] += x * pw3[i];
    }
  }
  rep(i, pw3[N]) OUT(A[i]), putchar(' ');
}

Error: TLE

Fixed submission:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
int N, x, y, z, sw, pw3 = 1, A[531441] = {0}, B[531441] = {0}, S[531441] = {0};
char T[200001];
void OUT(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
signed main(void) {
  x = scanf("%d %s", &N, T);
  rep(i, N) {
    rep(j, pw3) rep(k, 3) B[j + k * pw3] = k;
    sw = 1;
    rep(p, strlen(T)) {
      if (T[p] == 'R') {
        y = S[p];
        rep(j, 3) {
          x = y + j * pw3, z = B[x] * sw;
          if (z == -1)
            S[p] = x;
          else if (z == 2)
            S[p] = x;
          B[x]++;
          if (sw < 0)
            B[x]++;
          B[x] %= 3;
        }
      } else
        sw = -sw;
    }
    rep(j, pw3) {
      y = A[j];
      rep(k, 3) {
        x = j + k * pw3;
        if (sw > 0)
          z = B[x] % 3;
        else
          z = (3 - B[x]) % 3;
        A[x] = y + z * pw3;
      }
    }
    pw3 *= 3;
  }
  rep(i, pw3) OUT(A[i]), putchar(' ');
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 1594324;
int n, m, rt, tot, ch[N][3], ans[N], id[N];
char s[N];
bool rev[N];
inline void build(int &now, CI dep = 0, CI st = 0) {
  now = ++tot;
  if (dep == n)
    return (void)(id[now] = st);
  int len = pow(3, dep);
  build(ch[now][0], dep + 1, st);
  build(ch[now][1], dep + 1, st + len);
  build(ch[now][2], dep + 1, st + (len << 1));
}
inline void rever(CI now) {
  swap(ch[now][1], ch[now][2]);
  rev[now] ^= 1;
}
inline void pushdown(CI now) {
  if (!rev[now])
    return;
  rev[now] = 0;
  for (RI i = 0; i < 3; ++i)
    rever(ch[now][i]);
}
inline void swaping(CI now, CI dep = 0) {
  pushdown(now);
  if (dep == n)
    return;
  swap(ch[now][1], ch[now][2]);
  swap(ch[now][0], ch[now][1]);
  swaping(ch[now][0], dep + 1);
}
inline void DFS(CI now, CI dep = 0, CI st = 0) {
  pushdown(now);
  if (dep == n)
    return (void)(ans[id[now]] = st);
  int len = pow(3, dep);
  DFS(ch[now][0], dep + 1, st);
  DFS(ch[now][1], dep + 1, st + len);
  DFS(ch[now][2], dep + 1, st + (len << 1));
}
int main() {
  RI i;
  scanf("%d%s", &n, s + 1);
  m = strlen(s + 1);
  for (build(rt), i = 1; i <= m; ++i)
    if (s[i] == 'S')
      rever(rt);
    else
      swaping(rt);
  for (DFS(rt), n = pow(3, n), i = 0; i < n; ++i)
    printf("%d ", ans[i]);
  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 531442;
int n, m, rt, tot, ch[N][3], ans[N], id[N];
char s[N];
bool rev[N];
inline void build(int &now, CI dep = 0, CI st = 0) {
  now = ++tot;
  if (dep == n)
    return (void)(id[now] = st);
  int len = pow(3, dep);
  build(ch[now][0], dep + 1, st);
  build(ch[now][1], dep + 1, st + len);
  build(ch[now][2], dep + 1, st + (len << 1));
}
inline void rever(CI now) {
  swap(ch[now][1], ch[now][2]);
  rev[now] ^= 1;
}
inline void pushdown(CI now) {
  if (!rev[now])
    return;
  rev[now] = 0;
  for (RI i = 0; i < 3; ++i)
    rever(ch[now][i]);
}
inline void swaping(CI now, CI dep = 0) {
  pushdown(now);
  if (dep == n)
    return;
  swap(ch[now][1], ch[now][2]);
  swap(ch[now][0], ch[now][1]);
  swaping(ch[now][0], dep + 1);
}
inline void DFS(CI now, CI dep = 0, CI st = 0) {
  pushdown(now);
  if (dep == n)
    return (void)(ans[id[now]] = st);
  int len = pow(3, dep);
  DFS(ch[now][0], dep + 1, st);
  DFS(ch[now][1], dep + 1, st + len);
  DFS(ch[now][2], dep + 1, st + (len << 1));
}
int main() {
  RI i;
  scanf("%d%s", &n, s + 1);
  m = strlen(s + 1);
  for (build(rt), i = 1; i <= m; ++i)
    if (s[i] == 'S')
      rever(rt);
    else
      swaping(rt);
  for (DFS(rt), n = pow(3, n), i = 0; i < n; ++i)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, po[13], S[201000], Ans[13][550000];
vector<int> U[13][177147];
char p[201000];
int main() {
  int i, j;
  po[0] = 1;
  for (i = 0; i < 12; i++)
    po[i + 1] = po[i] * 3;
  scanf("%d", &n);
  scanf("%s", p);
  for (i = 0; p[i]; i++) {
    S[i + 1] = S[i];
    if (p[i] == 'S')
      S[i + 1]++;
  }
  int m = i;
  for (i = 0; i < 3; i++) {
    int cur = i;
    for (j = 0; p[j]; j++) {
      if (cur == 2 && p[j] == 'R') {
        U[1][i].push_back(j);
      }
      if (p[j] == 'S') {
        cur = (3 - cur) % 3;
      } else
        cur = (cur + 1) % 3;
    }
    Ans[1][i] = cur;
  }
  for (i = 1; i < n; i++) {
    for (j = 0; j < po[i + 1]; j++) {
      int a = j / po[i];
      int b = j % po[i];
      int pv = 0;
      for (auto &t : U[i][b]) {
        int s = 0;
        if (t)
          s += S[t];
        if (pv)
          s -= S[pv];
        if (s & 1) {
          a = (3 - a) % 3;
        }
        a = (a + 1) % 3;
        if (a == 0) {
          if (i + 1 < n)
            U[i + 1][j].push_back(t);
        }
        pv = t + 1;
      }
      int s = S[m];
      if (pv)
        s -= S[pv];
      if (s & 1)
        a = (3 - a) % 3;
      Ans[i + 1][j] = Ans[i][b] + po[i] * a;
    }
  }
  for (i = 0; i < po[n]; i++) {
    printf("%d ", Ans[n][i]);
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n, po[13], S[201000], Ans[13][177147];
vector<int> U[13][177147];
char p[201000];
int main() {
  int i, j;
  po[0] = 1;
  for (i = 0; i < 12; i++)
    po[i + 1] = po[i] * 3;
  scanf("%d", &n);
  scanf("%s", p);
  for (i = 0; p[i]; i++) {
    S[i + 1] = S[i];
    if (p[i] == 'S')
      S[i + 1]++;
  }
  int m = i;
  for (i = 0; i < 3; i++) {
    int cur = i;
    for (j = 0; p[j]; j++) {
      if (cur == 2 && p[j] == 'R') {
        U[1][i].push_back(j);
      }
      if (p[j] == 'S') {
        cur = (3 - cur) % 3;
      } else
        cur = (cur + 1) % 3;
    }
    Ans[1][i] = cur;
  }
  for (i = 1; i < n; i++) {
    for (j = 0; j < po[i + 1]; j++) {
      int a = j / po[i];
      int b = j % po[i];
      int pv = 0;
      for (auto &t : U[i][b]) {
        int s = 0;
        if (t)
          s += S[t];
        if (pv)
          s -= S[pv];
        if (s & 1) {
          a = (3 - a) % 3;
        }
        a = (a + 1) % 3;
        if (a == 0)
          U[i + 1][j].push_back(t);
        pv = t + 1;
      }
      int s = S[m];
      if (pv)
        s -= S[pv];
      if (s & 1)
        a = (3 - a) % 3;
      Ans[i + 1][j] = Ans[i][b] + po[i] * a;
    }
  }
  for (i = 0; i < po[n]; i++) {
    printf("%d ", Ans[n][i]);
  }
}