Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  long long n, i, j, k, x, a, b, c, ans, m, ct, aa, bb, cc;
  cin >> n >> k;
  vector<vector<long long>> v(3);
  for (i = 0; i < n; i++) {
    cin >> x;
    if (x >= 0)
      v[0].push_back(x);
    else
      v[1].push_back(x);
  }
  if (!v[0].empty())
    sort(v[0].begin(), v[0].end());
  if (!v[1].empty())
    sort(v[1].rbegin(), v[1].rend());
  v[2] = v[1];
  for (i = 0; i < v[1].size(); i++)
    v[1][i] = -v[1][i];
  sort(v[1].begin(), v[1].end());

  a = -(1LL << 60);
  b = 1LL << 60;

  while (a < b) {
    if (b - a == 1)
      c = a;
    else
      c = (a + b) / 2;

    ct = 0;
    for (i = 0; i < 2; i++) {
      // same sign
      for (j = 0; j < v[i].size() - 1; j++) {
        if (v[i][j] * v[i][j + 1] > c)
          continue;
        aa = j + 1;
        bb = v[i].size() - 1;
        while (aa < bb) {
          cc = (aa + bb + 1) / 2;
          if (v[i][j] * v[i][cc] > c)
            bb = cc - 1;
          else
            aa = cc;
        }
        ct += aa - j;
      }

      // different sign
      if (i == 1)
        continue; // avoid duplication

      m = v[2].size();
      if (m == 0)
        continue;

      for (j = 0; j < v[i].size(); j++) {
        if (v[i][j] * v[2][m - 1] > c)
          continue;
        aa = 0;
        bb = m - 1;
        while (aa < bb) {
          cc = (aa + bb) / 2;
          if (v[i][j] * v[2][cc] > c)
            aa = cc + 1;
          else
            bb = cc;
        }
        ct += m - aa;
      }
    }

    if (ct < k)
      a = c + 1;
    else
      b = c;
  }

  cout << a << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  long long n, i, j, k, x, a, b, c, ans, m, ct, aa, bb, cc;
  cin >> n >> k;
  vector<vector<long long>> v(3);
  for (i = 0; i < n; i++) {
    cin >> x;
    if (x >= 0)
      v[0].push_back(x);
    else
      v[1].push_back(x);
  }
  if (!v[0].empty())
    sort(v[0].begin(), v[0].end());
  if (!v[1].empty())
    sort(v[1].rbegin(), v[1].rend());
  v[2] = v[1];
  for (i = 0; i < v[1].size(); i++)
    v[1][i] = -v[1][i];
  sort(v[1].begin(), v[1].end());

  a = -(1LL << 60);
  b = 1LL << 60;

  while (a < b) {
    if (b - a == 1)
      c = a;
    else
      c = (a + b) / 2;

    ct = 0;
    for (i = 0; i < 2; i++) {
      if (v[i].empty())
        continue;
      // same sign
      for (j = 0; j < v[i].size() - 1; j++) {
        if (v[i][j] * v[i][j + 1] > c)
          continue;
        aa = j + 1;
        bb = v[i].size() - 1;
        while (aa < bb) {
          cc = (aa + bb + 1) / 2;
          if (v[i][j] * v[i][cc] > c)
            bb = cc - 1;
          else
            aa = cc;
        }
        ct += aa - j;
      }

      // different sign
      if (i == 1)
        continue; // avoid duplication

      m = v[2].size();
      if (m == 0)
        continue;

      for (j = 0; j < v[i].size(); j++) {
        if (v[i][j] * v[2][m - 1] > c)
          continue;
        aa = 0;
        bb = m - 1;
        while (aa < bb) {
          cc = (aa + bb) / 2;
          if (v[i][j] * v[2][cc] > c)
            aa = cc + 1;
          else
            bb = cc;
        }
        ct += m - aa;
      }
    }

    if (ct < k)
      a = c + 1;
    else
      b = c;
  }

  cout << a << "\n";

  return 0;
}