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

#include "bits/stdc++.h"

using ll = long long;

using namespace std;
#define int ll

// 0 -> a-1
#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
// a -> b-1
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
// a-1 -> 0
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
// a-1 -> b
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
// next_permutation(all(v))
#define PERM(v) next_permutation(all(v))
/*sort(all(v));
(v).erase(unique(all(v)), v.end())*/
#define UNIQUE(v)                                                              \\
  sort(all(v));                                                                \\
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \\
  type x;                                                                      \\
  cin >> x
#define YES(f)                                                                 \\
  if ((f)) {                                                                   \\
    cout << "YES" << endl;                                                     \\
  } else {                                                                     \\
    cout << "NO" << endl;                                                      \\
  }
#define Yes(f)                                                                 \\
  if ((f)) {                                                                   \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }
#define MINV(v) min_element(all(v))
#define MAXV(v) max_element(all(v))
#define MIN3(a, b, c) min(min(a, b), c)
#define MIN4(a, b, c, d) min(MIN3(a, b, c), d)
#define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e)
#define MIN6(a, b, c, d, e, f) min(MIN5(a, b, c, d, e), f)
#define MAX3(a, b, c) max(max(a, b), c)
#define MAX4(a, b, c, d) max(MAX3(a, b, c), d)
#define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e)
#define MAX6(a, b, c, d, e, f) max(MAX5(a, b, c, d, e), f)
// b is [a, c)
#define RANGE(a, b, c) ((a) <= (b) && (b) < (c))
// c is [a, e) && d is [b, f)
#define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f)))
#define chmin(a, b) a = min(a, (b))
#define chmin3(a, b, c) a = MIN3(a, (b), (c))
#define chmin4(a, b, c, d) a = MIN4(a, (b), (c), (d))
#define chmin5(a, b, c, d, e) a = MIN5(a, (b), (c), (d), (e))
#define chmin6(a, b, c, d, e, f) a = MIN6(a, (b), (c), (d), (e), (f))
#define chmax(a, b) a = max(a, (b))
#define chmax3(a, b, c) a = MAX3(a, (b), (c))
#define chmax4(a, b, c, d) a = MAX4(a, (b), (c), (d))
#define chmax5(a, b, c, d, e) a = MAX5(a, (b), (c), (d), (e))
#define chmax6(a, b, c, d, e, f) a = MAX6(a, (b), (c), (d), (e), (f))
#define fcout cout << fixed << setprecision(12)
#define RS resize
#define CINV(v, N)                                                             \\
  do {                                                                         \\
    v.RS(N);                                                                   \\
    rep(i, N) cin >> v[i];                                                     \\
  } while (0);

#define RCINV(v, N)                                                            \\
  do {                                                                         \\
    v.RS(N);                                                                   \\
    rrep(i, N) cin >> v[i];                                                    \\
  } while (0);
#define MOD 1000000007

template <class T> inline T GET() {
  T x;
  cin >> x;
  return x;
}

void init();
void solve();

signed main() {
  init();
  solve();
}

struct RollingHash {
  typedef long long int_type;
  typedef pair<int_type, int_type> hash_type;

  int_type base1;
  int_type base2;
  int_type mod1;
  int_type mod2;
  vector<int_type> hash1;
  vector<int_type> hash2;
  vector<int_type> pow1;
  vector<int_type> pow2;

  RollingHash()
      : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {}

  void init(const string &s) {
    int n = s.size();

    hash1.assign(n + 1, 0);
    hash2.assign(n + 1, 0);
    pow1.assign(n + 1, 1);
    pow2.assign(n + 1, 1);

    for (int i = 0; i < n; i++) {
      hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
      hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
      pow1[i + 1] = pow1[i] * base1 % mod1;
      pow2[i + 1] = pow2[i] * base2 % mod2;
    }
  }

  hash_type get(int l, int r) {
    int_type t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
    int_type t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
    return make_pair(t1, t2);
  }

  RollingHash::hash_type concat(hash_type h1, hash_type h2, int h2_len) {
    return make_pair((h1.first * pow1[h2_len] + h2.first) % mod1,
                     (h1.second * pow2[h2_len] + h2.second) % mod2);
  }
};

void init() {
  RollingHash ro;
  string t;
  int n, m;
  cin >> n >> m >> t;
  ro.init(t);
  set<pair<ll, ll>> s;
  int l = 0, r = 0;
  rep(i, m) {
    string a;
    cin >> a;
    if (a == "R++")
      r++;
    if (a == "R--")
      r--;
    if (a == "L++")
      l++;
    if (a == "L--")
      l--;
    auto x = ro.get(l, r + 1);
    s.insert(x);
  }
  cout << s.size() << endl;
}

void solve() {}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PRINT(f)                                                               \\
  if ((f)) {                                                                   \\
    cout << (TRUE__) << endl;                                                  \\
  } else {                                                                     \\
    cout << FALSE__ << endl;                                                   \\
  }
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \\
  sort(all(v));                                                                \\
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \\
  type x;                                                                      \\
  cin >> x

#ifdef LOCAL
#define lcout(a) cout << a;
#define lcoutln(a) cout << a << endl;
#define lcerr(a) cerr << a;
#define lcerrln(a) cerr << a << endl;
#else
#define lcout(a)
#define lcoutln(a)
#define lcerr(a)
#define lcerrln(a)
#endif

vector<vector<pair<int, int>>> v(100001);
string s;
int n, m;

bool comp(pair<int, int> p1, pair<int, int> p2) {
  int l1 = p1.first;
  int r1 = p1.second;
  int l2 = p2.first;
  reps(i, l1, r1) {
    if (s[i] < s[l2])
      return true;
    if (s[i] > s[l2])
      return false;
    l2++;
  }
  return false;
}

bool comp2(pair<int, int> p1, pair<int, int> p2) {
  int l1 = p1.first;
  int r1 = p1.second;
  int l2 = p2.first;
  reps(i, l1, r1) {
    if (s[i] != s[l2])
      return false;
    l2++;
  }
  return true;
}

int main() {
  cin >> n >> m >> s;
  int l = 0;
  int r = 1;
  int ma = 0;
  rep(i, m) {
    CIN(string, st);
    if (st[0] == 'R') {
      if (st[1] == '+') {
        r++;
      } else {
        r--;
      }
    } else if (st[0] == 'L') {
      if (st[1] == '+') {
        l++;
      } else {
        l--;
      }
    }
    v[r - l].PB(MP(l, r));
    ma = max(ma, r - l);
  }
  int res = 0;
  reps(i, 1, ma + 1) {
    sort(all(v[i]), comp);
    v[i].erase(unique(all(v[i]), comp2), v[i].end());
    res += v[i].size();
  }
  cout << res << endl;
}