Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int n;
  std::cin >> n;
  string s;
  std::cin >> s;
  vi r, g, b;
  rep(i, n) {
    if (s[i] == 'R')
      r.pb(i);
    else if (s[i] == 'G')
      g.pb(i);
    else
      b.pb(i);
  }
  int rsz = r.size(), gsz = g.size(), bsz = b.size();
  ll ans = 0;
  for (int p1 : r)
    for (int p2 : g) {
      int pp1 = p1;
      if (pp1 > p2)
        swap(pp1, p2);
      int diff = p2 - pp1;
      ans += bsz;
      if (binary_search(rng(b), pp1 - diff))
        ans--;
      if (diff % 2 == 0 && binary_search(rng(b), pp1 + diff / 2))
        ans--;
      if (binary_search(rng(b), p2 + diff))
        ans--;
    }
  int tmp = 0;
  rep(i, rsz) rep(j, gsz) rep(k, bsz) {
    vi vec = {r[i], g[j], b[k]};
    sort(rng(vec));
    int d1 = vec[1] - vec[0], d2 = vec[2] - vec[1];
    if (d1 != d2)
      tmp++;
  }
  std::cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int n;
  std::cin >> n;
  string s;
  std::cin >> s;
  vi r, g, b;
  rep(i, n) {
    if (s[i] == 'R')
      r.pb(i);
    else if (s[i] == 'G')
      g.pb(i);
    else
      b.pb(i);
  }
  int rsz = r.size(), gsz = g.size(), bsz = b.size();
  ll ans = 0;
  for (int p1 : r)
    for (int p2 : g) {
      int pp1 = p1;
      if (pp1 > p2)
        swap(pp1, p2);
      int diff = p2 - pp1;
      ans += bsz;
      if (binary_search(rng(b), pp1 - diff))
        ans--;
      if (diff % 2 == 0 && binary_search(rng(b), pp1 + diff / 2))
        ans--;
      if (binary_search(rng(b), p2 + diff))
        ans--;
    }
  std::cout << ans << "\n";
}