Problem p03466 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
inline char nchar() {
  static const int bufl = 1 << 20;
  static char buf[bufl], *a = NULL, *b = NULL;
  return a == b && (b = (a = buf) + fread(buf, 1, bufl, stdin), a == b) ? EOF
                                                                        : *a++;
}
inline int read() {
  int x = 0, f = 1;
  char c = nchar();
  for (; !isdigit(c); c = nchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = nchar())
    x = x * 10 + c - '0';
  return x * f;
}
inline char oth(char c) { return 'A' + 'B' - c; }
int d;
inline int up(int x, int y) { return (x + y - 1) / y; }
int get_d(int A, int B) {
  if (A < B)
    swap(A, B);
  int ret = (A + B) / (B + 1);
  while (ret > 1 && up(A, ret - 1) - 1 <= B)
    --ret;
  return ret;
}
void d_is_one(int A, int B, int l, int r) {
  if (B > A) {
    char c = l & 1 ? 'B' : 'A';
    for (int i = l; i <= r; ++i, c = oth(c))
      putchar(c);
  } else {
    char c = l & 1 ? 'A' : 'B';
    for (int i = l; i <= r; ++i, c = oth(c))
      putchar(c);
  }
  puts("");
}
void work(int A, int B, int l, int r, int sh = 0) {
  if (l > r || A + B == 0)
    return;
  assert(l <= r && r <= A + B);
  int x = A - up(B, d) + 1;
  if (sh || x <= 0) {
    int k = x <= 0 ? (B - 1) % d + 1 : 1;
    for (; l <= k && l <= r; ++l)
      putchar('B');
    return work(A, B - k, l - k, r - k);
  } else {
    int sh = min(x, d);
    for (; l <= sh && l <= r; ++l)
      putchar('A');
    return work(A - sh, B, l - sh, r - sh, 1);
  }
}
void deal(int A, int B, int l, int r) {
  if (l > r)
    return;
  int x = A - up(B, d) + 1;
  if (!B) {
    for (int i = l; i <= r; ++i)
      putchar('A');
    return;
  }
  if (x <= 0) {
    int k = B - d * A;
    assert(k > 0);
    for (; l <= k && l <= r; ++l)
      putchar('B');
    return deal(A, B - k, l - k, r - k);
  }
  if (x >= d) {
    int cnt = (1ll * d * (A + 1 - d) - B) / (1ll * d * d - 1) + 1;
    int h = min((l - 1) / (d + 1), cnt), len = (d + 1) * h;
    if (h)
      return deal(A - d * h, B - h, l - len, r - len);
    for (; l <= d && l <= r; ++l)
      putchar('A');
    for (; l <= d + 1 && l <= r; ++l)
      putchar('B');
    return deal(A - d, B - 1, l - (d + 1), r - (d + 1));
  }
  if (B % d) {
    int b = (B - 1) % d + 1;
    for (; l <= x && l <= r; ++l)
      putchar('A');
    for (; l <= x + b && l <= r; ++l)
      putchar('B');
    return deal(A - x, B - b, l - (x + b), r - (x + b));
  } else {
    assert(A * d == B);
    int cnt = (l - 1) / (1 + d), len = cnt * (d + 1);
    if (cnt)
      return deal(A - cnt, B - d * cnt, l - len, r - len);
    for (; l <= 1 && l <= r; ++l)
      putchar('A');
    for (; l <= 1 + d && l <= r; ++l)
      putchar('B');
    return deal(A - 1, B - d, l - (d + 1), r - (d + 1));
  }
}
void work() {
  int A = read(), B = read();
  int l = read(), r = read();
  d = get_d(A, B);
  if (d == 1)
    return d_is_one(A, B, l, r);
  deal(A, B, l, r);
  puts("");
}
int main() {
  for (int T = read(); T--;)
    work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline char nchar() {
  static const int bufl = 1 << 20;
  static char buf[bufl], *a = NULL, *b = NULL;
  return a == b && (b = (a = buf) + fread(buf, 1, bufl, stdin), a == b) ? EOF
                                                                        : *a++;
}
inline int read() {
  int x = 0, f = 1;
  char c = nchar();
  for (; !isdigit(c); c = nchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = nchar())
    x = x * 10 + c - '0';
  return x * f;
}
inline char oth(char c) { return 'A' + 'B' - c; }
int d;
inline int up(int x, int y) { return (x + y - 1) / y; }
int get_d(int A, int B) {
  if (A < B)
    swap(A, B);
  int ret = (A + B) / (B + 1);
  while (ret > 1 && up(A, ret - 1) - 1 <= B)
    --ret;
  return ret;
}
void d_is_one(int A, int B, int l, int r) {
  if (B > A) {
    char c = l & 1 ? 'B' : 'A';
    for (int i = l; i <= r; ++i, c = oth(c))
      putchar(c);
  } else {
    char c = l & 1 ? 'A' : 'B';
    for (int i = l; i <= r; ++i, c = oth(c))
      putchar(c);
  }
  puts("");
}
void work(int A, int B, int l, int r, int sh = 0) {
  if (l > r || A + B == 0)
    return;
  assert(l <= r && r <= A + B);
  int x = A - up(B, d) + 1;
  if (sh || x <= 0) {
    int k = x <= 0 ? (B - 1) % d + 1 : 1;
    for (; l <= k && l <= r; ++l)
      putchar('B');
    return work(A, B - k, l - k, r - k);
  } else {
    int sh = min(x, d);
    for (; l <= sh && l <= r; ++l)
      putchar('A');
    return work(A - sh, B, l - sh, r - sh, 1);
  }
}
void deal(int A, int B, int l, int r) {
  if (l > r)
    return;
  int x = A - up(B, d) + 1;
  if (!B) {
    for (int i = l; i <= r; ++i)
      putchar('A');
    return;
  }
  if (x <= 0) {
    int k = B - d * A;
    assert(k > 0);
    for (; l <= k && l <= r; ++l)
      putchar('B');
    return deal(A, B - k, l - k, r - k);
  }
  if (x >= d) {
    int cnt = (1ll * d * (A + 1 - d) - B) / (1ll * d * d - 1) + 1;
    int h = min((l - 1) / (d + 1), cnt), len = (d + 1) * h;
    if (h)
      return deal(A - d * h, B - h, l - len, r - len);
    for (; l <= d && l <= r; ++l)
      putchar('A');
    for (; l <= d + 1 && l <= r; ++l)
      putchar('B');
    return deal(A - d, B - 1, l - (d + 1), r - (d + 1));
  }
  if (B % d || A * d != B) {
    int b = (B - 1) % d + 1;
    for (; l <= x && l <= r; ++l)
      putchar('A');
    for (; l <= x + b && l <= r; ++l)
      putchar('B');
    return deal(A - x, B - b, l - (x + b), r - (x + b));
  } else {
    assert(A * d == B);
    int cnt = (l - 1) / (1 + d), len = cnt * (d + 1);
    if (cnt)
      return deal(A - cnt, B - d * cnt, l - len, r - len);
    for (; l <= 1 && l <= r; ++l)
      putchar('A');
    for (; l <= 1 + d && l <= r; ++l)
      putchar('B');
    return deal(A - 1, B - d, l - (d + 1), r - (d + 1));
  }
}
void work() {
  int A = read(), B = read();
  int l = read(), r = read();
  d = get_d(A, B);
  if (d == 1)
    return d_is_one(A, B, l, r);
  deal(A, B, l, r);
  puts("");
}
int main() {
  for (int T = read(); T--;)
    work();
  return 0;
}