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

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;
template <typename T> using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
// #define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int maxn = 1000006;

int curup[maxn];
pair<int, int> curdown[maxn];
char s[maxn];
int n, nn;
int godownS[maxn];

pair<string, int> add1(string ss) {
  bool all2 = true;
  for (int j = (int)ss.length() - 1; j >= 0; j--) {
    if (ss[j] != '2') {
      ss[j]++;
      all2 = false;
      break;
    } else {
      ss[j] = '0';
    }
  }
  return {ss, all2};
}

string calcS(string s) {
  for (auto &c : s)
    if (c != '0')
      c = '1' + '2' - c;
  return s;
}

int eval(string s) {
  int v = 0;
  for (auto c : s)
    v = v * 3 + c - '0';
  return v;
}

int main() {
  scanf("%d", &n);
  // n = 12;
  nn = 1;
  for (int i = 0; i < n; i++)
    nn *= 3;
  scanf("%s", s);
  int len = strlen(s);
  // int len = 200000;
  // for (int i = 0; i < len; i++) s[i] = 'R'; //(((int)rng() % 2) ? 'S' : 'R');
  int nhalf = (n + 1) / 2;
  int nnhalf = 1;
  for (int i = 0; i < nhalf; i++)
    nnhalf *= 3;
  string cur = string(nhalf, '0');
  for (int it = 0; it < nnhalf; it++) {
    godownS[it] = eval(calcS(cur));
    cur = add1(cur).fi;
  }
  int nntop = nn / nnhalf;

  for (int it = 0; it < nnhalf; it++) {
    curdown[it] = {it, 0};
  }
  for (int it = 0; it < nn; it++) {
    curup[it] = it / nnhalf;
  }
  for (int it = 0; it < len; it++) {
    // cout << "before " << it << ' ' << clock() << endl;
    // for (auto t : mp) cout << t.from << ' ' << t.to << ' ' << t.flip << endl;
    // cout << it << ' ' << mp.size() << endl;
    char op = s[it];
    if (op == 'S') {
      for (int i = 0; i < nnhalf; i++) {
        curdown[i].fi = godownS[curdown[i].fi];
        curdown[i].se ^= 1;
      }
    } else {
      for (int i = 0; i < nnhalf; i++) {
        if (curdown[i].fi != nnhalf - 1) {
          curdown[i].fi++;
        } else {
          for (int j = i; j < nn; j += nnhalf) {
            if (curdown[i].se) {
              curup[j] = godownS[curup[j]];
            }
            curup[j]++;
            if (curup[j] == nntop)
              curup[j] = 0;
          }
          curdown[i] = {0, 0};
        }
      }
    }
  }
  for (int i = 0; i < nn; i++) {
    if (curdown[i % nnhalf].se) {
      curup[i] = godownS[curup[i]];
    }
    int v = curup[i] * nnhalf + curdown[i % nnhalf].fi;
    printf("%d ", v);
  }
  printf("\\n");
  // for (int i = 0; i < nn; i++) printf("%d%d ", curup[i], curdown[i %
  // nnhalf].fi); printf("\\n");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;
template <typename T> using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
// #define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int MXSZ = 300;

struct tchange {
  string from, to;
  int flip;
};

const int maxn = 1000006;

vector<tchange> mp, newmp;
string curup[maxn];
pair<string, int> curdown[maxn];
char s[maxn];
int n, nn;

pair<string, int> add1(string ss) {
  bool all2 = true;
  for (int j = (int)ss.length() - 1; j >= 0; j--) {
    if (ss[j] != '2') {
      ss[j]++;
      all2 = false;
      break;
    } else {
      ss[j] = '0';
    }
  }
  return {ss, all2};
}

int main() {
  scanf("%d", &n);
  // n = 12;
  nn = 1;
  for (int i = 0; i < n; i++)
    nn *= 3;
  scanf("%s", s);
  int len = strlen(s);
  // int len = 200000;
  // for (int i = 0; i < len; i++) s[i] = 'R'; //(((int)rng() % 2) ? 'S' : 'R');
  int nhalf = (n + 1) / 2;
  int nnhalf = 1;
  for (int i = 0; i < nhalf; i++)
    nnhalf *= 3;
  string cur = string(nhalf, '0');
  for (int it = 0; it < nnhalf; it++) {
    curdown[it] = {cur, 0};
    cur = add1(cur).fi;
  }
  cur = string(n, '0');
  for (int it = 0; it < nn; it++) {
    curup[it] = cur.substr(0, n - nhalf);
    cur = add1(cur).fi;
  }
  for (int it = 0; it < len; it++) {
    // cout << "before " << it << endl;
    // for (auto t : mp) cout << t.from << ' ' << t.to << ' ' << t.flip << endl;
    // cout << it << ' ' << mp.size() << endl;
    char op = s[it];
    if (op == 'S') {
      for (int i = 0; i < nnhalf; i++) {
        for (auto &c : curdown[i].fi)
          if (c != '0')
            c = '1' + '2' - c;
        curdown[i].se ^= 1;
      }
    } else {
      for (int i = 0; i < nnhalf; i++) {
        auto newv = add1(curdown[i].fi);
        if (newv.se) // overflow
        {
          for (int j = i; j < nn; j += nnhalf) {
            if (curdown[i].se) {
              for (auto &c : curup[j])
                if (c != '0')
                  c = '1' + '2' - c;
            }
            curup[j] = add1(curup[j]).fi;
          }
          curdown[i] = {newv.fi, 0};
        } else
          curdown[i].fi = newv.fi;
      }
    }
  }
  for (int i = 0; i < nn; i++) {
    if (curdown[i % nnhalf].se) {
      for (auto &c : curup[i])
        if (c != '0')
          c = '1' + '2' - c;
    }
    int v = 0;
    for (auto c : curup[i])
      v = v * 3 + c - '0';
    for (auto c : curdown[i % nnhalf].fi)
      v = v * 3 + c - '0';
    printf("%d ", v);
  }
  printf("\\n");
  // for (int i = 0; i < nn; i++) printf("%s%s ", curup[i].c_str(), curdown[i %
  // nnhalf].fi.c_str()); printf("\\n");
  return 0;
}