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

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

const int N = 1e6 + 7, KK = 12, RT = 6;
int f[N], p3[N];

void pre() {
  p3[0] = 1;
  for (int i = 1; i <= KK; i++)
    p3[i] = p3[i - 1] * 3;

  for (int i = 0; i < N; i++) {
    for (int k = 0; k < KK; k++) {
      int d = (i / p3[k]) % 3;
      if (d == 1)
        f[i] += p3[k] * 2;
      else if (d == 2)
        f[i] += p3[k];
    }
  }
}

int aa[N], bb[N];
vector<int> v[N];
int asol[N];
int temp[N];

int vid[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  pre();

  int n;
  string s;
  cin >> n >> s;

  int mx = p3[max(0, n - RT)];
  int my = p3[min(RT, n)];
  int all = p3[n];

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      v[i].push_back(my * i + j);

  for (int j = 0; j < my; j++)
    asol[j] = j;
  for (int i = 0; i < mx; i++)
    vid[i] = i;

  for (char c : s) {
    if (c == 'R') {
      for (int i = 0; i < mx; i++)
        temp[i] = v[vid[i]][asol[my - 1]];

      int st = asol[my - 1];
      for (int i = my - 1; i > 0; i--)
        asol[i] = asol[i - 1];
      asol[0] = st;

      for (int i = 1; i < mx; i++)
        v[vid[i]][asol[0]] = temp[i - 1];
      v[vid[0]][asol[0]] = temp[mx - 1];
    } else {
      for (int i = 0; i < my; i++)
        if (f[i] > i)
          swap(asol[i], asol[f[i]]);

      for (int i = 0; i < mx; i++)
        if (f[i] > i)
          swap(vid[i], vid[f[i]]);
    }

    //        for (int i=0; i<my; i++)    cout<<asol[i]<<" "; cout<<":::"<<endl;
    //        for (int i=0; i<my; i++)    cout<<v[0][i]<<" "; cout<<"---"<<endl;
  }

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      aa[i * my + j] = v[vid[i]][asol[j]];

  for (int i = 0; i < all; i++)
    bb[aa[i]] = i;
  for (int i = 0; i < all; i++)
    cout << bb[i] << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

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

const int N = 1e6 + 7, K = 12, RT = 6;
int f[N], p3[N];

void pre() {
  p3[0] = 1;
  for (int i = 1; i <= K; i++)
    p3[i] = p3[i - 1] * 3;

  for (int i = 0; i < N; i++) {
    for (int k = 0; k < K; k++) {
      int d = (i / p3[k]) % 3;
      if (d == 1)
        f[i] += p3[k] * 2;
      else if (d == 2)
        f[i] += p3[k];
    }
  }
}

int aa[N], bb[N];
vector<int> v[N];
int asol[N];
int temp[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  pre();

  int n;
  string s;
  cin >> n >> s;

  int mx = p3[max(0, n - K)];
  int my = p3[min(K, n)];
  int all = p3[n];

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      v[i].push_back(my * i + j);

  for (int j = 0; j < my; j++)
    asol[j] = j;

  for (char c : s) {
    if (c == 'R') {
      for (int i = 0; i < mx; i++)
        temp[i] = v[i][asol[my - 1]];

      int st = asol[my - 1];
      for (int i = my - 1; i > 0; i--)
        asol[i] = asol[i - 1];
      asol[0] = st;

      for (int i = 1; i < mx; i++)
        v[i][asol[0]] = temp[i - 1];
      v[0][asol[0]] = temp[mx - 1];
    } else {
      for (int i = 0; i < my; i++)
        if (f[i] > i)
          swap(asol[i], asol[f[i]]);

      for (int i = 0; i < mx; i++)
        if (f[i] > i)
          v[i].swap(v[f[i]]);
    }

    //        for (int i=0; i<my; i++)    cout<<asol[i]<<" "; cout<<":::"<<endl;
    //        for (int i=0; i<my; i++)    cout<<v[0][i]<<" "; cout<<"---"<<endl;
  }

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      aa[i * my + j] = v[i][asol[j]];

  for (int i = 0; i < all; i++)
    bb[aa[i]] = i;
  for (int i = 0; i < all; i++)
    cout << bb[i] << " ";
  cout << endl;
}