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

#pragma GCC optimize("O2,unroll-loops")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
int maxn = 531441, B, C;
int n, p[531441], f[531441];
int rev(int a) {
  int r = 0, c = 1;
  while (c < maxn) {
    if (a % 3)
      r += (3 - (a % 3)) * c;
    c *= 3, a /= 3;
  }
  return r;
}

int s = 0;
vector<int> small[729];
int id[531441], lup[531441], u = 0;

void add() {
  s = (s + B - 1) % B;
  for (auto i : small[s]) {
    if (lup[i] != u) {
      if ((lup[i] ^ u) & 1)
        id[i] = f[id[i]];
      lup[i] = u;
    }
    id[i] = (id[i] + 1) % C;
  }
}

void flip() {
  u++;
  for (int i = 0; i < B; i++) {
    if (f[i] <= i) {
      swap(small[(s + i) % B], small[(s + f[i]) % B]);
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  string t;
  cin >> n >> t;
  maxn = B = 1;
  for (int i = 0; i < n; i++)
    maxn *= 3;
  for (int i = 0; i < n / 2; i++)
    B *= 3;
  C = maxn / B;
  for (int i = 0; i < maxn; i++) {
    f[i] = rev(i);
    small[i % B].push_back(i);
    id[i] = i / B;
  }

  for (auto i : t) {
    if (i == 'R')
      add();
    else
      flip();
  }
  for (int i = 0; i < B; i++)
    for (auto j : small[i])
      p[j] += (B + i - s) % B;
  for (int i = 0; i < maxn; i++) {
    if (lup[i] != u) {
      if ((lup[i] ^ u) & 1)
        id[i] = f[id[i]];
      lup[i] = u;
    }
    p[i] += B * id[i];
  }
  for (int i = 0; i < maxn; i++)
    cout << p[i] << " ";
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2,unroll-loops")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
int maxn = 531441, B, C;
int n, p[531441], f[531441];
int rev(int a) {
  int r = 0, c = 1;
  while (c < maxn) {
    if (a % 3)
      r += (3 - (a % 3)) * c;
    c *= 3, a /= 3;
  }
  return r;
}

int s = 0;
list<int> big[729], small[729];
list<int>::iterator pos[531441];
int id[531441], lup[531441], u = 0;

void add() {
  s = (s + B - 1) % B;
  for (auto i : small[s]) {
    if (lup[i] != u) {
      if ((lup[i] ^ u) & 1)
        id[i] = f[id[i]];
      lup[i] = u;
    }
    big[id[i]].erase(pos[i]);
    id[i] = (id[i] + 1) % C;
    big[id[i]].push_back(i);
    --(pos[i] = big[id[i]].end());
  }
}

void flip() {
  u++;
  for (int i = 0; i < B; i++) {
    if (f[i] <= i) {
      swap(small[(s + i) % B], small[(s + f[i]) % B]);
    }
  }
  for (int i = 0; i < C; i++) {
    if (f[i] <= i) {
      swap(big[i], big[f[i]]);
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  string t;
  cin >> n >> t;
  maxn = B = 1;
  for (int i = 0; i < n; i++)
    maxn *= 3;
  for (int i = 0; i < n / 2; i++)
    B *= 3;
  C = maxn / B;
  for (int i = 0; i < maxn; i++) {
    f[i] = rev(i);

    small[i % B].push_back(i);

    big[i / B].push_back(i);
    --(pos[i] = big[i / B].end());

    id[i] = i / B;
  }

  for (auto i : t) {
    if (i == 'R')
      add();
    else
      flip();
  }
  for (int i = 0; i < B; i++)
    for (auto j : small[i])
      p[j] += (B + i - s) % B;
  for (int i = 0; i < C; i++)
    for (auto j : big[i])
      p[j] += B * i;
  for (int i = 0; i < maxn; i++)
    cout << p[i] << " ";
}