Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;

bool red[4005];
bool green[4005];
bool blue[4005];

int rr[10005];
int gg[10005];
int bb[10005];

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      red[i] = true;
    else if (s[i] == 'G')
      green[i] = true;
    else
      blue[i] = true;
  }

  for (int j = n - 1; j >= 0; j--) {
    if (s[j] == 'R')
      rr[j]++;
    if (s[j] == 'G')
      gg[j]++;
    if (s[j] == 'B')
      bb[j]++;
    rr[j] += rr[j + 1];
    gg[j] += gg[j + 1];
    bb[j] += bb[j + 1];
  }

  ll ans = 0;
  // cout << rr[0] <<"  "<<gg[0]<<"  "<<bb[0] << endl;

  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'R') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'R')
          continue;
        if (s[j] == 'G') {
          ans += bb[j + 1];
          if (blue[2 * j - i])
            ans--;
        }
        if (s[j] == 'B') {
          ans += gg[j + 1];
          if (green[2 * j - i])
            ans--;
        }
      }
    }

    if (s[i] == 'G') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'G')
          continue;
        if (s[j] == 'R') {
          ans += bb[j + 1];
          if (blue[2 * j - i])
            ans--;
        }
        if (s[j] == 'B') {
          ans += rr[j + 1];
          if (red[2 * j - i])
            ans--;
        }
      }
    }

    if (s[i] == 'B') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'B')
          continue;
        if (s[j] == 'G') {
          ans += rr[j + 1];
          if (red[2 * j - i])
            ans--;
        }
        if (s[j] == 'R') {
          ans += gg[j + 1];
          if (green[2 * j - i])
            ans--;
        }
      }
    }
    //  cout << ans << endl;
  }

  cout << ans << endl;
}


Fixed submission:
#include <cstring>
#include <iostream>

using namespace std;
typedef long long ll;

bool red[10005];
bool green[10005];
bool blue[10005];

int rr[10005];
int gg[10005];
int bb[10005];

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      red[i] = true;
    else if (s[i] == 'G')
      green[i] = true;
    else
      blue[i] = true;
  }

  for (int j = n - 1; j >= 0; j--) {
    if (s[j] == 'R')
      rr[j]++;
    if (s[j] == 'G')
      gg[j]++;
    if (s[j] == 'B')
      bb[j]++;
    rr[j] += rr[j + 1];
    gg[j] += gg[j + 1];
    bb[j] += bb[j + 1];
  }

  ll ans = 0;
  // cout << rr[0] <<"  "<<gg[0]<<"  "<<bb[0] << endl;

  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'R') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'R')
          continue;
        if (s[j] == 'G') {
          ans += bb[j + 1];
          if (blue[2 * j - i])
            ans--;
        }
        if (s[j] == 'B') {
          ans += gg[j + 1];
          if (green[2 * j - i])
            ans--;
        }
      }
    }

    if (s[i] == 'G') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'G')
          continue;
        if (s[j] == 'R') {
          ans += bb[j + 1];
          if (blue[2 * j - i])
            ans--;
        }
        if (s[j] == 'B') {
          ans += rr[j + 1];
          if (red[2 * j - i])
            ans--;
        }
      }
    }

    if (s[i] == 'B') {

      for (int j = i + 1; j < n - 1; j++) {
        if (s[j] == 'B')
          continue;
        if (s[j] == 'G') {
          ans += rr[j + 1];
          if (red[2 * j - i])
            ans--;
        }
        if (s[j] == 'R') {
          ans += gg[j + 1];
          if (green[2 * j - i])
            ans--;
        }
      }
    }
    //  cout << ans << endl;
  }

  cout << ans << endl;
}