Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  if (n <= 3) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> r, g, b;
  rep(i, 0, n) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else if (s[i] == 'B')
      b.push_back(i);
  }
  ll an = 0;
  rep(i, 0, n - 2) {
    rep(j, i + 1, n - 1) {
      if (s[i] == s[j])
        continue;
      if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {
        ll tm = b.end() - lower_bound(b.begin(), b.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(b.begin(), b.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      } else if ((s[i] == 'B' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'B')) {
        ll tm = r.end() - lower_bound(r.begin(), r.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(r.begin(), r.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      } else if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')) {
        ll tm = g.end() - lower_bound(g.begin(), g.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(g.begin(), g.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      }
    }
  }
  cout << an << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  if (n <= 3) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> r, g, b;
  rep(i, 0, n) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else if (s[i] == 'B')
      b.push_back(i);
  }
  if (r.size() * b.size() * g.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  ll an = 0;
  rep(i, 0, n - 2) {
    rep(j, i + 1, n - 1) {
      if (s[i] == s[j])
        continue;
      if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {
        ll tm = b.end() - lower_bound(b.begin(), b.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(b.begin(), b.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      } else if ((s[i] == 'B' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'B')) {
        ll tm = r.end() - lower_bound(r.begin(), r.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(r.begin(), r.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      } else if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')) {
        ll tm = g.end() - lower_bound(g.begin(), g.end(), j + 1);
        an += tm;
        if (j + (j - i) < n) {
          auto upp = lower_bound(g.begin(), g.end(), 2 * j - i);
          ll tmp = *upp;
          if (upp != b.end() && tmp == 2 * j - i)
            an--;
        }
      }
    }
  }
  cout << an << endl;
}