Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define inf 1e18 //(10^18
#define pqmax priority_queue<int>
#define mii map<int, int>
#define ps(x, y) fixed << setprecision(y) << x
// double ans = 1.012345668992
// cout<<ps(ans , 6)
// ans = 1.012345 // 6 decimal places
#define pqmin priority_queue<int, vi, greater<int>>
#define mk(arr, n, type) type *arr = new type[n];
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
// 48->11000(4) (returns no. of zeros after first one)
#define mod 1000000007 // 1e9 +7
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(arr , arr +n , rng)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// pbds s;
// s.order_of_key(4);
//*s.find_by_order(4);
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if ((s[i] != s[j]) && (s[i] != s[k]) && (s[j] != s[k]) &&
            (j - i) != (k - j)) {
          count++;
        }
      }
    }
  }
  cout << count << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define inf 1e18 //(10^18
#define pqmax priority_queue<int>
#define mii map<int, int>
#define ps(x, y) fixed << setprecision(y) << x
// double ans = 1.012345668992
// cout<<ps(ans , 6)
// ans = 1.012345 // 6 decimal places
#define pqmin priority_queue<int, vi, greater<int>>
#define mk(arr, n, type) type *arr = new type[n];
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
// 48->11000(4) (returns no. of zeros after first one)
#define mod 1000000007 // 1e9 +7
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(arr , arr +n , rng)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// pbds s;
// s.order_of_key(4);
//*s.find_by_order(4);
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int count = 0;
  map<char, int> m;
  for (int i = 0; i < n; i++) {
    m[s[i]]++;
  }
  int r = m['R'];
  int g = m['G'];
  int b = m['B'];
  count = r * g * b;
  for (int i = 0; i <= (n - 3) / 2; i++) {

    for (int j = 0; j + 2 * i + 2 < n; j++) {

      if (s[j] != s[j + 1 + i] && s[j] != s[j + 2 * i + 2] &&
          s[j + i + 1] != s[j + i * 2 + 2])
        count--;
    }
  }
  cout << count << "\n";
  return 0;
}