Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define INF INT_MAX
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
#define sec second
#define fir first
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> pque;
typedef vector<int> Vec;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
};

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

  int n;
  string s;
  cin >> n >> s;
  Vec r;
  Vec g;
  Vec b;
  rep(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }

  int ans = 0;
  rep(i, r.size()) {
    rep(j, g.size()) {
      rep(k, b.size()) {
        int d1 = abs(r[i] - g[j]);
        int d2 = abs(g[j] - b[k]);
        int d3 = abs(r[i] - b[k]);
        if (d1 != d2 && d2 != d3 && d1 != d3)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define INF INT_MAX
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
#define sec second
#define fir first
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> pque;
typedef vector<int> Vec;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
};

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

  int n;
  string s;
  cin >> n >> s;
  Vec r;
  Vec g;
  Vec b;
  rep(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }

  int ans = 0;
  rep(i, r.size()) {
    rep(j, g.size()) {
      int d = abs(r[i] - g[j]);
      int L = min(r[i], g[j]);
      int R = max(r[i], g[j]);
      ans += b.size();
      if (L - d >= 0 && s[L - d] == 'B')
        ans--;
      if (R + d < n && s[R + d] == 'B')
        ans--;
      if (d % 2 == 0 && s[L + d / 2] == 'B')
        ans--;
    }
  }
  cout << ans << endl;
  return 0;
}