Problem p02774 (C++)

Buggy submission (status: Runtime Error):
// Etavioxy
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#define il inline
#define ll long long
#define rep(i, s, t) for (register int i = (s); i <= (t); i++)
#define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--)
#define each(i, u) for (int i = head[u]; i; i = bow[i].nxt)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci() {
  register char ch;
  int f = 1;
  while (!isdigit(ch = getchar()))
    f = ch == '-' ? -1 : 1;
  register int x = ch ^ '0';
  while (isdigit(ch = getchar()))
    x = (x * 10) + (ch ^ '0');
  return f * x;
}

enum { N = 100023 };
ll a[N];
ll n;

il ll *lower(ll *l, ll *r, ll x, ll k) {
  while (l < r) {
    ll *w = l + (r - l) / 2;
    if (x * (*w) <= k)
      r = w;
    else
      l = w + 1;
  }
  return l;
}
il ll *upper(ll *l, ll *r, ll x, ll k) {
  while (l < r) {
    ll *w = l + (r - l + 1) / 2;
    if (x * (*w) <= k)
      l = w;
    else
      r = w - 1;
  }
  return l;
}

il ll calc(ll w) {
  ll ans = 0;
  rep(i, 1, n) {
    if (a[i] == 0)
      ans += w >= 0 ? n - i : 0;
    else if (a[i] > 0) {
      ans += max(0ll, (ll)(upper(a + i, a + n, a[i], w) - a - i));
    } else {
      ans += max(0ll, (ll)(n - (lower(a + i + 1, a + n + 1, a[i], w) - a) + 1));
    }
    //		printf("[%d] : %lld\n",i,ans);
  }
  return ans;
}

int main() {
  n = ci();
  ll k;
  scanf("%lld", &k);
  rep(i, 1, n) a[i] = ci();
  sort(a + 1, a + n + 1);
  ll l = -1e18, r = 1e18;
  //	rep(i,-12,9) printf("%d : %lld\n",i,calc(i));
  //	return 0;
  while (l < r) {
    ll w = l + (r - l) / 2;
    if (calc(w) < k)
      l = w + 1;
    else
      r = w;
  }
  printf("%lld\n", l);
  return 0;
}


Fixed submission:
// Etavioxy
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#define il inline
#define ll long long
#define rep(i, s, t) for (register int i = (s); i <= (t); i++)
#define rev_rep(i, s, t) for (register int i = (s); i >= (t); i--)
#define each(i, u) for (int i = head[u]; i; i = bow[i].nxt)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci() {
  register char ch;
  int f = 1;
  while (!isdigit(ch = getchar()))
    f = ch == '-' ? -1 : 1;
  register int x = ch ^ '0';
  while (isdigit(ch = getchar()))
    x = (x * 10) + (ch ^ '0');
  return f * x;
}

enum { N = 200023 };
ll a[N];
ll n;

il ll *lower(ll *l, ll *r, ll x, ll k) {
  while (l < r) {
    ll *w = l + (r - l) / 2;
    if (x * (*w) <= k)
      r = w;
    else
      l = w + 1;
  }
  return l;
}
il ll *upper(ll *l, ll *r, ll x, ll k) {
  while (l < r) {
    ll *w = l + (r - l + 1) / 2;
    if (x * (*w) <= k)
      l = w;
    else
      r = w - 1;
  }
  return l;
}

il ll calc(ll w) {
  ll ans = 0;
  rep(i, 1, n) {
    if (a[i] == 0)
      ans += w >= 0 ? n - i : 0;
    else if (a[i] > 0) {
      ans += max(0ll, (ll)(upper(a + i, a + n, a[i], w) - a - i));
    } else {
      ans += max(0ll, (ll)(n - (lower(a + i + 1, a + n + 1, a[i], w) - a) + 1));
    }
    //		printf("[%d] : %lld\n",i,ans);
  }
  return ans;
}

int main() {
  n = ci();
  ll k;
  scanf("%lld", &k);
  rep(i, 1, n) a[i] = ci();
  sort(a + 1, a + n + 1);
  ll l = -1e18, r = 1e18;
  //	rep(i,-12,9) printf("%d : %lld\n",i,calc(i));
  //	return 0;
  while (l < r) {
    ll w = l + (r - l) / 2;
    if (calc(w) < k)
      l = w + 1;
    else
      r = w;
  }
  printf("%lld\n", l);
  return 0;
}