Problem p02671 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

// #define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define next next228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << endl;
#define TIME (ld) clock() / CLOCKS_PER_SEC

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 2e5 + 7, mod = 1e9 + 7, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n;
string s;
int next[MAXN];
const int N = 2187;
int dp1[N];
int dp2[N];
int xyz[N];

int get1(int mask, char q) {
  if (q == 'R') {
    ++mask;
    return mask;
  }
  int ptr = 1;
  int ans = 0;
  while (mask) {
    int t = mask % 3;
    mask /= 3;
    if (t == 2)
      ans += ptr;
    if (t == 1)
      ans += 2 * ptr;
    ptr *= 3;
  }
  return ans;
}

const int L = 12;
int pref[maxn];
int to[maxn];
int kek1[N], kek2[N];
int res[N], res1[N];
int add = 1;

void print(int x) {
  while (x) {
    cout << x % 3 << " ";
    x /= 3;
  }
  cout << '\n';
}

int get(int x) {
  vector<int> bts;
  for (int i = 0; i < L; ++i) {
    bts.pb(x % 3);
    x /= 3;
  }
  int left = 0;
  int right = 0;
  for (int i = 6; i >= 0; --i) {
    left *= 3;
    left += bts[i];
  }
  for (int i = L - 1; i >= 7; --i) {
    right *= 3;
    right += bts[i];
  }
  int id = 0;
  if (left && dp1[left] == -1) {
    if ((pref[(int)s.size()] - pref[id]) & 1) {
      right = xyz[right];
    }
    return res[left] + add * right;
  }
  if (left) {
    id = dp1[left];
    //        print(right);
    if (pref[id] & 1) {
      right = xyz[right];
    }
    ++right;
    //        print(right);
    left = 0;
  }
  while (next[id] != -1) {
    if ((pref[next[id]] - pref[id]) & 1) {
      right = xyz[right];
    }
    right++;
    id = next[id];
  }
  if ((pref[(int)s.size()] - pref[id]) & 1) {
    right = xyz[right];
  }
  left = to[id];
  return left + right * add;
}

