Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define fill0(n) setfill('0') << right << setw(n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define LONGMAX 1e18
#define INTMAX 1000000000
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const long double EPS = 0.0000000001;

int n;
string s;
vector<int> blue(4001), green(4001), red(4001);

int calc(int i, int j) {
  int ret;
  if (s[i] == 'R' and s[j] == 'G') {
    ret = blue[j];
    if (s[j + (j - i)] == 'B')
      ret--;
  } else if (s[i] == 'R' and s[j] == 'B') {
    ret = green[j];
    if (s[j + (j - i)] == 'G')
      ret--;
  } else if (s[i] == 'G' and s[j] == 'B') {
    ret = red[j];
    if (s[j + (j - i)] == 'R')
      ret--;
  } else if (s[i] == 'G' and s[j] == 'R') {
    ret = blue[j];
    if (s[j + (j - i)] == 'B')
      ret--;
  } else if (s[i] == 'B' and s[j] == 'R') {
    ret = green[j];
    if (s[j + (j - i)] == 'G')
      ret--;
  } else if (s[i] == 'B' and s[j] == 'G') {
    ret = red[j];
    if (s[j + (j - i)] == 'R')
      ret--;
  }
  return ret;
}

int main() {
  cin >> n;
  cin >> s;
  ll ans = 0;

  blue[n] = 0;
  green[n] = 0;
  red[n] = 0;
  per(i, 1, n) {
    if (s[i] == 'R') {
      red[i - 1] += red[i] + 1;
      blue[i - 1] += blue[i];
      green[i - 1] += green[i];
    } else if (s[i] == 'G') {
      red[i - 1] += red[i];
      blue[i - 1] += blue[i];
      green[i - 1] += green[i] + 1;
    } else {
      red[i - 1] += red[i];
      blue[i - 1] += blue[i] + 1;
      green[i - 1] += green[i];
    }
  }
  // for(int i = 0; i < n+1; i++)cout << red[i] << " ";
  // cout << endl;
  // for(int i = 0; i < n+1; i++)cout << blue[i] << " ";
  // cout << endl;
  // for(int i = 0; i < n+1; i++)cout << green[i] << " ";
  // cout << endl;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] == s[j])
        continue;
      // cout << i << " " << j << endl;
      ans += calc(i, j);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define fill0(n) setfill('0') << right << setw(n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define LONGMAX 1e18
#define INTMAX 1000000000
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const long double EPS = 0.0000000001;

int n;
string s;
vector<int> blue(5000), green(5000), red(5000);

int calc(int i, int j) {
  int ret;
  if (s[i] == 'R' and s[j] == 'G') {
    ret = blue[j];
    if (s[j + (j - i)] == 'B')
      ret--;
  } else if (s[i] == 'R' and s[j] == 'B') {
    ret = green[j];
    if (s[j + (j - i)] == 'G')
      ret--;
  } else if (s[i] == 'G' and s[j] == 'B') {
    ret = red[j];
    if (s[j + (j - i)] == 'R')
      ret--;
  } else if (s[i] == 'G' and s[j] == 'R') {
    ret = blue[j];
    if (s[j + (j - i)] == 'B')
      ret--;
  } else if (s[i] == 'B' and s[j] == 'R') {
    ret = green[j];
    if (s[j + (j - i)] == 'G')
      ret--;
  } else if (s[i] == 'B' and s[j] == 'G') {
    ret = red[j];
    if (s[j + (j - i)] == 'R')
      ret--;
  }
  return ret;
}

int main() {
  cin >> n;
  cin >> s;
  ll ans = 0;

  blue[n] = 0;
  green[n] = 0;
  red[n] = 0;
  per(i, 1, n) {
    if (s[i] == 'R') {
      red[i - 1] += red[i] + 1;
      blue[i - 1] += blue[i];
      green[i - 1] += green[i];
    } else if (s[i] == 'G') {
      red[i - 1] += red[i];
      blue[i - 1] += blue[i];
      green[i - 1] += green[i] + 1;
    } else {
      red[i - 1] += red[i];
      blue[i - 1] += blue[i] + 1;
      green[i - 1] += green[i];
    }
  }
  // for(int i = 0; i < n+1; i++)cout << red[i] << " ";
  // cout << endl;
  // for(int i = 0; i < n+1; i++)cout << blue[i] << " ";
  // cout << endl;
  // for(int i = 0; i < n+1; i++)cout << green[i] << " ";
  // cout << endl;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] == s[j])
        continue;
      // cout << i << " " << j << endl;
      ans += calc(i, j);
    }
  }
  cout << ans << endl;
}