Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

#define ll long long
using vt = ll; // ここで数値の型を変えられる
#define rep(i, n) for (vt i = 0; i < (vt)(n); i++)
#define reps(i, s, n) for (vt i = (vt)(s); i < (vt)(n); i++)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
const ll MOD = 1000000007;

int main() {
  ll n;
  string s;
  cin >> n >> s;
  vector<ll> r, g, b;
  rep(i, n) {
    if (s.at(i) == 'R')
      r.push_back(i);
    else if (s.at(i) == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  if (r.empty() || g.empty() || b.empty()) {
    cout << 0;
    return 0;
  }
  ll ans = r.size() * g.size() * b.size();
  for (auto i : r) {
    for (auto j : g) {
      for (auto k : b) {
        if (i < k && j < k && abs(i - j) < k - max(i, j))
          break;
        vector<ll> v = {i, j, k};
        sort(all(v));
        if (v.at(2) - v.at(1) == v.at(1) - v.at(0))
          ans--;
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

#define ll long long
using vt = ll; // ここで数値の型を変えられる
#define rep(i, n) for (vt i = 0; i < (vt)(n); i++)
#define reps(i, s, n) for (vt i = (vt)(s); i < (vt)(n); i++)
#define all(v) v.begin(), v.end()
#define P pair<ll, ll>
const ll MOD = 1000000007;

int main() {
  ll n;
  string s;
  cin >> n >> s;
  vector<ll> r, g, b;
  rep(i, n) {
    if (s.at(i) == 'R')
      r.push_back(i);
    else if (s.at(i) == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  if (r.empty() || g.empty() || b.empty()) {
    cout << 0;
    return 0;
  }
  ll ans = r.size() * g.size() * b.size();
  for (auto i : r) {
    for (auto j : g) {
      ll x = max(i, j);
      ll y = min(i, j);
      if (binary_search(all(b), y - (x - y)))
        ans--;
      if (binary_search(all(b), x + (x - y)))
        ans--;
      if ((x - y) % 2 == 0) {
        if (binary_search(all(b), y + (x - y) / 2))
          ans--;
      }
    }
  }
  cout << ans;
  return 0;
}