void solve() {
  for (int i = 0; i < N; ++i) {
    xyz[i] = get1(i, 'S') % N;
  }
  cin >> n >> s;
  int t = s.size();
  for (int i = 0; i < t; ++i) {
    pref[i + 1] = pref[i] + (s[i] == 'S');
  }

  for (int i = 0; i < N; ++i)
    dp1[i] = dp2[i] = -1;
  next[t] = -1;
  for (int i = 0; i < N; ++i)
    kek1[i] = i;
  for (int i = 0; i < N; ++i)
    res[i] = i;
  for (int i = t - 1; i >= 0; --i) {
    for (int mask = 0; mask < N; ++mask) {
      int mask1;
      if (s[i] == 'S')
        mask1 = xyz[mask];
      else
        mask1 = mask + 1;
      if (mask1 >= N)
        mask1 -= N;
      res1[mask] = res[mask1];
      kek2[mask] = kek1[mask1];
      if (mask && mask1 == 0) {
        dp2[mask] = i + 1;
      } else {
        dp2[mask] = dp1[mask1];
      }
    }
    to[i] = kek2[0];
    next[i] = dp2[0];
    for (int mask = 0; mask < N; ++mask) {
      res[mask] = res1[mask];
      kek1[mask] = kek2[mask];
      dp1[mask] = dp2[mask];
      dp2[mask] = -1;
    }
  }
  for (int i = 0; i < 7; ++i)
    add *= 3;
  int tet = 1;
  for (int i = 0; i < n; ++i)
    tet *= 3;
  for (int ptr = 0; ptr < tet; ++ptr) {
    cout << get(ptr) % tet << " ";
  }
  //    cout << TIME;
  return;
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

// #define int long long

#define pii pair<int, int>

#define x1 x1228
#define y1 y1228

#define left left228
#define right right228

#define next next228

#define pb push_back
#define eb emplace_back

#define mp make_pair

#define ff first
#define ss second

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << endl;
#define TIME (ld) clock() / CLOCKS_PER_SEC

using namespace std;
typedef long long ll;
typedef long double ld;

const int maxn = 2e5 + 7, mod = 1e9 + 7, MAXN = 1e6 + 7;
const double eps = 1e-9;
const ll inf = 1e18;
mt19937 rnd(time(0));
int n;
string s;
int next[MAXN];
const int N = 2187;
int dp1[N];
int dp2[N];
int xyz[N];

int get1(int mask, char q) {
  if (q == 'R') {
    ++mask;
    return mask;
  }
  int ptr = 1;
  int ans = 0;
  while (mask) {
    int t = mask % 3;
    mask /= 3;
    if (t == 2)
      ans += ptr;
    if (t == 1)
      ans += 2 * ptr;
    ptr *= 3;
  }
  return ans;
}

const int L = 12;
int pref[maxn];
int to[maxn];
int kek1[N], kek2[N];
int res[N], res1[N];
int add = 1;

void print(int x) {
  while (x) {
    cout << x % 3 << " ";
    x /= 3;
  }
  cout << '\n';
}

int get(int x) {
  vector<int> bts;
  for (int i = 0; i < L; ++i) {
    bts.pb(x % 3);
    x /= 3;
  }
  int left = 0;
  int right = 0;
  for (int i = 6; i >= 0; --i) {
    left *= 3;
    left += bts[i];
  }
  for (int i = L - 1; i >= 7; --i) {
    right *= 3;
    right += bts[i];
  }
  int id = 0;
  if (left && dp1[left] == -1) {
    if ((pref[(int)s.size()] - pref[id]) & 1) {
      right = xyz[right];
    }
    return res[left] + add * right;
  }
  if (left) {
    id = dp1[left];
    //        print(right);
    if (pref[id] & 1) {
      right = xyz[right];
    }
    ++right;
    //        print(right);
    left = 0;
  }
  while (next[id] != -1) {
    if ((pref[next[id]] - pref[id]) & 1) {
      right = xyz[right];
    }
    right++;
    id = next[id];
  }
  if ((pref[(int)s.size()] - pref[id]) & 1) {
    right = xyz[right];
  }
  left = to[id];
  return left + right * add;
}

void solve() {
  for (int i = 0; i < N; ++i) {
    xyz[i] = get1(i, 'S') % N;
  }
  cin >> n >> s;
  int t = s.size();
  for (int i = 0; i < t; ++i) {
    pref[i + 1] = pref[i] + (s[i] == 'S');
  }

  for (int i = 0; i < N; ++i)
    dp1[i] = dp2[i] = -1;
  next[t] = -1;
  for (int i = 0; i < N; ++i)
    kek1[i] = i;
  for (int i = 0; i < N; ++i)
    res[i] = i;
  for (int i = t - 1; i >= 0; --i) {
    for (int mask = 0; mask < N; ++mask) {
      int mask1;
      if (s[i] == 'S')
        mask1 = xyz[mask];
      else
        mask1 = mask + 1;
      if (mask1 >= N)
        mask1 -= N;
      res1[mask] = res[mask1];
      kek2[mask] = kek1[mask1];
      if (mask && mask1 == 0) {
        dp2[mask] = i + 1;
      } else {
        dp2[mask] = dp1[mask1];
      }
    }
    to[i] = kek2[0];
    next[i] = dp2[0];
    for (int mask = 0; mask < N; ++mask) {
      res[mask] = res1[mask];
      kek1[mask] = kek2[mask];
      dp1[mask] = dp2[mask];
      dp2[mask] = -1;
    }
  }
  for (int i = 0; i < 7; ++i)
    add *= 3;
  int tet = 1;
  for (int i = 0; i < n; ++i)
    tet *= 3;
  for (int ptr = 0; ptr < tet; ++ptr) {
    cout << get(ptr) % tet << " ";
  }
  //    cout << TIME;
  return;
}

signed main() {
#ifdef LOCAL
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#else

#endif // LOCAL
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}