Problem p02774 (C++)

Buggy submission (status: Runtime Error):
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif

#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision                                                         \
  cout.setf(ios::fixed);                                                       \
  cout.precision(15);
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
using ll = int_fast64_t;
using ld = long double;

// -----------------------------------------------------------------------------

const int N = 2e5 + 5, A = 1e9;
int n, k, a[N];
int x, y;

ll f(ll v) {
  ll ret = 0;
  int b = 0;
  for (int i = 0; i < x; ++i) {
    while (b != n && ll(1) * a[i] * a[b] >= v)
      ++b;
    while (b != 0 && ll(1) * a[i] * a[b - 1] < v)
      --b;
    ret += max(i - b, 0);
  }
  for (int i = x; i < y; ++i) {
    if (v > 0)
      ret += i;
  }
  for (int i = y; i < n; ++i) {
    while (b != n && ll(1) * a[i] * a[b] < v)
      ++b;
    while (b != 0 && ll(1) * a[i] * a[b - 1] >= v)
      --b;
    ret += min(i, b);
  }
  return ret;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  x = lower_bound(a, a + n, 0) - a;
  y = upper_bound(a, a + n, 0) - a;
  ll r = ll(1) * A * A + 1, l = -r;
  while (l + 1 < r) {
    ll m = (l + r) >> 1;
    ll n_less = f(m);
    if (n_less < k)
      l = m;
    else
      r = m;
  }
  cout << l << '\n';
}

int main() {
  fast_io;
  cin_exception;
  int t = 1; // cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
}


Fixed submission:
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif

#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision                                                         \
  cout.setf(ios::fixed);                                                       \
  cout.precision(15);
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
using ll = int_fast64_t;
using ld = long double;

// -----------------------------------------------------------------------------

const int N = 2e5 + 5, A = 1e9;
int n, a[N];
ll k;
int x, y;

ll f(ll v) {
  ll ret = 0;
  int b = 0;
  for (int i = 0; i < x; ++i) {
    while (b != n && ll(1) * a[i] * a[b] >= v)
      ++b;
    while (b != 0 && ll(1) * a[i] * a[b - 1] < v)
      --b;
    ret += max(i - b, 0);
  }
  for (int i = x; i < y; ++i) {
    if (v > 0)
      ret += i;
  }
  for (int i = y; i < n; ++i) {
    while (b != n && ll(1) * a[i] * a[b] < v)
      ++b;
    while (b != 0 && ll(1) * a[i] * a[b - 1] >= v)
      --b;
    ret += min(i, b);
  }
  return ret;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  x = lower_bound(a, a + n, 0) - a;
  y = upper_bound(a, a + n, 0) - a;
  ll r = ll(1) * A * A + 1, l = -r;
  while (l + 1 < r) {
    ll m = (l + r) >> 1;
    ll n_less = f(m);
    if (n_less < k)
      l = m;
    else
      r = m;
  }
  cout << l << '\n';
}

int main() {
  fast_io;
  cin_exception;
  int t = 1; // cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
}