Problem p02774 (C++)

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

using namespace std;

int n = 0, x = 0, k = 0;
int arr[200001];
vector<int> otr;
vector<int> pol;
vector<int> zeroes;
int otr_p = 0, pol_p = 0, zero_p = 0;

int pol_pol(int a, int b) {
  int ll = 0, rr = pol.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * pol[mm] > b)
      rr = mm;
    else
      ll = mm;
  }
  if (a * pol[ll] > b)
    return -1;
  else if (a * pol[rr] > b)
    return ll;
  else
    return rr;
}

int pol_otr(int a, int b) {
  int ll = 0, rr = otr.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * otr[mm] > b)
      rr = mm;
    else
      ll = mm;
  }
  if (a * otr[ll] > b)
    return -1;
  else if (a * otr[rr] > b)
    return ll;
  else
    return rr;
}

int otr_otr(int a, int b) {
  int ll = 0, rr = otr.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * otr[mm] > b)
      ll = mm;
    else
      rr = mm;
  }
  if (a * otr[rr] > b)
    return -1;
  else if (a * otr[ll] > b)
    return rr;
  else
    return ll;
}

int cnt(int x) {
  int res = 0;
  for (int i = 0; i < pol.size(); i++) {
    int ii = pol_pol(pol[i], x);
    if (ii == -1)
      continue;
    res += max(0ll, ii - i);
  }
  for (int i = 0; i < otr.size(); i++) {
    int ii = otr_otr(otr[i], x);
    if (ii == -1)
      continue;
    ii = max(ii, i + 1);
    res += otr.size() - ii;
  }
  for (int i = 0; i < pol.size(); i++) {
    int ii = pol_otr(pol[i], x);
    if (ii == -1)
      continue;
    res += ii + 1;
  }
  if (x >= 0)
    res += (zeroes.size() * (n - zeroes.size())) +
           (zeroes.size() * (zeroes.size() - 1) / 2);
  return res;
}

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 0; i < n; i++) {
    if (arr[i] < 0)
      otr.push_back(arr[i]);
    else if (arr[i] == 0)
      zeroes.push_back(arr[i]);
    else
      pol.push_back(arr[i]);
  }
  otr_p = otr.size() * pol.size();
  zero_p = zeroes.size() * (n - zeroes.size());
  pol_p = pol.size() * (pol.size() - 1) / 2 + otr.size() * (otr.size() - 1) / 2;
  int ll = LLONG_MIN / 3;
  int rr = LLONG_MAX / 3;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (cnt(mm) < k)
      ll = mm;
    else
      rr = mm;
  }
  if (cnt(rr) < k)
    cout << rr + 1;
  else if (cnt(ll) < k)
    cout << rr;
  else
    cout << ll;
  return 0;
}


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

using namespace std;

int n = 0, x = 0, k = 0;
int arr[200001];
vector<int> otr;
vector<int> pol;
vector<int> zeroes;
int otr_p = 0, pol_p = 0, zero_p = 0;

int pol_pol(int a, int b) {
  int ll = 0, rr = pol.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * pol[mm] > b)
      rr = mm;
    else
      ll = mm;
  }
  if (a * pol[ll] > b)
    return -1;
  else if (a * pol[rr] > b)
    return ll;
  else
    return rr;
}

int pol_otr(int a, int b) {
  if (otr.size() == 0)
    return -1;
  int ll = 0, rr = otr.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * otr[mm] > b)
      rr = mm;
    else
      ll = mm;
  }
  if (a * otr[ll] > b)
    return -1;
  else if (a * otr[rr] > b)
    return ll;
  else
    return rr;
}

int otr_otr(int a, int b) {
  int ll = 0, rr = otr.size() - 1;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (a * otr[mm] > b)
      ll = mm;
    else
      rr = mm;
  }
  if (a * otr[rr] > b)
    return -1;
  else if (a * otr[ll] > b)
    return rr;
  else
    return ll;
}

int cnt(int x) {
  int res = 0;
  for (int i = 0; i < pol.size(); i++) {
    int ii = pol_pol(pol[i], x);
    if (ii == -1)
      continue;
    res += max(0ll, ii - i);
  }
  for (int i = 0; i < otr.size(); i++) {
    int ii = otr_otr(otr[i], x);
    if (ii == -1)
      continue;
    ii = max(ii, i + 1);
    res += otr.size() - ii;
  }
  for (int i = 0; i < pol.size(); i++) {
    int ii = pol_otr(pol[i], x);
    if (ii == -1)
      continue;
    res += ii + 1;
  }
  if (x >= 0)
    res += (zeroes.size() * (n - zeroes.size())) +
           (zeroes.size() * (zeroes.size() - 1) / 2);
  return res;
}

signed main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 0; i < n; i++) {
    if (arr[i] < 0)
      otr.push_back(arr[i]);
    else if (arr[i] == 0)
      zeroes.push_back(arr[i]);
    else
      pol.push_back(arr[i]);
  }
  otr_p = otr.size() * pol.size();
  zero_p = zeroes.size() * (n - zeroes.size());
  pol_p = pol.size() * (pol.size() - 1) / 2 + otr.size() * (otr.size() - 1) / 2;
  int ll = LLONG_MIN / 3;
  int rr = LLONG_MAX / 3;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (cnt(mm) < k)
      ll = mm;
    else
      rr = mm;
  }
  if (cnt(rr) < k)
    cout << rr + 1;
  else if (cnt(ll) < k)
    cout << rr;
  else
    cout << ll;
  return 0;
}