Problem p03018 (C++)

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#if DEBUG && !ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...)
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001

#define trav(a, v) for (auto &a : v)

#define tt                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;

//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll powmod(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}
string s;
ll rigt[2 * ee5];
ll bc[2 * ee5], a[2 * ee5];
int main() {
#if !ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s;
  ll n = s.size();

  for (ll i = n - 1; i >= 0; i--) {
    if (i + 1 < n and s[i + 1] == 'A')
      rigt[i] = rigt[i + 1];
    else if (s[i] == 'A')
      rigt[i] = i;
  }

  for (ll i = n - 2; i >= 0; i--) {
    if (s[i] == 'B' and s[i + 1] == 'C')
      bc[i] = 1 + bc[i + 2];
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'A')
      a[i] = (i ? a[i - 1] : 0) + 1;
  }

  ll ans = 0;
  for (ll i = 0; i + 2 < n; i++) {
    if (s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C') {
      ans += (a[i] * bc[i + 1]);
      if (i + 1 + 2 * bc[i + 1] < n and s[i + 1 + 2 * bc[i + 1]] == 'A')
        a[rigt[i + 1 + 2 * bc[i + 1]]] += a[i];
    }
  }
  cout << ans;
}

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#if DEBUG && !ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...)
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001

#define trav(a, v) for (auto &a : v)

#define tt                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;

//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll powmod(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}
string s;
ll rigt[2 * ee5];
ll bc[2 * ee5], a[2 * ee5];
int main() {
#if !ONLINE_JUDGE
  //   freopen("in.txt","r",stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s;
  ll n = s.size();

  for (ll i = n - 1; i >= 0; i--) {
    if (i + 1 < n and s[i + 1] == 'A')
      rigt[i] = rigt[i + 1];
    else if (s[i] == 'A')
      rigt[i] = i;
  }

  for (ll i = n - 2; i >= 0; i--) {
    if (s[i] == 'B' and s[i + 1] == 'C')
      bc[i] = 1 + bc[i + 2];
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'A')
      a[i] = (i ? a[i - 1] : 0) + 1;
  }

  ll ans = 0;
  for (ll i = 0; i + 2 < n; i++) {
    if (s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C') {
      ans += (a[i] * bc[i + 1]);
      if (i + 1 + 2 * bc[i + 1] < n and s[i + 1 + 2 * bc[i + 1]] == 'A')
        a[rigt[i + 1 + 2 * bc[i + 1]]] += a[i];
    }
  }
  cout << ans;
}