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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 1e6 + 10;

int a[maxn * 3][3], d[maxn * 3], id[maxn * 3], ans[maxn];
int n, p[15];
string s;

void build(int c, int x, int pos) {
  if (c == n) {
    //	cout<<x<<" "<<pos<<endl;
    id[x] = pos;
    return;
  }
  for (int i = 0; i < 3; i++) {
    a[x][i] = x * 3 + i;
    build(c + 1, x * 3 + i, pos + i * p[c]);
  }
}

void apply(int x) {
  d[x] = !d[x];
  swap(a[x][1], a[x][2]);
  //	cout<<a[x][1]<<" "<<a[x][2]<<endl;
}

void update(int x) {
  if (d[x] == false)
    return;
  d[x] = false;
  for (int i = 0; i < 3; i++)
    apply(a[x][i]);
}

void add(int c, int x) {
  if (c == n) {
    return;
  } else {
    update(x);
    swap(a[x][1], a[x][2]);
    swap(a[x][0], a[x][1]);
    add(c + 1, a[x][0]);
  }
}

void dfs(int c, int x, int pos) {
  if (c == n) {
    ans[id[x]] = pos;
  } else {
    update(x);
    for (int i = 0; i < 3; i++) {
      dfs(c + 1, a[x][i], pos + i * p[c]);
    }
  }
}

int main() {
  p[0] = 1;
  for (int i = 1; i < 15; i++)
    p[i] = p[i - 1] * 3;
  cin >> n >> s;
  int n2 = n;
  build(0, 1, 0);
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S') {
      apply(1);
      //	cout<<i<<endl;
    } else {
      add(0, 1);
    }
    //	dfs(0,1,0);
    //	for(int i=0;i<3;i++) cout<<ans[i]<<" "; cout<<endl;
  }
  dfs(0, 1, 0);
  n = 1;
  for (int i = 0; i < n2; i++)
    n = n * 3;
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 1e6 + 10;

int a[maxn * 3][3], d[maxn * 3], id[maxn * 3], ans[maxn];
int n, p[15];
string s;

void build(int c, int x, int pos) {
  if (c == n) {
    //	cout<<x<<" "<<pos<<endl;
    id[x] = pos;
    return;
  }
  for (int i = 0; i < 3; i++) {
    a[x][i] = x * 3 + i;
    build(c + 1, x * 3 + i, pos + i * p[c]);
  }
}

void apply(int x) {
  d[x] = !d[x];
  swap(a[x][1], a[x][2]);
  //	cout<<a[x][1]<<" "<<a[x][2]<<endl;
}

void update(int x) {
  if (d[x] == false)
    return;
  d[x] = false;
  for (int i = 0; i < 3; i++)
    apply(a[x][i]);
}

void add(int c, int x) {
  if (c == n) {
    return;
  } else {
    update(x);
    swap(a[x][1], a[x][2]);
    swap(a[x][0], a[x][1]);
    add(c + 1, a[x][0]);
  }
}

void dfs(int c, int x, int pos) {
  if (c == n) {
    ans[id[x]] = pos;
  } else {
    update(x);
    for (int i = 0; i < 3; i++) {
      dfs(c + 1, a[x][i], pos + i * p[c]);
    }
  }
}

int main() {
  p[0] = 1;
  for (int i = 1; i < 15; i++)
    p[i] = p[i - 1] * 3;
  cin >> n >> s;
  int n2 = n;
  build(0, 1, 0);
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S') {
      apply(1);
      //	cout<<i<<endl;
    } else {
      add(0, 1);
    }
    dfs(0, 1, 0);
    //	for(int i=0;i<3;i++) cout<<ans[i]<<" "; cout<<endl;
  }
  dfs(0, 1, 0);
  n = 1;
  for (int i = 0; i < n2; i++)
    n = n * 3;
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
  return 0;
}