Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string s;
  cin >> s;

  vvl cs(3);
  rep(i, n) {
    if (s[i] == 'R')
      cs[0].push_back(i);
    else if (s[i] == 'G')
      cs[1].push_back(i);
    else if (s[i] == 'B')
      cs[2].push_back(i);
  }
  ll ans = 0;
  rep(i, 3) {
    rep(j, 2) {
      int c1 = i, c2 = (c1 + j + 1) % 3, c3 = 3 - c1 - c2;
      for (auto r : cs[c1]) {
        auto it = lower_bound(rng(cs[c2]), r);
        for (; it != cs[c2].end(); ++it) {
          auto it2 = lower_bound(rng(cs[c3]), *it);
          ans += cs[c3].end() - it2;
          if (*lower_bound(rng(cs[c3]), 2 * (*it) - r) == 2 * (*it) - r)
            ans--;
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string s;
  cin >> s;

  vvl cs(3);
  rep(i, n) {
    if (s[i] == 'R')
      cs[0].push_back(i);
    else if (s[i] == 'G')
      cs[1].push_back(i);
    else if (s[i] == 'B')
      cs[2].push_back(i);
  }
  ll ans = 0;
  rep(i, 3) {
    rep(j, 2) {
      int c1 = i, c2 = (c1 + j + 1) % 3, c3 = 3 - c1 - c2;
      for (auto r : cs[c1]) {
        auto it = lower_bound(rng(cs[c2]), r);
        for (; it != cs[c2].end(); ++it) {
          auto it2 = lower_bound(rng(cs[c3]), *it);
          ans += cs[c3].end() - it2;
          auto it3 = lower_bound(rng(cs[c3]), 2 * (*it) - r);
          if (it3 != cs[c3].end() && *it3 == 2 * (*it) - r)
            ans--;
        }
      }
    }
  }
  cout << ans << endl;
}