Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (600006)
ll n, A[MAXN], p3[15], ofs2, half, mp[729], edge[729], opp[MAXN];
string T;
vector<ll> v[729];
ll flip(ll x) {
  if (~opp[x])
    return opp[x];
  ll res = 0, ox = x;
  DEC(j, n - 1, half) if (x >= p3[j] * 2) {
    res += p3[j];
    x -= p3[j] * 2;
  }
  else if (x >= p3[j]) {
    res += p3[j] * 2;
    x -= p3[j];
  }
  return opp[ox] = res;
}
int main() {
  p3[0] = 1;
  FOR(i, 1, 14) p3[i] = p3[i - 1] * 3;
  mmst(opp, -1);
  FAST cin >> n >> T;
  half = n / 2;
  FOR(i, 0, p3[half] - 1) mp[i] = i;
  FOR(i, 0, p3[half] - 1) {
    edge[i] = i;
    ll add = 0;
    DEC(j, half - 1, 0) if (edge[i] >= p3[j] * 2) {
      edge[i] -= p3[j] * 2;
      add += p3[j];
    }
    else if (edge[i] >= p3[j]) {
      edge[i] -= p3[j];
      add += p3[j] * 2;
    }
    assert(edge[i] == 0);
    edge[i] = add;
  }
  FOR(i, 0, p3[n] - 1) {
    A[i] = i;
    DEC(j, n - 1, half) if (A[i] >= p3[j] * 2) A[i] -= p3[j] * 2;
    else if (A[i] >= p3[j]) A[i] -= p3[j];
    v[A[i]].eb(i), A[i] = i - A[i];
  }
  FOR(i, 0, siz(T) - 1) {
    if (T[i] == 'R') { //+1
      ll act = -1;
      FOR(i, 0, p3[half] - 1)
      if (mp[i] == p3[half] - 1) assert(act == -1), act = i;
      assert(~act);
      for (auto i : v[act]) {
        if (ofs2)
          A[i] = flip(A[i]);
        A[i] += p3[half];
        if (A[i] >= p3[n])
          A[i] -= p3[n], assert(A[i] == 0);
        if (ofs2)
          A[i] = flip(A[i]);
      }
      FOR(i, 0, p3[half] - 1)++ mp[i], mp[i] %= p3[half];
    } else {
      ofs2 ^= 1;
      FOR(i, 0, p3[half] - 1) { mp[i] = edge[mp[i]]; }
    }
  }
  FOR(i, 0, p3[n] - 1) if (ofs2) A[i] = flip(A[i]);
  FOR(i, 0, p3[half] - 1) {
    for (auto j : v[i])
      A[j] += mp[i];
  }
  FOR(i, 0, p3[n] - 1) cout << A[i] << ' ';
  cout << '\\n';
}


Error: TLE

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (600006)
ll n, A[MAXN], p3[15], ofs2, half, mp[729], edge[729];
string T;
vector<ll> v[729];
ll flip(ll x) {
  ll res = 0;
  DEC(j, n - 1, half) if (x >= p3[j] * 2) {
    res += p3[j];
    x -= p3[j] * 2;
  }
  else if (x >= p3[j]) {
    res += p3[j] * 2;
    x -= p3[j];
  }
  return res;
}
int main() {
  p3[0] = 1;
  FOR(i, 1, 14) p3[i] = p3[i - 1] * 3;
  FAST cin >> n >> T;
  half = n / 2;
  FOR(i, 0, p3[half] - 1) mp[i] = i;
  FOR(i, 0, p3[half] - 1) {
    edge[i] = i;
    ll add = 0;
    DEC(j, half - 1, 0) if (edge[i] >= p3[j] * 2) {
      edge[i] -= p3[j] * 2;
      add += p3[j];
    }
    else if (edge[i] >= p3[j]) {
      edge[i] -= p3[j];
      add += p3[j] * 2;
    }
    assert(edge[i] == 0);
    edge[i] = add;
  }
  FOR(i, 0, p3[n] - 1) {
    A[i] = i;
    DEC(j, n - 1, half) if (A[i] >= p3[j] * 2) A[i] -= p3[j] * 2;
    else if (A[i] >= p3[j]) A[i] -= p3[j];
    v[A[i]].eb(i), A[i] = i - A[i];
  }
  FOR(i, 0, siz(T) - 1) {
    if (T[i] == 'R') { //+1
      ll act = -1;
      FOR(i, 0, p3[half] - 1)
      if (mp[i] == p3[half] - 1) assert(act == -1), act = i;
      assert(~act);
      for (auto i : v[act]) {
        if (ofs2)
          A[i] = flip(A[i]);
        A[i] += p3[half];
        if (A[i] >= p3[n])
          A[i] -= p3[n], assert(A[i] == 0);
        if (ofs2)
          A[i] = flip(A[i]);
      }
      FOR(i, 0, p3[half] - 1)++ mp[i], mp[i] %= p3[half];
    } else {
      ofs2 ^= 1;
      FOR(i, 0, p3[half] - 1) { mp[i] = edge[mp[i]]; }
    }
  }
  FOR(i, 0, p3[n] - 1) if (ofs2) A[i] = flip(A[i]);
  FOR(i, 0, p3[half] - 1) {
    for (auto j : v[i])
      A[j] += mp[i];
  }
  FOR(i, 0, p3[n] - 1) cout << A[i] << ' ';
  cout << '\\n';
}