Problem p03466 (C++)

Buggy submission (status: Time Limit Exceeded):
// 実装が重そうな問題はある程度考えてから書く
// 初期化を忘れずに（特に二分探索とか）
// コーナーケースを考えて（特に場合分けとか）
#include <bits/stdc++.h>
#define YES "YES"
#define NO "NO"
#define YESNO OUT(three(solve(), YES, NO))
#define ECHO OUT(solve())
#define three(A, B, C) ((A) ? (B) : (C))
#define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++)
#define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++)
#define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--)
#define REP(i, b) FOR(i, zero, b)
#define EREP(i, b) EFOR(i, zero, b)
#define RREP(i, b) RFOR(i, b, zero)
#define ALL(c) c.begin(), c.end()
#define UNIQUE(c)                                                              \
  sort(ALL(c));                                                                \
  c.erase(unique(ALL(c)), c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a)     // OUT(a)
#define DEBUG2(a, b) // OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a, b) cout << (a) << " " << (b) << endl
#define int LL
#define zero 0LL
using namespace std;
template <typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template <typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template <typename T> inline bool middle(T a, T b, T c) {
  return b <= a && a <= c;
}
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
inline void outputVI(VI x) {
  REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }
  OUT("");
}
const int SIZE1 = 1e6 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 400;
const int SIZE = SIZE1;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
string s;
ut N, M, K, X, L, Y, Q;
// ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U;
VI edges[SIZE];
LL vals[SIZE], nums[SIZE], maps[SIZE2][SIZE2], answer = zero;
char ss[SIZE];
////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////
LL maxby(LL A, LL B) {
  if (A < 0 or B < 0)
    return INF;
  return (A + B) / (min(A, B) + 1);
}
void init() {}
string solve2(LL A, LL B, LL C, LL D) {
  int sizes = A + B;
  if (A < B) {
    string ns = solve2(B, A, (A + B) - D - 1, (A + B) - C - 1);
    reverse(ALL(ns));
    REP(i, SI(ns)) {
      if (ns[i] == 'A')
        ns[i] = 'B';
      else
        ns[i] = 'A';
    }
    return ns;
  }
  LL maxim = maxby(A, B);
  LL s = 0, e = A / maxim + 1;
  DEBUG2(maxim, A);
  while (s <= e) {
    int f = (s + e) / 2;
    DEBUG2(f, maxby(A - f * maxim, B - f + 1));
    if (maxim >= maxby(A - f * maxim, B - f + 1))
      s = f + 1;
    else
      e = f - 1;
  }
  int fis = e * (maxim + 1);
  A -= e * maxim;
  B -= e;
  int now = fis;
  DEBUG(e);
  DEBUG2(A, B);
  while (A * maxim > B - 1) {
    now++;
    A--;
  }
  int sec = now;
  string ns;
  DEBUG2(fis, sec);
  EFOR(i, C, D) {
    if (i < fis) {
      if (i % (maxim + 1) == maxim)
        ns += 'B';
      else
        ns += 'A';
    } else if (i < sec) {
      ns += 'A';
    } else {
      // cat;
      // cout << A << " " << B << " " << i << endl;
      if ((sizes - i) % (maxim + 1) == 0) {
        ns += 'A';
      } else {
        ns += 'B';
      }
    }
  }
  return ns;
}
LL solve() {
  LL A, B, C, D;
  cin >> A >> B >> C >> D;
  C--;
  D--;
  OUT(solve2(A, B, C, D));

  return 0;
}
signed main() {
  cin >> Q;
  REP(i, Q) { solve(); }
  return 0;
}

Fixed submission:
// 実装が重そうな問題はある程度考えてから書く
// 初期化を忘れずに（特に二分探索とか）
// コーナーケースを考えて（特に場合分けとか）
#include <bits/stdc++.h>
#define YES "YES"
#define NO "NO"
#define YESNO OUT(three(solve(), YES, NO))
#define ECHO OUT(solve())
#define three(A, B, C) ((A) ? (B) : (C))
#define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++)
#define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++)
#define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--)
#define REP(i, b) FOR(i, zero, b)
#define EREP(i, b) EFOR(i, zero, b)
#define RREP(i, b) RFOR(i, b, zero)
#define ALL(c) c.begin(), c.end()
#define UNIQUE(c)                                                              \
  sort(ALL(c));                                                                \
  c.erase(unique(ALL(c)), c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a)     // OUT(a)
#define DEBUG2(a, b) // OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a, b) cout << (a) << " " << (b) << endl
#define int LL
#define zero 0LL
using namespace std;
template <typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template <typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template <typename T> inline bool middle(T a, T b, T c) {
  return b <= a && a <= c;
}
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
inline void outputVI(VI x) {
  REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }
  OUT("");
}
const int SIZE1 = 1e6 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 400;
const int SIZE = SIZE1;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
string s;
ut N, M, K, X, L, Y, Q;
// ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U;
VI edges[SIZE];
LL vals[SIZE], nums[SIZE], maps[SIZE2][SIZE2], answer = zero;
char ss[SIZE];
////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////
LL maxby(LL A, LL B) {
  if (A < 0 or B < 0)
    return INF;
  return (A + B) / (min(A, B) + 1);
}
void init() {}
string solve2(LL A, LL B, LL C, LL D) {
  int sizes = A + B;
  if (A < B) {
    string ns = solve2(B, A, (A + B) - D - 1, (A + B) - C - 1);
    reverse(ALL(ns));
    REP(i, SI(ns)) {
      if (ns[i] == 'A')
        ns[i] = 'B';
      else
        ns[i] = 'A';
    }
    return ns;
  }
  LL maxim = maxby(A, B);
  LL s = 0, e = A / maxim + 1;
  DEBUG2(maxim, A);
  while (s <= e) {
    int f = (s + e) / 2;
    DEBUG2(f, maxby(A - f * maxim, B - f + 1));
    if (maxim >= maxby(A - f * maxim, B - f + 1))
      s = f + 1;
    else
      e = f - 1;
  }
  int fis = e * (maxim + 1);
  A -= e * maxim;
  B -= e;
  int now = fis;
  DEBUG(e);
  DEBUG2(A, B);

  if (A)
    now += (A * maxim - (B - 1) + maxim - 1) / maxim;

  int sec = now;
  string ns;
  DEBUG2(fis, sec);
  EFOR(i, C, D) {
    if (i < fis) {
      if (i % (maxim + 1) == maxim)
        ns += 'B';
      else
        ns += 'A';
    } else if (i < sec) {
      ns += 'A';
    } else {
      // cat;
      // cout << A << " " << B << " " << i << endl;
      if ((sizes - i) % (maxim + 1) == 0) {
        ns += 'A';
      } else {
        ns += 'B';
      }
    }
  }
  return ns;
}
LL solve() {
  LL A, B, C, D;
  cin >> A >> B >> C >> D;
  C--;
  D--;
  OUT(solve2(A, B, C, D));

  return 0;
}
signed main() {
  cin >> Q;
  REP(i, Q) { solve(); }
  return 0;
}