Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long

using namespace std;

const int N = 1e3 + 7;
const int INF = 1e18;
const int mod = 1e9 + 7;

int R[N], G[N], B[N], n;

string s;

int check(int k, char c) {

  if (k >= n) {
    return 0;
  }
  return (s[k] == c);
}

int32_t main() {
  speed;
  cin >> n;
  cin >> s;

  for (int i = n - 1; i >= 0; i--) {

    R[i] += R[i + 1];
    G[i] += G[i + 1];
    B[i] += B[i + 1];

    if (s[i] == 'R') {
      R[i]++;
    }
    if (s[i] == 'G') {
      G[i]++;
    }
    if (s[i] == 'B') {
      B[i]++;
    }
  }
  int ans = 0;

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

      if (s[i] == s[j]) {
        continue;
      }
      int d = j - i;
      int k = j + d;

      if (j + 1 >= n) {
        continue;
      }
      if (s[i] == 'R' && s[j] == 'G') {
        ans += B[j + 1];

        ans -= check(k, 'B');
      }
      if (s[i] == 'R' && s[j] == 'B') {
        ans += G[j + 1];

        ans -= check(k, 'G');
      }
      if (s[i] == 'G' && s[j] == 'R') {
        ans += B[j + 1];

        ans -= check(k, 'B');
      }
      if (s[i] == 'G' && s[j] == 'B') {
        ans += R[j + 1];

        ans -= check(k, 'R');
      }
      if (s[i] == 'B' && s[j] == 'R') {
        ans += G[j + 1];

        ans -= check(k, 'G');
      }
      if (s[i] == 'B' && s[j] == 'G') {
        ans += R[j + 1];

        ans -= check(k, 'R');
      }
      // for ( int k = j + 1; k < n; k++ ) {

      // 	if ( j - i == k - j || s[i] == s[j] || s[i] == s[k] || s[j] ==
      // s[k] ) { 		continue;
      // 	}
      // 	ans++;
      // }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long

using namespace std;

const int N = 1e5 + 7;
const int INF = 1e18;
const int mod = 1e9 + 7;

int R[N], G[N], B[N], n;

string s;

int check(int k, char c) {

  if (k >= n) {
    return 0;
  }
  return (s[k] == c);
}

int32_t main() {
  speed;
  cin >> n;
  cin >> s;

  for (int i = n - 1; i >= 0; i--) {

    R[i] += R[i + 1];
    G[i] += G[i + 1];
    B[i] += B[i + 1];

    if (s[i] == 'R') {
      R[i]++;
    }
    if (s[i] == 'G') {
      G[i]++;
    }
    if (s[i] == 'B') {
      B[i]++;
    }
  }
  int ans = 0;

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

      if (s[i] == s[j]) {
        continue;
      }
      int d = j - i;
      int k = j + d;

      if (j + 1 >= n) {
        continue;
      }
      if (s[i] == 'R' && s[j] == 'G') {
        ans += B[j + 1];

        ans -= check(k, 'B');
      }
      if (s[i] == 'R' && s[j] == 'B') {
        ans += G[j + 1];

        ans -= check(k, 'G');
      }
      if (s[i] == 'G' && s[j] == 'R') {
        ans += B[j + 1];

        ans -= check(k, 'B');
      }
      if (s[i] == 'G' && s[j] == 'B') {
        ans += R[j + 1];

        ans -= check(k, 'R');
      }
      if (s[i] == 'B' && s[j] == 'R') {
        ans += G[j + 1];

        ans -= check(k, 'G');
      }
      if (s[i] == 'B' && s[j] == 'G') {
        ans += R[j + 1];

        ans -= check(k, 'R');
      }
      // for ( int k = j + 1; k < n; k++ ) {

      // 	if ( j - i == k - j || s[i] == s[j] || s[i] == s[k] || s[j] ==
      // s[k] ) { 		continue;
      // 	}
      // 	ans++;
      // }
    }
  }
  cout << ans;
}