Problem p03788 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 200006
#define mo 1000000009
using namespace std;
bool f[30 * N];
char c[N];
int n, m, cs, nowl, nowr, sum, s[30 * N], t[30 * N], p[N];
inline int read() {
  int x = 0, c = getchar();
  while (c < 48)
    c = getchar();
  while (c > 47)
    x = x * 10 + c - 48, c = getchar();
  return x;
}
void down(int i, int l, int r) {
  if (f[i]) {
    f[i] = 0;
    swap(s[i], t[i]);
    if (l != r)
      f[i << 1] ^= 1, f[i << 1 | 1] ^= 1;
  }
}
void change(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (l == r) {
    s[i] = y;
    t[i] = s[i] ^ 1;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    change(i << 1, l, mid, x, y);
  else
    change(i << 1 | 1, mid + 1, r, x, y);
  down(i << 1, l, mid);
  down(i << 1 | 1, mid + 1, r);
  s[i] = (1LL * s[i << 1] * p[r - mid] + 1LL * s[i << 1 | 1]) % mo;
  t[i] = (1LL * t[i << 1] * p[r - mid] + 1LL * t[i << 1 | 1]) % mo;
}
int query(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (x <= l && r <= y)
    return s[i];
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(i << 1, l, mid, x, y);
  if (x > mid)
    return query(i << 1 | 1, mid + 1, r, x, y);
  int a = query(i << 1, l, mid, x, y), b = query(i << 1 | 1, mid + 1, r, x, y);
  return (1LL * a * p[min(y, r) - mid] + 1LL * b) % mo;
}
void rev(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (x <= l && r <= y) {
    f[i] ^= 1;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    rev(i << 1, l, mid, x, y);
  if (y > mid)
    rev(i << 1 | 1, mid + 1, r, x, y);
  down(i << 1, l, mid);
  down(i << 1 | 1, mid + 1, r);
  s[i] = (1LL * s[i << 1] * p[r - mid] + 1LL * s[i << 1 | 1]) % mo;
  t[i] = (1LL * t[i << 1] * p[r - mid] + 1LL * t[i << 1 | 1]) % mo;
}
int main() {
  freopen("1.in", "r", stdin);
  //	freopen("edit.out","w",stdout);
  n = read();
  m = read();
  scanf("%s", c + 1);
  nowl = 1;
  nowr = n;
  p[0] = 1;
  for (int i = 1; i <= n; i++)
    change(1, 1, 3 * n, i, c[i] - 65);
  for (int i = 1; i <= n; i++)
    p[i] = p[i - 1] * 2 % mo;
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      if (i & 1)
        sum = sum * 2 % mo;
      else
        sum = (sum * 2 + 1) % mo;
    while (1) {
      if (query(1, 1, 3 * n, nowl, nowr) == sum)
        break;
      if (query(1, 1, 3 * n, nowl, nowl) == 0)
        change(1, 1, 3 * n, nowl, 1);
      else {
        nowl++;
        nowr++;
        rev(1, 1, 3 * n, nowl, nowr - 1);
        change(1, 1, 3 * n, nowr, 0);
      }
      cs++;
      if (cs == m) {
        for (int i = nowl; i <= nowr; i++)
          putchar(query(1, 1, 3 * n, i, i) + 65);
        return 0;
      }
    }
    if ((m - cs) & 1)
      putchar('B');
    else
      putchar('A');
    for (int i = 2; i <= n; i++)
      if (i & 1)
        putchar('A');
      else
        putchar('B');
  } else {
    for (int i = 1; i <= n; i++)
      if (i & 1)
        sum = (sum * 2 + 1) % mo;
      else
        sum = sum * 2 % mo;
    while (1) {
      if (query(1, 1, 3 * n, nowl, nowr) == sum)
        break;
      if (query(1, 1, 3 * n, nowl, nowl) == 0)
        change(1, 1, 3 * n, nowl, 1);
      else {
        nowl++;
        nowr++;
        rev(1, 1, 3 * n, nowl, nowr - 1);
        change(1, 1, 3 * n, nowr, 0);
      }
      cs++;
      if (cs == m) {
        for (int i = nowl; i <= nowr; i++)
          putchar(query(1, 1, 3 * n, i, i) + 65);
        return 0;
      }
    }
    for (int i = 1; i <= n; i++)
      if (i & 1)
        putchar('B');
      else
        putchar('A');
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define N 200006
#define mo 1000000009
using namespace std;
bool f[30 * N];
char c[N];
int n, m, cs, nowl, nowr, sum, s[30 * N], t[30 * N], p[N];
inline int read() {
  int x = 0, c = getchar();
  while (c < 48)
    c = getchar();
  while (c > 47)
    x = x * 10 + c - 48, c = getchar();
  return x;
}
void down(int i, int l, int r) {
  if (f[i]) {
    f[i] = 0;
    swap(s[i], t[i]);
    if (l != r)
      f[i << 1] ^= 1, f[i << 1 | 1] ^= 1;
  }
}
void change(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (l == r) {
    s[i] = y;
    t[i] = s[i] ^ 1;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    change(i << 1, l, mid, x, y);
  else
    change(i << 1 | 1, mid + 1, r, x, y);
  down(i << 1, l, mid);
  down(i << 1 | 1, mid + 1, r);
  s[i] = (1LL * s[i << 1] * p[r - mid] + 1LL * s[i << 1 | 1]) % mo;
  t[i] = (1LL * t[i << 1] * p[r - mid] + 1LL * t[i << 1 | 1]) % mo;
}
int query(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (x <= l && r <= y)
    return s[i];
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(i << 1, l, mid, x, y);
  if (x > mid)
    return query(i << 1 | 1, mid + 1, r, x, y);
  int a = query(i << 1, l, mid, x, y), b = query(i << 1 | 1, mid + 1, r, x, y);
  return (1LL * a * p[min(y, r) - mid] + 1LL * b) % mo;
}
void rev(int i, int l, int r, int x, int y) {
  down(i, l, r);
  if (x <= l && r <= y) {
    f[i] ^= 1;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    rev(i << 1, l, mid, x, y);
  if (y > mid)
    rev(i << 1 | 1, mid + 1, r, x, y);
  down(i << 1, l, mid);
  down(i << 1 | 1, mid + 1, r);
  s[i] = (1LL * s[i << 1] * p[r - mid] + 1LL * s[i << 1 | 1]) % mo;
  t[i] = (1LL * t[i << 1] * p[r - mid] + 1LL * t[i << 1 | 1]) % mo;
}
int main() {
  n = read();
  m = read();
  scanf("%s", c + 1);
  nowl = 1;
  nowr = n;
  p[0] = 1;
  for (int i = 1; i <= n; i++)
    change(1, 1, 3 * n, i, c[i] - 65);
  for (int i = 1; i <= n; i++)
    p[i] = p[i - 1] * 2 % mo;
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      if (i & 1)
        sum = sum * 2 % mo;
      else
        sum = (sum * 2 + 1) % mo;
    while (1) {
      if (query(1, 1, 3 * n, nowl, nowr) == sum)
        break;
      if (query(1, 1, 3 * n, nowl, nowl) == 0)
        change(1, 1, 3 * n, nowl, 1);
      else {
        nowl++;
        nowr++;
        rev(1, 1, 3 * n, nowl, nowr - 1);
        change(1, 1, 3 * n, nowr, 0);
      }
      cs++;
      if (cs == m) {
        for (int i = nowl; i <= nowr; i++)
          putchar(query(1, 1, 3 * n, i, i) + 65);
        return 0;
      }
    }
    if ((m - cs) & 1)
      putchar('B');
    else
      putchar('A');
    for (int i = 2; i <= n; i++)
      if (i & 1)
        putchar('A');
      else
        putchar('B');
  } else {
    for (int i = 1; i <= n; i++)
      if (i & 1)
        sum = (sum * 2 + 1) % mo;
      else
        sum = sum * 2 % mo;
    while (1) {
      if (query(1, 1, 3 * n, nowl, nowr) == sum)
        break;
      if (query(1, 1, 3 * n, nowl, nowl) == 0)
        change(1, 1, 3 * n, nowl, 1);
      else {
        nowl++;
        nowr++;
        rev(1, 1, 3 * n, nowl, nowr - 1);
        change(1, 1, 3 * n, nowr, 0);
      }
      cs++;
      if (cs == m) {
        for (int i = nowl; i <= nowr; i++)
          putchar(query(1, 1, 3 * n, i, i) + 65);
        return 0;
      }
    }
    for (int i = 1; i <= n; i++)
      if (i & 1)
        putchar('B');
      else
        putchar('A');
  }
}