Problem p02774 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 1e9
// #define long long long long long long

typedef long long ll;
ll mod = 1e9 + 7;

long long fast_exp(long long a, long long b) {
  if (b <= 0)
    return 1;
  else {
    long long res = 1;
    res = fast_exp(a, b / 2);
    res = (res * res) % mod;
    if (b % 2 == 1)
      res = (res * a) % mod;
    return res;
  }
}

typedef long double ld;
const ll N = 1e5 + 5;
long long num_strictly_less(vector<long long> &v, long long val) {
  long long n = v.size();
  long long truecount = 0;
  for (long long i = 0; i < n - 1; i++) {
    long long l = i + 1, r = n - 1;
    long long cnt = 0;
    if (v[i] < 0) {
      while (l < r) {
        long long mid = (l + r) / 2;
        if (v[mid] * v[i] < val)
          r = mid;
        else
          l = mid + 1;
      }
      if (v[l] * v[i] < val)
        cnt += n - l;

    } else if (v[i] == 0) {
      if (val > 0)
        cnt += r - l + 1;
    } else {
      while (l < r) {
        long long mid = (l + r + 1) / 2;
        if (v[mid] * v[i] < val)
          l = mid;
        else
          r = mid - 1;
      }
      if (v[l] * v[i] < val)
        cnt += l - i;
    }
    truecount += cnt;
  }
  return truecount;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<long long> v;
  long long x, n, k;
  cin >> n >> k;
  k--;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  long long l = -1e18, r = 1e18;
  while (l < r) {
    long long mid = (l + r) / 2;
    long long lesser = num_strictly_less(v, mid);

    if (lesser <= k)
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;

  return 0;
}


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

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 1e9
// #define long long long long long long

typedef long long ll;
ll mod = 1e9 + 7;

long long fast_exp(long long a, long long b) {
  if (b <= 0)
    return 1;
  else {
    long long res = 1;
    res = fast_exp(a, b / 2);
    res = (res * res) % mod;
    if (b % 2 == 1)
      res = (res * a) % mod;
    return res;
  }
}

typedef long double ld;
const ll N = 1e5 + 5;
long long num_strictly_less(vector<long long> &v, long long val) {
  long long n = v.size();
  long long truecount = 0;
  for (long long i = 0; i < n - 1; i++) {
    long long l = i + 1, r = n - 1;
    long long cnt = 0;
    if (v[i] < 0) {
      while (l < r) {
        long long mid = (l + r) / 2;
        if (v[mid] * v[i] < val)
          r = mid;
        else
          l = mid + 1;
      }
      if (v[l] * v[i] < val)
        cnt += n - l;

    } else if (v[i] == 0) {
      if (val > 0)
        cnt += r - l + 1;
    } else {
      while (l < r) {
        long long mid = (l + r + 1) / 2;
        if (v[mid] * v[i] < val)
          l = mid;
        else
          r = mid - 1;
      }
      if (v[l] * v[i] < val)
        cnt += l - i;
    }
    truecount += cnt;
  }
  return truecount;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<long long> v;
  long long x, n, k;
  cin >> n >> k;
  k--;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.pb(x);
  }
  sort(v.begin(), v.end());
  long long l = -1e18, r = 1e18;
  while (l < r) {
    long long mid = (l + r + 1) / 2;
    long long lesser = num_strictly_less(v, mid);

    if (lesser <= k)
      l = mid;
    else
      r = mid - 1;
  }
  cout << l << endl;

  return 0;
}