Problem p02714 (C++)

Buggy submission (status: Runtime Error):
/*

Coded and Decoded by : Yash Kapoor

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

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll n;
  string s;
  cin >> n >> s;

  ll dp[4][n + 5];
  memset(dp, 0, sizeof(dp));
  lp(i, 0, n) {
    if (i) {
      dp[0][i] += dp[0][i - 1];
      dp[1][i] += dp[1][i - 1];
      dp[2][i] += dp[2][i - 1];
    }
    if (s[i] == 'R') {
      s[i] = 0;
      dp[0][i]++;
    }
    if (s[i] == 'G') {
      s[i] = 1;
      dp[1][i]++;
    }
    if (s[i] == 'B') {
      s[i] = 2;
      dp[2][i]++;
    }
  }
  ll sum = 0;
  lp(i, 0, n) lp(j, i + 1, n) {
    if (s[i] == s[j])
      continue;
    lp(k, 0, 3) {

      if (k == s[i] or k == s[j])
        continue;

      sum += dp[k][n - 1] - dp[k][j];

      if (j + (j - i) < n && s[j + (j - i)] == k)
        sum--;
    }
  }
  cout << sum << endl;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:
/*

Coded and Decoded by : Yash Kapoor

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

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll n;
  string s;
  cin >> n >> s;

  ll dp[4][n + 5];
  memset(dp, 0, sizeof(dp));
  lp(i, 0, n) {
    if (i) {
      dp[0][i] += dp[0][i - 1];
      dp[1][i] += dp[1][i - 1];
      dp[2][i] += dp[2][i - 1];
    }
    if (s[i] == 'R') {
      s[i] = 0;
      dp[0][i]++;
    }
    if (s[i] == 'G') {
      s[i] = 1;
      dp[1][i]++;
    }
    if (s[i] == 'B') {
      s[i] = 2;
      dp[2][i]++;
    }
  }
  ll sum = 0;
  lp(i, 0, n) lp(j, i + 1, n) {
    if (s[i] == s[j])
      continue;
    lp(k, 0, 3) {

      if (k == s[i] or k == s[j])
        continue;

      sum += dp[k][n - 1] - dp[k][j];

      if (j + (j - i) < n && s[j + (j - i)] == k)
        sum--;
    }
  }
  cout << sum << endl;
  return 0;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}