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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mae(v) max_element(v.begin(), v.end())
#define mie(v) min_element(v.begin(), v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
typedef long long ll;
typedef unsigned long long ull;
////////////////////////////////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;

  vector<ull> pow(300010), hs(300010), ans;

  ull b = mod;

  pow[0] = 1;
  hs[0] = 0;

  rep(i, 0, s.size()) {
    pow[i + 1] = pow[i] * b;
    hs[i + 1] = (hs[i] * b + s[i]);
  }

  ll l = 0, r = 1;

  rep(i, 0, m) {
    string in;
    cin >> in;
    if (in == "R++")
      r++;
    else if (in == "R--")
      r--;
    else if (in == "L++")
      l++;
    else
      l--;

    ull num = hs[r] - hs[l] * pow[r - l];
    ans.push_back(num);
  }

  vsort(ans);

  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  cout << ans.size() << endl;

  ret 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define P(p) cout << (p) << endl
#define rep(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(), greater<int>());
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define ret return
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define mae(v) max_element(v.begin(), v.end())
#define mie(v) min_element(v.begin(), v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
typedef long long ll;
////////////////////////////////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  int l = 0, r = 0;
  map<string, bool> mp;
  rep(i, 0, m) {
    string in;
    cin >> in;
    if (in == "R++")
      r++;
    else if (in == "R--")
      r--;
    else if (in == "L++")
      l++;
    else
      l--;

    l = max(l, 0);
    l = min(l, n - 1);
    r = max(r, 0);
    r = min(r, n);

    string t = "";

    if (l <= r) {
      rep(i, l, r + 1) t += s[i];
    } else {
      rrep(i, l, r) t += s[i];
    }

    // cout << t << endl;

    mp[t] = 1;
  }

  cout << mp.size() << endl;
  ret 0;
}