Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
How to lambda
  ex) sort(ALL(cont),[](dat a,dat b){return a.l<b.l;});
lower_bound/upper_bound
  1 2 2 2 3 3
    ^lb   ^ub */
#include <bits/stdc++.h>
#define BR "\n"
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define ALL(cont) begin(cont), end(cont)
#define AS_MOD(a, b) ((((a) % (b)) + (b)) % (b))
#define FEACH(it, cont) for (auto(it) = begin(cont); it != end(cont); ++it)
#define FEACHR(it, cont) for (auto(it) = rbegin(cont); it != rend(cont); ++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll()                                                                \
  ([]() {                                                                      \
    ll s;                                                                      \
    scanf("%lld", &s);                                                         \
    return s;                                                                  \
  })()
#define getld()                                                                \
  ([]() {                                                                      \
    ld s;                                                                      \
    scanf("%Lf", &s);                                                          \
    return s;                                                                  \
  })()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define assign_if_greater(V, T) V = max(V, T)
using namespace std;
typedef long long ll;
typedef long double ld;
int Q;
int main() {
  cout << fixed << setprecision(15);
  cin >> Q;
  REP(i, Q) {
    ll A, B;
    cin >> A >> B;
    if (B > A)
      swap(A, B);
    ll k = 0;
    for (; k * (k + 1) <= A * B; ++k)
      ;
    --k;
    if (A == B + 1) {
      cout << (k - 1) * 2 << endl;
    } else {
      cout << k + min(k, B - 1) +
                  min(A - k - 1, max(0ll, (A * B - 1) / (k + 1) - B))
           << endl;
    }
  }

  return 0;
}


Fixed submission:
/*
How to lambda
  ex) sort(ALL(cont),[](dat a,dat b){return a.l<b.l;});
lower_bound/upper_bound
  1 2 2 2 3 3
    ^lb   ^ub */
#include <bits/stdc++.h>
#define BR "\n"
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define ALL(cont) begin(cont), end(cont)
#define AS_MOD(a, b) ((((a) % (b)) + (b)) % (b))
#define FEACH(it, cont) for (auto(it) = begin(cont); it != end(cont); ++it)
#define FEACHR(it, cont) for (auto(it) = rbegin(cont); it != rend(cont); ++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll()                                                                \
  ([]() {                                                                      \
    ll s;                                                                      \
    scanf("%lld", &s);                                                         \
    return s;                                                                  \
  })()
#define getld()                                                                \
  ([]() {                                                                      \
    ld s;                                                                      \
    scanf("%Lf", &s);                                                          \
    return s;                                                                  \
  })()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define assign_if_greater(V, T) V = max(V, T)
using namespace std;
typedef long long ll;
typedef long double ld;
int Q;
int main() {
  cout << fixed << setprecision(15);
  cin >> Q;
  REP(i, Q) {
    ll A, B;
    cin >> A >> B;
    if (B > A)
      swap(A, B);
    ll k = 0;
    ll l = 0, r = 1000 * 1000 * 1000ll + 10ll;
    while (r - l > 1) {
      ll m = l + (r - l) / 2;
      if (m * (m + 1) <= A * B)
        l = m;
      else
        r = m;
    }
    k = l;
    if (A == B + 1) {
      cout << (k - 1) * 2 << endl;
    } else {
      cout << k + min(k, B - 1) +
                  min(A - k - 1, max(0ll, (A * B - 1) / (k + 1) - B))
           << endl;
    }
  }

  return 0;
}