Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int inf = 2e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  sort(a.begin(), a.end());
  vector<ll> sgn(3);
  sgn[0] = lower_bound(a.begin(), a.end(), (ll)0) - a.begin();
  sgn[1] = upper_bound(a.begin(), a.end(), (ll)0) -
           lower_bound(a.begin(), a.end(), (ll)0);
  sgn[2] = a.end() - upper_bound(a.begin(), a.end(), (ll)0);
  vector<ll> cnt(3);
  cnt[0] = sgn[0] * sgn[2];
  cnt[2] = sgn[0] * (sgn[0] - 1) / 2 + sgn[2] * (sgn[2] - 1) / 2;
  cnt[1] = n * (n - 1) / 2 - cnt[0] - cnt[2];
  if (cnt[0] >= k) {
    vector<ll> aneg(sgn[0]), apos(sgn[2]);
    rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i];
    rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]];
    ll l = 0, r = INF;
    while (r - l > 1) {
      ll m = (l + r) / 2, res = cnt[0];
      rep(i, sgn[0]) {
        ll d = m / aneg[i];
        res -= upper_bound(apos.begin(), apos.end(), d) - apos.begin();
      }
      if (res >= k)
        l = m;
      else
        r = m;
    }
    cout << -r << endl;
  } else if (cnt[0] < k && cnt[0] + cnt[1] >= k) {
    cout << 0 << endl;
  } else {
    vector<ll> aneg(sgn[0]), apos(sgn[2]);
    rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i];
    rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]];
    ll l = 0, r = INF;
    while (r - l > 1) {
      ll m = (l + r) / 2, res = cnt[0] + cnt[1];
      rep(i, sgn[0]) {
        ll d = m / aneg[i];
        ll now = upper_bound(aneg.begin(), aneg.end(), d) - aneg.begin() - i;
        if (d >= aneg[i])
          now--;
        if (now > 0)
          res += now;
      }
      rep(i, sgn[2]) {
        ll d = m / apos[i];
        ll now = upper_bound(apos.begin(), apos.end(), d) - apos.begin() - i;
        if (d >= aneg[i])
          now--;
        if (now > 0)
          res += now;
      }
      if (res >= k)
        r = m;
      else
        l = m;
    }
    cout << r << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int inf = 2e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  sort(a.begin(), a.end());
  vector<ll> sgn(3);
  sgn[0] = lower_bound(a.begin(), a.end(), (ll)0) - a.begin();
  sgn[1] = upper_bound(a.begin(), a.end(), (ll)0) -
           lower_bound(a.begin(), a.end(), (ll)0);
  sgn[2] = a.end() - upper_bound(a.begin(), a.end(), (ll)0);
  vector<ll> cnt(3);
  cnt[0] = sgn[0] * sgn[2];
  cnt[2] = sgn[0] * (sgn[0] - 1) / 2 + sgn[2] * (sgn[2] - 1) / 2;
  cnt[1] = n * (n - 1) / 2 - cnt[0] - cnt[2];
  if (cnt[0] >= k) {
    vector<ll> aneg(sgn[0]), apos(sgn[2]);
    rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i];
    rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]];
    ll l = 0, r = INF;
    while (r - l > 1) {
      ll m = (l + r) / 2, res = cnt[0];
      rep(i, sgn[0]) {
        ll d = m / aneg[i];
        res -= upper_bound(apos.begin(), apos.end(), d) - apos.begin();
      }
      if (res >= k)
        l = m;
      else
        r = m;
    }
    cout << -r << endl;
  } else if (cnt[0] < k && cnt[0] + cnt[1] >= k) {
    cout << 0 << endl;
  } else {
    vector<ll> aneg(sgn[0]), apos(sgn[2]);
    rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i];
    rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]];
    ll l = 0, r = INF;
    while (r - l > 1) {
      ll m = (l + r) / 2, res = cnt[0] + cnt[1];
      rep(i, sgn[0]) {
        ll d = m / aneg[i];
        ll now = upper_bound(aneg.begin(), aneg.end(), d) - aneg.begin() - i;
        if (d >= aneg[i])
          now--;
        if (now > 0)
          res += now;
      }
      rep(i, sgn[2]) {
        ll d = m / apos[i];
        ll now = upper_bound(apos.begin(), apos.end(), d) - apos.begin() - i;
        if (d >= apos[i])
          now--;
        if (now > 0)
          res += now;
      }
      if (res >= k)
        r = m;
      else
        l = m;
    }
    cout << r << endl;
  }

  return 0;
}