Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#if !ONLINE_JUDGE
#define debug
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001

#define trav(a, v) for (auto &a : v)

#define tt                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;

//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef debug
#define dbg(...)                                                               \
  {                                                                            \
    cerr << "[ ";                                                              \
    dump(#__VA_ARGS__, __VA_ARGS__);                                           \
  }
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
  return out << "(" << x.ff << "," << x.ss << ")";
}

template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const unordered_map<Arg1, Arg2> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1> void dump(const string name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void dump(const string names, Arg1 &&arg1, Args &&...args) {
  const string name = names.substr(0, names.find(','));
  cerr << name << " : " << arg1 << " | ";
  dump(names.substr(1 + (int)name.size()), args...);
}
#else
#define dbg(args...)
#endif

ll powmod(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}

int main() {
#if !ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  tt {
    ll a, b;
    cin >> a >> b;
    ll low = 0, high = 2 * sqrt(a * b);
    while (low + 1 < high) {

      ll mid = (low + high) / 2;

      ll x = mid / 2;
      ll y = (mid) / 2 + 1;
      if (mid % 2) {
        if (mid / 2 + 1 == a and mid / 2 + 1 == b) {
          x = mid / 2;
          y = mid / 2 + 2;
        } else if (mid / 2 + 1 == a or mid / 2 + 1 == b) {
          if (min(a, b) < mid / 2 + 1) {
            x = mid / 2 + 1;
            y = mid / 2 + 2;
          } else {
            x = mid / 2;
            y = mid / 2 + 2;
          }
        } else
          x = y = mid / 2 + 1;
      } else {
        if (mid / 2 == a and mid / 2 == b) {
          x = y = mid / 2 + 1;
        } else if (mid / 2 + 1 == a and mid / 2 + 1 == b) {
          x = mid / 2 - 1;
          y = mid / 2 + 2; //(mid/2+2>mid)?mid/2-1:mid/2+2;
        }
      }
      if (x * y < a * b)
        low = mid;
      else
        high = mid;
    }

    cout << low - (min(a, b) <= low) << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#if !ONLINE_JUDGE
#define debug
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i, k) for (int i = k; i >= 0; i--)
#define rep(i, k) for (int i = 0; i < k; i++)
#define repn(i, k1, k2) for (ll i = k1; i < k2; i++)
#define sz(x) (ll)(x).size()
#define ff first
#define ss second

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001

#define trav(a, v) for (auto &a : v)

#define tt                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr> vvpr;
typedef vector<vi> vvi;

//*X.find_by_order(2) element at index=2
// X.order_of_key(1) how many elements strictly less than 1

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef debug
#define dbg(...)                                                               \
  {                                                                            \
    cerr << "[ ";                                                              \
    dump(#__VA_ARGS__, __VA_ARGS__);                                           \
  }
#undef endl
template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const pair<Arg1, Arg2> &x) {
  return out << "(" << x.ff << "," << x.ss << ")";
}

template <typename Arg1>
ostream &operator<<(ostream &out, const vector<Arg1> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1> ostream &operator<<(ostream &out, const set<Arg1> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const map<Arg1, Arg2> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1, typename Arg2>
ostream &operator<<(ostream &out, const unordered_map<Arg1, Arg2> &a) {
  out << "[";
  for (const auto &x : a)
    out << x << ",";
  return out << "]";
}

template <typename Arg1> void dump(const string name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void dump(const string names, Arg1 &&arg1, Args &&...args) {
  const string name = names.substr(0, names.find(','));
  cerr << name << " : " << arg1 << " | ";
  dump(names.substr(1 + (int)name.size()), args...);
}
#else
#define dbg(args...)
#endif

ll powmod(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1; // y = y/2
    x = (x * x) % MOD;
  }
  return res;
}

int main() {
#if !ONLINE_JUDGE
// freopen("in.txt","r",stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  tt {
    ll a, b;
    cin >> a >> b;
    ll low = 0, high = 2 * sqrt(a * b);
    while (low + 1 < high) {

      ll mid = (low + high) / 2;

      ll x = mid / 2;
      ll y = (mid) / 2 + 1;
      if (mid % 2) {
        if (mid / 2 + 1 == a and mid / 2 + 1 == b) {
          x = mid / 2;
          y = mid / 2 + 2;
        } else if (mid / 2 + 1 == a or mid / 2 + 1 == b) {
          if (min(a, b) < mid / 2 + 1) {
            x = mid / 2 + 1;
            y = mid / 2 + 2;
          } else {
            x = mid / 2;
            y = mid / 2 + 2;
          }
        } else
          x = y = mid / 2 + 1;
      } else {
        if (mid / 2 == a and mid / 2 == b) {
          x = y = mid / 2 + 1;
        } else if (mid / 2 + 1 == a and mid / 2 + 1 == b) {
          x = mid / 2 - 1;
          y = mid / 2 + 2; //(mid/2+2>mid)?mid/2-1:mid/2+2;
        }
      }
      if (x * y < a * b)
        low = mid;
      else
        high = mid;
    }

    cout << low - (min(a, b) <= low) << "\n";
  }
}