Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
// by kcz 1552920354@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;

template <typename T> void chmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
template <typename T> void chmax(T &x, const T &y) {
  if (x < y)
    x = y;
}
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, r, l) for (int i = r; i >= l; --i)
#define pb push_back
#define mp make_pair
#define gc (c = getchar())
char readc() {
  char c;
  while (gc < '-')
    ;
  return c;
}
int read() {
  char c;
  while (gc < '-')
    ;
  if (c == '-') {
    int x = gc - '0';
    while (gc >= '0')
      x = x * 10 + c - '0';
    return -x;
  }
  int x = c - '0';
  while (gc >= '0')
    x = x * 10 + c - '0';
  return x;
}
#undef gc

#define mid ((l + r) / 2)
ll a, b;
#define F(l) (r1 - (l)) * (l2 + (l))
bool check(ll l1, ll r1, ll l2) {
  int l = 0, r = r1 - l1;
  while (l != r) {
    if (F(mid) > F(mid + 1))
      r = mid;
    else
      l = mid + 1;
  }
  return F(l) < a * b;
}
bool ok(int n) {
  int r2 = n + (n >= b);
  if (n < a)
    return 1;
  if (a > 1) {
    int l2 = r2 - (a - 1) + 1;
    if (l2 > b || r2 < b) {
      if (!check(1, a - 1, l2))
        return 0;
    } else {
      int len = r2 - b;
      if (!check(1, len, b + 1))
        return 0;
      int len2 = a - 1 - len;
      if (!check(len + 1, a - 1, b - len2))
        return 0;
    }
  }
  if (r2 > b && r2 - (a - 1) <= b)
    --r2;
  r2 -= a - 1;
  int len = n - (a - 1);
  if (r2 < b || r2 - len + 1 > b) {
    if (!check(a + 1, n + 1, r2 - len + 1))
      return 0;
  } else {
    int len1 = r2 - b;
    if (!check(a + 1, a + len1, r2 - len1 + 1))
      return 0;
    int len2 = len - len1;
    if (!check(a + len1 + 1, n + 1, b - len2))
      return 0;
  }
  return 1;
}
int main() {
  // freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    int l = 0, r = 2e9 + 5;
    while (l + 1 != r)
      if (ok(mid))
        l = mid;
      else
        r = mid;
    cout << l << endl;
  }
}


Fixed submission:
// by kcz 1552920354@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;

template <typename T> void chmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
template <typename T> void chmax(T &x, const T &y) {
  if (x < y)
    x = y;
}
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, r, l) for (int i = r; i >= l; --i)
#define pb push_back
#define mp make_pair
#define gc (c = getchar())
char readc() {
  char c;
  while (gc < '-')
    ;
  return c;
}
int read() {
  char c;
  while (gc < '-')
    ;
  if (c == '-') {
    int x = gc - '0';
    while (gc >= '0')
      x = x * 10 + c - '0';
    return -x;
  }
  int x = c - '0';
  while (gc >= '0')
    x = x * 10 + c - '0';
  return x;
}
#undef gc

#define mid ((l + r) / 2)
ll a, b;
#define F(l) (r1 - (l)) * (l2 + (l))
bool check(ll l1, ll r1, ll l2) {
  int l = 0, r = r1 - l1;
  while (l != r) {
    if (F(mid) > F(mid + 1))
      r = mid;
    else
      l = mid + 1;
  }
  return F(l) < a * b;
}
bool ok(int n) {
  int r2 = n + (n >= b);
  if (n < a)
    return 1;
  if (a > 1) {
    int l2 = r2 - (a - 1) + 1;
    if (l2 > b || r2 < b) {
      if (!check(1, a - 1, l2))
        return 0;
    } else {
      int len = r2 - b;
      if (!check(1, len, b + 1))
        return 0;
      int len2 = a - 1 - len;
      if (!check(len + 1, a - 1, b - len2))
        return 0;
    }
  }
  if (r2 > b && r2 - (a - 1) <= b)
    --r2;
  r2 -= a - 1;
  int len = n - (a - 1);
  if (r2 < b || r2 - len + 1 > b) {
    if (!check(a + 1, n + 1, r2 - len + 1))
      return 0;
  } else {
    int len1 = r2 - b;
    if (!check(a + 1, a + len1, r2 - len1 + 1))
      return 0;
    int len2 = len - len1;
    if (!check(a + len1 + 1, n + 1, b - len2))
      return 0;
  }
  return 1;
}
int main() {
  // freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    ll l = 0, r = 2e9 + 5;
    while (l + 1 != r)
      if (ok(mid))
        l = mid;
      else
        r = mid;
    cout << l << endl;
  }
}