Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include <conio.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define SZ(x) ((ll)x.size())
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define F first
#define S second
#define int ll
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const ll MOD = INT_MAX;
const double PI = acos(-1);

void openfile() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
const int N = 2005;
// vector<int> a;
int a[N];
int n, k;

int count(int x) {
  int cnt = 0;
  rep(i, n) {
    if (a[i] * a[i] <= x)
      cnt--;

    if (a[i] == 0 && x >= 0)
      cnt += n;
    else if (a[i] < 0) {
      // cnt += n - (lower_bound(a.begin(), a.end(), (long double)x / a[i]) -
      // a.begin());
      cnt += n - (lower_bound(a, a + n, (long double)x / a[i]) - a);
    } else if (a[i] > 0) {
      // int pos = upper_bound(a.begin(), a.end(), (long double)x / a[i]) -
      // a.begin(); cnt += pos;
      cnt += upper_bound(a, a + n, (long double)x / a[i]) - a;
    }

    // if(a[i] * a[i] < x)	cnt--;

    // if(a[i] <= 0){ // negative or zero
    // 	int l = -1,r = n;
    // 	while(r-l > 1){
    // 		int mid = (l+r) / 2;
    // 		if(a[i] * a[mid] < x)	r = mid;
    // 		else	l = mid;
    // 	}
    // 	cnt += n-r;
    // }
    // else{			// positive
    // 	int l = -1,r = n;
    // 	while(r-l > 1){
    // 		int mid = (l+r) / 2;
    // 		if(a[i] * a[mid] < x)	l = mid;
    // 		else	r = mid;
    // 	}
    // 	cnt += r;
    // }

    // cout << cnt << ' ';
  }
  // cout << endl << x << ' ' << cnt << endl;

  return cnt / 2;
}
// O(n * log(n) * log(C)), C = 1e18

signed main() {
  IOS cin >> n >> k;
  // a.resize(n);
  rep(i, n) cin >> a[i];
  // sort(a.begin(), a.end());
  sort(a, a + n);
  int l = -1e18 + 7, r = 1e18 + 7;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (count(mid) < k)
      l = mid;
    else
      r = mid;
  }
  cout << r << endl;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include <conio.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define SZ(x) ((ll)x.size())
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define F first
#define S second
#define int ll
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const ll MOD = INT_MAX;
const double PI = acos(-1);

void openfile() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
const int N = 2e5 + 5;
// vector<int> a;
int a[N];
int n, k;

int count(int x) {
  int cnt = 0;
  rep(i, n) {
    if (a[i] * a[i] <= x)
      cnt--;

    if (a[i] == 0 && x >= 0)
      cnt += n;
    else if (a[i] < 0) {
      // cnt += n - (lower_bound(a.begin(), a.end(), (long double)x / a[i]) -
      // a.begin());
      cnt += n - (lower_bound(a, a + n, (long double)x / a[i]) - a);
    } else if (a[i] > 0) {
      // int pos = upper_bound(a.begin(), a.end(), (long double)x / a[i]) -
      // a.begin(); cnt += pos;
      cnt += upper_bound(a, a + n, (long double)x / a[i]) - a;
    }

    // if(a[i] * a[i] < x)	cnt--;

    // if(a[i] <= 0){ // negative or zero
    // 	int l = -1,r = n;
    // 	while(r-l > 1){
    // 		int mid = (l+r) / 2;
    // 		if(a[i] * a[mid] < x)	r = mid;
    // 		else	l = mid;
    // 	}
    // 	cnt += n-r;
    // }
    // else{			// positive
    // 	int l = -1,r = n;
    // 	while(r-l > 1){
    // 		int mid = (l+r) / 2;
    // 		if(a[i] * a[mid] < x)	l = mid;
    // 		else	r = mid;
    // 	}
    // 	cnt += r;
    // }

    // cout << cnt << ' ';
  }
  // cout << endl << x << ' ' << cnt << endl;

  return cnt / 2;
}
// O(n * log(n) * log(C)), C = 1e18

signed main() {
  IOS cin >> n >> k;
  // a.resize(n);
  rep(i, n) cin >> a[i];
  // sort(a.begin(), a.end());
  sort(a, a + n);
  int l = -1e18 + 7, r = 1e18 + 7;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (count(mid) < k)
      l = mid;
    else
      r = mid;
  }
  cout << r << endl;
}