Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;
static const ll INF = 1e18;
static const ll MAX = 1e5 + 7;
static const ll MOD = 1e9 + 7;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

ll N, Q;
string s;
char t[MAX], d[MAX];

ll L(ll now) {
  ll i;
  for (i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now--;
      if (d[i] == 'R')
        now++;

      if (now < 0)
        return 1;
    }
  }

  return 0;
}

ll R(ll now) {
  ll i;
  for (i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now--;
      if (d[i] == 'R')
        now++;

      if (now >= N)
        return 1;
    }
  }

  return 0;
}

int main(void) {
  cin >> N >> Q;
  cin >> s;
  ll i, j;

  for (i = 0; i < Q; i++)
    cin >> t[i] >> d[i];

  ll l = 0, r = N;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (L(mid))
      l = mid;
    else
      r = mid;
  }

  ll le = l;

  l = 0;
  r = N;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (R(mid))
      r = mid;
    else
      l = mid;
  }
  ll ri = l;

  pt(max(0, ri - le));
}


Fixed submission:
#include <bits/stdc++.h>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;
static const ll INF = 1e18;
static const ll MAX = 1e5 + 7;
static const ll MOD = 1e9 + 7;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

ll N, Q;
string s;
char t[MAX * 2], d[MAX * 2];

ll L(ll now) {
  ll i;
  for (i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now--;
      if (d[i] == 'R')
        now++;

      if (now < 0)
        return 1;
    }
  }

  return 0;
}

ll R(ll now) {
  ll i;
  for (i = 0; i < Q; i++) {
    if (s[now] == t[i]) {
      if (d[i] == 'L')
        now--;
      if (d[i] == 'R')
        now++;

      if (now >= N)
        return 1;
    }
  }

  return 0;
}

int main(void) {
  cin >> N >> Q;
  cin >> s;
  ll i, j;

  for (i = 0; i < Q; i++)
    cin >> t[i] >> d[i];

  ll l = 0, r = N;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (L(mid))
      l = mid;
    else
      r = mid;
  }

  ll le = l;

  l = 0;
  r = N;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (R(mid))
      r = mid;
    else
      l = mid;
  }
  ll ri = l;

  pt(max(0, ri - le));
}