Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int maxn = 1e5 + 5;
int n;
ll k;
ll a[maxn];
vector<ll> pos, neg, zero;
int psz, nsz, zsz;
ll NEG, ZERO;

ll count(ll x) {
  if (x < 0) {
    ll ans = 0;
    rep(i, psz) {
      ll thr = -((-x + pos[i] - 1) / pos[i]);
      ans += upper_bound(neg.begin(), neg.end(), thr) - neg.begin();
    }
    return ans;
  } else if (x == 0) {
    return NEG + ZERO;
  } else {
    ll ans = NEG + ZERO;
    rep(i, psz) {
      ll thr = x / pos[i];
      ans += min(pos.begin() + i, upper_bound(pos.begin(), pos.end(), thr)) -
             pos.begin();
    }
    rep(i, nsz) {
      ll thr = -(x / -neg[i]);
      ans += neg.end() -
             max(neg.begin() + i + 1, lower_bound(neg.begin(), neg.end(), thr));
    }
    return ans;
  }
}

int main() {
  scanf("%d %lld", &n, &k);
  rep(i, n) scanf("%lld", &a[i]);
  rep(i, n) {
    if (a[i] > 0)
      pos.pb(a[i]);
    else if (a[i] == 0)
      zero.pb(a[i]);
    else
      neg.pb(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  sort(zero.begin(), zero.end());
  psz = pos.size(), nsz = neg.size(), zsz = zero.size();
  NEG = 1LL * psz * nsz;
  ZERO = 1LL * zsz * (zsz - 1) / 2 + 1LL * zsz * (psz + nsz);
  ll lb = -1e18, rb = 1e18;
  while (lb < rb) {
    ll md = lb + (rb - lb) / 2;
    if (count(md) < k)
      lb = md + 1;
    else
      rb = md;
  }
  printf("%lld\n", lb);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int maxn = 2e5 + 5;
int n;
ll k;
ll a[maxn];
vector<ll> pos, neg, zero;
int psz, nsz, zsz;
ll NEG, ZERO;

ll count(ll x) {
  if (x < 0) {
    ll ans = 0;
    rep(i, psz) {
      ll thr = -((-x + pos[i] - 1) / pos[i]);
      ans += upper_bound(neg.begin(), neg.end(), thr) - neg.begin();
    }
    return ans;
  } else if (x == 0) {
    return NEG + ZERO;
  } else {
    ll ans = NEG + ZERO;
    rep(i, psz) {
      ll thr = x / pos[i];
      ans += min(pos.begin() + i, upper_bound(pos.begin(), pos.end(), thr)) -
             pos.begin();
    }
    rep(i, nsz) {
      ll thr = -(x / -neg[i]);
      ans += neg.end() -
             max(neg.begin() + i + 1, lower_bound(neg.begin(), neg.end(), thr));
    }
    return ans;
  }
}

int main() {
  scanf("%d %lld", &n, &k);
  rep(i, n) scanf("%lld", &a[i]);
  rep(i, n) {
    if (a[i] > 0)
      pos.pb(a[i]);
    else if (a[i] == 0)
      zero.pb(a[i]);
    else
      neg.pb(a[i]);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  sort(zero.begin(), zero.end());
  psz = pos.size(), nsz = neg.size(), zsz = zero.size();
  NEG = 1LL * psz * nsz;
  ZERO = 1LL * zsz * (zsz - 1) / 2 + 1LL * zsz * (psz + nsz);
  ll lb = -1e18, rb = 1e18;
  while (lb < rb) {
    ll md = lb + (rb - lb) / 2;
    if (count(md) < k)
      lb = md + 1;
    else
      rb = md;
  }
  printf("%lld\n", lb);
  return 0;
}