Problem p03466 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a, _e = b; i <= _e; ++i)
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
int q, a, b, c, d, len, xa, xb, la, lb;
int is[2];
vector<P> w[2];
void get(vector<P> &w, int xa, int a) {
  int g = 0;
  for (int l = 0, r = xa, mid; mid = l + r >> 1, l <= r;)
    if ((ll)mid * len + (xa - mid) <= a)
      g = mid, l = mid + 1;
    else
      r = mid - 1;
  if (g)
    w.push_back(P(g, len));
  a -= g * len;
  xa -= g;
  if (xa) {
    w.push_back(P(1, a - (xa - 1)));
    w.push_back(P(xa - 1, 1));
  }
}
int sum2(vector<P> &w, int a) {
  if (!a)
    return 0;
  int s = 0;
  fo(i, 0, w.size() - 1) {
    if (a <= w[i].fi) {
      s += a * w[i].se;
      break;
    }
    s += w[i].fi * w[i].se;
    a -= w[i].fi;
  }
  return s;
}
int sum(int x) {
  return sum2(w[0], x + is[0] >> 1) + sum2(w[1], x + is[1] >> 1);
}
P find(int x) {
  int wz = 0;
  for (int l = 1, r = xa + xb, mid; mid = l + r >> 1, l <= r;)
    if (sum(mid) < x)
      l = mid + 1, wz = mid;
    else
      r = mid - 1;
  return P(wz + 1, x - sum(wz));
}
int get_len2(vector<P> &w, int x) {
  fo(i, 0, w.size() - 1) if ((x -= w[i].fi) <= 0) return w[i].se;
}
int get_len(int x) {
  return (x & 1) == is[0] ? get_len2(w[0], x + is[0] >> 1)
                          : get_len2(w[1], x + is[1] >> 1);
}
int main() {
  for (cin >> q; q--;) {
    cin >> a >> b >> c >> d;
    for (int l = 1, r = a, mid; mid = l + r >> 1, l <= r;) {
      int la = (a + mid - 1) / mid, lb = (b + mid - 1) / mid;
      if (a + 1 < lb || b + 1 < la)
        l = mid + 1;
      else
        len = mid, r = mid - 1;
    }
    la = (a + len - 1) / len;
    lb = (b + len - 1) / len;
    w[0].clear();
    w[1].clear();
    if (a + 1 == lb)
      xa = a, xb = lb;
    else if (b + 1 == la)
      xa = la, xb = b;
    else {
      int k = 0;
      for (int l = 0, r = a / len, mid; mid = l + r >> 1, l <= r;)
        if ((ll)len * (a - mid * len - 1 + 1) < b - mid)
          r = mid - 1;
        else
          k = mid, l = mid + 1;
      xa = xb = k + (b - k + len - 1) / len;
    }
    if (xa >= xb)
      is[0] = 1, is[1] = 0;
    else
      is[0] = 0, is[1] = 1;
    if (len == 1)
      w[0].push_back(P(a, 1)), w[1].push_back(P(b, 1));
    else {
      get(w[0], xa, a);
      get(w[1], xb, b);
      reverse(w[1].begin(), w[1].end());
    }
    P A = find(c);
    fo(i, c, d) {
      putchar((A.fi & 1) == is[0] ? 'A' : 'B');
      if (++A.se > get_len(A.fi))
        A = P(A.fi + 1, 1);
    }
    putchar('\n');
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a, _e = b; i <= _e; ++i)
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
int q, a, b, c, d, len, xa, xb, la, lb;
int is[2];
vector<P> w[2];
void get(vector<P> &w, int xa, int a) {
  int g = 0;
  for (int l = 0, r = xa, mid; mid = l + r >> 1, l <= r;)
    if ((ll)mid * len + (xa - mid) <= a)
      g = mid, l = mid + 1;
    else
      r = mid - 1;
  if (g)
    w.push_back(P(g, len));
  a -= g * len;
  xa -= g;
  if (xa) {
    w.push_back(P(1, a - (xa - 1)));
    w.push_back(P(xa - 1, 1));
  }
}
int sum2(vector<P> &w, int a) {
  if (!a)
    return 0;
  int s = 0;
  fo(i, 0, w.size() - 1) {
    if (a <= w[i].fi) {
      s += a * w[i].se;
      break;
    }
    s += w[i].fi * w[i].se;
    a -= w[i].fi;
  }
  return s;
}
int sum(int x) {
  return sum2(w[0], x + is[0] >> 1) + sum2(w[1], x + is[1] >> 1);
}
P find(int x) {
  int wz = 0;
  for (int l = 1, r = xa + xb, mid; mid = l + r >> 1, l <= r;)
    if (sum(mid) < x)
      l = mid + 1, wz = mid;
    else
      r = mid - 1;
  return P(wz + 1, x - sum(wz));
}
int get_len2(vector<P> &w, int x) {
  fo(i, 0, w.size() - 1) if ((x -= w[i].fi) <= 0) return w[i].se;
}
int get_len(int x) {
  return (x & 1) == is[0] ? get_len2(w[0], x + is[0] >> 1)
                          : get_len2(w[1], x + is[1] >> 1);
}
int main() {
  for (cin >> q; q--;) {
    cin >> a >> b >> c >> d;
    len = 0;
    for (int l = 1, r = max(a, b), mid; mid = l + r >> 1, l <= r;) {
      int la = (a + mid - 1) / mid, lb = (b + mid - 1) / mid;
      if (a + 1 < lb || b + 1 < la)
        l = mid + 1;
      else
        len = mid, r = mid - 1;
    }
    la = (a + len - 1) / len;
    lb = (b + len - 1) / len;
    w[0].clear();
    w[1].clear();
    if (a + 1 == lb)
      xa = a, xb = lb;
    else if (b + 1 == la)
      xa = la, xb = b;
    else {
      int k = 0;
      for (int l = 0, r = a / len, mid; mid = l + r >> 1, l <= r;)
        if ((ll)len * (a - mid * len - 1 + 1) < b - mid)
          r = mid - 1;
        else
          k = mid, l = mid + 1;
      xa = xb = k + (b - k + len - 1) / len;
    }
    if (xa >= xb)
      is[0] = 1, is[1] = 0;
    else
      is[0] = 0, is[1] = 1;
    if (len == 1)
      w[0].push_back(P(a, 1)), w[1].push_back(P(b, 1));
    else {
      get(w[0], xa, a);
      get(w[1], xb, b);
      reverse(w[1].begin(), w[1].end());
    }
    P A = find(c);
    fo(i, c, d) {
      putchar((A.fi & 1) == is[0] ? 'A' : 'B');
      if (++A.se > get_len(A.fi))
        A = P(A.fi + 1, 1);
    }
    putchar('\n');
  }
}