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

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

int n, Q;
string s;

void update(int x, vector<int> &d, vector<int> &salsas) {
  if (salsas[x] != Q) {
    salsas[x] ^= 1;
    if (d[x] == 1)
      d[x] = 2;
    else if (d[x] == 2)
      d[x] = 1;
  }
}

pair<vector<int>, vector<int>> solve(vector<int> last_p, vector<int> last_w) {
  vector<int> p(3 * last_p.size()), w;
  int n = last_p.size();

  Q = 0;
  vector<int> d(3 * last_p.size());
  vector<int> salsas(3 * last_p.size());
  for (int i = 0; i < n; i++)
    d[i] = 0;
  for (int i = n; i < 2 * n; i++)
    d[i] = 1;
  for (int i = 2 * n; i < 3 * n; i++)
    d[i] = 2;

  for (int i = 0; i < s.size(); i++) {
    int x = last_w[i];

    update(x, d, salsas);
    update(x + n, d, salsas);
    update(x + 2 * n, d, salsas);

    if (d[x] == 2)
      w.push_back(x);
    if (d[n + x] == 2)
      w.push_back(n + x);
    if (d[2 * n + x] == 2)
      w.push_back(2 * n + x);
    if (s[i] == 'S') {
      Q ^= 1;
    } else {
      int x = last_w[i];
      d[x] = (d[x] + 1) % 3;
      d[n + x] = (d[n + x] + 1) % 3;
      d[2 * n + x] = (d[2 * n + x] + 1) % 3;
    }
  }

  for (int i = 0; i < 3 * n; i++)
    update(i, d, salsas);
  for (int i = 0; i < 3 * n; i++) {
    p[i] = last_p[i % n] + d[i] * n;
  }

  return {p, w};
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
#ifdef LOCAL
  string ss;
  cin >> ss;
  while (s.size() < 200000)
    s += ss;
#else
  cin >> s;
#endif // LOCAL

  vector<int> last_p = {0, 1, 2};
  vector<int> last_w;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 3; j++)
      if (last_p[j] == 2)
        last_w.push_back(j);
    if (s[i] == 'S') {
      int t = 1;
      for (int k = 0; t && k < 3; k++) {
        for (int j = 0; t && j < 3; j++) {
          if (last_p[k] == 2 && last_p[j] == 1) {
            swap(last_p[k], last_p[j]);
            t = 0;
          }
        }
      }
    } else {
      for (int j = 0; j < 3; j++)
        last_p[j] = (last_p[j] + 1) % 3;
    }
  }
  for (int i = 1; i < n; i++) {
    pair<vector<int>, vector<int>> gg = solve(last_p, last_w);
    last_p = gg.first;
    last_w = gg.second;
  }
#ifdef LOCAL
// return 0;
#endif // LOCAL
  for (int i = 0; i < last_p.size(); i++) {
    cout << last_p[i] << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

int n;
string s;

void update(int x, vector<int> &d, vector<int> &salsas) {
  if (salsas[x] % 2) {
    salsas[x] ^= 1;
    if (d[x] == 1)
      d[x] = 2;
    else if (d[x] == 2)
      d[x] = 1;
  }
}

pair<vector<int>, vector<int>> solve(vector<int> last_p, vector<int> last_w) {
  vector<int> p(3 * last_p.size()), w;
  int n = last_p.size();

  vector<int> d(3 * last_p.size());
  vector<int> salsas(3 * last_p.size());
  for (int i = 0; i < n; i++)
    d[i] = 0;
  for (int i = n; i < 2 * n; i++)
    d[i] = 1;
  for (int i = 2 * n; i < 3 * n; i++)
    d[i] = 2;

  for (int i = 0; i < s.size(); i++) {
    int x = last_w[i];

    update(x, d, salsas);
    update(x + n, d, salsas);
    update(x + 2 * n, d, salsas);

    if (d[x] == 2)
      w.push_back(x);
    if (d[n + x] == 2)
      w.push_back(n + x);
    if (d[2 * n + x] == 2)
      w.push_back(2 * n + x);
    if (s[i] == 'S') {
      for (int j = 0; j < d.size(); j++)
        salsas[j] ^= 1;
    } else {
      int x = last_w[i];
      d[x] = (d[x] + 1) % 3;
      d[n + x] = (d[n + x] + 1) % 3;
      d[2 * n + x] = (d[2 * n + x] + 1) % 3;
    }
  }

  for (int i = 0; i < 3 * n; i++)
    update(i, d, salsas);
  for (int i = 0; i < 3 * n; i++) {
    p[i] = last_p[i % n] + d[i] * n;
  }

  return {p, w};
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s;

  vector<int> last_p = {0, 1, 2};
  vector<int> last_w;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 3; j++)
      if (last_p[j] == 2)
        last_w.push_back(j);
    if (s[i] == 'S') {
      int t = 1;
      for (int k = 0; t && k < 3; k++) {
        for (int j = 0; t && j < 3; j++) {
          if (last_p[k] == 2 && last_p[j] == 1) {
            swap(last_p[k], last_p[j]);
            t = 0;
          }
        }
      }
    } else {
      for (int j = 0; j < 3; j++)
        last_p[j] = (last_p[j] + 1) % 3;
    }
  }
  for (int i = 1; i < n; i++) {
    pair<vector<int>, vector<int>> gg = solve(last_p, last_w);
    last_p = gg.first;
    last_w = gg.second;
  }
#ifdef LOCAL
// return 0;
#endif // LOCAL
  for (int i = 0; i < last_p.size(); i++) {
    cout << last_p[i] << " ";
  }
}