Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
typedef long int l;
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1)
#define endl '\n'
#define N 1000000007 // prime modulo value
#define all(x) x.begin(), x.end()
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0)
using namespace std;
#define MAX 1000001
l spf[MAX];
void precompute() {
  spf[1] = 1;
  for (l i = 2; i < MAX; i++)
    spf[i] = i;

  for (l i = 4; i < MAX; i += 2)
    spf[i] = 2;

  for (l i = 3; i * i < MAX; i++) {
    if (spf[i] == i) {
      for (l j = i * i; j < MAX; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  FAST;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> red;
  vector<ll> green;
  vector<ll> blue;
  f(i, 0, n) {
    if (s[i] == 'R')
      red.push_back(i);
    else if (s[i] == 'B')
      blue.push_back(i);
    else
      green.push_back(i);
  }
  ll r = red.size(), g = green.size(), b = blue.size();
  ll ans = 0;
  f(i, 0, r) {
    f(j, 0, g) {
      f(k, 0, b) {
        ll mini = min(red[i], min(green[j], blue[k]));
        ll maxi = max(red[i], max(green[j], blue[k]));
        ll mid = red[i] + green[j] + blue[k] - mini - maxi;
        if ((2 * mid) != mini + maxi)
          ans += 1;
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
typedef long int l;
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1)
#define endl '\n'
#define N 1000000007 // prime modulo value
#define all(x) x.begin(), x.end()
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0)
using namespace std;
#define MAX 1000001
l spf[MAX];
void precompute() {
  spf[1] = 1;
  for (l i = 2; i < MAX; i++)
    spf[i] = i;

  for (l i = 4; i < MAX; i += 2)
    spf[i] = 2;

  for (l i = 3; i * i < MAX; i++) {
    if (spf[i] == i) {
      for (l j = i * i; j < MAX; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  FAST;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> red;
  vector<ll> green;
  vector<ll> blue;
  f(i, 0, n) {
    if (s[i] == 'R')
      red.push_back(i);
    else if (s[i] == 'B')
      blue.push_back(i);
    else
      green.push_back(i);
  }
  ll r = red.size(), g = green.size(), b = blue.size();
  ll ans = 0;
  f(i, 0, r) {
    f(j, 0, g) {
      f(k, 0, b) {
        ll mini = min(red[i], min(green[j], blue[k]));
        ll maxi = max(red[i], max(green[j], blue[k]));
        ll mid = red[i] + green[j] + blue[k] - mini - maxi;
        if ((2 * mid) != mini + maxi)
          ans += 1;
      }
    }
  }
  cout << ans;
  return 0;
}