Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned ui;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef pair<ul, ul> puu;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define ls (i << 1)
#define rs (i << 1 | 1)
#define mid (l + r >> 1)
#define enum(i, j, k) for (int i = j; i <= (k); i++)
#define open(i, j, k) for (int i = j; i < (k); i++)
#define dec(i, j, k) for (int i = j; i >= (k); i--)
#define ae(x) for (node *p = h[x]; p; p = p->nxt)
#define mfil(x, k) memset(x, k, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
#define fio(x)                                                                 \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
template <class T> bool chkmin(T &x, T y) {
  return y < x ? (x = y, true) : false;
}
template <class T> bool chkmax(T &x, T y) {
  return y > x ? (x = y, true) : false;
}
struct __init {
  __init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(16);
    cout << fixed;
    srand(time(0) ^ ui(ul(new char)));
  }
} static __init__;
/* default code ends here */

const int maxn = 2e5 + 10;
int n, k;
bool a[3 * maxn], lazy;
int front = 1;
int main() {
  cin >> n >> k;
  if (k > 3 * n + 10)
    k -= (k - 3 * n) / 2 * 2 - 4;
  string foo;
  cin >> foo;
  enum(i, 1, n) a[i] = foo[i - 1] == 'A';
  while (k--) {
    if (a[front] ^ lazy) {
      a[front] ^= 1;
      continue;
    }
    front++;
    lazy ^= 1;
    a[front + n - 1] = lazy ^ 1;
  }
  enum(i, front, front + n - 1) cout << (a[i] ^ lazy ? 'A' : 'B');

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned ui;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef pair<ul, ul> puu;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define ls (i << 1)
#define rs (i << 1 | 1)
#define mid (l + r >> 1)
#define enum(i, j, k) for (int i = j; i <= (k); i++)
#define open(i, j, k) for (int i = j; i < (k); i++)
#define dec(i, j, k) for (int i = j; i >= (k); i--)
#define ae(x) for (node *p = h[x]; p; p = p->nxt)
#define mfil(x, k) memset(x, k, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
#define fio(x)                                                                 \
  freopen(x ".in", "r", stdin);                                                \
  freopen(x ".out", "w", stdout)
template <class T> bool chkmin(T &x, T y) {
  return y < x ? (x = y, true) : false;
}
template <class T> bool chkmax(T &x, T y) {
  return y > x ? (x = y, true) : false;
}
struct __init {
  __init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(16);
    cout << fixed;
    srand(time(0) ^ ui(ul(new char)));
  }
} static __init__;
/* default code ends here */

const int maxn = 2e5 + 10;
int n, k;
bool a[10 * maxn], lazy;
int front = 1;
int main() {
  cin >> n >> k;
  if (k > 3 * n + 10)
    k -= (k - 3 * n) / 2 * 2 - 4;
  string foo;
  cin >> foo;
  enum(i, 1, n) a[i] = foo[i - 1] == 'A';
  while (k--) {
    if (a[front] ^ lazy) {
      a[front] ^= 1;
      continue;
    }
    front++;
    lazy ^= 1;
    a[front + n - 1] = lazy ^ 1;
  }
  enum(i, front, front + n - 1) cout << (a[i] ^ lazy ? 'A' : 'B');

  return 0;
}