Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e9
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;
string t;
int d[200010];
int n, q;
string s;
bool left(int p) {
  for (int i = 0; i < q; i++) {
    if (s[p] - 'A' == t[i] - 'A') {
      p += d[i];
      if (p < 0)
        return true;
      if (p >= n)
        return false;
    }
  }
  return false;
}
bool right(int p) {
  for (int i = 0; i < q; i++) {
    if (s[p] - 'A' == t[i] - 'A') {
      p += d[i];
      if (p >= n)
        return true;
      if (p < 0)
        return false;
    }
  }
  return false;
}

signed main() {

  cin >> n >> q;
  cin >> s;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
    char x;
    cin >> x;
    if (x == 'L')
      d[i] = -1;
    else
      d[i] = 1;
  }
  int ret = n;
  int l = -1, r = n;
  while (r - l > 1) {
    int mid = l + (r - l) / 2;
    if (left(mid))
      l = mid;
    else
      r = mid;
  }
  ret -= l;
  l = -1, r = n;
  while (r - l > 1) {
    int mid = l + (r - l) / 2;
    if (right(mid))
      r = mid;
    else
      l = mid;
  }
  ret -= n - l;
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e9
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}
template <class T> void show(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
typedef long long ll;
char t[200010];
int d[200010];
int n, q;
string s;
bool left(int p) {
  for (int i = 0; i < q; i++) {
    if (s[p] - 'A' == t[i] - 'A') {
      p += d[i];
      if (p < 0)
        return true;
      if (p >= n)
        return false;
    }
  }
  return false;
}
bool right(int p) {
  for (int i = 0; i < q; i++) {
    if (s[p] - 'A' == t[i] - 'A') {
      p += d[i];
      if (p >= n)
        return true;
      if (p < 0)
        return false;
    }
  }
  return false;
}

signed main() {

  cin >> n >> q;
  cin >> s;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
    char x;
    cin >> x;
    if (x == 'L')
      d[i] = -1;
    else
      d[i] = 1;
  }
  int ret = n;
  int l = -1, r = n;
  while (r - l > 1) {
    int mid = l + (r - l) / 2;
    if (left(mid))
      l = mid;
    else
      r = mid;
  }
  ret -= l;
  l = -1, r = n;
  while (r - l > 1) {
    int mid = l + (r - l) / 2;
    if (right(mid))
      r = mid;
    else
      l = mid;
  }
  ret -= n - l;
  cout << ret << endl;
  return 0;
}