Problem p03466 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll cul(ll a, ll b, ll x) {
  ll l = 0, r = b + 1;
  while (r - l > 1) {
    ll m = l + (r - l) / 2;
    if (m * x > a || m > b || (b - m) / x > a - (m * x)) {
      r = m;
    } else {
      l = m;
    }
  }
  return l;
}

// 1-indexed
string cul(ll a, ll b, ll c, ll d) {
  string ret;
  ll x = (a + b) / (b + 1);
  if (a == b || x == 1) {
    for (ll i = c; i <= d; i++) {
      ret.push_back(i & 1 ? 'A' : 'B');
    }
    return ret;
  }
  ll L = cul(a, b, x), R = cul(b, a, x);
  for (ll i = c; i <= d; i++) {
    if (i <= L * (x + 1)) {
      ret.push_back(i % (x + 1) ? 'A' : 'B');
    } else if (a + b - i + 1 <= R * (x + 1)) {
      ret.push_back((a + b - i + 1) % (x + 1) ? 'B' : 'A');
    } else {
      ll I = i - (x + 1) * L;
      ll A = a - L * x - R;
      ll B = b - L - R * x;
      // cout<<i<<" "<<L<<" "<<R<<" "<<A<<" "<<B<<endl;
      assert(A < x && B < x);
      ret.push_back(I <= A ? 'A' : 'B');
    }
  }
  return ret;
}

int main() {
  ll q;
  cin >> q;
  while (q--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    string ans;
    if (a > b) {
      ans = cul(a, b, c, d);
    } else {
      ans = cul(b, a, a + b - d + 1, a + b - c + 1);
      reverse(ans.begin(), ans.end());
      for (auto &I : ans) {
        I = (I == 'A' ? 'B' : 'A');
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll cul(ll a, ll b, ll x) {
  ll l = 0, r = b + 1;
  while (r - l > 1) {
    ll m = l + (r - l) / 2;
    if (m * x > a || m > b || (b - m) / x > a - (m * x)) {
      r = m;
    } else {
      l = m;
    }
  }
  return l;
}

// 1-indexed
string cul(ll a, ll b, ll c, ll d) {
  string ret;
  ll x = (a + b) / (b + 1);
  if (a == b || x == 1) {
    for (ll i = c; i <= d; i++) {
      ret.push_back(i & 1 ? 'A' : 'B');
    }
    return ret;
  }
  ll L = cul(a, b, x), R = cul(b, a, x);
  for (ll i = c; i <= d; i++) {
    if (i <= L * (x + 1)) {
      ret.push_back(i % (x + 1) ? 'A' : 'B');
    } else if (a + b - i + 1 <= R * (x + 1)) {
      ret.push_back((a + b - i + 1) % (x + 1) ? 'B' : 'A');
    } else {
      ll I = i - (x + 1) * L;
      ll A = a - L * x - R;
      ll B = b - L - R * x;
      // cout<<i<<" "<<L<<" "<<R<<" "<<A<<" "<<B<<endl;
      ret.push_back(I <= A ? 'A' : 'B');
    }
  }
  return ret;
}

int main() {
  ll q;
  cin >> q;
  while (q--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    string ans;
    if (a > b) {
      ans = cul(a, b, c, d);
    } else {
      ans = cul(b, a, a + b - d + 1, a + b - c + 1);
      reverse(ans.begin(), ans.end());
      for (auto &I : ans) {
        I = (I == 'A' ? 'B' : 'A');
      }
    }
    cout << ans << endl;
  }

  return 0;
}