Problem p03390 (C++)

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
// #define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define insum(a, b) a = modsum(a, (b))
#define insubtract(a, b) a = modsubtract(a, (b))
#define inmultiply(a, b) a = modmultiply(a, (b))
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#ifdef MAX_HOME
#define watch(x) cout << (#x) << " = " << x << endl;
#else
#define watch(x) cerr << (#x) << " = " << x << endl;
#endif

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";

template <class T> ostream &operator<<(ostream &out, vector<T> &a) {
  out << "{";
  for (auto x : a)
    out << x << " ";
  return out << "}";
}

template <class T> ostream &operator<<(ostream &out, pair<T, T> &a) {
  out << "{";
  cout << a.first << ", " << a.second;
  return out << "}";
}

void smain();

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifdef MAX_HOME
  freopen("input.txt", "r", stdin);
#endif

  cout << setprecision(12) << fixed;

  smain();
}
/* END OF TEMPLATE
   ==========================================================================================================================================
 */
;

void smain() {

  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll c = a * b;
    ll k = sqrt(c);
    while (k * k >= c)
      --k;
    ll mx = (c - 1) / k;
    ll lol = 0;
    if (mx == k)
      --lol;
    if (min(a, b) <= k)
      --lol;
    lol += k + k;
    cout << lol << "\n";
  }
}


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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
// #define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b)-M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define insum(a, b) a = modsum(a, (b))
#define insubtract(a, b) a = modsubtract(a, (b))
#define inmultiply(a, b) a = modmultiply(a, (b))
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#ifdef MAX_HOME
#define watch(x) cout << (#x) << " = " << x << endl;
#else
#define watch(x) cerr << (#x) << " = " << x << endl;
#endif

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";

template <class T> ostream &operator<<(ostream &out, vector<T> &a) {
  out << "{";
  for (auto x : a)
    out << x << " ";
  return out << "}";
}

template <class T> ostream &operator<<(ostream &out, pair<T, T> &a) {
  out << "{";
  cout << a.first << ", " << a.second;
  return out << "}";
}

void smain();

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifdef MAX_HOME
  freopen("input.txt", "r", stdin);
#endif

  cout << setprecision(12) << fixed;

  smain();
}
/* END OF TEMPLATE
   ==========================================================================================================================================
 */
;

void smain() {

  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll c = a * b;
    if (c == 1) {
      cout << "0\n";
      continue;
    }
    ll k = sqrt(c);
    while (k * k >= c)
      --k;
    ll mx = (c - 1) / k;
    ll lol = 0;
    if (mx == k)
      --lol;
    if (min(a, b) <= k)
      --lol;
    lol += k + k;
    cout << lol << "\n";
  }
}