Problem p03466 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define int long long

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

void solve(int a, int b, int c, int d, string &ans) {
  int k = (a + b) / (b + 1);
  int l = a / k;
  int r = a % k;
  int rem = b - l - (r != 0);
  int ed_pos = -1;
  int ed_num = -1;
  int ed_cnt = -1;
  int cri = -1;
  // cout << k << " " << l << " " << r << "\n";
  if (rem < 0) {
    cri = 0;
  } else {
    if (r != 0) {
      if (rem <= k - 1 + (r - 1) * k) {
        ed_pos = 1;
        if (rem <= k - 1) {
          ed_num = 1;
          ed_cnt = rem;
        } else {
          rem -= k - 1;
          ed_num = (rem + k - 1) / k + 1;
          ed_cnt = rem - (ed_num - 2) * k;
        }
        rem = 0;
      } else {
        rem -= k - 1 + (r - 1) * k;
      }
    }
    // show(r);
    // show(rem);
    if (ed_pos < 0) {
      int syuki = k * (k - 1) + k - 1;
      ed_pos = (rem + syuki - 1) / syuki + 1;
      rem -= (ed_pos - 2) * syuki;
      if (rem <= k - 1) {
        ed_num = 1;
        ed_cnt = rem;
      } else {
        rem -= k - 1;
        ed_num = (rem + k - 1) / k + 1;
        ed_cnt = rem - (ed_num - 2) * k;
      }
    }
    // cout << ed_pos << " " << ed_num << " " << ed_cnt << "\n";
    int rp = -1;
    if (ed_pos == 1) {
      rp = (ed_num - 1);
    } else {
      rp = r + (ed_pos - 2) * k + (ed_num - 1);
    }
    cri = rp * (k + 1) + ed_cnt;
    if (ed_num == 1)
      cri++;
  }
  // show(cri);
  // [st,ed]
  int st = a + b - d + 1;
  int ed = a + b - c + 1;
  // cout << st << " " << ed << "\n";
  for (int j = st; j <= min(cri, ed); j++) {
    if ((j - 1) % (k + 1) == k) {
      ans.pb('A');
    } else {
      ans.pb('B');
    }
  }
  for (int j = min(a + b - cri, d); j >= c; j--) {
    if ((j - 1) % (k + 1) == k) {
      ans.pb('B');
    } else {
      ans.pb('A');
    }
  }
  reverse(all(ans));
  return;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  rep(i, q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    string ans;
    if (a < b) {
      solve(b, a, a + b + 1 - d, a + b + 1 - c, ans);
    } else {
      solve(a, b, c, d, ans);
    }
    if (a < b) {
      reverse(all(ans));
      rep(i, len(ans)) {
        if (ans[i] == 'A') {
          cout << 'B';
        } else {
          cout << 'A';
        }
      }
      cout << "\n";
    } else {
      cout << ans << "\n";
    }
  }
  return 0;
};

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define int long long

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

void solve(int a, int b, int c, int d, string &ans) {
  int k = (a + b) / (b + 1);
  int l = a / k;
  int r = a % k;
  int rem = b - l - (r != 0);
  int ed_pos = -1;
  int ed_num = -1;
  int ed_cnt = -1;
  int cri = -1;
  // cout << k << " " << l << " " << r << "\n";
  if (rem < 0) {
    cri = 0;
  } else if (rem == 0) {
    cri = 1;
  } else {
    if (r != 0) {
      if (rem <= k - 1 + (r - 1) * k) {
        ed_pos = 1;
        if (rem <= k - 1) {
          ed_num = 1;
          ed_cnt = rem;
        } else {
          rem -= k - 1;
          ed_num = (rem + k - 1) / k + 1;
          ed_cnt = rem - (ed_num - 2) * k;
        }
        rem = 0;
      } else {
        rem -= k - 1 + (r - 1) * k;
      }
    }
    // show(r);
    // show(rem);
    if (ed_pos < 0) {
      int syuki = k * (k - 1) + k - 1;
      ed_pos = (rem + syuki - 1) / syuki + 1;
      rem -= (ed_pos - 2) * syuki;
      if (rem <= k - 1) {
        ed_num = 1;
        ed_cnt = rem;
      } else {
        rem -= k - 1;
        ed_num = (rem + k - 1) / k + 1;
        ed_cnt = rem - (ed_num - 2) * k;
      }
    }
    // cout << ed_pos << " " << ed_num << " " << ed_cnt << "\n";
    int rp = -1;
    if (ed_pos == 1) {
      rp = (ed_num - 1);
    } else {
      rp = r + (ed_pos - 2) * k + (ed_num - 1);
    }
    cri = rp * (k + 1) + ed_cnt;
    if (ed_num == 1)
      cri++;
  }
  // show(cri);
  // [st,ed]
  int st = a + b - d + 1;
  int ed = a + b - c + 1;
  // cout << st << " " << ed << "\n";
  for (int j = st; j <= min(cri, ed); j++) {
    if ((j - 1) % (k + 1) == k) {
      ans.pb('A');
    } else {
      ans.pb('B');
    }
  }
  for (int j = min(a + b - cri, d); j >= c; j--) {
    if ((j - 1) % (k + 1) == k) {
      ans.pb('B');
    } else {
      ans.pb('A');
    }
  }
  reverse(all(ans));
  return;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  rep(i, q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    string ans;
    if (a < b) {
      solve(b, a, a + b + 1 - d, a + b + 1 - c, ans);
    } else {
      solve(a, b, c, d, ans);
    }
    if (a < b) {
      reverse(all(ans));
      rep(i, len(ans)) {
        if (ans[i] == 'A') {
          cout << 'B';
        } else {
          cout << 'A';
        }
      }
      cout << "\n";
    } else {
      cout << ans << "\n";
    }
  }
  return 0;
};