Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define SHOWP(x) cerr << "[" << (x).FI << ", " << (x).SE << "]";
#define SHOWX(x) cerr << #x << ": " << x << endl;
#define SHOWVEC(v, e)                                                          \
  REP(i, e) cerr << (v[i]) << ' ';                                             \
  cerr << endl;
#define SHOW2D(a, h, w)                                                        \
  REP(i, h) {                                                                  \
    REP(j, w) cerr << setw(3) << (a[i][j]) << ' ';                             \
    cerr << endl;                                                              \
  }
constexpr int INF = 1 << 30 - 1;
constexpr long long LLINF = 1LL << 60;
constexpr long long MOD = 1000000007;

typedef long long ll;
typedef pair<ll, ll> P;

char t[110000], d[110000];
int N, Q;
string s;

int sim(int idx) {
  REP(i, Q) {
    if (s[idx] == t[i]) {
      if (d[i] == 'L')
        idx--;
      else
        idx++;
    }
    if (idx < 0 || idx >= N)
      return idx;
  }
  return idx;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  REP(i, Q) cin >> t[i] >> d[i];

  int l, r;

  int cnt = 0;
  l = -1, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (sim(mid) >= N)
      r = mid;
    else
      l = mid;
  }
  cnt += N - r;

  l = -1, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (sim(mid) < 0)
      l = mid;
    else
      r = mid;
  }
  cnt += l + 1;
  cout << (N - cnt) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define SHOWP(x) cerr << "[" << (x).FI << ", " << (x).SE << "]";
#define SHOWX(x) cerr << #x << ": " << x << endl;
#define SHOWVEC(v, e)                                                          \
  REP(i, e) cerr << (v[i]) << ' ';                                             \
  cerr << endl;
#define SHOW2D(a, h, w)                                                        \
  REP(i, h) {                                                                  \
    REP(j, w) cerr << setw(3) << (a[i][j]) << ' ';                             \
    cerr << endl;                                                              \
  }
constexpr int INF = 1 << 30 - 1;
constexpr long long LLINF = 1LL << 60;
constexpr long long MOD = 1000000007;

typedef long long ll;
typedef pair<ll, ll> P;

char t[210000], d[210000];
int N, Q;
string s;

int sim(int idx) {
  REP(i, Q) {
    if (s[idx] == t[i]) {
      if (d[i] == 'L')
        idx--;
      else
        idx++;
    }
    if (idx < 0 || idx >= N)
      return idx;
  }
  return idx;
}

int main() {
  cin >> N >> Q;
  cin >> s;
  REP(i, Q) cin >> t[i] >> d[i];

  int l, r;

  int cnt = 0;
  l = -1, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (sim(mid) >= N)
      r = mid;
    else
      l = mid;
  }
  cnt += N - r;

  l = -1, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (sim(mid) < 0)
      l = mid;
    else
      r = mid;
  }
  cnt += l + 1;
  cout << (N - cnt) << endl;

  return 0;
}