Problem p02774 (C++)

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N), B(N);
  ll mx = 0;
  REP(i, N) {
    cin >> A[i];
    B[i] = A[i];
    chmax(mx, abs(A[i]));
  }

  SORT(A);
  SORT(B);
  REVERSE(B);

  ll ans = 0;
  ll xl = -mx * mx, xr = -xl, X = (xl + xr) / 2;
  while (xr - xl > 1) {
    ll tmp = 0;
    REP(i, N - 1) {
      ll l = -1, r = N, n = (l + r) / 2;
      vector<ll> v;
      if (A[i] < 0)
        v = B;
      else
        v = A;
      while (r - l > 1) {
        if (A[i] * v[n] <= X)
          l = n;
        else
          r = n;
        n = (l + r) / 2;
      }
      if (A[i] >= 0)
        tmp += max((ll)0, r - i - 1);
      else
        tmp += min(N - i - 1, r);
    }

    if (tmp < K)
      xl = X;
    else
      xr = X;
    X = (xl + xr) / 2;
  }
  ans = xr;

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N), B(N);
  ll mx = 0;
  REP(i, N) {
    cin >> A[i];
    B[i] = A[i];
    chmax(mx, abs(A[i]));
  }

  SORT(A);
  SORT(B);
  REVERSE(B);

  ll ans = 0;
  ll xl = -mx * mx, xr = -xl, X = (xl + xr) / 2;
  while (xr - xl > 1) {
    ll tmp = 0;
    REP(i, N - 1) {
      ll l = -1, r = N, n = (l + r) / 2;

      if (A[i] < 0) {
        while (r - l > 1) {
          if (A[i] * B[n] <= X)
            l = n;
          else
            r = n;
          n = (l + r) / 2;
        }
      } else {
        while (r - l > 1) {
          if (A[i] * A[n] <= X)
            l = n;
          else
            r = n;
          n = (l + r) / 2;
        }
      }
      if (A[i] >= 0)
        tmp += max((ll)0, r - i - 1);
      else
        tmp += min(N - i - 1, r);
    }

    if (tmp < K)
      xl = X;
    else
      xr = X;
    X = (xl + xr) / 2;
  }
  ans = xr;

  cout << ans << endl;
  return 0;
}