Problem p02714 (C++)

Buggy submission (status: Runtime Error):
/*

PAIN demands to be felt
so do RE, TLE, MLE, SIGSEV & WA

*/

#include <bits/stdc++.h>
using namespace std;
#define CIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(x) (x).begin(), (x).end()
// -----------------<TypeDef Start>------------------
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<std::vector<ll>> matrix;
typedef map<ll, ll> mii;
typedef vector<pii> vpii;

map<char, vi> loc;

ll addd(char c1, char c2, char c3) {
  ll n1, n2, n3, n4, ans = 0, i, j;
  for (i = 0; i < sz(loc[c1]); i++) {
    n1 = loc[c1][i];
    n2 = upper_bound(all(loc[c2]), n1) - loc[c2].begin();
    n3 = 0;
    for (j = n2; j < sz(loc[c2]); j++) {
      n2 = loc[c2][j];
      n3 = upper_bound(all(loc[c3]), n2) - loc[c3].begin();
      n4 = lower_bound(all(loc[c3]), 2 * n2 - n1) - loc[c3].begin();
      if (loc[c3][n4] - n2 == n2 - n1)
        n3++;
      ans += sz(loc[c3]) - n3;
    }
  }
  return ans;
}

void solve() {
  ll n = 0, i = 0, ans = 0;
  string str;
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  cin >> n >> str;

  for (i = 0; i < n; i++) {
    loc[str[i]].pb(i);
  }
  //{r , g , b}
  ans += addd('R', 'G', 'B');
  ans += addd('R', 'B', 'G');
  ans += addd('G', 'R', 'B');
  ans += addd('G', 'B', 'R');
  ans += addd('B', 'R', 'G');
  ans += addd('B', 'G', 'R');
  cout << ans << endl;
}

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

  return 0;
}


Fixed submission:
/*

PAIN demands to be felt
so do RE, TLE, MLE, SIGSEV & WA

*/

#include <bits/stdc++.h>
using namespace std;
#define CIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(x) (x).begin(), (x).end()
// -----------------<TypeDef Start>------------------
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<std::vector<ll>> matrix;
typedef map<ll, ll> mii;
typedef vector<pii> vpii;

map<char, vi> loc;

ll addd(char c1, char c2, char c3) {
  ll n1, n2, n3, n4, ans = 0, i, j;
  if (loc[c2].empty() || loc[c3].empty() || loc[c1].empty())
    return 0;
  for (i = 0; i < sz(loc[c1]); i++) {
    n1 = loc[c1][i];
    n2 = upper_bound(all(loc[c2]), n1) - loc[c2].begin();
    n3 = 0;
    for (j = n2; j < sz(loc[c2]); j++) {
      n2 = loc[c2][j];
      n3 = upper_bound(all(loc[c3]), n2) - loc[c3].begin();
      n4 = lower_bound(all(loc[c3]), 2 * n2 - n1) - loc[c3].begin();
      if (loc[c3][n4] - n2 == n2 - n1)
        n3++;
      ans += sz(loc[c3]) - n3;
    }
  }
  return ans;
}

void solve() {
  ll n = 0, i = 0, ans = 0;
  string str;
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  cin >> n >> str;

  for (i = 0; i < n; i++) {
    loc[str[i]].pb(i);
  }
  //{r , g , b}
  ans += addd('R', 'G', 'B');
  ans += addd('R', 'B', 'G');
  ans += addd('G', 'R', 'B');
  ans += addd('G', 'B', 'R');
  ans += addd('B', 'R', 'G');
  ans += addd('B', 'G', 'R');
  cout << ans << endl;
}

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

  return 0;
}