Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  if (n <= 3) {
    cout << 0 << endl;
    return 0;
  }
  string s;
  cin >> s;
  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      r.push_back(i);
    } else if (s[i] == 'G') {
      g.push_back(i);
    } else {
      b.push_back(i);
    }
  }

  set<vector<int>> check;
  //  cout << r.size() << endl;
  //  cout << g.size() << endl;
  //  cout << b.size() << endl;
  //  cout << (int)r.size() * (int)b.size() * (int) g.size() << endl;
  ll sum = (ll)r.size() * (ll)b.size() * (ll)g.size();
  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)g.size(); j++) {
      int rg = r[i] + g[j];
      if (rg % 2 == 1) {
        continue;
      }
      if (rg / 2 > b.back()) {
        break;
      }
      sum -= (int)(binary_search(b.begin(), b.end(), rg / 2));
    }
  }

  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      int rb = r[i] + b[j];
      if (rb % 2 == 1) {
        continue;
      }
      if (rb / 2 > g.back()) {
        break;
      }
      sum -= (int)(binary_search(g.begin(), g.end(), rb / 2));
    }
  }
  for (int i = 0; i < (int)g.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      int gb = g[i] + b[j];
      if (gb % 2 == 1) {
        continue;
      }
      if (gb / 2 > r.back()) {
        break;
      }
      sum -= (int)(binary_search(r.begin(), r.end(), gb / 2));
    }
  }

  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  if (n <= 3) {
    cout << 0 << endl;
    return 0;
  }
  string s;
  cin >> s;
  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      r.push_back(i);
    } else if (s[i] == 'G') {
      g.push_back(i);
    } else {
      b.push_back(i);
    }
  }

  set<vector<int>> check;
  //  cout << r.size() << endl;
  //  cout << g.size() << endl;
  //  cout << b.size() << endl;
  //  cout << (int)r.size() * (int)b.size() * (int) g.size() << endl;
  ll sum = (ll)r.size() * (ll)b.size() * (ll)g.size();
  if (sum == 0) {
    cout << sum << endl;
    return 0;
  }
  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)g.size(); j++) {
      int rg = r[i] + g[j];
      if (rg % 2 == 1) {
        continue;
      }
      if (rg / 2 > b.back()) {
        break;
      }
      sum -= (int)(binary_search(b.begin(), b.end(), rg / 2));
    }
  }

  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      int rb = r[i] + b[j];
      if (rb % 2 == 1) {
        continue;
      }
      if (rb / 2 > g.back()) {
        break;
      }
      sum -= (int)(binary_search(g.begin(), g.end(), rb / 2));
    }
  }
  for (int i = 0; i < (int)g.size(); i++) {
    for (int j = 0; j < (int)b.size(); j++) {
      int gb = g[i] + b[j];
      if (gb % 2 == 1) {
        continue;
      }
      if (gb / 2 > r.back()) {
        break;
      }
      sum -= (int)(binary_search(r.begin(), r.end(), gb / 2));
    }
  }

  cout << sum << endl;
  return 0;
}