Problem p02774 (C++)

Buggy submission (status: Time Limit Exceeded):
// Optimize
#pragma GCC optimize("Ofast")

// Head File
#include <bits/stdc++.h>
using namespace std;

#define il inline

// Variable
#define ll long long
#define ull unsigned long long
#define db double
#define lb long double
#define ui unsigned int

// Debug
#define B cerr << "Break Point" << endl;
#define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl;
#define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' ';
#define ml(x)                                                                  \
  cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl;

// Vector
#define vc vector
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define vbe(x) x.begin(), x.end()

// Memset
#define ms(x) memset(x, 0, sizeof(x))
#define MS(x) memset(x, 0x3f3f3f3f, sizeof(x))

// Pair
#define fi first
#define se second

// File

#define fin(x) freopen(x, "r", stdin)
#define fou(x) freopen(x, "w", stdout)
void fio() {
#ifndef ONLINE_JUDGE
  freopen("sample.in", "r", stdin);
  freopen("sample.out", "w", stdout);
#endif
}
void pti() {
  double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
  cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() {
  pti();
  exit(0);
}

// Inf
#define INF 0x3f3f3f3f
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))

// IO
#define pc(s) putchar(s)
#define say(s) cout << s << endl
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I> il void gi(I &w) {
  ch = getchar(), op = 1, w = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      op = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    w = w * 10 + ch - '0';
    ch = getchar();
  }
  w *= op;
}
template <class I> il void print(I w) {
  qr = 0;
  if (!w)
    putchar('0');
  if (w < 0)
    putchar('-'), w = -w;
  while (w)
    que[++qr] = w % 10 + '0', w /= 10;
  while (qr)
    putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;

const int N = 2e5 + 5;

ll n, cnta, cntb, cntc, num, k;
ll a[N], b[N], c[N];

int find(int l, int r, ll s, ll d, ll *x) {
  int pos = l - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (x[mid] * s >= d)
      l = mid + 1, pos = mid;
    else
      r = mid - 1;
  }
  return pos;
}
bool check(ll v) {
  ll res = 0;
  for (int i = 1; i <= cnta; ++i) {
    int pos = find(i + 1, cnta, a[i], v, a);
    res += pos - i;
  }
  if (v <= 0)
    res += num * cnta + num * cntb + num * (num - 1) / 2;
  for (int i = 1; i <= cntb; ++i) {
    int pos = find(i + 1, cntb, b[i], v, b);
    res += pos - i;
  }
  for (int i = 1; i <= cnta; ++i) {
    int pos = find(1, cntc, a[i], v, c);
    res += pos;
  }
  res = n * (n - 1) / 2 - res;
  if (res < k)
    return true;
  else
    return false;
}
int main() {
  fio();
  gi(n), gi(k);
  for (int i = 1; i <= n; ++i) {
    ll x;
    gi(x);
    if (x > 0)
      a[++cnta] = x;
    else if (x < 0)
      b[++cntb] = x, c[++cntc] = x;
    else
      ++num;
  }
  sort(a + 1, a + cnta + 1, [](ll x, ll y) { return x > y; });
  sort(b + 1, b + cntb + 1, [](ll x, ll y) { return x < y; });
  sort(c + 1, c + cntc + 1, [](ll x, ll y) { return x > y; });
  ll l = -1e18 - 1, r = 1e18 + 1, ans;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  print(ans);
  end();
}


Fixed submission:
// Optimize
#pragma GCC optimize("Ofast")

// Head File
#include <bits/stdc++.h>
using namespace std;

#define il inline

// Variable
#define ll long long
#define ull unsigned long long
#define db double
#define lb long double
#define ui unsigned int

// Debug
#define B cerr << "Break Point" << endl;
#define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl;
#define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' ';
#define ml(x)                                                                  \
  cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl;

// Vector
#define vc vector
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define vbe(x) x.begin(), x.end()

// Memset
#define ms(x) memset(x, 0, sizeof(x))
#define MS(x) memset(x, 0x3f3f3f3f, sizeof(x))

// Pair
#define fi first
#define se second

// File

#define fin(x) freopen(x, "r", stdin)
#define fou(x) freopen(x, "w", stdout)
void fio() {
#ifndef ONLINE_JUDGE
  freopen("sample.in", "r", stdin);
  freopen("sample.out", "w", stdout);
#endif
}
void pti() {
  double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
  cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() {
  pti();
  exit(0);
}

// Inf
#define INF 0x3f3f3f3f
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))

// IO
#define pc(s) putchar(s)
#define say(s) cout << s << endl
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I> il void gi(I &w) {
  ch = getchar(), op = 1, w = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      op = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    w = w * 10 + ch - '0';
    ch = getchar();
  }
  w *= op;
}
template <class I> il void print(I w) {
  qr = 0;
  if (!w)
    putchar('0');
  if (w < 0)
    putchar('-'), w = -w;
  while (w)
    que[++qr] = w % 10 + '0', w /= 10;
  while (qr)
    putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;

const int N = 2e5 + 5;

ll n, cnta, cntb, cntc, num, k;
ll a[N], b[N], c[N];

int find(int l, int r, ll s, ll d, ll *x) {
  int pos = l - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (x[mid] * s >= d)
      l = mid + 1, pos = mid;
    else
      r = mid - 1;
  }
  return pos;
}
bool check(ll v) {
  ll res = 0;
  for (int i = 1; i <= cnta; ++i) {
    int pos = find(i + 1, cnta, a[i], v, a);
    res += pos - i;
  }
  if (v <= 0)
    res += num * cnta + num * cntb + num * (num - 1) / 2;
  for (int i = 1; i <= cntb; ++i) {
    int pos = find(i + 1, cntb, b[i], v, b);
    res += pos - i;
  }
  for (int i = 1; i <= cnta; ++i) {
    int pos = find(1, cntc, a[i], v, c);
    res += pos;
  }
  res = n * (n - 1) / 2 - res;
  if (res < k)
    return true;
  else
    return false;
}
int main() {
  gi(n), gi(k);
  for (int i = 1; i <= n; ++i) {
    ll x;
    gi(x);
    if (x > 0)
      a[++cnta] = x;
    else if (x < 0)
      b[++cntb] = x, c[++cntc] = x;
    else
      ++num;
  }
  sort(a + 1, a + cnta + 1, [](ll x, ll y) { return x > y; });
  sort(b + 1, b + cntb + 1, [](ll x, ll y) { return x < y; });
  sort(c + 1, c + cntc + 1, [](ll x, ll y) { return x > y; });
  ll l = -1e18 - 1, r = 1e18 + 1, ans;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  print(ans);
  end();
}