Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bits/stdc++.h> // isupper, islower, isdigit, toupper, tolower
#include <bitset>        // bitset
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#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
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pl;
typedef pair<int, int> P;

int main() {
  int N;
  string S;
  cin >> N >> S;

  ll R = 0;
  ll G = 0;
  ll B = 0;
  ll ans = 0;
  vector<char> nvec; // Nの各桁の数字を格納するベクター

  rep(i, N) {
    nvec.push_back(S.at(i));
    if (S.at(i) == 'R') {
      R++;
    }
    if (S.at(i) == 'G') {
      G++;
    }
    if (S.at(i) == 'B') {
      B++;
    }
  }
  ans = R * G * B;
  ll condition2 = 0;
  rep(i, N) {
    rep2(j, i + 1, N) {
      rep2(k, j + 1, N) {
        if (j - i == k - j) {
          if (S.at(i) != S.at(j) && S.at(j) != S.at(k) && S.at(k) != S.at(i)) {
            condition2++;
          }
        }
      }
    }
  }

  cout << ans - condition2 << endl;

  return 0;
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bits/stdc++.h> // isupper, islower, isdigit, toupper, tolower
#include <bitset>        // bitset
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#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
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pl;
typedef pair<int, int> P;

int main() {
  int N;
  string S;
  cin >> N >> S;

  ll R = 0;
  ll G = 0;
  ll B = 0;
  ll ans = 0;
  vector<char> nvec; // Nの各桁の数字を格納するベクター

  rep(i, N) {
    nvec.push_back(S.at(i));
    if (S.at(i) == 'R') {
      R++;
    }
    if (S.at(i) == 'G') {
      G++;
    }
    if (S.at(i) == 'B') {
      B++;
    }
  }
  ans = R * G * B;
  ll condition2 = 0;
  rep(i, N) {
    rep2(j, i + 1, N) {
      ll k = 2 * j - i;
      if (k >= N) {
        continue;
      } else {
        if (S.at(i) != S.at(j) && S.at(j) != S.at(k) && S.at(k) != S.at(i)) {
          condition2++;
        }
      }
    }
  }

  cout << ans - condition2 << endl;

  return 0;
}