Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 1e5 + 100;
ll n, m;
ll k;
ll a[maxn], b[maxn];
ll a2[maxn], b2[maxn];
ll tmp, tmp2, tmp3, tmp4;

ll s[maxn];

bool check(ll m, ll x, ll y) {
  if (m * y <= x) {
    return true;
  }
  return false;
}

ll gao1(ll x, ll y) {
  ll l = 0, r = tmp3 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check(b[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check(b[l], x, y))
    return l;
  return -1;
}

ll gao2(ll x, ll y) {
  ll l = 0, r = tmp4 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check(b2[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check(b2[l], x, y))
    return l;
  return -1;
}
bool check2(ll m, ll x, ll y) {
  if (m * y < x) {
    return true;
  }
  return false;
}
ll gao3(ll x, ll y) {
  ll l = 0, r = tmp3 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check2(b[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check2(b[l], x, y))
    return l;
  return -1;
}

ll gao4(ll x, ll y) {
  ll l = 0, r = tmp4 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check2(b2[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check2(b2[l], x, y))
    return l;
  return -1;
}

bool gao(ll x) {
  ll cnt = 0;

  rep(i, 0, tmp) {
    if (tmp3) {
      // + * +
      if (x >= 0) {
        ll ps = gao1(x, a[i]) + 1;
        cnt += ps;
        //                dd("1"),de(ps);
      }
    }
    if (tmp4) {
      if (x >= 0)
        cnt += tmp4;
      else {
        ll ps = gao4(-x, a[i]);
        ps = tmp4 - ps - 1;
        cnt += ps;
      }
    }
  }
  rep(j, 0, tmp2) {
    if (tmp3) {
      // - * +
      if (x >= 0) {
        cnt += tmp3;
      } else {
        ll ps = gao3(-x, -a2[j]);
        ps = tmp3 - ps - 1;
        cnt += ps;
      }
    }
    if (tmp4) {
      // - * - => +
      if (x >= 0) {
        ll ps = gao2(x, -a2[j]) + 1;
        cnt += ps;
        //                dd("4"),de(ps);
      }
    }
  }
  //    dd(x),de(cnt);
  cnt -= (upper_bound(s, s + n, x) - s);
  cnt /= 2;
  //    dd(x),de(cnt);
  return cnt >= k;
}

int main(int argc, char const *argv[]) {
#ifdef indiewar
  freopen("in.txt", "r", stdin);
//    freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  ll x;
  tmp = tmp2 = 0;
  tmp3 = tmp4 = 0;
  m = n;
  rep(i, 0, n) {
    cin >> x;
    s[i] = x * x;
    if (x >= 0) {
      a[tmp++] = x;
    } else {
      a2[tmp2++] = x;
    }
    if (x >= 0) {
      b[tmp3++] = x;
    } else {
      b2[tmp4++] = -x;
    }
  }

  sort(s, s + n);

  sort(a, a + tmp);
  sort(a2, a2 + tmp2);
  sort(b, b + tmp3);
  sort(b2, b2 + tmp4);

  ll l = -1e18, r = 1e18;
  while (l < r) {
    ll mid = (l + r) >> 1;
    //        dd(l),de(r);
    if (gao(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int maxn = 2e5 + 100;
ll n, m;
ll k;
ll a[maxn], b[maxn];
ll a2[maxn], b2[maxn];
ll tmp, tmp2, tmp3, tmp4;

ll s[maxn];

bool check(ll m, ll x, ll y) {
  if (m * y <= x) {
    return true;
  }
  return false;
}

ll gao1(ll x, ll y) {
  ll l = 0, r = tmp3 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check(b[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check(b[l], x, y))
    return l;
  return -1;
}

ll gao2(ll x, ll y) {
  ll l = 0, r = tmp4 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check(b2[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check(b2[l], x, y))
    return l;
  return -1;
}
bool check2(ll m, ll x, ll y) {
  if (m * y < x) {
    return true;
  }
  return false;
}
ll gao3(ll x, ll y) {
  ll l = 0, r = tmp3 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check2(b[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check2(b[l], x, y))
    return l;
  return -1;
}

ll gao4(ll x, ll y) {
  ll l = 0, r = tmp4 - 1;
  while (l < r) {
    ll mid = (l + r + 1) >> 1;
    if (check2(b2[mid], x, y)) {
      l = mid;
    } else {
      r = mid - 1;
    }
  }
  if (check2(b2[l], x, y))
    return l;
  return -1;
}

bool gao(ll x) {
  ll cnt = 0;

  rep(i, 0, tmp) {
    if (tmp3) {
      // + * +
      if (x >= 0) {
        ll ps = gao1(x, a[i]) + 1;
        cnt += ps;
        //                dd("1"),de(ps);
      }
    }
    if (tmp4) {
      if (x >= 0)
        cnt += tmp4;
      else {
        ll ps = gao4(-x, a[i]);
        ps = tmp4 - ps - 1;
        cnt += ps;
      }
    }
  }
  rep(j, 0, tmp2) {
    if (tmp3) {
      // - * +
      if (x >= 0) {
        cnt += tmp3;
      } else {
        ll ps = gao3(-x, -a2[j]);
        ps = tmp3 - ps - 1;
        cnt += ps;
      }
    }
    if (tmp4) {
      // - * - => +
      if (x >= 0) {
        ll ps = gao2(x, -a2[j]) + 1;
        cnt += ps;
        //                dd("4"),de(ps);
      }
    }
  }
  //    dd(x),de(cnt);
  cnt -= (upper_bound(s, s + n, x) - s);
  cnt /= 2;
  //    dd(x),de(cnt);
  return cnt >= k;
}

int main(int argc, char const *argv[]) {
#ifdef indiewar
  freopen("in.txt", "r", stdin);
//    freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  ll x;
  tmp = tmp2 = 0;
  tmp3 = tmp4 = 0;
  m = n;
  rep(i, 0, n) {
    cin >> x;
    s[i] = x * x;
    if (x >= 0) {
      a[tmp++] = x;
    } else {
      a2[tmp2++] = x;
    }
    if (x >= 0) {
      b[tmp3++] = x;
    } else {
      b2[tmp4++] = -x;
    }
  }

  sort(s, s + n);

  sort(a, a + tmp);
  sort(a2, a2 + tmp2);
  sort(b, b + tmp3);
  sort(b2, b2 + tmp4);

  ll l = -1e18, r = 1e18;
  while (l < r) {
    ll mid = (l + r) >> 1;
    //        dd(l),de(r);
    if (gao(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}