Problem p02714 (C++)

Buggy submission (status: Runtime Error):
/*
 * Matheus Oliveira
 * 09:20:02 12/04/2020
 * d.cpp
 */

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF
#define MAXN 4040
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d %d", &x, &y);
#define sc3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, bg, ed)                                                    \
  printf("%s: ", #x);                                                          \
  for (int i = bg; i < ed; i++)                                                \
    printf("%d%c", x[i], (i == ed - 1) ? '\n' : ' ');

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

char inp[MAXN];
int r[MAXN], g[MAXN], b[MAXN];

char getLast(char f, char s) {
  if ('R' != f and 'R' != s)
    return 'R';
  if ('G' != f and 'G' != s)
    return 'G';
  return 'B';
}

int main() {
  char last;
  int n, dist;

  scanf("%d", &n);
  getchar();

  scanf("%s", inp);
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    if (i == 0) {
      r[i] = (inp[i] == 'R');
      g[i] = (inp[i] == 'G');
      b[i] = (inp[i] == 'B');
    } else {
      r[i] = r[i - 1] + (inp[i] == 'R');
      g[i] = g[i - 1] + (inp[i] == 'G');
      b[i] = b[i - 1] + (inp[i] == 'B');
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (inp[i] != inp[j]) {
        last = getLast(inp[i], inp[j]);
        dist = j - i;

        if (last == 'R')
          ans += ((ll)(r[n - 1] - r[j]) - (inp[j + dist] == 'R'));
        if (last == 'G')
          ans += ((ll)(g[n - 1] - g[j]) - (inp[j + dist] == 'G'));
        if (last == 'B')
          ans += ((ll)(b[n - 1] - b[j]) - (inp[j + dist] == 'B'));
      }
    }
  }

  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
/*
 * Matheus Oliveira
 * 09:20:02 12/04/2020
 * d.cpp
 */

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF
#define MAXN 14040
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d %d", &x, &y);
#define sc3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, bg, ed)                                                    \
  printf("%s: ", #x);                                                          \
  for (int i = bg; i < ed; i++)                                                \
    printf("%d%c", x[i], (i == ed - 1) ? '\n' : ' ');

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

char inp[MAXN];
int r[MAXN], g[MAXN], b[MAXN];

char getLast(char f, char s) {
  if ('R' != f and 'R' != s)
    return 'R';
  if ('G' != f and 'G' != s)
    return 'G';
  return 'B';
}

int main() {
  char last;
  int n, dist;

  scanf("%d", &n);
  getchar();

  scanf("%s", inp);
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    if (i == 0) {
      r[i] = (inp[i] == 'R');
      g[i] = (inp[i] == 'G');
      b[i] = (inp[i] == 'B');
    } else {
      r[i] = r[i - 1] + (inp[i] == 'R');
      g[i] = g[i - 1] + (inp[i] == 'G');
      b[i] = b[i - 1] + (inp[i] == 'B');
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (inp[i] != inp[j]) {
        last = getLast(inp[i], inp[j]);
        dist = j - i;

        if (last == 'R')
          ans += ((ll)(r[n - 1] - r[j]) - (inp[j + dist] == 'R'));
        if (last == 'G')
          ans += ((ll)(g[n - 1] - g[j]) - (inp[j + dist] == 'G'));
        if (last == 'B')
          ans += ((ll)(b[n - 1] - b[j]) - (inp[j + dist] == 'B'));
      }
    }
  }

  printf("%lld\n", ans);
  return 0;
}