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

#include <bits/stdc++.h>
#define rep(i, a, ...)                                                         \\
  for (int i = (a) * (strlen(#__VA_ARGS__) != 0);                              \\
       i < (int)(strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)); ++i)
#define per(i, a, ...)                                                         \\
  for (int i = (strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)) - 1;                 \\
       i >= (int)(strlen(#__VA_ARGS__) ? (a) : 0); --i)
#define foreach(i, n) for (auto &i : (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
using ll = long long;
// const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9 + 7;
const ll INFLL = (ll)1e18;
using namespace std;
template <class t> using vvector = vector<vector<t>>;
template <class t> using vvvector = vector<vector<vector<t>>>;
template <class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template <class t, class u> bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> bool chmin(t &a, u b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#ifdef DEBUG
#define debug(x)                                                               \\
  cout << "LINE " << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define debug(x) (void)0
#endif

namespace templates {
ll modpow(ll x, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    b >>= 1;
  }
  return res;
}

ll modinv(ll x) { return modpow(x, MOD - 2); }

bool was_output = false;
template <class t> void output(t a) {
  if (was_output)
    cout << " ";
  cout << a;
  was_output = true;
}
void outendl() {
  was_output = false;
  cout << endl;
}
ll in() {
  ll res;
  scanf("%lld", &res);
  return res;
}

template <class t> istream &operator>>(istream &is, vector<t> &x) {
  for (auto &i : x)
    is >> i;
  return is;
}

template <class t, class u> istream &operator>>(istream &is, pair<t, u> &x) {
  is >> x.first >> x.second;
  return is;
}

template <class t> void in(t &x) { cin >> x; }

template <class t> t in() {
  t res;
  cin >> res;
  return res;
}

template <class t> void out(t x) { cout << x; }

template <class t>
vector<t> sorted(
    vector<t> line,
    function<bool(t, t)> comp = [](t a, t b) { return a < b; }) {
  sort(line.begin(), line.end(), comp);
  return line;
}

template <class t> vector<t> reversed(vector<t> line) {
  reverse(line.begin(), line.end());
  return line;
}
} // namespace templates

using namespace templates;

vector<int> inputs() {
  string str = in<string>();
  vector<int> res;
  foreach (i, str) {
    if (i == 'S') {
      res.emplace_back(1);
    } else {
      res.emplace_back(0);
    }
  }
  return res;
}

ll k3(int x) {
  if (x == 0)
    return 1;
  static vector<ll> dp(50, -1);
  ll &it = dp[x];
  if (it >= 0)
    return it;
  return it = 3 * k3(x - 1);
}

class tree {
public:
  class node {
  public:
    int depth;
    int value;
    int turn;
    node *node0;
    node *node1;
    node *node2;
    node(int d, int b, int sum) {
      turn = 0;
      depth = d;
      if (depth == b) {
        value = sum;
        return;
      }
      node0 = new node(d + 1, b, sum);
      node1 = new node(d + 1, b, sum + k3(d));
      node2 = new node(d + 1, b, sum + 2 * k3(d));
    }
  };
  node *root;
  int bottom;
  tree(int b) {
    bottom = b;
    root = new node(0, bottom, 0);
  }

  void turn() { ++(root->turn); }

  void add() { add(root); }

  void add(node *current) {
    if (current->depth == bottom)
      return;
    if (current->turn & 1) {
      swap(current->node1, current->node2);
      ++(current->node0->turn);
      ++(current->node1->turn);
      ++(current->node2->turn);
    }
    current->turn = 0;
    swap(current->node2, current->node1);
    swap(current->node1, current->node0);
    add(current->node0);
  }

  vector<int> to_string() {
    vector<int> ans(k3(bottom));
    to_string(ans, root, 0);
    return ans;
  }

  void to_string(vector<int> &res, node *current, int sum) {
    if (current->depth == bottom) {
      res[current->value] = sum;
      return;
    }
    if (current->turn & 1) {
      swap(current->node1, current->node2);
      ++(current->node0->turn);
      ++(current->node1->turn);
      ++(current->node2->turn);
    }
    current->turn = 0;
    to_string(res, current->node0, sum);
    to_string(res, current->node1, sum + k3(current->depth));
    to_string(res, current->node2, sum + 2 * k3(current->depth));
  }
};

int main() {
  int n = in();
  vector<int> line = inputs();
  tree t(n);
  foreach (i, line) {
    if (i) {
      t.turn();
    } else {
      t.add();
    }
  }
  vector<int> ans = t.to_string();
  foreach (i, ans) {
    output(i);
  }
  outendl();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, ...)                                                         \\
  for (int i = (a) * (strlen(#__VA_ARGS__) != 0);                              \\
       i < (int)(strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)); ++i)
#define per(i, a, ...)                                                         \\
  for (int i = (strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)) - 1;                 \\
       i >= (int)(strlen(#__VA_ARGS__) ? (a) : 0); --i)
#define foreach(i, n) for (auto &i : (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
using ll = long long;
// const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9 + 7;
const ll INFLL = (ll)1e18;
using namespace std;
template <class t> using vvector = vector<vector<t>>;
template <class t> using vvvector = vector<vector<vector<t>>>;
template <class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template <class t, class u> bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> bool chmin(t &a, u b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#ifdef DEBUG
#define debug(x)                                                               \\
  cout << "LINE " << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define debug(x) (void)0
#endif

namespace templates {
ll modpow(ll x, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    b >>= 1;
  }
  return res;
}

ll modinv(ll x) { return modpow(x, MOD - 2); }

bool was_output = false;
template <class t> void output(t a) {
  if (was_output)
    cout << " ";
  cout << a;
  was_output = true;
}
void outendl() {
  was_output = false;
  cout << endl;
}
ll in() {
  ll res;
  scanf("%lld", &res);
  return res;
}

template <class t> istream &operator>>(istream &is, vector<t> &x) {
  for (auto &i : x)
    is >> i;
  return is;
}

template <class t, class u> istream &operator>>(istream &is, pair<t, u> &x) {
  is >> x.first >> x.second;
  return is;
}

template <class t> void in(t &x) { cin >> x; }

template <class t> t in() {
  t res;
  cin >> res;
  return res;
}

template <class t> void out(t x) { cout << x; }

template <class t>
vector<t> sorted(
    vector<t> line,
    function<bool(t, t)> comp = [](t a, t b) { return a < b; }) {
  sort(line.begin(), line.end(), comp);
  return line;
}

template <class t> vector<t> reversed(vector<t> line) {
  reverse(line.begin(), line.end());
  return line;
}
} // namespace templates

using namespace templates;

vector<int> inputs() {
  string str = in<string>();
  vector<int> res;
  foreach (i, str) {
    if (i == 'S') {
      res.emplace_back(1);
    } else {
      res.emplace_back(0);
    }
  }
  return res;
}

vector<ll> k_3;

int turn(int x) {
  static vector<int> dp(400000, -1);
  if (x == 0)
    return 0;
  int &it = dp[x];
  if (it >= 0)
    return it;
  return it = turn(x / 3) * 3 + ((x % 3 == 0) ? 0 : (3 - x % 3));
}

int main() {
  int n = in();
  int n2 = n / 2;
  vector<int> line = inputs();
  k_3.emplace_back(1);
  rep(i, 20) { k_3.emplace_back(k_3.back() * 3); }
  vvector<int> moves(k_3[n2]);
  vector<int> turn_sum(line.size() + 1, 0);
  rep(i, line.size()) { turn_sum[i + 1] = turn_sum[i] + line[i]; }

  rep(i, k_3[n2]) {
    vector<int> &move = moves[i];
    move.emplace_back(i);
    int current = i;
    foreach (j, line) {
      if (j) {
        current = current / k_3[n2] * k_3[n2] + turn(current % k_3[n2]);
      } else {
        ++current;
      }
      move.emplace_back(current);
    }
  }

  vector<int> ans(k_3[n], -1);
  rep(i, k_3[n]) {
    int current = i;
    int index = current % k_3[n2];
    int p = 0;
    vector<int> &move = moves[index];
    while (p < move.size()) {
      int np = lower_bound(all(move), (move[p] + k_3[n2]) / k_3[n2] * k_3[n2]) -
               move.begin();
      if (np == move.size()) {
        if ((turn_sum.back() - turn_sum[p]) & 1) {
          int upper = current / k_3[n2];
          current = turn(upper) * k_3[n2] + current % k_3[n2];
        }
        current += move.back() - move[p];
        current %= k_3[n];
        break;
      }
      if ((turn_sum[np] - turn_sum[p]) & 1) {
        int upper = current / k_3[n2];
        current = turn(upper) * k_3[n2] + current % k_3[n2];
      }
      current = (current + k_3[n2]) / k_3[n2] * k_3[n2];
      current %= k_3[n];
      p = np;
    }
    ans[i] = current;
  }

  foreach (i, ans) {
    output(i);
  }
  outendl();

  return 0;
}