Problem p03081 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, N) for (i = 0; i < N; i++)
typedef long long ll;
typedef pair<int, int> P;
typedef struct {
  int first;
  int second;
  int third;
} T;

// 昇順
bool comp_Se(T &l, T &r) { return l.second < r.second; }

int N, Q;
string s;
vector<pair<char, char>> v;

int fall(int x) {
  if (x < 0)
    return 1;
  else if (x >= N)
    return 2;
  int i;
  REP(i, v.size()) {
    if (v[i].first == s[x]) {
      if (v[i].second == 'L')
        x--;
      else
        x++;
    }
    if (x < 0)
      return 1;
    else if (x >= N)
      return 2;
  }
  return 0;
}

// mode 0 左側に落ちない最小値，mode 1 右側に落ちる最小値
int binary_search(int start, int end, int mode) {
  while (true) {
    int mid = (start + end) / 2;
    int f = fall(mid);
    if (end - start <= 1)
      break;
    if (mode) {
      if (!f)
        start = mid;
      else
        end = mid;
    } else {
      if (!f)
        end = mid;
      else
        start = mid;
    }
  }
  return end;
}

int main(void) {
  int i, j;
  cin >> N >> Q;
  cin >> s;
  REP(i, Q) {
    char x, y;
    cin >> x >> y;
    v.push_back(pair<char, char>(x, y));
  }
  int start = 0, end = N, mid;

  // xが落ちない位置になるようにする．
  while (true) {
    mid = (start + end) / 2;
    int f = fall(mid);
    if (!f)
      break;
    else if (f == 1)
      start = mid;
    else
      end = mid;
    if (end - start < 1)
      break;
  }
  int S, E;
  // S左側に落ちない最初
  // E右側に落ちる最初
  if (fall(start) % 2 == 0)
    S = start;
  else
    S = binary_search(start, mid, 0);
  if (fall(end) != 2)
    E = end + 1;
  else
    E = binary_search(mid, end, 1);
  cout << E - S << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, N) for (i = 0; i < N; i++)
typedef long long ll;
typedef pair<int, int> P;
typedef struct {
  int first;
  int second;
  int third;
} T;

// 昇順
bool comp_Se(T &l, T &r) { return l.second < r.second; }

int N, Q;
string s;
vector<pair<char, char>> v;

int fall(int x) {
  if (x < 0)
    return 1;
  else if (x >= N)
    return 2;
  int i;
  REP(i, v.size()) {
    if (v[i].first == s[x]) {
      if (v[i].second == 'L')
        x--;
      else
        x++;
    }
    if (x < 0)
      return 1;
    else if (x >= N)
      return 2;
  }
  return 0;
}

// mode 0 左側に落ちない最小値，mode 1 右側に落ちる最小値
int binary_search(int start, int end, int mode) {
  while (true) {
    int mid = (start + end) / 2;
    int f = fall(mid);
    if (end - start <= 1)
      break;
    if (mode) {
      if (!f)
        start = mid;
      else
        end = mid;
    } else {
      if (!f)
        end = mid;
      else
        start = mid;
    }
  }
  return end;
}

int main(void) {
  int i, j;
  cin >> N >> Q;
  cin >> s;
  REP(i, Q) {
    char x, y;
    cin >> x >> y;
    v.push_back(pair<char, char>(x, y));
  }
  int start = 0, end = N, mid;

  // xが落ちない位置になるようにする．
  while (true) {
    mid = (start + end) / 2;
    int f = fall(mid);
    if (!f)
      break;
    else if (f == 1)
      start = mid + 1;
    else
      end = mid;
    if (end - start < 1)
      break;
  }
  int S, E;
  // S左側に落ちない最初
  // E右側に落ちる最初
  if (fall(start) % 2 == 0)
    S = start;
  else
    S = binary_search(start, mid, 0);
  if (fall(end) != 2)
    E = end + 1;
  else
    E = binary_search(mid, end, 1);
  cout << E - S << endl;
  return 0;
}