Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
set<int> oo;
char s[555555];
int n;
void upd(int t) {
  bool u = (t >= 0) && (s[t] == 'A') && (s[t + 1] == 'B') && s[t + 2] == 'C';
  if (u)
    oo.insert(t);
  else
    oo.erase(t);
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; ++i)
    upd(i);
  int ans = 0;
  while (!oo.empty()) {
    int x = *oo.begin();
    ++ans;
    oo.erase(oo.begin());
    s[x] = 'B';
    s[x + 1] = 'C';
    s[x + 2] = 'A';
    upd(x + 2);
    upd(x - 1);
  }
  cout << ans << "\n";
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
set<int> oo;
char s[555555];
int n;
void upd(int t) {
  bool u = (t >= 0) && (s[t] == 'A') && (s[t + 1] == 'B') && s[t + 2] == 'C';
  if (u)
    oo.insert(t);
  else
    oo.erase(t);
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  vector<int> v;
  for (int j = 0; j < n; ++j) {
    if (s[j] == 'A')
      v.pb(0);
    else if (s[j] == 'B' && s[j + 1] == 'C')
      v.pb(1), j += 1;
    else
      v.pb(2);
  }
  v.pb(2);
  ll ans = 0, c = 0;
  for (auto t : v) {
    if (t == 0)
      ++c;
    else if (t == 1)
      ans += c;
    else
      c = 0;
  }
  cout << ans << "\n";
}