Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 20005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}

int N, Q;
char s[MAXN];
pii op[MAXN];
int getplace(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (s[p] == 'A' + op[i].fi) {
      p += op[i].se;
    }
  }
  return p;
}
int findL() {
  int L = 0, R = N;
  while (L < R) {
    int mid = (L + R + 1) >> 1;
    if (getplace(mid) == 0)
      L = mid;
    else
      R = mid - 1;
  }
  return L;
}
int findR() {
  int L = 1, R = N + 1;
  while (L < R) {
    int mid = (L + R) >> 1;
    if (getplace(mid) == N + 1)
      R = mid;
    else
      L = mid + 1;
  }
  return L;
}
void Solve() {
  read(N);
  read(Q);
  scanf("%s", s + 1);
  char t[5], d[5];
  for (int i = 1; i <= Q; ++i) {
    scanf("%s%s", t + 1, d + 1);
    int a, b;
    if (d[1] == 'L')
      b = -1;
    else
      b = 1;
    a = t[1] - 'A';
    op[i] = mp(a, b);
  }
  int L = findL(), R = findR();
  out(R - L - 1);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}

int N, Q;
char s[MAXN];
pii op[MAXN];
int getplace(int p) {
  for (int i = 1; i <= Q; ++i) {
    if (s[p] == 'A' + op[i].fi) {
      p += op[i].se;
    }
  }
  return p;
}
int findL() {
  int L = 0, R = N;
  while (L < R) {
    int mid = (L + R + 1) >> 1;
    if (getplace(mid) == 0)
      L = mid;
    else
      R = mid - 1;
  }
  return L;
}
int findR() {
  int L = 1, R = N + 1;
  while (L < R) {
    int mid = (L + R) >> 1;
    if (getplace(mid) == N + 1)
      R = mid;
    else
      L = mid + 1;
  }
  return L;
}
void Solve() {
  read(N);
  read(Q);
  scanf("%s", s + 1);
  char t[5], d[5];
  for (int i = 1; i <= Q; ++i) {
    scanf("%s%s", t + 1, d + 1);
    int a, b;
    if (d[1] == 'L')
      b = -1;
    else
      b = 1;
    a = t[1] - 'A';
    op[i] = mp(a, b);
  }
  int L = findL(), R = findR();
  out(R - L - 1);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}