alexjercan/bugnet huggingface dataset:
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 2e6 + 50;
int n, p[N];
char S[N];
int ch[N][3], tag[N], vl[N], nd, pw[20];
int build(int d, int v) {
  if (!d)
    return vl[++nd] = v, nd;
  int x = ++nd;
  ch[x][0] = build(d - 1, v);
  ch[x][1] = build(d - 1, v + pw[n - d]);
  ch[x][2] = build(d - 1, v + 2 * pw[n - d]);
  return x;
}
void put(int x) {
  if (!x)
    return;
  tag[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
void down(int x) {
  if (tag[x])
    put(ch[x][0]), put(ch[x][1]), put(ch[x][2]), tag[x] = 0;
}
void work(int x, int d) {
  if (!d)
    return;
  down(x);
  int v = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = ch[x][0];
  ch[x][0] = v;
  work(v, d - 1);
}
void dfs(int x, int d, int v = 0) {
  if (!d)
    return p[vl[x]] = v, void();
  down(x);
  dfs(ch[x][0], d - 1, v);
  dfs(ch[x][1], d - 1, v + pw[n - d]);
  dfs(ch[x][2], d - 1, v + 2 * pw[n - d]);
}
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  scanf("%s", S);
  int m = strlen(S);
  build(n, 0);
  for (int i = 0; i < m; i++) {
    if (S[i] == 'R')
      work(1, n);
    if (S[i] == 'S')
      put(1);
  }
  dfs(1, n);
  for (int i = 0; i < pw[n]; i++)
    cout << p[i] << " ";
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 6e5 + 50;
int n, p[N];
char S[N];
int ch[N][3], tag[N], vl[N], nd, pw[20];
int build(int d, int v) {
  if (!d)
    return vl[++nd] = v, nd;
  int x = ++nd;
  ch[x][0] = build(d - 1, v);
  ch[x][1] = build(d - 1, v + pw[n - d]);
  ch[x][2] = build(d - 1, v + 2 * pw[n - d]);
  return x;
}
void put(int x) {
  if (!x)
    return;
  tag[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
void down(int x) {
  if (tag[x])
    put(ch[x][0]), put(ch[x][1]), put(ch[x][2]), tag[x] = 0;
}
void work(int x, int d) {
  if (!d)
    return;
  down(x);
  int v = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = ch[x][0];
  ch[x][0] = v;
  work(v, d - 1);
}
void dfs(int x, int d, int v = 0) {
  if (!d)
    return p[vl[x]] = v, void();
  down(x);
  dfs(ch[x][0], d - 1, v);
  dfs(ch[x][1], d - 1, v + pw[n - d]);
  dfs(ch[x][2], d - 1, v + 2 * pw[n - d]);
}
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  scanf("%s", S);
  int m = strlen(S);
  build(n, 0);
  for (int i = 0; i < m; i++) {
    if (S[i] == 'R')
      work(1, n);
    if (S[i] == 'S')
      put(1);
  }
  dfs(1, n);
  for (int i = 0; i < pw[n]; i++)
    cout << p[i] << " ";
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef vector<bool> vi;
const int maxn = 1e6 + 10;
char s[maxn];
int n, ans[maxn], len, p3[13];
void solve(int x, int v, vi &a) {
  int res[3];
  vi tmp[3];
  for (int i = 0; i < 3; i++) {
    tmp[i].clear();
    int now = i;
    for (int j = 0; j < a.size(); j++) {
      if (a[j]) {
        if (now > 0)
          now = 3 - now;
        tmp[i].pb(1);
        int m = tmp[i].size();
        if (m >= 2 && tmp[i][m - 1] && tmp[i][m - 2])
          tmp[i].resize(m - 2);
      } else {
        now++;
        if (now == 3) {
          now = 0;
          tmp[i].pb(0);
        }
      }
    }
    res[i] = now;
  }
  for (int i = 0; i < p3[n] / p3[x]; i++) {
    ans[i * p3[x] + v] += p3[x] * res[i % 3];
  }
  if (x < n - 1) {
    for (int i = 0; i < 3; i++)
      solve(x + 1, v + i * p3[x], tmp[i]);
  }
}
int main() {
  cin >> n >> s;
  len = strlen(s);
  vector<vi> dp;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  vi res;
  res.clear();
  for (int i = 0; i < len; i++) {
    res.pb(s[i] == 'S');
    int m = res.size();
    if (m >= 2 && res[m - 1] && res[m - 2])
      res.resize(m - 2);
  }
  solve(0, 0, res);
  for (int i = 0; i < p3[n]; i++)
    cout << ans[i] << ' ';
  cout << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef vector<bool> vi;
const int maxn = 2e5 + 10;
char s[maxn];
int n, ans[maxn], len, p3[13];
void solve(int x, int v, vi &a) {
  int res[3];
  vi tmp[3];
  for (int i = 0; i < 3; i++) {
    tmp[i].clear();
    int now = i;
    for (int j = 0; j < a.size(); j++) {
      if (a[j]) {
        if (now > 0)
          now = 3 - now;
        tmp[i].pb(1);
        int m = tmp[i].size();
        if (m >= 2 && tmp[i][m - 1] && tmp[i][m - 2])
          tmp[i].resize(m - 2);
      } else {
        now++;
        if (now == 3) {
          now = 0;
          tmp[i].pb(0);
        }
      }
    }
    res[i] = now;
  }
  for (int i = 0; i < p3[n] / p3[x]; i++) {
    ans[i * p3[x] + v] += p3[x] * res[i % 3];
  }
  if (x < n - 1) {
    for (int i = 0; i < 3; i++)
      solve(x + 1, v + i * p3[x], tmp[i]);
  }
}
int main() {
  cin >> n >> s;
  len = strlen(s);
  vector<vi> dp;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  vi res;
  res.clear();
  for (int i = 0; i < len; i++) {
    res.pb(s[i] == 'S');
    int m = res.size();
    if (m >= 2 && res[m - 1] && res[m - 2])
      res.resize(m - 2);
  }
  solve(0, 0, res);
  for (int i = 0; i < p3[n]; i++)
    cout << ans[i] << ' ';
  cout << endl;
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

int lim = 1, f[600001], n, nl, top, fin[1000001];
char T[1000001], ope[1000001];
void write(int x) {
  if (x > 9)
    write(x / 10);
  putchar((x % 10) + '0');
}
int main() {
  scanf("%d", &n);
  int v1 = 1, v2 = 1;
  for (int i = 1; i <= n; i++)
    lim *= 3;
  for (int i = 1; i <= (n >> 1); i++)
    v1 *= 3;
  for (int i = 1 + (n >> 1); i <= n; i++)
    v2 *= 3;
  for (int i = 0; i < lim; i++)
    f[i] = f[i / 3] * 3 + (3 - i % 3) % 3;
  scanf("%s", T + 1);
  nl = strlen(T + 1);
  for (int i = 0; i < v1; i++) {
    int tem = i;
    top = 0;
    for (int j = 1; j <= nl; j++)
      if (T[j] == 'S') {
        tem = f[tem];
        (ope[top] == 'S') ? --top : ope[++top] = 'S';
      } else {
        ++tem;
        if (tem == v1)
          ope[++top] = 'R', tem = 0;
      }
    for (int j = 0; j < v2; j++) {
      int tem2 = j;
      for (int k = 1; k <= top; k++)
        if (ope[k] == 'S')
          tem2 = f[tem2];
        else if (ope[k] == 'R')
          if ((++tem2) == v2)
            tem2 = 0;
      fin[i + j * v1] = tem2 * v1 + tem;
    }
  }
  for (int i = 0; i < lim; i++)
    write(fin[i]), putchar(' ');
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

int lim = 1, f[600001], n, nl, top, fin[1000001];
char T[1000001], ope[1000001];
void write(int x) {
  if (x > 9)
    write(x / 10);
  putchar((x % 10) + '0');
}
int main() {
  scanf("%d", &n);
  int v1 = 1, v2 = 1;
  for (int i = 1; i <= n; i++)
    lim *= 3;
  for (int i = 1; i <= (n >> 1); i++)
    v1 *= 3;
  for (int i = 1 + (n >> 1); i <= n; i++)
    v2 *= 3;
  for (int i = 0; i < lim; i++)
    f[i] = f[i / 3] * 3 + (3 - i % 3) % 3;
  scanf("%s", T + 1);
  nl = strlen(T + 1);
  for (int i = 0; i < v1; i++) {
    int tem = i;
    top = 0;
    for (int j = 1; j <= nl; j++)
      if (T[j] == 'S') {
        tem = f[tem];
        (ope[top] == 'S') ? --top : ope[++top] = 'S';
      } else {
        if (tem + 1 == v1)
          ope[++top] = 'R';
        (++tem) %= v1;
      }
    for (int j = 0; j < v2; j++) {
      int tem2 = j;
      for (int k = 1; k <= top; k++)
        if (ope[k] == 'S')
          tem2 = f[tem2];
        else if (ope[k] == 'R')
          (++tem2) %= v2;
      fin[i + j * v1] = tem2 * v1 + tem;
    }
  }
  for (int i = 0; i < lim; i++)
    write(fin[i]), putchar(' ');
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int ch[10000005][3], ed[10000005], cnt = 1, ans[10000005];
bool rev[10000005];
char s[200005];
void pushr(int x) {
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}
void pushdown(int x) {
  if (!rev[x])
    return;
  for (int i = 0; i <= 2; i++)
    if (ch[x][i])
      pushr(ch[x][i]);
  rev[x] = 0;
}
void dfs(int x) {
  pushdown(x);
  swap(ch[x][0], ch[x][2]), swap(ch[x][1], ch[x][2]);
  if (ch[x][0])
    dfs(ch[x][0]);
}
void get(int x, int now, int pw) {
  pushdown(x);
  if (ed[x] != 0xffffffff)
    ans[ed[x]] = now;
  if (ch[x][0])
    get(ch[x][0], now, pw * 3);
  if (ch[x][1])
    get(ch[x][1], now + pw, pw * 3);
  if (ch[x][2])
    get(ch[x][2], now + 2 * pw, pw * 3);
}
int main() {
  int n, m = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    m *= 3;
  memset(ed, 0xff, sizeof(ed));
  for (int i = 0; i < m; i++) {
    int now = 1, tmp = i;
    for (int k = 0; k < n; k++) {
      int t = tmp % 3;
      tmp /= 3;
      if (!ch[now][t])
        ch[now][t] = ++cnt;
      now = ch[now][t];
    }
    ed[now] = i;
  }
  scanf("%s", s + 1);
  int q = strlen(s + 1);
  for (int i = 1; i <= q; i++) {
    if (s[i] == 'S')
      pushr(1);
    else
      dfs(1);
  }
  get(1, 0, 1);
  for (int i = 0; i < m; i++)
    printf("%d ", ans[i]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ch[600005][3], ed[600005], cnt = 1, ans[600005];
bool rev[600005];
char s[200005];
void pushr(int x) {
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}
void pushdown(int x) {
  if (!rev[x])
    return;
  for (int i = 0; i <= 2; i++)
    if (ch[x][i])
      pushr(ch[x][i]);
  rev[x] = 0;
}
void dfs(int x) {
  pushdown(x);
  swap(ch[x][0], ch[x][2]), swap(ch[x][1], ch[x][2]);
  if (ch[x][0])
    dfs(ch[x][0]);
}
void get(int x, int now, int pw) {
  pushdown(x);
  if (ed[x] != 0xffffffff)
    ans[ed[x]] = now;
  if (ch[x][0])
    get(ch[x][0], now, pw * 3);
  if (ch[x][1])
    get(ch[x][1], now + pw, pw * 3);
  if (ch[x][2])
    get(ch[x][2], now + 2 * pw, pw * 3);
}
int main() {
  int n, m = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    m *= 3;
  memset(ed, 0xff, sizeof(ed));
  for (int i = 0; i < m; i++) {
    int now = 1, tmp = i;
    for (int k = 0; k < n; k++) {
      int t = tmp % 3;
      tmp /= 3;
      if (!ch[now][t])
        ch[now][t] = ++cnt;
      now = ch[now][t];
    }
    ed[now] = i;
  }
  scanf("%s", s + 1);
  int q = strlen(s + 1);
  for (int i = 1; i <= q; i++) {
    if (s[i] == 'S')
      pushr(1);
    else
      dfs(1);
  }
  get(1, 0, 1);
  for (int i = 0; i < m; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

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

const int maxN = 2e6 + 10;

int n, len, cnt, root;
int ch[maxN + 1][3], a[maxN + 1];
int rev[maxN + 1], pw[15], ans[maxN + 1];
char s[maxN + 1];

inline void reverse(int x) {
  if (!x)
    return;
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}

inline void pushdown(int x) {
  if (!rev[x])
    return;
  reverse(ch[x][0]);
  reverse(ch[x][1]);
  reverse(ch[x][2]);
  rev[x] = 0;
}

inline void insert(int &node, int x, int k) {
  if (!node)
    node = ++cnt;
  if (k == n) {
    a[node] = x;
    return;
  }
  int d = x / pw[k] % 3;
  insert(ch[node][d], x, k + 1);
}

inline void change(int node) {
  if (!node)
    return;
  pushdown(node);
  change(ch[node][2]);
  swap(ch[node][1], ch[node][2]);
  swap(ch[node][0], ch[node][1]);
}

inline void dfs(int node, int k, int x) {
  if (!node)
    return;
  if (k == n) {
    ans[a[node]] = x;
    return;
  }
  pushdown(node);
  dfs(ch[node][0], k + 1, x);
  dfs(ch[node][1], k + 1, x + pw[k]);
  dfs(ch[node][2], k + 1, x + 2 * pw[k]);
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  len = strlen(s + 1);

  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;

  for (int i = 0; i < pw[n]; i++)
    insert(root, i, 0);

  for (int i = 1; i <= len; i++)
    if (s[i] == 'S')
      reverse(root);
    else
      change(root);

  dfs(root, 0, 0);

  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  puts("");
  return 0;
}


Error: WA

Fixed submission:

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

const int maxN = 6e5 + 10;

int n, len, cnt, root;
int ch[maxN + 1][3], a[maxN + 1];
int rev[maxN + 1], pw[15], ans[maxN + 1];
char s[maxN + 1];

inline void reverse(int x) {
  if (!x)
    return;
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}

inline void pushdown(int x) {
  if (!rev[x])
    return;
  reverse(ch[x][0]);
  reverse(ch[x][1]);
  reverse(ch[x][2]);
  rev[x] = 0;
}

inline void insert(int &node, int x, int k) {
  if (!node)
    node = ++cnt;
  if (k == n) {
    a[node] = x;
    return;
  }
  int d = x / pw[k] % 3;
  insert(ch[node][d], x, k + 1);
}

inline void change(int node) {
  if (!node)
    return;
  pushdown(node);
  change(ch[node][2]);
  swap(ch[node][1], ch[node][2]);
  swap(ch[node][0], ch[node][1]);
}

inline void dfs(int node, int k, int x) {
  if (!node)
    return;
  if (k == n) {
    ans[a[node]] = x;
    return;
  }
  pushdown(node);
  dfs(ch[node][0], k + 1, x);
  dfs(ch[node][1], k + 1, x + pw[k]);
  dfs(ch[node][2], k + 1, x + 2 * pw[k]);
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  len = strlen(s + 1);

  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;

  for (int i = 0; i < pw[n]; i++)
    insert(root, i, 0);

  for (int i = 1; i <= len; i++)
    if (s[i] == 'S')
      reverse(root);
    else
      change(root);

  dfs(root, 0, 0);

  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  puts("");
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long LL;

const int N = 9e6;

int n;
struct {
  int v;
  int tag;
} t[N];

void build(int p, int v, int d) {
  if (d > n)
    return;
  t[p].v = v;
  for (int i = 0; i < 3; i++) {
    build(p * 3 + i, i, d + 1);
  }
}

void pushdown(int p) {

  for (int i = 0; i < 3; i++) {
    t[p * 3 + i].tag += t[p].tag;
  }
  if (t[p].tag & 1 && t[p].v) {
    t[p].v ^= 3;
  }
  t[p].tag = 0;
}

void update(int p, int d) {
  if (d >= n)
    return;
  for (int i = 0; i < 3; i++) {
    int x = p * 3 + i;
    pushdown(x);

    t[x].v++;
    if (t[x].v == 3) {
      t[x].v = 0;
      update(x, d + 1);
    }
  }
}

int query(int p, int x, int d) {
  if (d > n)
    return 0;
  pushdown(p);
  int res = t[p].v;
  return query(p * 3 + x % 3, x / 3, d + 1) * 3 + res;
}

signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  cin >> n;
  string s;
  cin >> s;
  build(1, 0, 0);

  for (auto c : s) {
    if (c == 'S') {
      t[1].tag++;
    } else {
      pushdown(1);
      update(1, 0);
    }
  }
  int t = 1;
  for (int i = 1; i <= n; i++)
    t *= 3;
  for (int i = 0; i < t; i++)
    cout << query(1, i, 0) / 3 << ' ';
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long LL;

const int N = 9e6;

int n;
struct {
  int v;
  int tag;
} t[N];

void build(int p, int v, int d) {
  if (d > n)
    return;
  t[p].v = v;
  for (int i = 0; i < 3; i++) {
    build(p * 3 + i, i, d + 1);
  }
}

void pushdown(int p) {
  for (int i = 0; i < 3; i++) {
    t[p * 3 + i].tag += t[p].tag;
  }
  if (t[p].tag & 1 && t[p].v) {
    t[p].v ^= 3;
  }
  t[p].tag = 0;
}

void update(int p, int d) {
  if (d > n)
    return;
  for (int i = 0; i < 3; i++) {
    int x = p * 3 + i;
    pushdown(x);
    t[x].v++;
    if (t[x].v == 3) {
      t[x].v = 0;
      update(x, d + 1);
    }
  }
}

int query(int p, int x, int d) {
  if (d > n)
    return 0;
  pushdown(p);
  int res = t[p].v;
  return query(p * 3 + x % 3, x / 3, d + 1) * 3 + res;
}

signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  cin >> n;
  string s;
  cin >> s;
  build(1, 0, 0);

  for (auto c : s) {
    if (c == 'S') {
      t[1].tag++;
    } else {
      pushdown(1);
      update(1, 0);
    }
  }
  int t = 1;
  for (int i = 1; i <= n; i++)
    t *= 3;
  for (int i = 0; i < t; i++)
    cout << query(1, i, 0) / 3 << ' ';
  cout << endl;
  return 0;
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 2000000 + 10;
int a[N], b[N], mark[N], p[N], n, S;
vector<int> v[N];
char s[N];
void push(int i) {
  if (mark[i] ^ S) {
    mark[i] ^= 1;
    swap(v[i][1], v[i][2]);
  }
}
int main() {
  scanf("%d%s", &n, s);
  p[0] = 1;
  for (int i = 1; i < 13; i++)
    p[i] = p[i - 1] * 3;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < p[i]; j++)
      v[j] = {0, 1, 2};
    S = 0;
    for (int j = 0; s[j]; j++) {
      push(b[j]);
      int nxt = b[j] + p[i] * v[b[j]][2];
      if (s[j] == 'S')
        S ^= 1;
      else {
        swap(v[b[j]][1], v[b[j]][2]), swap(v[b[j]][0], v[b[j]][1]);
      }
      b[j] = nxt;
    }
    for (int j = 0; j < p[i]; j++)
      push(j), mark[j] = 0;
    for (int j = 0; j < p[i]; j++) {
      int tmp = a[j];
      for (int k = 0; k < 3; k++)
        a[j + p[i] * v[j][k]] = tmp + p[i] * k;
    }
  }
  for (int i = 0; i < p[n]; i++)
    printf("%d%c", a[i], " \\n"[i == p[n] - 1]);
}


Error: TLE

Fixed submission:

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
// #pragma GCC target ("avx2")
// #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 729 + 10;
int p[13];
char s[200000 + 10];
int a[N][N], b[N], r[N], ans[N * N];
int main() {
  p[0] = 1;
  for (int i = 1; i < 13; i++)
    p[i] = p[i - 1] * 3;
  int n;
  scanf("%d%s", &n, s);
  int x = n / 2, y = n - x;
  /// y: low, x: high
  for (int i = 0; i < p[6]; i++) {
    int t = i;
    for (int j = 0; j < 6; j++) {
      if (t / p[j] % 3 == 1)
        t += p[j];
      else if (t / p[j] % 3 == 2)
        t -= p[j];
    }
    r[i] = t;
    if (i >= r[i])
      r[i] = 0;
  }
  for (int i = 0; i < p[n]; i++) {
    a[i % p[y]][i / p[y]] = i;
  }
  int R = 0;
  for (int i = 0; s[i]; i++) {
    if (s[i] == 'S') {
      R ^= 1;
      for (int j = 0; j < p[y]; j++)
        if (r[j]) {
          swap(a[j], a[r[j]]);
          swap(b[j], b[r[j]]);
        }
    } else {
      for (int j = p[y] - 1; j > 0; j--) {
        swap(a[j], a[j - 1]);
        swap(b[j], b[j - 1]);
      }
      if (b[0] ^ R) {
        for (int j = 0; j < p[x]; j++)
          if (r[j])
            swap(a[0][j], a[0][r[j]]);
        b[0] ^= 1;
      }
      for (int j = p[x] - 1; j > 0; j--) {
        swap(a[0][j], a[0][j - 1]);
      }
    }
  }
  for (int i = 0; i < p[y]; i++)
    if (b[i] ^ R) {
      for (int j = 0; j < p[x]; j++)
        if (j < r[j])
          swap(a[i][j], a[i][r[j]]);
      b[i] ^= 1;
    }
  for (int i = 0; i < p[n]; i++)
    ans[a[i % p[y]][i / p[y]]] = i;
  for (int i = 0; i < p[n]; i++)
    printf("%d%c", ans[i], " \\n"[i == p[n] - 1]);
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
string T;
using pvv = pair<vector<int>, vector<int>>;

void update(int, int &, int &);
pvv createbit(int);

int main() {
  int N;
  cin >> N >> T;
  pvv ans = createbit(N);
  for (int x : ans.first) {
    cout << x << " ";
  }
  cout << endl;
}

void update(int q, int &last, int &next) {
  if (next != 0 && last % 2 != q % 2)
    next = 3 - next;
  last = q;
}

pvv createbit(int N) {
  if (N == 0) {
    pvv send;
    send.first = {0};
    send.second = vector<int>(T.size(), 0);
    return send;
  }
  pvv receve = createbit(N - 1);
  pvv send;
  int pot = (int)receve.first.size();
  send.first = vector<int>(pot * 3, 0);
  send.second = vector<int>(T.size());
  vector<int> next(pot * 3);
  int q = 0;
  vector<int> last(pot * 3, 0);
  for (int i = 0; i < 3; i++)
    for (int x = pot * i; x < pot * (i + 1); x += 1)
      next[x] = i;

  for (int i = 0; i < T.size(); i++) {
    if (T[i] == 'S') {
      q++;
      continue;
    }

    int k = receve.second[i];

    for (int x = k; x < pot * 3; x += pot) {
      update(q, last[x], next[x]);

      if (next[x] == 2)
        send.second[i] = x;
    }

    for (int x = k; x <= pot * 3; x += pot)
      next[x] = (next[x] + 1) % 3;
  }

  for (int i = 0; i < pot * 3; i++) {
    update(q, last[i], next[i]);
    send.first[i] = receve.first[i % pot] + next[i] * pot;
  }

  return send;
}

Error: TLE

Fixed submission:

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

int change3(int A) {
  vector<int> b3(0);
  while (A > 2) {
    int aw3 = A % 3;
    b3.push_back((aw3 == 0) ? 0 : (aw3 == 1) ? 2 : 1);
    A /= 3;
  }
  int aw3 = A % 3;
  b3.push_back((aw3 == 0) ? 0 : (aw3 == 1) ? 2 : 1);
  int sum = 0;
  for (int i = 0; i < b3.size(); i++) {
    sum += pow(3, i) * b3.at(i);
  }
  return sum;
}

int main() {
  int N;
  cin >> N;
  int N3 = pow(3, N);
  vector<int> dan(N3);
  vector<int> c3b(N3);
  for (int i = 0; i < N3; i++) {
    dan.at(i) = i;
    c3b.at(i) = change3(i);
  }
  string recode;
  cin >> recode;

  for (char i : recode) {
    if (i == 'S') {
      for (int &j : dan) {
        j = c3b.at(j);
      }
    } else {
      for (int &j : dan) {
        j++;
        if (j >= N3)
          j -= N3;
      }
    }
  }

  for (int i : dan) {
    cout << i << " ";
  }
  cout << endl;
}
Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
using namespace std;

const int maxn = 8e5;
int n, m, tot, val[maxn + 5], ch[maxn + 5][3], cnt, res[maxn + 5];
bool sal[maxn + 5];
char s[maxn + 5];

int dfs(int k, int t, int s) {
  int x = ++tot;
  if (!k)
    return val[x] = s, x;
  ch[x][0] = dfs(k - 1, t * 3, s);
  ch[x][1] = dfs(k - 1, t * 3, s + t);
  ch[x][2] = dfs(k - 1, t * 3, s + t * 2);
  return x;
}

void salsa(int x) { sal[x] ^= 1, swap(ch[x][1], ch[x][2]); }

void push_down(int x) {
  if (sal[x]) {
    rep(i, 0, 2) salsa(ch[x][i]);
    sal[x] = false;
  }
}

void rumba(int x) {
  if (!ch[x][0])
    return;
  push_down(x);
  int t = ch[x][0];
  ch[x][0] = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = t;
  rumba(ch[x][0]);
}

void work(int x, int t, int s) {
  if (!ch[x][0]) {
    res[val[x]] = s, cnt++;
    return;
  }
  push_down(x);
  rep(i, 0, 2) work(ch[x][i], t * 3, s + i * t);
}

int main() {
  scanf("%d %s", &n, s + 1);
  m = strlen(s + 1);
  dfs(n, 1, 0);
  rep(i, 1, m) {
    if (s[i] == 'S')
      salsa(1);
    else
      rumba(1);
  }
  work(1, 1, 0);
  rep(i, 0, cnt - 1) printf("%d%c", res[i], " \\n"[i == cnt - 1]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
using namespace std;

const int maxn = 6e5;
int n, m, tot, val[maxn + 5], ch[maxn + 5][3], cnt, res[maxn + 5];
bool sal[maxn + 5];
char s[maxn + 5];

int dfs(int k, int t, int s) {
  int x = ++tot;
  if (!k)
    return val[x] = s, x;
  ch[x][0] = dfs(k - 1, t * 3, s);
  ch[x][1] = dfs(k - 1, t * 3, s + t);
  ch[x][2] = dfs(k - 1, t * 3, s + t * 2);
  return x;
}

void salsa(int x) { sal[x] ^= 1, swap(ch[x][1], ch[x][2]); }

void push_down(int x) {
  if (sal[x]) {
    rep(i, 0, 2) salsa(ch[x][i]);
    sal[x] = false;
  }
}

void rumba(int x) {
  if (!ch[x][0])
    return;
  push_down(x);
  int t = ch[x][0];
  ch[x][0] = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = t;
  rumba(ch[x][0]);
}

void work(int x, int t, int s) {
  if (!ch[x][0]) {
    res[val[x]] = s, cnt++;
    return;
  }
  push_down(x);
  rep(i, 0, 2) work(ch[x][i], t * 3, s + i * t);
}

int main() {
  scanf("%d %s", &n, s + 1);
  m = strlen(s + 1);
  dfs(n, 1, 0);
  rep(i, 1, m) {
    if (s[i] == 'S')
      salsa(1);
    else
      rumba(1);
  }
  work(1, 1, 0);
  rep(i, 0, cnt - 1) printf("%d%c", res[i], " \\n"[i == cnt - 1]);
  return 0;
}